-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuttons.c
202 lines (171 loc) · 4.47 KB
/
buttons.c
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
/*
* This file is part of badge2019.
* Copyright 2019 Emil Renner Berthing <esmil@labitat.dk>
*
* badge2019 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 3 of the License, or
* (at your option) any later version.
*
* badge2019 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 badge2019. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "geckonator/gpio.h"
#include "timer.h"
#include "events.h"
#include "buttons.h"
#define POLL_RATE 50
static const gpio_pin_t buttonpin[BTN_MAX] = {
[BTN_SUP] = GPIO_PC10,
[BTN_SMID] = GPIO_PC9,
[BTN_SDOWN] = GPIO_PC8,
[BTN_UP] = GPIO_PF2,
[BTN_DOWN] = GPIO_PF3,
[BTN_LEFT] = GPIO_PF4,
[BTN_RIGHT] = GPIO_PF5,
[BTN_CENTER] = GPIO_PB11,
/* [BTN_POWER] = GPIO_PC4, */
};
static const struct button_config *volatile buttonconfig;
static volatile uint8_t pressed;
struct button_state {
struct timer_node n;
uint16_t delay_left;
};
static struct button_state buttonstate[BTN_MAX];
static void
button_cb(struct timer_node *n)
{
struct button_state *s = (struct button_state *)n;
enum button btn = (enum button)(s - buttonstate);
const struct button_config *c = &buttonconfig[btn];
if (gpio_in(buttonpin[btn])) {
if (c->release > 0)
event_add(c->release);
gpio_flag_clear(buttonpin[btn]);
gpio_flag_enable(buttonpin[btn]);
pressed -= 1;
} else if (s->delay_left == 0) {
if (c->repeat > 0)
event_add(c->repeat);
s->n.timeout += (c->rate > 0) ? c->rate : POLL_RATE;
timer_add(&s->n);
} else if (s->delay_left <= POLL_RATE) {
s->delay_left = 0;
if (c->longpress > 0)
event_add(c->longpress);
else if (c->repeat > 0)
event_add(c->repeat);
s->n.timeout += (c->rate > 0) ? c->rate : POLL_RATE;
timer_add(&s->n);
} else {
s->delay_left -= POLL_RATE;
s->n.timeout += (s->delay_left > POLL_RATE) ? POLL_RATE : s->delay_left;
timer_add(&s->n);
}
}
static void
button_click(enum button btn)
{
const struct button_config *c;
struct button_state *s;
uint16_t delay;
gpio_flag_disable(buttonpin[btn]);
pressed += 1;
c = &buttonconfig[btn];
delay = (c->delay > 0) ? c->delay : POLL_RATE;
s = &buttonstate[btn];
s->delay_left = delay;
s->n.timeout = timer_now();
s->n.timeout += (delay > POLL_RATE) ? POLL_RATE : delay;
timer_add(&s->n);
if (c->press > 0)
event_add(c->press);
}
void
GPIO_EVEN_IRQHandler(void)
{
uint32_t flags = gpio_flags_enabled(gpio_flags());
if (gpio_flag(flags, GPIO_PF2))
button_click(BTN_UP);
/*
if (gpio_flag(flags, GPIO_PC4))
button_click(BTN_POWER);
*/
if (gpio_flag(flags, GPIO_PF4))
button_click(BTN_LEFT);
if (gpio_flag(flags, GPIO_PC8))
button_click(BTN_SDOWN);
if (gpio_flag(flags, GPIO_PE10))
button_click(BTN_SUP);
}
void
GPIO_ODD_IRQHandler(void)
{
uint32_t flags = gpio_flags_enabled(gpio_flags());
if (gpio_flag(flags, GPIO_PF3))
button_click(BTN_DOWN);
if (gpio_flag(flags, GPIO_PF5))
button_click(BTN_RIGHT);
if (gpio_flag(flags, GPIO_PC9))
button_click(BTN_SMID);
if (gpio_flag(flags, GPIO_PB11))
button_click(BTN_CENTER);
}
void
buttons_init(const struct button_config config[BTN_MAX])
{
unsigned int i;
for (i = 0; i < BTN_MAX; i++) {
gpio_pin_t pin = buttonpin[i];
gpio_set(pin);
gpio_mode(pin, GPIO_MODE_INPUTPULL);
buttonstate[i].n.cb = button_cb;
gpio_flag_select(pin);
gpio_flag_falling_enable(pin);
gpio_flag_clear(pin);
gpio_flag_enable(pin);
}
/* init power button */
gpio_set(GPIO_PC4);
gpio_mode(GPIO_PC4, GPIO_MODE_INPUTPULL);
buttonconfig = config;
NVIC_SetPriority(GPIO_EVEN_IRQn, 3);
NVIC_SetPriority(GPIO_ODD_IRQn, 3);
NVIC_EnableIRQ(GPIO_EVEN_IRQn);
NVIC_EnableIRQ(GPIO_ODD_IRQn);
}
void
buttons_uninit(void)
{
unsigned int i;
/* disable button gpio */
for (i = 0; i < BTN_MAX; i++) {
gpio_pin_t pin = buttonpin[i];
gpio_flag_disable(pin);
gpio_mode(pin, GPIO_MODE_DISABLED);
}
/* wait for button callbacks to complete */
while (pressed > 0)
__WFI();
}
void
buttons_config(const struct button_config config[BTN_MAX])
{
while (1) {
__disable_irq();
if (pressed == 0)
break;
__enable_irq();
__WFI();
}
events_clear();
buttonconfig = config;
__enable_irq();
}