Skip to content

Commit 351cd82

Browse files
committed
enhance software encoder
1 parent 8ad448e commit 351cd82

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

libraries/RotaryEncoder/SwRotaryEncoder.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ bool SwRotaryEncoder::begin(uint8_t pina, uint8_t pinb)
7474
_pina = pina;
7575
_pinb = pinb;
7676

77-
pinMode(_pina, INPUT);
78-
pinMode(_pinb, INPUT);
77+
pinMode(_pina, INPUT_PULLUP);
78+
pinMode(_pinb, INPUT_PULLUP);
7979

8080
_a_last = digitalRead(_pina);
8181

@@ -120,13 +120,14 @@ void SwRotaryEncoder::clearAbs(void)
120120

121121
void SwRotaryEncoder::_irq_handler(void)
122122
{
123-
uint8_t val = (uint8_t) digitalRead(_pina);
123+
uint32_t val = NRF_GPIO->IN;
124+
uint8_t bita = bitRead(val, _pina);
124125

125-
if ( val != _a_last )
126+
if ( bita != _a_last)
126127
{
127-
int32_t step = 0;
128+
int32_t step;
128129

129-
if ( val != ((uint8_t) digitalRead(_pinb)) )
130+
if ( bita != bitRead(val, _pinb) )
130131
{
131132
step = 1;
132133
}else
@@ -135,7 +136,7 @@ void SwRotaryEncoder::_irq_handler(void)
135136
}
136137

137138
_abs += step;
138-
_a_last = val;
139+
_a_last = bita;
139140

140141
if (_cb) ada_callback_fromISR(NULL, _cb, step);
141142
}

libraries/RotaryEncoder/SwRotaryEncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class SwRotaryEncoder
7373
uint8_t _a_last;
7474

7575
int32_t _abs;
76-
int32_t _last_read;
76+
int32_t _last_read; // debouncing
7777

7878
callback_t _cb;
7979
};

0 commit comments

Comments
 (0)