update hw encoder

This commit is contained in:
hathach committed 2017-09-27 18:19:21 +07:00
1 parent c4fe926de9
commit 8ad448ebe6
2 files changed
+18 -19

No files matched your search

+12 -15
View File
@@ -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)
+6 -4
View File
@@ -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;
};