Encoder: Add debounce

main
radex 2024-02-14 17:25:05 +01:00
parent 8eae11c530
commit e2ea10152d
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
1 changed files with 8 additions and 1 deletions

View File

@ -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;