From 10e72573a5d481957a4c66c32f21ea6c5e11bcd1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Sep 2017 23:16:13 +0700 Subject: [PATCH] update Encoder to singleton pattern --- libraries/RotaryEncoder/RotaryEncoder.cpp | 43 +++++++++++++++++-- libraries/RotaryEncoder/RotaryEncoder.h | 22 ++++++---- .../examples/OneKnob/OneKnob.ino | 9 ++-- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/libraries/RotaryEncoder/RotaryEncoder.cpp b/libraries/RotaryEncoder/RotaryEncoder.cpp index d87edcec..5e6dbaa2 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.cpp +++ b/libraries/RotaryEncoder/RotaryEncoder.cpp @@ -36,11 +36,17 @@ #include "RotaryEncoder.h" +class RotaryEncoder Qei; + /** * Initialize Hardware Encoder */ -void RotaryEncoder::begin(void) +void RotaryEncoder::begin(int8_t pina, int8_t pinb, int8_t pinled) { + _pina = pina; + _pinb = pinb; + _pinled = pinled; + // default sample period NRF_QDEC->SAMPLEPER = QDEC_SAMPLEPER_SAMPLEPER_128us; @@ -67,8 +73,11 @@ void RotaryEncoder::begin(void) void RotaryEncoder::start(void) { // Enable IRQ -// NVIC_ClearPendingIRQ(IRQn); -// NVIC_EnableIRQ(IRQn); + if ( NRF_QDEC->INTENSET ) + { + NVIC_ClearPendingIRQ(QDEC_IRQn); + NVIC_EnableIRQ(QDEC_IRQn); + } // Enable NRF_QDEC->ENABLE = 1; @@ -79,6 +88,11 @@ void RotaryEncoder::start(void) void RotaryEncoder::stop(void) { + if ( NRF_QDEC->INTENSET ) + { + NVIC_DisableIRQ(QDEC_IRQn); + } + // Stop NRF_QDEC->TASKS_STOP = 1; @@ -118,6 +132,19 @@ void RotaryEncoder::setReporter(int8_t sample_num) } } +void RotaryEncoder::setCallback(callback_t fp) +{ + _cb = fp; + + if (fp) + { + NRF_QDEC->INTENSET = QDEC_INTENSET_SAMPLERDY_Msk; + }else + { + NRF_QDEC->INTENCLR = QDEC_INTENCLR_SAMPLERDY_Msk; + } +} + int32_t RotaryEncoder::read(void) { // Trigger READ CLR ACC @@ -160,7 +187,17 @@ void RotaryEncoder::clearAbs(void) _abs = 0; } + void QDEC_IRQHandler(void) { + if ( (NRF_QDEC->INTENSET & QDEC_INTENSET_SAMPLERDY_Msk) && NRF_QDEC->EVENTS_SAMPLERDY ) + { + NRF_QDEC->EVENTS_SAMPLERDY = 0; +// if ( _cb ) +// { +// int32_t step = read(); +// if ( step ) _cb(step); +// } + } } diff --git a/libraries/RotaryEncoder/RotaryEncoder.h b/libraries/RotaryEncoder/RotaryEncoder.h index e94df64f..4cffca55 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.h +++ b/libraries/RotaryEncoder/RotaryEncoder.h @@ -41,17 +41,18 @@ class RotaryEncoder { public: - RotaryEncoder(uint8_t pina, uint8_t pinb, int8_t pinled = -1) + typedef void (*callback_t) (int32_t step); + + RotaryEncoder(void) { - _pina = pina; - _pinb = pinb; - _pinled = pinled; + _pina = _pinb = _pinled = -1; _abs = 0; _step = 0; + _cb = NULL; } - void begin(void); + void begin(int8_t pina, int8_t pinb, int8_t pinled = -1); void setSampler(uint8_t period); void setDebounce(bool enable); @@ -65,15 +66,20 @@ class RotaryEncoder void writeAbs(int32_t value); void clearAbs(void); + void setCallback(callback_t fp); + private: - uint8_t _pina, _pinb; - int8_t _pinled; + int8_t _pina, _pinb; + int8_t _pinled; // Note For each turn, encoder generate 2 transitions int32_t _abs; // Absolute position - int32_t _step; // Moving step that take intermidiate transition in account + int32_t _step; // Moving step that take intermediate transition in account + + callback_t _cb; }; +extern class RotaryEncoder Qei; #endif /* ROTARYENCODER_H_ */ diff --git a/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino b/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino index 5e98cd4c..5aac317e 100644 --- a/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino +++ b/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino @@ -15,7 +15,8 @@ #include #include "RotaryEncoder.h" -RotaryEncoder encoder(A0, A1); +#define PIN_A A0 +#define PIN_B A1 void setup() { @@ -24,15 +25,15 @@ void setup() Serial.println("--------------------------------------------\n"); // Initialize Encoder - encoder.begin(); + Qei.begin(PIN_A, PIN_B); // Start encoder - encoder.start(); + Qei.start(); } void loop() { - int value = encoder.read(); + int value = Qei.read(); if (value) {