From 5380376dbc50d9a660d1a4be6d8f813f7f1b2d58 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Sun, 16 Apr 2006 22:28:24 +0000 Subject: [PATCH] H300 (and H1x0): Improved button debouncing. Solves the possible 'fake doubleclick' effect by not pretending no button is pressed if the reading is unstable. Now it uses the latest stable reading instead. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@9691 a1c6a512-1295-4272-9138-f99709370657 --- firmware/drivers/button.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/firmware/drivers/button.c b/firmware/drivers/button.c index e3998ae8a5..0c2e1e51f5 100644 --- a/firmware/drivers/button.c +++ b/firmware/drivers/button.c @@ -824,7 +824,8 @@ static int button_read(void) #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) static bool hold_button = false; static bool remote_hold_button = false; - static int last_button_val= 0xff; + static int prev_data = 0xff; + static int last_valid = 0xff; /* light handling */ if (hold_button && !button_hold()) @@ -843,8 +844,16 @@ static int button_read(void) if (!hold_button) { 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 CONFIG_KEYPAD == IRIVER_H100_PAD - if ((data < 0xf0) && ((unsigned)(data - last_button_val + 1) <= 2)) + if (data < 0xf0) { if (data < 0x80) if (data < 0x30) @@ -870,7 +879,7 @@ static int button_read(void) btn = BUTTON_REC; } #else /* H300 */ - if ((data < 0xba) && ((unsigned)(data - last_button_val + 1) <= 2)) + if (data < 0xba) { if (data < 0x54) if (data < 0x30) @@ -890,7 +899,6 @@ static int button_read(void) btn = BUTTON_OFF; } #endif - last_button_val = data; } /* remote buttons */