summaryrefslogtreecommitdiff
path: root/src/drivers/ao_button.c
blob: f6a9676bc481ad934b6d794625092a84f531f5d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <ao.h>
#include <ao_button.h>
#include <ao_exti.h>
#if AO_EVENT
#include <ao_event.h>
#define ao_button_queue(b,v)	ao_event_put_isr(AO_EVENT_BUTTON, b, v)
#else
#define ao_button_queue(b,v)
#endif

#define AO_BUTTON_DEBOUNCE_INTERVAL	AO_MS_TO_TICKS(50)

struct ao_button_state {
	AO_TICK_TYPE	time;
	uint8_t		value;
};

static struct ao_button_state	ao_button_state[AO_BUTTON_COUNT];

#define port(q)	AO_BUTTON_ ## q ## _PORT
#define bit(q) AO_BUTTON_ ## q
#define pin(q) AO_BUTTON_ ## q ## _PIN

#ifndef AO_BUTTON_INVERTED
#define AO_BUTTON_INVERTED	1
#endif

#if AO_BUTTON_INVERTED
/* pins are inverted */
#define ao_button_value(b)	!ao_gpio_get(port(b), bit(b), pin(b))
#else
#define ao_button_value(b)	ao_gpio_get(port(b), bit(b), pin(b))
#endif

static uint8_t
_ao_button_get(uint8_t b)
{
	switch (b) {
#if AO_BUTTON_COUNT > 0
	case 0: return ao_button_value(0);
#endif
#if AO_BUTTON_COUNT > 1
	case 1: return ao_button_value(1);
#endif
#if AO_BUTTON_COUNT > 2
	case 2: return ao_button_value(2);
#endif
#if AO_BUTTON_COUNT > 3
	case 3: return ao_button_value(3);
#endif
#if AO_BUTTON_COUNT > 4
	case 4: return ao_button_value(4);
#endif
#if AO_BUTTON_COUNT > 5
	case 5: return ao_button_value(5);
#endif
#if AO_BUTTON_COUNT > 6
	case 6: return ao_button_value(6);
#endif
#if AO_BUTTON_COUNT > 7
	case 7: return ao_button_value(7);
#endif
#if AO_BUTTON_COUNT > 8
	case 8: return ao_button_value(8);
#endif
#if AO_BUTTON_COUNT > 9
	case 9: return ao_button_value(9);
#endif
#if AO_BUTTON_COUNT > 10
	case 10: return ao_button_value(10);
#endif
#if AO_BUTTON_COUNT > 11
	case 11: return ao_button_value(11);
#endif
#if AO_BUTTON_COUNT > 12
	case 12: return ao_button_value(12);
#endif
#if AO_BUTTON_COUNT > 13
	case 13: return ao_button_value(13);
#endif
#if AO_BUTTON_COUNT > 14
	case 14: return ao_button_value(14);
#endif
#if AO_BUTTON_COUNT > 15
	case 15: return ao_button_value(15);
#endif
	}
	return 0;
}

static void
_ao_button_check(uint8_t b)
{
	uint8_t	value = _ao_button_get(b);

	if (value != ao_button_state[b].value) {
		AO_TICK_TYPE	now = ao_time();

		if ((now - ao_button_state[b].time) >= AO_BUTTON_DEBOUNCE_INTERVAL) {
			ao_button_state[b].value = value;
			ao_button_queue(b, value);
		}
		ao_button_state[b].time = now;
	}
}

static void
_ao_button_init(uint8_t b)
{
	uint8_t	m = ao_arch_irqsave();
	uint8_t value = _ao_button_get(b);
	ao_button_state[b].value = value;
	ao_button_state[b].time = ao_time();
	ao_button_queue(b, value);
	ao_arch_irqrestore(m);

}

uint8_t
ao_button_get(uint8_t b)
{
	return ao_button_state[b].value;
}

static void
ao_button_isr(void)
{
	uint8_t	b;

	for (b = 0; b < AO_BUTTON_COUNT; b++)
		_ao_button_check(b);
}

#define init(b) do {							\
		ao_enable_port(port(b));				\
									\
		ao_exti_setup(port(b), bit(b),				\
			      AO_BUTTON_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
			      ao_button_isr);				\
		ao_exti_enable(port(b), bit(b));			\
		_ao_button_init(b);					\
	} while (0)

void
ao_button_init(void)
{
#if AO_BUTTON_COUNT > 0
	init(0);
#endif
#if AO_BUTTON_COUNT > 1
	init(1);
#endif
#if AO_BUTTON_COUNT > 2
	init(2);
#endif
#if AO_BUTTON_COUNT > 3
	init(3);
#endif
#if AO_BUTTON_COUNT > 4
	init(4);
#endif
#if AO_BUTTON_COUNT > 5
	init(5);
#endif
#if AO_BUTTON_COUNT > 6
	init(6);
#endif
#if AO_BUTTON_COUNT > 7
	init(7);
#endif
#if AO_BUTTON_COUNT > 8
	init(8);
#endif
#if AO_BUTTON_COUNT > 9
	init(9);
#endif
#if AO_BUTTON_COUNT > 10
	init(10);
#endif
#if AO_BUTTON_COUNT > 11
	init(11);
#endif
#if AO_BUTTON_COUNT > 12
	init(12);
#endif
#if AO_BUTTON_COUNT > 13
	init(13);
#endif
#if AO_BUTTON_COUNT > 14
	init(14);
#endif
#if AO_BUTTON_COUNT > 15
	init(15);
#endif
#if AO_BUTTON_COUNT > 16
	#error too many buttons
#endif
}