update rotary encoder

This commit is contained in:
hathach committed 2017-09-13 21:38:58 +07:00
1 parent a63f53f75d
commit 9177cb48db
2 files changed
+26 -13

No files matched your search

+21 -11
View File
@@ -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)
+5 -2
View File
@@ -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
};