diff --git a/controllers/include/gpio.h b/controllers/include/gpio.h index 8f217548505bfc9101b71eb9801f1454a89afe2a..bda03130b0a5dd22d083fa3daec19c8d66e913d4 100644 --- a/controllers/include/gpio.h +++ b/controllers/include/gpio.h @@ -16,6 +16,8 @@ typedef enum {LED_BAT=0x01,LED_TxD=0x02,LED_RxD=0x04,LED_AUX=0x08,LED_MANAGE=0x1 typedef enum {BTN_UP=0x10,BTN_DOWN=0x20,BTN_LEFT=0x40,BTN_RIGHT=0x80,BTN_START=0x01} pushbuttons_t; #define NUM_BUTTONS 5 +#define REBOUND_COUNT 20 + /** * \brief Function to initialize the LED's module * diff --git a/controllers/src/gpio.c b/controllers/src/gpio.c index 349880c42ef79a80081600d0db80342ead37cd07..e4631b9f8d5824d163068f890590729de47e031c 100644 --- a/controllers/src/gpio.c +++ b/controllers/src/gpio.c @@ -5,6 +5,7 @@ typedef struct{ volatile uint8_t rising_edge; volatile uint8_t falling_edge; volatile uint8_t old_state; + volatile uint8_t state_count; }TButtonState; /* private variables */ @@ -22,85 +23,155 @@ void pushbuttons_loop(void) button_up.pressed=0x01; if(button_up.old_state==0x00) { - button_up.rising_edge=0x01; - button_up.old_state=0x01; + button_up.state_count++; + if(button_up.state_count==REBOUND_COUNT) + { + button_up.rising_edge=0x01; + button_up.old_state=0x01; + button_up.state_count=0; + } } + else + button_up.state_count=0; } else { if(button_up.old_state==0x01) { - button_up.falling_edge=0x01; - button_up.old_state=0x00; + button_up.state_count++; + if(button_up.state_count==REBOUND_COUNT) + { + button_up.falling_edge=0x01; + button_up.old_state=0x00; + button_up.state_count=0; + } } + else + button_up.state_count=0; } if((~PINE & BTN_DOWN)!=0x00) { button_down.pressed=0x01; if(button_down.old_state==0x00) { - button_down.rising_edge=0x01; - button_down.old_state=0x01; + button_down.state_count++; + if(button_down.state_count==REBOUND_COUNT) + { + button_down.rising_edge=0x01; + button_down.old_state=0x01; + button_down.state_count=0; + } } + else + button_down.state_count=0; } else { if(button_down.old_state==0x01) { - button_down.falling_edge=0x01; - button_down.old_state=0x00; + button_down.state_count++; + if(button_down.state_count==REBOUND_COUNT) + { + button_down.falling_edge=0x01; + button_down.old_state=0x00; + button_down.state_count=0; + } } + else + button_down.state_count=0; } if((~PINE & BTN_LEFT)!=0x00) { button_left.pressed=0x01; if(button_left.old_state==0x00) { - button_left.rising_edge=0x01; - button_left.old_state=0x01; + button_left.state_count++; + if(button_left.state_count==REBOUND_COUNT) + { + button_left.rising_edge=0x01; + button_left.old_state=0x01; + button_left.state_count=0; + } } + else + button_left.state_count=0; } else { if(button_left.old_state==0x01) { - button_left.falling_edge=0x01; - button_left.old_state=0x00; + button_left.state_count++; + if(button_left.state_count==REBOUND_COUNT) + { + button_left.falling_edge=0x01; + button_left.old_state=0x00; + button_left.state_count=0; + } } + else + button_left.state_count=0; } if((~PINE & BTN_RIGHT)!=0x00) { button_right.pressed=0x01; if(button_right.old_state==0x00) { - button_right.rising_edge=0x01; - button_right.old_state=0x01; + button_right.state_count++; + if(button_right.state_count==REBOUND_COUNT) + { + button_right.rising_edge=0x01; + button_right.old_state=0x01; + button_right.state_count=0; + } } + else + button_right.state_count=0; } else { if(button_right.old_state==0x01) { - button_right.falling_edge=0x01; - button_right.old_state=0x00; + button_right.state_count++; + if(button_right.state_count==REBOUND_COUNT) + { + button_right.falling_edge=0x01; + button_right.old_state=0x00; + button_right.state_count=0; + } } + else + button_right.state_count=0; } if((~PIND & BTN_START)!=0x00) { button_start.pressed=0x01; if(button_start.old_state==0x00) { - button_start.rising_edge=0x01; - button_start.old_state=0x01; + button_start.state_count++; + if(button_start.state_count==REBOUND_COUNT) + { + button_start.rising_edge=0x01; + button_start.old_state=0x01; + button_start.state_count=0; + } } + else + button_start.state_count=0; } else { if(button_start.old_state==0x01) { - button_start.falling_edge=0x01; - button_start.old_state=0x00; + button_start.state_count++; + if(button_start.state_count==REBOUND_COUNT) + { + button_start.falling_edge=0x01; + button_start.old_state=0x00; + button_start.state_count=0; + } } + else + button_start.state_count=0; } } @@ -117,22 +188,27 @@ void init_leds(void) button_up.rising_edge=0x00; button_up.falling_edge=0x00; button_up.old_state=0x00; + button_up.state_count=0x00; button_down.pressed=0x00; button_down.rising_edge=0x00; button_down.falling_edge=0x00; button_down.old_state=0x00; + button_down.state_count=0x00; button_left.pressed=0x00; button_left.rising_edge=0x00; button_left.falling_edge=0x00; button_left.old_state=0x00; + button_left.state_count=0x00; button_right.pressed=0x00; button_right.rising_edge=0x00; button_right.falling_edge=0x00; button_right.old_state=0x00; + button_right.state_count=0x00; button_start.pressed=0x00; button_start.rising_edge=0x00; button_start.falling_edge=0x00; button_start.old_state=0x00; + button_start.state_count=0x00; } void turn_led_on(leds_t led_id)