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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_button.h>
24 #define ao_button_queue(b,v) ao_event_put_isr(AO_EVENT_BUTTON, b, v)
26 #define ao_button_queue(b,v)
29 #define AO_BUTTON_DEBOUNCE_INTERVAL AO_MS_TO_TICKS(50)
31 struct ao_button_state {
36 static struct ao_button_state ao_button_state[AO_BUTTON_COUNT];
38 #define port(q) AO_BUTTON_ ## q ## _PORT
39 #define bit(q) AO_BUTTON_ ## q
41 #ifndef AO_BUTTON_INVERTED
42 #define AO_BUTTON_INVERTED 1
45 #if AO_BUTTON_INVERTED
46 /* pins are inverted */
47 #define ao_button_value(b) !ao_gpio_get(port(b), bit(b))
49 #define ao_button_value(b) ao_gpio_get(port(b), bit(b))
53 _ao_button_get(uint8_t b)
56 #if AO_BUTTON_COUNT > 0
57 case 0: return ao_button_value(0);
59 #if AO_BUTTON_COUNT > 1
60 case 1: return ao_button_value(1);
62 #if AO_BUTTON_COUNT > 2
63 case 2: return ao_button_value(2);
65 #if AO_BUTTON_COUNT > 3
66 case 3: return ao_button_value(3);
68 #if AO_BUTTON_COUNT > 4
69 case 4: return ao_button_value(4);
71 #if AO_BUTTON_COUNT > 5
72 case 5: return ao_button_value(5);
74 #if AO_BUTTON_COUNT > 6
75 case 6: return ao_button_value(6);
77 #if AO_BUTTON_COUNT > 7
78 case 7: return ao_button_value(7);
80 #if AO_BUTTON_COUNT > 8
81 case 8: return ao_button_value(8);
83 #if AO_BUTTON_COUNT > 9
84 case 9: return ao_button_value(9);
86 #if AO_BUTTON_COUNT > 10
87 case 10: return ao_button_value(10);
89 #if AO_BUTTON_COUNT > 11
90 case 11: return ao_button_value(11);
92 #if AO_BUTTON_COUNT > 12
93 case 12: return ao_button_value(12);
95 #if AO_BUTTON_COUNT > 13
96 case 13: return ao_button_value(13);
98 #if AO_BUTTON_COUNT > 14
99 case 14: return ao_button_value(14);
101 #if AO_BUTTON_COUNT > 15
102 case 15: return ao_button_value(15);
109 _ao_button_check(uint8_t b)
111 uint8_t value = _ao_button_get(b);
113 if (value != ao_button_state[b].value) {
114 AO_TICK_TYPE now = ao_time();
116 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
117 ao_button_state[b].value = value;
118 ao_button_queue(b, value);
120 ao_button_state[b].time = now;
125 _ao_button_init(uint8_t b)
127 uint8_t m = ao_arch_irqsave();
128 uint8_t value = _ao_button_get(b);
129 ao_button_state[b].value = value;
130 ao_button_state[b].time = ao_time();
131 ao_button_queue(b, value);
132 ao_arch_irqrestore(m);
137 ao_button_get(uint8_t b)
139 return ao_button_state[b].value;
147 for (b = 0; b < AO_BUTTON_COUNT; b++)
151 #define init(b) do { \
152 ao_enable_port(port(b)); \
154 ao_exti_setup(port(b), bit(b), \
155 AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
157 ao_exti_enable(port(b), bit(b)); \
158 _ao_button_init(b); \
164 #if AO_BUTTON_COUNT > 0
167 #if AO_BUTTON_COUNT > 1
170 #if AO_BUTTON_COUNT > 2
173 #if AO_BUTTON_COUNT > 3
176 #if AO_BUTTON_COUNT > 4
179 #if AO_BUTTON_COUNT > 5
182 #if AO_BUTTON_COUNT > 6
185 #if AO_BUTTON_COUNT > 7
188 #if AO_BUTTON_COUNT > 8
191 #if AO_BUTTON_COUNT > 9
194 #if AO_BUTTON_COUNT > 10
197 #if AO_BUTTON_COUNT > 11
200 #if AO_BUTTON_COUNT > 12
203 #if AO_BUTTON_COUNT > 13
206 #if AO_BUTTON_COUNT > 14
209 #if AO_BUTTON_COUNT > 15
212 #if AO_BUTTON_COUNT > 16
213 #error too many buttons