From 8e38170bf83a3e441f0fc9f34ca9a56842a5446a Mon Sep 17 00:00:00 2001
From: lfreixas <lfreixas@iri.upc.edu>
Date: Fri, 10 Jun 2016 17:37:40 +0200
Subject: [PATCH] Added some logic to avoid false edge detections due to
 rebounds on the pushbuttons.

---
 controllers/include/gpio.h |   2 +
 controllers/src/gpio.c     | 116 ++++++++++++++++++++++++++++++-------
 2 files changed, 98 insertions(+), 20 deletions(-)

diff --git a/controllers/include/gpio.h b/controllers/include/gpio.h
index 8f21754..bda0313 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 349880c..e4631b9 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)
-- 
GitLab