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
40 #define pin(q) AO_BUTTON_ ## q ## _PIN
42 /* pins are inverted */
43 #define ao_button_value(b) !ao_gpio_get(port(b), bit(b), pin(b))
46 _ao_button_get(uint8_t b)
49 #if AO_BUTTON_COUNT > 0
50 case 0: return ao_button_value(0);
52 #if AO_BUTTON_COUNT > 1
53 case 1: return ao_button_value(1);
55 #if AO_BUTTON_COUNT > 2
56 case 2: return ao_button_value(2);
58 #if AO_BUTTON_COUNT > 3
59 case 3: return ao_button_value(3);
61 #if AO_BUTTON_COUNT > 4
62 case 4: return ao_button_value(4);
69 _ao_button_check(uint8_t b)
71 uint8_t value = _ao_button_get(b);
73 if (value != ao_button_state[b].value) {
74 AO_TICK_TYPE now = ao_time();
76 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
77 ao_button_state[b].value = value;
78 ao_button_queue(b, value);
80 ao_button_state[b].time = now;
85 _ao_button_init(uint8_t b)
87 uint8_t m = ao_arch_irqsave();
88 uint8_t value = _ao_button_get(b);
89 ao_button_state[b].value = value;
90 ao_button_state[b].time = ao_time();
91 ao_button_queue(b, value);
92 ao_arch_irqrestore(m);
97 ao_button_get(uint8_t b)
99 return ao_button_state[b].value;
107 for (b = 0; b < AO_BUTTON_COUNT; b++)
111 #define init(b) do { \
112 ao_enable_port(port(b)); \
114 ao_exti_setup(port(b), bit(b), \
115 AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
117 ao_exti_enable(port(b), bit(b)); \
118 _ao_button_init(b); \
124 #if AO_BUTTON_COUNT > 0
127 #if AO_BUTTON_COUNT > 1
130 #if AO_BUTTON_COUNT > 2
133 #if AO_BUTTON_COUNT > 3
136 #if AO_BUTTON_COUNT > 4