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>
22 #include <ao_fast_timer.h>
25 #define ao_button_queue(b,v) ao_event_put_isr(AO_EVENT_BUTTON, b, v)
27 #define ao_button_queue(b,v)
30 #define AO_BUTTON_DEBOUNCE_INTERVAL AO_MS_TO_TICKS(50)
32 struct ao_button_state {
37 static struct ao_button_state ao_button_state[AO_BUTTON_COUNT];
39 #define port(q) AO_BUTTON_ ## q ## _PORT
40 #define bit(q) AO_BUTTON_ ## q
42 #ifndef AO_BUTTON_INVERTED
43 #define AO_BUTTON_INVERTED 1
46 #if AO_BUTTON_INVERTED
47 /* pins are inverted */
48 #define ao_button_value(b) !ao_gpio_get(port(b), bit(b))
50 #define ao_button_value(b) ao_gpio_get(port(b), bit(b))
54 _ao_button_get(uint8_t b)
57 #if AO_BUTTON_COUNT > 0
58 case 0: return ao_button_value(0);
60 #if AO_BUTTON_COUNT > 1
61 case 1: return ao_button_value(1);
63 #if AO_BUTTON_COUNT > 2
64 case 2: return ao_button_value(2);
66 #if AO_BUTTON_COUNT > 3
67 case 3: return ao_button_value(3);
69 #if AO_BUTTON_COUNT > 4
70 case 4: return ao_button_value(4);
72 #if AO_BUTTON_COUNT > 5
73 case 5: return ao_button_value(5);
75 #if AO_BUTTON_COUNT > 6
76 case 6: return ao_button_value(6);
78 #if AO_BUTTON_COUNT > 7
79 case 7: return ao_button_value(7);
81 #if AO_BUTTON_COUNT > 8
82 case 8: return ao_button_value(8);
84 #if AO_BUTTON_COUNT > 9
85 case 9: return ao_button_value(9);
87 #if AO_BUTTON_COUNT > 10
88 case 10: return ao_button_value(10);
90 #if AO_BUTTON_COUNT > 11
91 case 11: return ao_button_value(11);
93 #if AO_BUTTON_COUNT > 12
94 case 12: return ao_button_value(12);
96 #if AO_BUTTON_COUNT > 13
97 case 13: return ao_button_value(13);
99 #if AO_BUTTON_COUNT > 14
100 case 14: return ao_button_value(14);
102 #if AO_BUTTON_COUNT > 15
103 case 15: return ao_button_value(15);
110 _ao_button_check(uint8_t b)
112 uint8_t value = _ao_button_get(b);
114 if (value != ao_button_state[b].value) {
115 AO_TICK_TYPE now = ao_time();
117 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
118 ao_button_state[b].value = value;
119 ao_button_queue(b, value);
121 ao_button_state[b].time = now;
126 _ao_button_init(uint8_t b)
128 uint8_t m = ao_arch_irqsave();
129 uint8_t value = _ao_button_get(b);
130 ao_button_state[b].value = value;
131 ao_button_state[b].time = ao_time();
132 ao_button_queue(b, value);
133 ao_arch_irqrestore(m);
138 ao_button_get(uint8_t b)
140 return ao_button_state[b].value;
148 for (b = 0; b < AO_BUTTON_COUNT; b++)
152 #define init(b) do { \
153 ao_enable_input(port(b), bit(b), AO_BUTTON_MODE); \
154 _ao_button_init(b); \
160 #if AO_BUTTON_COUNT > 0
163 #if AO_BUTTON_COUNT > 1
166 #if AO_BUTTON_COUNT > 2
169 #if AO_BUTTON_COUNT > 3
172 #if AO_BUTTON_COUNT > 4
175 #if AO_BUTTON_COUNT > 5
178 #if AO_BUTTON_COUNT > 6
181 #if AO_BUTTON_COUNT > 7
184 #if AO_BUTTON_COUNT > 8
187 #if AO_BUTTON_COUNT > 9
190 #if AO_BUTTON_COUNT > 10
193 #if AO_BUTTON_COUNT > 11
196 #if AO_BUTTON_COUNT > 12
199 #if AO_BUTTON_COUNT > 13
202 #if AO_BUTTON_COUNT > 14
205 #if AO_BUTTON_COUNT > 15
208 #if AO_BUTTON_COUNT > 16
209 #error too many buttons
211 ao_fast_timer_init();
212 ao_fast_timer_on(ao_button_isr);