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