2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_button.h>
23 #define ao_button_queue(b,v) ao_event_put_isr(AO_EVENT_BUTTON, b, v)
25 #define ao_button_queue(b,v)
28 #define AO_BUTTON_DEBOUNCE_INTERVAL AO_MS_TO_TICKS(50)
30 struct ao_button_state {
35 static struct ao_button_state ao_button_state[AO_BUTTON_COUNT];
37 #define port(q) AO_BUTTON_ ## q ## _PORT
38 #define bit(q) AO_BUTTON_ ## q
39 #define pin(q) AO_BUTTON_ ## q ## _PIN
41 /* pins are inverted */
42 #define ao_button_value(b) !ao_gpio_get(port(b), bit(b), pin(b))
45 _ao_button_get(uint8_t b)
48 #if AO_BUTTON_COUNT > 0
49 case 0: return ao_button_value(0);
51 #if AO_BUTTON_COUNT > 1
52 case 1: return ao_button_value(1);
54 #if AO_BUTTON_COUNT > 2
55 case 2: return ao_button_value(2);
57 #if AO_BUTTON_COUNT > 3
58 case 3: return ao_button_value(3);
60 #if AO_BUTTON_COUNT > 4
61 case 4: return ao_button_value(4);
68 _ao_button_check(uint8_t b)
70 uint8_t value = _ao_button_get(b);
72 if (value != ao_button_state[b].value) {
73 AO_TICK_TYPE now = ao_time();
75 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
76 ao_button_state[b].value = value;
77 ao_button_queue(b, value);
79 ao_button_state[b].time = now;
84 _ao_button_init(uint8_t b)
86 uint8_t m = ao_arch_irqsave();
87 uint8_t value = _ao_button_get(b);
88 ao_button_state[b].value = value;
89 ao_button_state[b].time = ao_time();
90 ao_button_queue(b, value);
91 ao_arch_irqrestore(m);
100 for (b = 0; b < AO_BUTTON_COUNT; b++)
104 #define init(b) do { \
105 ao_enable_port(port(b)); \
107 ao_exti_setup(port(b), bit(b), \
108 AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
110 ao_exti_enable(port(b), bit(b)); \
111 _ao_button_init(b); \
117 #if AO_BUTTON_COUNT > 0
120 #if AO_BUTTON_COUNT > 1
123 #if AO_BUTTON_COUNT > 2
126 #if AO_BUTTON_COUNT > 3
129 #if AO_BUTTON_COUNT > 4