Skip to content
Snippets Groups Projects
Commit 8e38170b authored by Laia Freixas Mateu's avatar Laia Freixas Mateu
Browse files

Added some logic to avoid false edge detections due to rebounds on the pushbuttons.

parent ed45c5cf
No related branches found
No related tags found
No related merge requests found
......@@ -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
*
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment