Switch from GPLv2 to GPLv2+
[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 /* pins are inverted */
43 #define ao_button_value(b)      !ao_gpio_get(port(b), bit(b), pin(b))
44
45 static uint8_t
46 _ao_button_get(uint8_t b)
47 {
48         switch (b) {
49 #if AO_BUTTON_COUNT > 0
50         case 0: return ao_button_value(0);
51 #endif
52 #if AO_BUTTON_COUNT > 1
53         case 1: return ao_button_value(1);
54 #endif
55 #if AO_BUTTON_COUNT > 2
56         case 2: return ao_button_value(2);
57 #endif
58 #if AO_BUTTON_COUNT > 3
59         case 3: return ao_button_value(3);
60 #endif
61 #if AO_BUTTON_COUNT > 4
62         case 4: return ao_button_value(4);
63 #endif
64         }
65         return 0;
66 }
67
68 static void
69 _ao_button_check(uint8_t b)
70 {
71         uint8_t value = _ao_button_get(b);
72
73         if (value != ao_button_state[b].value) {
74                 AO_TICK_TYPE    now = ao_time();
75
76                 if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
77                         ao_button_state[b].value = value;
78                         ao_button_queue(b, value);
79                 }
80                 ao_button_state[b].time = now;
81         }
82 }
83
84 static void
85 _ao_button_init(uint8_t b)
86 {
87         uint8_t m = ao_arch_irqsave();
88         uint8_t value = _ao_button_get(b);
89         ao_button_state[b].value = value;
90         ao_button_state[b].time = ao_time();
91         ao_button_queue(b, value);
92         ao_arch_irqrestore(m);
93
94 }
95
96 static void
97 ao_button_isr(void)
98 {
99         uint8_t b;
100
101         for (b = 0; b < AO_BUTTON_COUNT; b++)
102                 _ao_button_check(b);
103 }
104
105 #define init(b) do {                                                    \
106                 ao_enable_port(port(b));                                \
107                                                                         \
108                 ao_exti_setup(port(b), bit(b),                          \
109                               AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
110                               ao_button_isr);                           \
111                 ao_exti_enable(port(b), bit(b));                        \
112                 _ao_button_init(b);                                     \
113         } while (0)
114
115 void
116 ao_button_init(void)
117 {
118 #if AO_BUTTON_COUNT > 0
119         init(0);
120 #endif
121 #if AO_BUTTON_COUNT > 1
122         init(1);
123 #endif
124 #if AO_BUTTON_COUNT > 2
125         init(2);
126 #endif
127 #if AO_BUTTON_COUNT > 3
128         init(3);
129 #endif
130 #if AO_BUTTON_COUNT > 4
131         init(4);
132 #endif
133 }