diff --git a/firmware/input.c b/firmware/input.c index cc1bdef..afa0b6b 100644 --- a/firmware/input.c +++ b/firmware/input.c @@ -21,29 +21,35 @@ inline int8_t get_rotary_delta() return rotary_delta; } +#define ROTARY_DEBOUNCE 150 static inline void input_proc_rotary() { static int8_t last_a; int8_t a; int8_t b; int8_t p; + uint8_t silly_debounce = 0; - p = PORT_ROTARY(PIN); - a = (p & PIN_ROTARY_A); - b = (p & PIN_ROTARY_B); - a = !a; - b = !b; + if (silly_debounce == 0) { + p = PORT_ROTARY(PIN); + a = (p & PIN_ROTARY_A); + b = (p & PIN_ROTARY_B); + a = !a; + b = !b; - if (a == 1 && last_a == 0) { - _delay_ms(1); // TODO - rotary_delta += !!(a ^ b); + if (a == 1 && last_a == 0) { + silly_debounce = ROTARY_DEBOUNCE; + rotary_delta += !!(a ^ b); + } + if (a == 0 && last_a == 1) { + silly_debounce = ROTARY_DEBOUNCE; + rotary_delta -= !(a ^ b); + } + + last_a = a; + } else { + silly_debounce--; } - if (a == 0 && last_a == 1) { - _delay_ms(1); // TODO - rotary_delta -= !(a ^ b); - } - - last_a = a; } // TODO: Pressing two keys at the same time can lead to unfortunate behavior.