8e7dead71df6d075014a233c0d43c77f973a59a9
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_button.h>
20 #include <ao_exti.h>
21 #if AO_EVENT
22 #include <ao_event.h>
23 #define ao_button_queue(b,v)    ao_event_put_isr(AO_EVENT_BUTTON, b, v)
24 #else
25 #define ao_button_queue(b,v)
26 #endif
27
28 #define AO_BUTTON_DEBOUNCE_INTERVAL     AO_MS_TO_TICKS(50)
29
30 struct ao_button_state {
31         AO_TICK_TYPE    time;
32         uint8_t         value;
33 };
34
35 static struct ao_button_state   ao_button_state[AO_BUTTON_COUNT];
36
37 #define port(q) AO_BUTTON_ ## q ## _PORT
38 #define bit(q) AO_BUTTON_ ## q
39 #define pin(q) AO_BUTTON_ ## q ## _PIN
40
41 /* pins are inverted */
42 #define ao_button_value(b)      !ao_gpio_get(port(b), bit(b), pin(b))
43
44 static uint8_t
45 _ao_button_get(uint8_t b)
46 {
47         switch (b) {
48 #if AO_BUTTON_COUNT > 0
49         case 0: return ao_button_value(0);
50 #endif
51 #if AO_BUTTON_COUNT > 1
52         case 1: return ao_button_value(1);
53 #endif
54 #if AO_BUTTON_COUNT > 2
55         case 2: return ao_button_value(2);
56 #endif
57 #if AO_BUTTON_COUNT > 3
58         case 3: return ao_button_value(3);
59 #endif
60 #if AO_BUTTON_COUNT > 4
61         case 4: return ao_button_value(4);
62 #endif
63         }
64         return 0;
65 }
66
67 static void
68 _ao_button_check(uint8_t b)
69 {
70         uint8_t value = _ao_button_get(b);
71
72         if (value != ao_button_state[b].value) {
73                 AO_TICK_TYPE    now = ao_time();
74
75                 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
76                         ao_button_state[b].value = value;
77                         ao_button_queue(b, value);
78                 }
79                 ao_button_state[b].time = now;
80         }
81 }
82
83 static void
84 _ao_button_init(uint8_t b)
85 {
86         uint8_t m = ao_arch_irqsave();
87         uint8_t value = _ao_button_get(b);
88         ao_button_state[b].value = value;
89         ao_button_state[b].time = ao_time();
90         ao_button_queue(b, value);
91         ao_arch_irqrestore(m);
92
93 }
94
95 static void
96 ao_button_isr(void)
97 {
98         uint8_t b;
99
100         for (b = 0; b < AO_BUTTON_COUNT; b++)
101                 _ao_button_check(b);
102 }
103
104 #define init(b) do {                                                    \
105                 ao_enable_port(port(b));                                \
106                                                                         \
107                 ao_exti_setup(port(b), bit(b),                          \
108                               AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
109                               ao_button_isr);                           \
110                 ao_exti_enable(port(b), bit(b));                        \
111                 _ao_button_init(b);                                     \
112         } while (0)
113
114 void
115 ao_button_init(void)
116 {
117 #if AO_BUTTON_COUNT > 0
118         init(0);
119 #endif
120 #if AO_BUTTON_COUNT > 1
121         init(1);
122 #endif
123 #if AO_BUTTON_COUNT > 2
124         init(2);
125 #endif
126 #if AO_BUTTON_COUNT > 3
127         init(3);
128 #endif
129 #if AO_BUTTON_COUNT > 4
130         init(4);
131 #endif
132 }