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>
21 #include <ao_debounce.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_HOLD 10
31 static struct ao_debounce ao_button_debounce[AO_BUTTON_COUNT];
33 #define port(q) AO_BUTTON_ ## q ## _PORT
34 #define bit(q) AO_BUTTON_ ## q
35 #define pin(q) AO_BUTTON_ ## q ## _PIN
37 /* pins are inverted */
38 #define ao_button_value(b) !ao_gpio_get(port(b), bit(b), pin(b))
41 _ao_button_get(struct ao_debounce *debounce)
43 uint8_t b = debounce - ao_button_debounce;
46 #if AO_BUTTON_COUNT > 0
47 case 0: return ao_button_value(0);
49 #if AO_BUTTON_COUNT > 1
50 case 1: return ao_button_value(1);
52 #if AO_BUTTON_COUNT > 2
53 case 2: return ao_button_value(2);
55 #if AO_BUTTON_COUNT > 3
56 case 3: return ao_button_value(3);
58 #if AO_BUTTON_COUNT > 4
59 case 4: return ao_button_value(4);
66 _ao_button_set(struct ao_debounce *debounce, uint8_t value)
68 uint8_t b = debounce - ao_button_debounce;
70 ao_button_queue(b, value);
74 #define ao_button_update(b) ao_button_do(b, ao_gpio_get(port(b), bit(b), pin(b)))
77 ao_button_debounce_init(struct ao_debounce *debounce) {
78 ao_debounce_config(debounce,
81 AO_BUTTON_DEBOUNCE_HOLD);
89 for (b = 0; b < AO_BUTTON_COUNT; b++)
90 _ao_debounce_start(&ao_button_debounce[b]);
93 #define init(b) do { \
94 ao_button_debounce_init(&ao_button_debounce[b]); \
95 ao_enable_port(port(b)); \
97 ao_exti_setup(port(b), bit(b), \
98 AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
100 ao_exti_enable(port(b), bit(b)); \
106 #if AO_BUTTON_COUNT > 0
109 #if AO_BUTTON_COUNT > 1
112 #if AO_BUTTON_COUNT > 2
115 #if AO_BUTTON_COUNT > 3
118 #if AO_BUTTON_COUNT > 4