-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.cpp
272 lines (202 loc) · 6.42 KB
/
keypad.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "keypad.h"
/** Duration of time a button spends bouncing before stabilizing */
constexpr auto DEBOUNCE_THRESH = 60ms;
/** Duration of time a button must be pressed to be considered long-pressed */
constexpr auto LONG_PRESS_THRESH = 300ms;
// Constructors
Keypad::Keypad(PinName r0, PinName r1, PinName r2, PinName r3, PinName c0, PinName c1, PinName c2, PinName c3)
: row{{r0}, {r1}, {r2}, {r3}}
, col{{c0}, {c1}, {c2}, {c3}}
{
// Use the internal pullup resistors for all the interrupt pins (cols) and switch the rows off
// whenever a button is pressed, its corresponding column is pulled low, i.e. a fall interrupt happens
// whenever a button is lifted, its corresponding column is pulled high, i.e. a rise interrupt happens
// register fall and rise handlers for each of the interrupt pins (cols)
for (auto &e : col) {
e.mode(PullUp);
}
for (auto &e : row) {
e = 0;
}
static_assert(KEYPAD_NUM_COLS == 4, "Constructor does not initialize all columns!");
col[0].fall(callback(this, &Keypad::fall_handler<0>));
col[1].fall(callback(this, &Keypad::fall_handler<1>));
col[2].fall(callback(this, &Keypad::fall_handler<2>));
col[3].fall(callback(this, &Keypad::fall_handler<3>));
col[0].rise(callback(this, &Keypad::rise_handler<0>));
col[1].rise(callback(this, &Keypad::rise_handler<1>));
col[2].rise(callback(this, &Keypad::rise_handler<2>));
col[3].rise(callback(this, &Keypad::rise_handler<3>));
}
// Public Methods
bool
Keypad::initialize() {
// return if the thread was already initialized; move forward otherwise
// allocate the thread and return if the allocation fails; move forward otherwise
// try to start the thread and return if successful
// in case of failure, delete the allocated thread and return
if (is_initialized()) {
return false;
}
threadHandle = new (std::nothrow) Thread();
if (threadHandle == nullptr) {
return false;
}
auto status = threadHandle->start(callback(this, &Keypad::dispatch_events));
if (status != osOK) {
delete threadHandle;
threadHandle = nullptr;
return false;
}
return true;
}
bool
Keypad::finalize() {
// return if the thread was not initialized; move forward otherwise
// break the queues dispatch and use shouldTerminate to wait until the action is complete
// terminate the thread and return if that fails
// free the thread and return
if (!is_initialized()) {
return false;
}
queue.break_dispatch();
threadHandle->join();
delete threadHandle;
threadHandle = nullptr;
return true;
}
bool
Keypad::is_initialized() const {
// if threadHandle is NULL, then the object was not initialized/finalized before
// otherwise it is still in the initialized state
return threadHandle != nullptr;
}
void
Keypad::register_onpress(Callback<void(uint32_t, uint32_t)> cb) {
onPress = std::move(cb);
pressCbEnabled = true;
}
void
Keypad::remove_onpress() {
pressCbEnabled = false;
}
bool
Keypad::is_onpress_registered() const {
return pressCbEnabled;
}
void
Keypad::register_onrelease(Callback<void(uint32_t, uint32_t)> cb) {
onRelease = std::move(cb);
releaseCbEnabled = true;
}
void
Keypad::remove_onrelease() {
releaseCbEnabled = false;
}
bool
Keypad::is_onrelease_registered() const {
return releaseCbEnabled;
}
void
Keypad::register_onlongpress(Callback<void(uint32_t, uint32_t)> cb) {
onLongpress = std::move(cb);
longpressCbEnabled = true;
}
void
Keypad::remove_onlongpress() {
longpressCbEnabled = false;
}
bool
Keypad::is_onlongpress_registered() const {
return longpressCbEnabled;
}
// Private Methods
template<uint32_t curCol>
void
Keypad::fall_handler() {
if (state != ButtonState::RELEASED) {
return;
}
//transition_state(ButtonState::RELEASED, ButtonState::PRESS_BOUNCING);
state = ButtonState::PRESS_BOUNCING;
toRowScan.attach(callback(this, &Keypad::row_scan_handler<curCol>), DEBOUNCE_THRESH);
}
template<uint32_t curCol>
void
Keypad::rise_handler() {
if (state != ButtonState::PRESSED && state != ButtonState::LONG_PRESSED) {
return;
}
//transition_state(ButtonState::PRESSED, ButtonState::RELEASE_BOUNCING)
//|| transition_state(ButtonState::LONG_PRESSED, ButtonState::RELEASE_BOUNCING);
state = ButtonState::RELEASE_BOUNCING;
toButtonScan.attach(callback(this, &Keypad::button_scan_handler), DEBOUNCE_THRESH);
}
template<uint32_t curCol>
void
Keypad::row_scan_handler() {
if (state != ButtonState::PRESS_BOUNCING) {
return;
}
for (uint32_t i = 0; i < KEYPAD_NUM_ROWS; ++i) {
row[i] = 1;
auto on = col[curCol].read();
row[i] = 0;
if (!on) {
continue;
}
//transition_state(ButtonState::PRESS_BOUNCING, ButtonState::PRESSED);
state = ButtonState::PRESSED;
pressedRow = i;
pressedCol = curCol;
toLongPressed.attach(callback(this, &Keypad::long_press_handler), LONG_PRESS_THRESH);
if (pressCbEnabled) {
queue.call([this, i]() { onPress(i, curCol); });
}
return;
}
//transition_state(ButtonState::PRESS_BOUNCING, ButtonState::RELEASED);
state = ButtonState::RELEASED;
}
void
Keypad::button_scan_handler() {
if (state != RELEASE_BOUNCING) {
return;
}
auto curRow = pressedRow;
auto curCol = pressedCol;
if (!col[pressedCol].read()) {
//transition_state(ButtonState::RELEASE_BOUNCING, ButtonState::PRESSED);
state = ButtonState::PRESSED;
return;
}
//transition_state(ButtonState::RELEASE_BOUNCING, ButtonState::RELEASED);
state = ButtonState::RELEASED;
if (toLongPressed.remaining_time().count() > 0) {
toLongPressed.detach();
}
if (releaseCbEnabled) {
queue.call(
[this, curRow, curCol]() {
onRelease(curRow, curCol);
}
);
}
}
void
Keypad::long_press_handler() {
if (state != ButtonState::PRESSED) {
return;
}
auto curRow = pressedRow;
auto curCol = pressedCol;
//transition_state(ButtonState::PRESSED, ButtonState::LONG_PRESSED);
state = ButtonState::LONG_PRESSED;
if (longpressCbEnabled) {
queue.call([this, curRow, curCol]() { onLongpress(curRow, curCol); });
}
}
void
Keypad::dispatch_events() {
queue.dispatch_forever();
}