From 8ad448ebe67f4b16b8928c3f9efe514583fbc211 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Sep 2017 18:19:21 +0700 Subject: [PATCH] update hw encoder --- libraries/RotaryEncoder/RotaryEncoder.cpp | 27 ++++++++++------------- libraries/RotaryEncoder/RotaryEncoder.h | 10 +++++---- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libraries/RotaryEncoder/RotaryEncoder.cpp b/libraries/RotaryEncoder/RotaryEncoder.cpp index ad3bb193..3cd65781 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.cpp +++ b/libraries/RotaryEncoder/RotaryEncoder.cpp @@ -51,8 +51,8 @@ void HwRotaryEncoder::begin(uint8_t pina, uint8_t pinb, int8_t pinled) // default sample period NRF_QDEC->SAMPLEPER = QDEC_SAMPLEPER_SAMPLEPER_128us; - pinMode(pina, INPUT); - pinMode(pinb, INPUT); + pinMode(pina, INPUT_PULLUP); + pinMode(pinb, INPUT_PULLUP); NRF_QDEC->PSEL.A = pina; NRF_QDEC->PSEL.B = pinb; @@ -151,24 +151,21 @@ int32_t HwRotaryEncoder::read(void) // Trigger READ CLR ACC NRF_QDEC->TASKS_RDCLRACC = 1; - // Nordic QDEC CW is negative, CCW is positve --> invert + // Nordic QDEC CW is negative, CCW is positive --> invert int32_t val = -(NRF_QDEC->ACCREAD); // Add to absolute value and buffered step - _abs += val; - _step += val; + _abs += val; - if ( (_step > 1) || (_step < -1) ) - { - val = _step / 2; - _step = _step%2; - }else - { - // not enough to report the turn - val = 0; - } + int32_t diff = (_abs - _last) / 2; - return val; + if ( diff ) _last = _abs; + return diff; +} + +int32_t HwRotaryEncoder::readDebug(void) +{ + return _abs; } int32_t HwRotaryEncoder::readAbs(void) diff --git a/libraries/RotaryEncoder/RotaryEncoder.h b/libraries/RotaryEncoder/RotaryEncoder.h index d31d9049..ce38d7b1 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.h +++ b/libraries/RotaryEncoder/RotaryEncoder.h @@ -46,9 +46,8 @@ class HwRotaryEncoder HwRotaryEncoder(void) { - _abs = 0; - _step = 0; - _cb = NULL; + _abs = _last = 0; + _cb = NULL; } void begin(uint8_t pina, uint8_t pinb, int8_t pinled = -1); @@ -66,13 +65,16 @@ class HwRotaryEncoder void writeAbs(int32_t value); void clearAbs(void); + + int32_t readDebug(void); + // Internal API void _irq_handler(void); private: // Note For each turn, encoder generate 2 transitions int32_t _abs; // Absolute position - int32_t _step; // Moving step that take intermediate transition in account + int32_t _last; callback_t _cb; };