From 9177cb48dbd1ad9aa38befa105ebbcf1ede3b80e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Sep 2017 21:38:58 +0700 Subject: [PATCH] update rotary encoder --- libraries/RotaryEncoder/RotaryEncoder.cpp | 32 +++++++++++++++-------- libraries/RotaryEncoder/RotaryEncoder.h | 7 +++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libraries/RotaryEncoder/RotaryEncoder.cpp b/libraries/RotaryEncoder/RotaryEncoder.cpp index 16f27251..d87edcec 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.cpp +++ b/libraries/RotaryEncoder/RotaryEncoder.cpp @@ -37,9 +37,7 @@ #include "RotaryEncoder.h" /** - * - * @param debounce Enable hw debouncing - + * Initialize Hardware Encoder */ void RotaryEncoder::begin(void) { @@ -59,8 +57,8 @@ void RotaryEncoder::begin(void) NRF_QDEC->PSEL.LED = _pinled; } - // Enable debounce by default - NRF_QDEC->DBFEN = 1; + // Disable debounce by default + NRF_QDEC->DBFEN = 0; // Reporter is disabled by default @@ -125,19 +123,31 @@ int32_t RotaryEncoder::read(void) // Trigger READ CLR ACC NRF_QDEC->TASKS_RDCLRACC = 1; - // Add to absolute value - int32_t accread = NRF_QDEC->ACCREAD; - _abs += accread; + int32_t val = NRF_QDEC->ACCREAD; - return accread; + // Add to absolute value and buffered step + _abs += val; + _step += val; + + if ( (_step > 1) || (_step < -1) ) + { + val = _step / 2; + _step = _step%2; + }else + { + // not enough to report the turn + val = 0; + } + + return val; } int32_t RotaryEncoder::readAbs(void) { - // first update the abs + // first update the abs value read(); - return _abs; + return _abs/2; } void RotaryEncoder::writeAbs(int32_t value) diff --git a/libraries/RotaryEncoder/RotaryEncoder.h b/libraries/RotaryEncoder/RotaryEncoder.h index 10a2e968..e94df64f 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.h +++ b/libraries/RotaryEncoder/RotaryEncoder.h @@ -47,7 +47,8 @@ class RotaryEncoder _pinb = pinb; _pinled = pinled; - _abs = 0; + _abs = 0; + _step = 0; } void begin(void); @@ -68,7 +69,9 @@ class RotaryEncoder uint8_t _pina, _pinb; int8_t _pinled; - int32_t _abs; + // Note For each turn, encoder generate 2 transitions + int32_t _abs; // Absolute position + int32_t _step; // Moving step that take intermidiate transition in account };