-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.cpp
388 lines (300 loc) · 12 KB
/
buttons.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <myDebug.h>
#include "buttons.h"
#include "expSystem.h"
#include "myLeds.h"
#define dbg_btn 1
#define LONG_PRESS_TIME 800
#define DOUBLE_CLICK_TIME 360
#define DO_DEBOUNCE 1
// does not seem to be necessary.
// in timer version, a period of time is implicit, so this should be turned off
// in loop() version, maybe we are getting by because of display() calls
// so *this* might still be needed.
#if DO_DEBOUNCE
#define DEBOUNCE_MILLIS 50
#endif
buttonArray theButtons;
int row_pins[NUM_BUTTON_COLS] = {PIN_BUTTON_OUT0,PIN_BUTTON_OUT1,PIN_BUTTON_OUT2,PIN_BUTTON_OUT3,PIN_BUTTON_OUT4};
int col_pins[NUM_BUTTON_ROWS] = {PIN_BUTTON_IN0,PIN_BUTTON_IN1,PIN_BUTTON_IN2,PIN_BUTTON_IN3,PIN_BUTTON_IN4};
//--------------------------------------
// arrayedButton
//--------------------------------------
arrayedButton::arrayedButton()
{
// see notes below why event state is not
// cleared during initDefaults()
m_event_state = 0;
initDefaults();
}
void arrayedButton::initDefaults()
{
m_event_mask = 0;
m_press_time = 0;
m_debounce_time = 0;
m_repeat_time = 0;
m_default_color = LED_BLUE;
m_selected_color = LED_CYAN;
m_touch_color = LED_YELLOW;
}
//--------------------------------------
// buttonArray
//--------------------------------------
buttonArray::buttonArray()
{}
void buttonArray::init()
{
display(dbg_btn,"buttonArray::init()",0);
for (int row=0; row<NUM_BUTTON_ROWS; row++)
{
pinMode(row_pins[row],OUTPUT);
digitalWrite(row_pins[row],0);
}
for (int col=0; col<NUM_BUTTON_COLS; col++)
pinMode(col_pins[col],INPUT_PULLDOWN); // guessing that pins 7 and 8 doent have pulldowns
#if DO_DEBOUNCE
display(dbg_btn," WITH DEBOUNCE!",0);
#endif
}
void buttonArray::clear()
// we have to be careful about folks calling back into
// us to do things from button events. Like starting
// a new window and resetting all the masks, in the middle
// of an event.
{
for (int row=0; row<NUM_BUTTON_ROWS; row++)
{
for (int col=0; col<NUM_BUTTON_COLS; col++)
{
m_buttons[row][col].initDefaults();
setLED(row,col,0);
}
}
}
// static
const char *buttonArray::buttonEventName(int event)
{
if (event == BUTTON_EVENT_PRESS ) return "PRESS";
if (event == BUTTON_EVENT_RELEASE ) return "RELEASE";
if (event == BUTTON_EVENT_CLICK ) return "CLICK";
if (event == BUTTON_EVENT_LONG_CLICK ) return "LONG_CLICK";
return "UNKNOWN BUTTON EVENT";
}
void buttonArray::setEventState(int num, int state)
// retains the state of the pressed bit,
// but clears the m_press_time (handled)
{
// display(0,"setEventState num=%d state=%04x",num,state);
arrayedButton *button = &m_buttons[num / NUM_BUTTON_COLS][num % NUM_BUTTON_COLS];
state &= ~BUTTON_STATE_PRESSED;
state |= (button->m_event_state & BUTTON_STATE_PRESSED);
button->m_event_state = state;
button->m_press_time = 0;
int color =
state & BUTTON_STATE_SELECTED ?
button->m_selected_color :
(button->m_event_mask & BUTTON_MASK_TOUCH) &&
(state & BUTTON_STATE_TOUCHED) ?
button->m_touch_color :
button->m_default_color;
// display(0,"color(num)=%d",color);
setLED(num,color);
}
void buttonArray::select(int num, int value)
// -1 == pressed (this method is NOT called if m_press_time==0)
// 0 == deselect
// 1 == select
{
arrayedButton *button = &m_buttons[num / NUM_BUTTON_COLS][num % NUM_BUTTON_COLS];
int mask = button->m_event_mask;
int state = button->m_event_state;
display(dbg_btn,"select(%d,%d) mask=%04x state=%04x",num,value,mask,state);
// fake the previous selected bit if from a press
// and it's a toggle button
bool selected = value;
if (value==-1 && (mask & BUTTON_MASK_TOGGLE))
selected = !(state & BUTTON_STATE_SELECTED);
// determine the default, unselected, color
int color = button->m_default_color;
if (mask & BUTTON_MASK_TOUCH && state & BUTTON_STATE_TOUCHED)
color = button->m_touch_color;
if ((state & BUTTON_STATE_SELECTED) != selected)
{
if (selected)
{
if (mask & BUTTON_MASK_TOGGLE)
{
button->m_event_state |= BUTTON_STATE_SELECTED | BUTTON_STATE_TOUCHED;
color = button->m_selected_color;
}
else if (mask & BUTTON_MASK_RADIO)
{
int group = BUTTON_GROUP_OF(button->m_event_mask);
// clear any other selected button in the group
for (int r=0; r<NUM_BUTTON_ROWS; r++)
{
for (int c=0; c<NUM_BUTTON_COLS; c++)
{
arrayedButton *b = &m_buttons[r][c];
if (b != button &&
BUTTON_GROUP_OF(b->m_event_mask) == group &&
b->isSelected())
{
b->m_event_state &= ~BUTTON_STATE_SELECTED;
int c2 = b->m_default_color;
if (b->m_event_mask & BUTTON_MASK_TOUCH &&
b->m_event_state & BUTTON_STATE_TOUCHED)
c2 = b->m_touch_color;
setLED(r,c,c2);
}
}
}
button->m_event_state |= BUTTON_STATE_SELECTED | BUTTON_STATE_TOUCHED;
color = button->m_selected_color;
}
}
else
{
button->m_event_state &= ~BUTTON_STATE_SELECTED;
}
}
// this may be called by the client from a button event handler.
// if so, and it effects the current button, then we don't want
// the rest of the task() code for that button to run ...
if (value != -1)
button->m_press_time = 0;
setLED(num,color);
}
void buttonArray::task()
{
unsigned time = millis();
for (int row=0; row<NUM_BUTTON_ROWS; row++)
{
digitalWrite(row_pins[row],1);
for (int col=0; col<NUM_BUTTON_COLS; col++)
{
// only poll registered buttons
arrayedButton *pButton = &m_buttons[row][col];
int mask = pButton->m_event_mask;
if (!mask) continue;
#if DO_DEBOUNCE
if (time <= pButton->m_debounce_time)
continue;
#endif
// if state changed, process the button
int num = row * NUM_BUTTON_COLS + col;
bool is_pressed = digitalRead(col_pins[col]);
bool was_pressed = pButton->m_event_state & BUTTON_STATE_PRESSED;
if (is_pressed != was_pressed)
{
display(dbg_btn,"BUTTON(%d,%d) %-6s mask=%04x state=%04x",
row,col,is_pressed?"DOWN":"UP",mask,pButton->m_event_state);
#if DO_DEBOUNCE
pButton->m_debounce_time = time + DEBOUNCE_MILLIS;
#endif
//---------------------------
// pressed
//---------------------------
if (is_pressed) // button pressed
{
pButton->m_event_state |= BUTTON_STATE_PRESSED | BUTTON_STATE_TOUCHED;
if (mask & BUTTON_EVENT_PRESS)
{
select(num,-1);
showLEDs();
display(dbg_btn,"BUTTON_EVENT_PRESS(%d,%d)",row,col);
theSystem.buttonEvent(row, col, BUTTON_EVENT_PRESS);
}
else
{
setLED(row,col,LED_WHITE);
showLEDs();
}
pButton->m_press_time = time;
}
//---------------------------
// released
//---------------------------
// only do something if not handled (m_press_time != 0)
else // button released
{
pButton->m_event_state &= ~BUTTON_STATE_PRESSED;
if (pButton->m_press_time) // && !(mask & BUTTON_EVENT_PRESS))
{
pButton->m_press_time = 0;
select(num,-1);
showLEDs();
if (mask & BUTTON_EVENT_RELEASE)
{
display(dbg_btn,"BUTTON_EVENT_RELEASE(%d,%d)",row,col);
theSystem.buttonEvent(row, col, BUTTON_EVENT_RELEASE);
}
if (mask & BUTTON_EVENT_CLICK)
{
display(dbg_btn,"BUTTON_EVENT_CLICK(%d,%d)",row,col);
theSystem.buttonEvent(row, col, BUTTON_EVENT_CLICK);
}
}
}
}
//--------------------------------
// state did not change
//--------------------------------
//
else if (is_pressed && pButton->m_press_time)
{
// repeat generates PRESS events
int dif = millis() - pButton->m_press_time;
if ((mask & BUTTON_MASK_REPEAT) && dif > 300)
{
// starts repeating after 300ms
// starts at 10 per second and accelerates to 100 per second over one second
dif -= 300;
if (dif > 1000) dif = 1000;
unsigned interval = 100;
interval -= 90*dif/1000;
if (pButton->m_repeat_time > interval)
{
display(dbg_btn,"repeat BUTTON_EVENT_PRESS(%d,%d)",row,col);
theSystem.buttonEvent(row, col, BUTTON_EVENT_PRESS);
pButton->m_repeat_time = 0;
}
}
// which is generally exclusive of long clicks
else if ((mask & BUTTON_EVENT_LONG_CLICK) && dif > LONG_PRESS_TIME)
{
display(dbg_btn,"BUTTON_EVENT_LONG_CLICK(%d,%d)",row,col);
pButton->m_press_time = 0;
theSystem.buttonEvent(row, col, BUTTON_EVENT_LONG_CLICK);
}
} // pressed and not handled yet
} // for each col
digitalWrite(row_pins[row],0);
} // for each row
}
void buttonArray::setButtonType(int num, int mask, int default_color, int selected_color, int touch_color)
{
arrayedButton *pb = &m_buttons[num / NUM_BUTTON_COLS][num % NUM_BUTTON_COLS];
pb->m_event_mask = mask;
pb->m_default_color = default_color == -1 ? LED_BLUE : default_color;
pb->m_selected_color = selected_color == -1 ? LED_CYAN : selected_color;
pb->m_touch_color = touch_color == -1 ? LED_YELLOW : touch_color;
setLED(num,pb->m_default_color);
}
void buttonArray::clearRadioGroup(int group)
{
display(0,"clearRadioGroup(%d)",group);
for (int row=0; row<NUM_BUTTON_ROWS; row++)
{
for (int col=0; col<NUM_BUTTON_COLS; col++)
{
arrayedButton *button = &m_buttons[row][col];
if (group == BUTTON_GROUP_OF(button->m_event_mask))
{
button->m_event_state &= ~BUTTON_STATE_TOUCHED;
// really clear em ...
select(row*NUM_BUTTON_COLS+col,0);
}
}
}
showLEDs();
}