altos/telefireone-v1.0: Track ao_led_init API change
[fw/altos] / src / drivers / ao_button.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_button.h>
21 #include <ao_exti.h>
22 #if AO_EVENT
23 #include <ao_event.h>
24 #define ao_button_queue(b,v)    ao_event_put_isr(AO_EVENT_BUTTON, b, v)
25 #else
26 #define ao_button_queue(b,v)
27 #endif
28
29 #define AO_BUTTON_DEBOUNCE_INTERVAL     AO_MS_TO_TICKS(50)
30
31 struct ao_button_state {
32         AO_TICK_TYPE    time;
33         uint8_t         value;
34 };
35
36 static struct ao_button_state   ao_button_state[AO_BUTTON_COUNT];
37
38 #define port(q) AO_BUTTON_ ## q ## _PORT
39 #define bit(q) AO_BUTTON_ ## q
40
41 #ifndef AO_BUTTON_INVERTED
42 #define AO_BUTTON_INVERTED      1
43 #endif
44
45 #if AO_BUTTON_INVERTED
46 /* pins are inverted */
47 #define ao_button_value(b)      !ao_gpio_get(port(b), bit(b))
48 #else
49 #define ao_button_value(b)      ao_gpio_get(port(b), bit(b))
50 #endif
51
52 static uint8_t
53 _ao_button_get(uint8_t b)
54 {
55         switch (b) {
56 #if AO_BUTTON_COUNT > 0
57         case 0: return ao_button_value(0);
58 #endif
59 #if AO_BUTTON_COUNT > 1
60         case 1: return ao_button_value(1);
61 #endif
62 #if AO_BUTTON_COUNT > 2
63         case 2: return ao_button_value(2);
64 #endif
65 #if AO_BUTTON_COUNT > 3
66         case 3: return ao_button_value(3);
67 #endif
68 #if AO_BUTTON_COUNT > 4
69         case 4: return ao_button_value(4);
70 #endif
71 #if AO_BUTTON_COUNT > 5
72         case 5: return ao_button_value(5);
73 #endif
74 #if AO_BUTTON_COUNT > 6
75         case 6: return ao_button_value(6);
76 #endif
77 #if AO_BUTTON_COUNT > 7
78         case 7: return ao_button_value(7);
79 #endif
80 #if AO_BUTTON_COUNT > 8
81         case 8: return ao_button_value(8);
82 #endif
83 #if AO_BUTTON_COUNT > 9
84         case 9: return ao_button_value(9);
85 #endif
86 #if AO_BUTTON_COUNT > 10
87         case 10: return ao_button_value(10);
88 #endif
89 #if AO_BUTTON_COUNT > 11
90         case 11: return ao_button_value(11);
91 #endif
92 #if AO_BUTTON_COUNT > 12
93         case 12: return ao_button_value(12);
94 #endif
95 #if AO_BUTTON_COUNT > 13
96         case 13: return ao_button_value(13);
97 #endif
98 #if AO_BUTTON_COUNT > 14
99         case 14: return ao_button_value(14);
100 #endif
101 #if AO_BUTTON_COUNT > 15
102         case 15: return ao_button_value(15);
103 #endif
104         }
105         return 0;
106 }
107
108 static void
109 _ao_button_check(uint8_t b)
110 {
111         uint8_t value = _ao_button_get(b);
112
113         if (value != ao_button_state[b].value) {
114                 AO_TICK_TYPE    now = ao_time();
115
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);
119                 }
120                 ao_button_state[b].time = now;
121         }
122 }
123
124 static void
125 _ao_button_init(uint8_t b)
126 {
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);
133
134 }
135
136 uint8_t
137 ao_button_get(uint8_t b)
138 {
139         return ao_button_state[b].value;
140 }
141
142 static void
143 ao_button_isr(void)
144 {
145         uint8_t b;
146
147         for (b = 0; b < AO_BUTTON_COUNT; b++)
148                 _ao_button_check(b);
149 }
150
151 #define init(b) do {                                                    \
152                 ao_enable_port(port(b));                                \
153                                                                         \
154                 ao_exti_setup(port(b), bit(b),                          \
155                               AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
156                               ao_button_isr);                           \
157                 ao_exti_enable(port(b), bit(b));                        \
158                 _ao_button_init(b);                                     \
159         } while (0)
160
161 void
162 ao_button_init(void)
163 {
164 #if AO_BUTTON_COUNT > 0
165         init(0);
166 #endif
167 #if AO_BUTTON_COUNT > 1
168         init(1);
169 #endif
170 #if AO_BUTTON_COUNT > 2
171         init(2);
172 #endif
173 #if AO_BUTTON_COUNT > 3
174         init(3);
175 #endif
176 #if AO_BUTTON_COUNT > 4
177         init(4);
178 #endif
179 #if AO_BUTTON_COUNT > 5
180         init(5);
181 #endif
182 #if AO_BUTTON_COUNT > 6
183         init(6);
184 #endif
185 #if AO_BUTTON_COUNT > 7
186         init(7);
187 #endif
188 #if AO_BUTTON_COUNT > 8
189         init(8);
190 #endif
191 #if AO_BUTTON_COUNT > 9
192         init(9);
193 #endif
194 #if AO_BUTTON_COUNT > 10
195         init(10);
196 #endif
197 #if AO_BUTTON_COUNT > 11
198         init(11);
199 #endif
200 #if AO_BUTTON_COUNT > 12
201         init(12);
202 #endif
203 #if AO_BUTTON_COUNT > 13
204         init(13);
205 #endif
206 #if AO_BUTTON_COUNT > 14
207         init(14);
208 #endif
209 #if AO_BUTTON_COUNT > 15
210         init(15);
211 #endif
212 #if AO_BUTTON_COUNT > 16
213         #error too many buttons
214 #endif
215 }