-
Notifications
You must be signed in to change notification settings - Fork 6
/
buttons.h
281 lines (234 loc) · 6.44 KB
/
buttons.h
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
#ifndef BUTTONS_H
#define BUTTONS_H
byte kp_key = 0;
byte kp_duration = 0;
int kp_repeat_count = 0;
unsigned long kp_repeat = 0;
#define KP_NORMAL 100
#define KP_LONG 1000
#define KP_REPEAT 75
#if DebugButton == true
const char *keynames[16] = { "None", "Enter", "Start", "3", "Down", "5", "6", "7", "Up", "9", "10", "11", "Escape", "13", "14", "All" };
#endif
const byte buttonNone = 0;
const byte buttonEnter = 1;
const byte buttonStart = 2;
const byte buttonDown = 4;
const byte buttonUp = 8;
const byte buttonEscape = 12;
/*
Initialize this thread.
*/
void ButtonsInit() {
pinMode (ButtonUpPin, INPUT_PULLUP);
pinMode (ButtonDownPin, INPUT_PULLUP);
pinMode (ButtonStartPin, INPUT_PULLUP);
pinMode (ButtonEnterPin, INPUT_PULLUP);
}
/*
Buttons thread, register the current button state.
*/
void ButtonsThread() {
static byte _kp_key_current = 0;
static byte _kp_key_last = 0;
static unsigned long _kp_key_start = 0;
static unsigned long lastcall = 0;
if (gCurrentTimeInMS < (lastcall + 20)) {
return;
}
lastcall = gCurrentTimeInMS;
_kp_key_current = 0;
if (digitalRead (ButtonEnterPin) == 0)
_kp_key_current += 1;
if (digitalRead (ButtonStartPin) == 0)
_kp_key_current += 2;
if (digitalRead (ButtonDownPin) == 0)
_kp_key_current += 4;
if (digitalRead (ButtonUpPin) == 0)
_kp_key_current += 8;
if (_kp_key_current) {
// Some key is pressed.
if (_kp_key_current != _kp_key_last) {
// Steady key.
_kp_key_last = _kp_key_current;
_kp_key_start = gCurrentTimeInMS;
} else {
if ((gCurrentTimeInMS > (_kp_key_start + KP_NORMAL)) && (! kp_duration)) {
// At least 100 mSec pressed.
kp_key = _kp_key_current;
kp_duration = gCurrentTimeInMS - _kp_key_start;
#if DebugButton == true
Serial.print(F("ButtonsThread: key: "));
Serial.print(kp_key);
Serial.print(F(" "));
Serial.print(keynames[kp_key]);
Serial.print(F(" duration: "));
Serial.print(kp_duration);
Serial.println(F(" single"));
#endif
}
if ((gCurrentTimeInMS > (_kp_key_start + KP_LONG)) && (! kp_repeat)) {
kp_repeat = gCurrentTimeInMS;
}
if ((gCurrentTimeInMS > (_kp_key_start + KP_LONG)) && (gCurrentTimeInMS > (kp_repeat + KP_REPEAT))) {
kp_key = _kp_key_current;
kp_duration = gCurrentTimeInMS - _kp_key_start;
kp_repeat_count++;
#if DebugButton == true
Serial.print(F("ButtonsThread: key: "));
Serial.print(kp_key);
Serial.print(F(" "));
Serial.print(keynames[kp_key]);
Serial.print(F(" duration: "));
Serial.print(kp_duration);
Serial.print(F(" repeated: "));
Serial.println(kp_repeat_count);
#endif
kp_repeat = 0;
}
}
} else {
// No key pressed, clear variables.
kp_key = 0;
kp_duration = 0;
kp_repeat = 0;
kp_repeat_count = 0;
_kp_key_current = _kp_key_last = 0;
}
}
/*
Non-blocking read the pressed key and remove it from the buffer.
*/
byte ReadKey() {
byte rc = 0;
if (kp_key) {
rc = kp_key;
kp_key = 0;
}
return rc;
}
/*
Test Up/Down buttons and register the time if pressed.
*/
byte ReadButton(byte& Direction, unsigned long& Timer, byte button) {
boolean f_btnUp, f_btnDn;
if (button == buttonUp) { // Up button is pressed
if (Direction != DirectionUp)
Timer = millis(); // Register time of new direction
f_btnUp = true;
Direction = DirectionUp; // Register new direction
} else
f_btnUp = false;
if (button == buttonDown) { // Down button is pressed
if (Direction != DirectionDown)
Timer = millis(); // Register time of new direction
f_btnDn = true;
Direction = DirectionDown; // Register new direction
} else
f_btnDn = false;
if (f_btnUp == false && f_btnDn == false)
Direction = DirectionNone; // No Up or Down direction.
}
int Set(int& Set, int Up, int Low, int Step, long Timer, byte Direction, byte button) {
int step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
if (Direction == DirectionUp) {
ButtonPressed = (button == buttonUp);
}
if (Direction == DirectionDown) {
ButtonPressed = (button == buttonDown);
}
if (ButtonPressed && Direction != DirectionNone) {
if (kp_repeat_count > 20) {
step_size = (Step * 20);
} else if (kp_repeat_count > 5) {
step_size = (Step * 5);
} else {
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
float Set(float& Set, float Up, float Low, float Step, long Timer, byte Direction, byte button) {
float step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
if (Direction == DirectionUp) {
ButtonPressed = (button == buttonUp);
}
if (Direction == DirectionDown) {
ButtonPressed = (button == buttonDown);
}
if (ButtonPressed && Direction != DirectionNone) {
if (kp_repeat_count > 20) {
step_size = (Step * 50.0);
} else if (kp_repeat_count > 5) {
step_size = (Step * 5.0);
} else {
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
byte Set(byte& Set, byte Up, byte Low, byte Step, long Timer, byte Direction, byte button) {
byte step_size;
int ButtonPressed;
if (Set > Up)
Set = Up;
if (Set < Low)
Set = Low;
if (Direction == DirectionUp) {
ButtonPressed = (button == buttonUp);
}
if (Direction == DirectionDown) {
ButtonPressed = (button == buttonDown);
}
if (ButtonPressed && Direction != DirectionNone) {
if (kp_repeat_count > 20) {
step_size = (Step * 20);
} else if (kp_repeat_count > 5) {
step_size = (Step * 5);
} else {
step_size = Step;
}
if (Direction == DirectionUp) {
if (Set + step_size > Up)
Set = Up;
else
Set += step_size;
} else if (Direction == DirectionDown) {
if (Set - step_size < Low)
Set = Low;
else
Set -= step_size;
}
}
}
#endif