Skip to content

Commit 8ad448e

Browse files
committed
update hw encoder
1 parent c4fe926 commit 8ad448e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

libraries/RotaryEncoder/RotaryEncoder.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void HwRotaryEncoder::begin(uint8_t pina, uint8_t pinb, int8_t pinled)
5151
// default sample period
5252
NRF_QDEC->SAMPLEPER = QDEC_SAMPLEPER_SAMPLEPER_128us;
5353

54-
pinMode(pina, INPUT);
55-
pinMode(pinb, INPUT);
54+
pinMode(pina, INPUT_PULLUP);
55+
pinMode(pinb, INPUT_PULLUP);
5656

5757
NRF_QDEC->PSEL.A = pina;
5858
NRF_QDEC->PSEL.B = pinb;
@@ -151,24 +151,21 @@ int32_t HwRotaryEncoder::read(void)
151151
// Trigger READ CLR ACC
152152
NRF_QDEC->TASKS_RDCLRACC = 1;
153153

154-
// Nordic QDEC CW is negative, CCW is positve --> invert
154+
// Nordic QDEC CW is negative, CCW is positive --> invert
155155
int32_t val = -(NRF_QDEC->ACCREAD);
156156

157157
// Add to absolute value and buffered step
158-
_abs += val;
159-
_step += val;
158+
_abs += val;
160159

161-
if ( (_step > 1) || (_step < -1) )
162-
{
163-
val = _step / 2;
164-
_step = _step%2;
165-
}else
166-
{
167-
// not enough to report the turn
168-
val = 0;
169-
}
160+
int32_t diff = (_abs - _last) / 2;
170161

171-
return val;
162+
if ( diff ) _last = _abs;
163+
return diff;
164+
}
165+
166+
int32_t HwRotaryEncoder::readDebug(void)
167+
{
168+
return _abs;
172169
}
173170

174171
int32_t HwRotaryEncoder::readAbs(void)

libraries/RotaryEncoder/RotaryEncoder.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class HwRotaryEncoder
4646

4747
HwRotaryEncoder(void)
4848
{
49-
_abs = 0;
50-
_step = 0;
51-
_cb = NULL;
49+
_abs = _last = 0;
50+
_cb = NULL;
5251
}
5352

5453
void begin(uint8_t pina, uint8_t pinb, int8_t pinled = -1);
@@ -66,13 +65,16 @@ class HwRotaryEncoder
6665
void writeAbs(int32_t value);
6766
void clearAbs(void);
6867

68+
69+
int32_t readDebug(void);
70+
6971
// Internal API
7072
void _irq_handler(void);
7173

7274
private:
7375
// Note For each turn, encoder generate 2 transitions
7476
int32_t _abs; // Absolute position
75-
int32_t _step; // Moving step that take intermediate transition in account
77+
int32_t _last;
7678

7779
callback_t _cb;
7880
};

0 commit comments

Comments
 (0)