altos: debounce buttons
authorKeith Packard <keithp@keithp.com>
Sun, 13 Nov 2011 02:30:56 +0000 (18:30 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 13 Nov 2011 02:30:56 +0000 (18:30 -0800)
Provide API to clear out any button events that happen during startup,
and then discard button events 'too close' together.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/cc1111/ao_arch.h
src/cc1111/ao_button.c

index 001165fafd9bae0b592a4be9f1da975ce8f6e9eb..eb8ce9be01bf835ddd5cbf7ac505029af05a1dd3 100644 (file)
@@ -231,6 +231,9 @@ ao_button_init(void);
 char
 ao_button_get(void) __critical;
 
+void
+ao_button_clear(void) __critical;
+
 /* ao_string.c */
 
 void
index 77a8dde8b6918f4bbaeb49e7b4199d0c2b20ef84..69f3475fe660d43125073a40b19118f3f545c07d 100644 (file)
@@ -36,11 +36,17 @@ static __code struct {
 
 #define NUM_BUTTONS    ((sizeof ao_buttons) / sizeof (ao_buttons[0]))
 
+static __xdata uint16_t ao_button_tick[NUM_BUTTONS];
+
 static void
 ao_button_insert(char n)
 {
-       ao_fifo_insert(ao_button_fifo, n);
-       ao_wakeup(&ao_button_fifo);
+       uint16_t        now = ao_time();
+       if ((now - ao_button_tick[n]) > 20) {
+               ao_button_tick[n] = now;
+               ao_fifo_insert(ao_button_fifo, n);
+               ao_wakeup(&ao_button_fifo);
+       }
 }
 
 static void
@@ -77,6 +83,15 @@ ao_button_get(void) __critical
        return b;
 }
 
+void
+ao_button_clear(void) __critical
+{
+       char b;
+
+       while (!ao_fifo_empty(ao_button_fifo))
+               ao_fifo_remove(ao_button_fifo, b);
+}
+
 void
 ao_p0_isr(void) ao_arch_interrupt(13)
 {