HD200 - more work on remote handling

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27306 a1c6a512-1295-4272-9138-f99709370657
diff --git a/firmware/target/coldfire/mpio/hd200/button-hd200.c b/firmware/target/coldfire/mpio/hd200/button-hd200.c
index 880f5fd..fa0a5cf 100644
--- a/firmware/target/coldfire/mpio/hd200/button-hd200.c
+++ b/firmware/target/coldfire/mpio/hd200/button-hd200.c
@@ -26,6 +26,15 @@
 #include "backlight.h"
 #include "adc.h"
 
+static bool remote_detect(void)
+{
+    /* When there is no remote adc readout
+     * is exactly 0. We add some margin
+     * for ADC readout instability
+     */
+    return adc_scan(ADC_REMOTE)>10?true:false;
+}
+
 void button_init_device(void)
 {
     /* GPIO56 (main PLAY) 
@@ -44,7 +53,11 @@
 
 bool remote_button_hold(void)
 {
-    return adc_scan(ADC_REMOTE)<50?true:false;
+    /* On my remote hold gives readout of 44 */
+    if (remote_detect())
+        return adc_scan(ADC_REMOTE)<50?true:false;
+    else
+        return false;
 }
 
 /*
@@ -55,9 +68,13 @@
     int btn = BUTTON_NONE;
     int data = 0;
     static bool hold_button = false;
-    static bool remote_hold_button = false;
+    bool remote_hold_button = false;
 
     bool hold_button_old;
+    bool remote_present;
+
+    /* check if we have remote connected */
+    remote_present = remote_detect();
 
     /* read hold buttons status */
     hold_button_old = hold_button;
@@ -70,6 +87,7 @@
         backlight_hold_changed(hold_button);
 #endif
 
+    /* Skip if main hold is active */
     if (!hold_button)
     {
         data = adc_scan(ADC_BUTTONS);
@@ -110,7 +128,8 @@
         }
     }
 
-    if (!remote_hold_button)
+    /* Skip if remote is not present or remote_hold is active */
+    if (remote_present && !remote_hold_button)
     {
         data = adc_scan(ADC_REMOTE);
 
@@ -146,6 +165,9 @@
         }
     }
 
+    /* PLAY buttons (both remote and main) are
+     * GPIOs not ADC
+     */
     data = GPIO1_READ;
 
     /* GPIO56 active high main PLAY/PAUSE/ON */
@@ -153,7 +175,7 @@
         btn |= BUTTON_PLAY;
 
     /* GPIO41 active high remote PLAY/PAUSE/ON */
-    if (!remote_hold_button && ((data & (1<<9))))
+    if (remote_present && !remote_hold_button && ((data & (1<<9))))
         btn |= BUTTON_RC_PLAY;
         
     return btn;