fw: Fix debouncing for rotary encoder

This commit is contained in:
Markus Koch 2020-07-30 21:41:22 +02:00
parent ba069861ff
commit 86b6b0897a
1 changed files with 20 additions and 14 deletions

View File

@ -21,29 +21,35 @@ inline int8_t get_rotary_delta()
return rotary_delta; return rotary_delta;
} }
#define ROTARY_DEBOUNCE 150
static inline void input_proc_rotary() static inline void input_proc_rotary()
{ {
static int8_t last_a; static int8_t last_a;
int8_t a; int8_t a;
int8_t b; int8_t b;
int8_t p; int8_t p;
uint8_t silly_debounce = 0;
p = PORT_ROTARY(PIN); if (silly_debounce == 0) {
a = (p & PIN_ROTARY_A); p = PORT_ROTARY(PIN);
b = (p & PIN_ROTARY_B); a = (p & PIN_ROTARY_A);
a = !a; b = (p & PIN_ROTARY_B);
b = !b; a = !a;
b = !b;
if (a == 1 && last_a == 0) { if (a == 1 && last_a == 0) {
_delay_ms(1); // TODO silly_debounce = ROTARY_DEBOUNCE;
rotary_delta += !!(a ^ b); 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. // TODO: Pressing two keys at the same time can lead to unfortunate behavior.