Encoder: Add debounce
parent
8eae11c530
commit
e2ea10152d
|
@ -51,6 +51,8 @@ SwitchEvent handleSwitch() {
|
|||
return isPressed ? SWITCH_PRESSED : SWITCH_RELEASED;
|
||||
}
|
||||
|
||||
unsigned long lastEncoderChangeAt = 0;
|
||||
|
||||
EncoderEvent handleEncoder() {
|
||||
bool clk = digitalRead(CLK_PIN);
|
||||
bool dt = digitalRead(DT_PIN);
|
||||
|
@ -60,7 +62,11 @@ EncoderEvent handleEncoder() {
|
|||
return ENCODER_NONE;
|
||||
}
|
||||
|
||||
// TODO: Do we need to debounce encoder?
|
||||
// debounce
|
||||
auto now = millis();
|
||||
if (now - lastEncoderChangeAt < DEBOUNCE_MS) {
|
||||
return SWITCH_NONE;
|
||||
}
|
||||
|
||||
// clockwise pattern:
|
||||
// clk: 1001 1001
|
||||
|
@ -76,6 +82,7 @@ EncoderEvent handleEncoder() {
|
|||
// save state
|
||||
previousClk = clk;
|
||||
previousDt = dt;
|
||||
lastEncoderChangeAt = now;
|
||||
|
||||
// return event
|
||||
return event;
|
||||
|
|
Loading…
Reference in New Issue