From dd00a86a8554831bc6f606b0dda3f1b9e134bc55 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 2 May 2015 11:23:10 +0200 Subject: [PATCH] fix debouncing --- input.c | 60 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/input.c b/input.c index df302d3..125802f 100644 --- a/input.c +++ b/input.c @@ -24,13 +24,35 @@ /* *********************************************************************** */ +#define INPUT_DEBOUNCE_TIMER 60 + +/* *********************************************************************** */ + static uint8_t pcint_old; static uint8_t quad_state; static uint8_t quad_state_old; static uint8_t debounce_timer[2]; +static uint8_t input_old_state[2]; /* *********************************************************************** */ +static void input_send_event(uint8_t channel, uint8_t state) +{ + if ((channel != EVENT_NUM_INPUT_BUTTON) && (channel != EVENT_NUM_INPUT_DOOR)) + { + return; + } + + if ((debounce_timer[channel -2] == 0) && (state != input_old_state[channel -2])) + { + event_queue(EVENT_TYPE_INPUT, channel, state); + + debounce_timer[channel -2] = INPUT_DEBOUNCE_TIMER; + input_old_state[channel -2] = state; + } +} /* input_send_event */ + + ISR(PCINT1_vect) { uint8_t pcint_new = PINC & PCMSK1; @@ -80,28 +102,12 @@ ISR(PCINT1_vect) /* quadruple decoder switch changed */ else if (pcint & (1< 0) { debounce_timer[0]--; + if (debounce_timer[0] == 0) + { + input_send_event(EVENT_NUM_INPUT_BUTTON, !!(PINC & (1< 0) { debounce_timer[1]--; + if (debounce_timer[1] == 0) + { + input_send_event(EVENT_NUM_INPUT_DOOR, !!(PINC & (1<