Add extra filtering to the X5 and M5 button drivers to prevent erratic button events (ported from H300).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12627 a1c6a512-1295-4272-9138-f99709370657
diff --git a/firmware/target/coldfire/iaudio/m5/button-m5.c b/firmware/target/coldfire/iaudio/m5/button-m5.c
index a5fdd79..5d49bf1 100644
--- a/firmware/target/coldfire/iaudio/m5/button-m5.c
+++ b/firmware/target/coldfire/iaudio/m5/button-m5.c
@@ -60,6 +60,8 @@
     int  btn = BUTTON_NONE;
     bool hold_button_old;
     bool remote_hold_button_old;
+    static int prev_data = 0xff;
+    static int last_valid = 0xff;
     int  data;
 
     /* normal buttons */
@@ -76,6 +78,13 @@
     {
         data = adc_scan(ADC_BUTTONS);
 
+        /* ADC debouncing: Only accept new reading if it's
+         * stable (+/-1). Use latest stable value otherwise. */
+        if ((unsigned)(data - prev_data + 1) <= 2)
+            last_valid = data;
+        prev_data = data;
+        data = last_valid;
+        
         if (data < 0xf0)
         {
             if(data < 0x7c)
diff --git a/firmware/target/coldfire/iaudio/x5/button-x5.c b/firmware/target/coldfire/iaudio/x5/button-x5.c
index 7cb86ff..6182262 100644
--- a/firmware/target/coldfire/iaudio/x5/button-x5.c
+++ b/firmware/target/coldfire/iaudio/x5/button-x5.c
@@ -60,6 +60,8 @@
     int  btn = BUTTON_NONE;
     bool hold_button_old;
     bool remote_hold_button_old;
+    static int prev_data = 0xff;
+    static int last_valid = 0xff;
     int  data;
 
     /* normal buttons */
@@ -76,6 +78,13 @@
     {
         data = adc_scan(ADC_BUTTONS);
 
+        /* ADC debouncing: Only accept new reading if it's
+         * stable (+/-1). Use latest stable value otherwise. */
+        if ((unsigned)(data - prev_data + 1) <= 2)
+            last_valid = data;
+        prev_data = data;
+        data = last_valid;
+        
         if (data < 0xf0)
         {
             if(data < 0x7c)