f6a9676bc481ad934b6d794625092a84f531f5d0
[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 #define pin(q) AO_BUTTON_ ## q ## _PIN
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), pin(b))
49 #else
50 #define ao_button_value(b)      ao_gpio_get(port(b), bit(b), pin(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         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);
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_port(port(b));                                \
154                                                                         \
155                 ao_exti_setup(port(b), bit(b),                          \
156                               AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
157                               ao_button_isr);                           \
158                 ao_exti_enable(port(b), bit(b));                        \
159                 _ao_button_init(b);                                     \
160         } while (0)
161
162 void
163 ao_button_init(void)
164 {
165 #if AO_BUTTON_COUNT > 0
166         init(0);
167 #endif
168 #if AO_BUTTON_COUNT > 1
169         init(1);
170 #endif
171 #if AO_BUTTON_COUNT > 2
172         init(2);
173 #endif
174 #if AO_BUTTON_COUNT > 3
175         init(3);
176 #endif
177 #if AO_BUTTON_COUNT > 4
178         init(4);
179 #endif
180 #if AO_BUTTON_COUNT > 5
181         init(5);
182 #endif
183 #if AO_BUTTON_COUNT > 6
184         init(6);
185 #endif
186 #if AO_BUTTON_COUNT > 7
187         init(7);
188 #endif
189 #if AO_BUTTON_COUNT > 8
190         init(8);
191 #endif
192 #if AO_BUTTON_COUNT > 9
193         init(9);
194 #endif
195 #if AO_BUTTON_COUNT > 10
196         init(10);
197 #endif
198 #if AO_BUTTON_COUNT > 11
199         init(11);
200 #endif
201 #if AO_BUTTON_COUNT > 12
202         init(12);
203 #endif
204 #if AO_BUTTON_COUNT > 13
205         init(13);
206 #endif
207 #if AO_BUTTON_COUNT > 14
208         init(14);
209 #endif
210 #if AO_BUTTON_COUNT > 15
211         init(15);
212 #endif
213 #if AO_BUTTON_COUNT > 16
214         #error too many buttons
215 #endif
216 }