altos: Add telerepeat-v1.0
[fw/altos] / src / kernel / ao_debounce.c
1 /*
2  * Copyright © 2013 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_debounce.h>
20 #include <ao_fast_timer.h>
21
22 static uint8_t                  ao_debounce_initialized;
23 static uint8_t                  ao_debounce_running;
24 static struct ao_debounce       *ao_debounce;
25
26 static uint8_t  values[64];
27 static uint8_t  n;
28
29 #define d_step(n)       (((n) + 1) & 63)
30
31 static void
32 _ao_debounce_set(struct ao_debounce *debounce, uint8_t value)
33 {
34         if (value != debounce->value) {
35                 values[n] = value;
36                 n = (n + 1) & 63;
37                 debounce->value = value;
38                 debounce->_set(debounce, value);
39         }
40         _ao_debounce_stop(debounce);
41 }
42
43 void
44 ao_debounce_dump(void)
45 {
46         uint8_t s;
47
48         for (s = 0; s < n; s++) {
49                 printf ("%d: %d\n",
50                         s, values[s]);
51         }
52         n = 0;
53 }
54
55 /*
56  * Get the current value, set the result when we've
57  * reached the debounce count limit
58  */
59 static void
60 _ao_debounce_check(struct ao_debounce *debounce)
61 {
62         uint8_t next = debounce->_get(debounce);
63
64         if (next == debounce->current) {
65                 if (debounce->count < debounce->hold) {
66                         if (++debounce->count == debounce->hold)
67                                 _ao_debounce_set(debounce, debounce->current);
68                 }
69         } else {
70                 debounce->count = 0;
71                 debounce->current = next;
72         }
73 }
74
75 static void
76 _ao_debounce_isr(void)
77 {
78         struct ao_debounce *debounce, *next;
79
80         for (debounce = ao_debounce; debounce; debounce = next) {
81                 next = debounce->next;
82                 _ao_debounce_check(debounce);
83         }
84 }
85
86 static void
87 ao_debounce_on(void)
88 {
89         ao_fast_timer_on(_ao_debounce_isr);
90 }
91
92 static void
93 ao_debounce_off(void)
94 {
95         ao_fast_timer_off(_ao_debounce_isr);
96 }
97
98 /*
99  * Start monitoring one pin
100  */
101 void
102 _ao_debounce_start(struct ao_debounce *debounce)
103 {
104         uint32_t        m;
105
106         m = ao_arch_irqsave();
107         if (!debounce->running) {
108                 debounce->running = 1;
109
110                 /* Reset the counter */
111                 debounce->count = 0;
112
113                 /* Link into list */
114                 debounce->next = ao_debounce;
115                 ao_debounce = debounce;
116
117                 /* Make sure the timer is running */
118                 if (!ao_debounce_running++)
119                         ao_debounce_on();
120
121                 /* And go check the current value */
122                 _ao_debounce_check(debounce);
123         }
124         ao_arch_irqrestore(m);
125 }
126
127 /*
128  * Stop monitoring one pin
129  */
130 void
131 _ao_debounce_stop(struct ao_debounce *debounce)
132 {
133         struct ao_debounce **prev;
134         uint32_t m;
135
136         m = ao_arch_irqsave();
137         if (debounce->running) {
138                 debounce->running = 0;
139
140                 /* Unlink */
141                 for (prev = &ao_debounce; (*prev); prev = &((*prev)->next)) {
142                         if (*prev == debounce) {
143                                 *prev = debounce->next;
144                                 break;
145                         }
146                 }
147                 debounce->next = NULL;
148
149                 /* Turn off the timer if possible */
150                 if (!--ao_debounce_running)
151                         ao_debounce_off();
152         }
153         ao_arch_irqrestore(m);
154 }
155
156 void
157 ao_debounce_init(void)
158 {
159         if (ao_debounce_initialized)
160                 return;
161         ao_debounce_initialized = 1;
162         ao_fast_timer_init();
163 }