-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXButton.cpp
More file actions
306 lines (242 loc) · 9.05 KB
/
XButton.cpp
File metadata and controls
306 lines (242 loc) · 9.05 KB
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
// -----
// XButton.cpp - Library for detecting button clicks, doubleclicks and long press pattern on a single button.
// This class is implemented for use with the Arduino environment.
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// More information on: http://www.mathertel.de/Arduino
// -----
// Changelog: see XButton.h
// -----
#include "XButton.h"
// ----- Initialization and Default Values -----
XButton::XButton(int pin, int activeLow)
{
pinMode(pin, INPUT_PULLUP); // sets the MenuPin as input
_pin = pin;
_input.attach(_pin);
_clickTicks = 200; // number of millisec that have to pass by before a click is detected.
_pressTicks = 600; // number of millisec that have to pass by before a long button press is detected.
_state = 0; // starting with state 0: waiting for button to be pressed
_isLongPressed = false; // Keep track of long press state
if (activeLow) {
// button connects ground to the pin when pressed.
_buttonReleased = HIGH; // notPressed
_buttonPressed = LOW;
// Removed because of SAMD (Arduino Zero) new implementation
//digitalWrite(pin, HIGH); // turn on pullUp resistor
} else {
// button connects VCC to the pin when pressed.
_buttonReleased = LOW;
_buttonPressed = HIGH;
} // if
_doubleClickFunc = NULL;
_pressFunc = NULL;
_longPressStartFunc = NULL;
_longPressStopFunc = NULL;
_duringLongPressFunc = NULL;
} // XButton
// explicitly set the number of millisec that have to pass by before a click is detected.
void XButton::setClickTicks(int ticks) {
_clickTicks = ticks;
} // setClickTicks
// explicitly set the number of millisec that have to pass by before a long button press is detected.
void XButton::setPressTicks(int ticks) {
_pressTicks = ticks;
} // setPressTicks
// save function for click event
void XButton::attachClick(callbackFunction newFunction)
{
_clickFunc = newFunction;
} // attachClick
// save function for doubleClick event
void XButton::attachDoubleClick(callbackFunction newFunction)
{
_doubleClickFunc = newFunction;
} // attachDoubleClick
void XButton::attachTripleClick(callbackFunction newFunction)
{
_tripleClickFunc = newFunction;
} // attachDoubleClick
// save function for press event
// DEPRECATED, is replaced by attachLongPressStart, attachLongPressStop, attachDuringLongPress,
void XButton::attachPress(callbackFunction newFunction)
{
_pressFunc = newFunction;
} // attachPress
// save function for longPressStart event
void XButton::attachLongPressStart(callbackFunction newFunction)
{
_longPressStartFunc = newFunction;
} // attachLongPressStart
// save function for longPressStop event
void XButton::attachLongPressStop(callbackFunction newFunction)
{
_longPressStopFunc = newFunction;
} // attachLongPressStop
// save function for during longPress event
void XButton::attachDuringLongPress(callbackFunction newFunction)
{
_duringLongPressFunc = newFunction;
} // attachDuringLongPress
// function to get the current long pressed state
bool XButton::isLongPressed(){
return _isLongPressed;
}
void XButton::tick2(void)
{
_input.read();
static long second = 0;
// we will print off the state every second
if(millis() - second >= 1000) {
Serial.print("pin.high(): ");
Serial.print(_input.high());
Serial.print(", pin.low(): ");
Serial.print(_input.low());
if(_input.low()) {
Serial.println(", Button is DOWN");
}
else {
Serial.println(", Button is UP");
}
second = millis();
}
if(_input.changing()) {
Serial.print("Button changing");
}
if(_input.rising()) {
Serial.println(", button being RELEASED");
}
if(_input.falling()) {
Serial.println(", button being PRESSED");
}
int buttonLevel = 0;
// This doesn't support pin to VCC, but only pin to ground (mode true)
if (_input.low()) {
// button is down
buttonLevel = _buttonPressed;
} else {
buttonLevel = _buttonReleased;
}
unsigned long now = millis(); // current (relative) time in msecs.
// Implementation of the state machine
if (_state == 0) { // waiting for menu pin being pressed.
if (buttonLevel == _buttonPressed) {
_state = 1; // step to state 1
_startTime = now; // remember starting time
} // if
} else if (_state == 1) { // waiting for menu pin being released.
if ((buttonLevel == _buttonReleased) && ((unsigned long)(now - _startTime) < _debounceTicks)) {
// button was released to quickly so I assume some debouncing.
// go back to state 0 without calling a function.
_state = 0;
} else if (buttonLevel == _buttonReleased) {
_state = 2; // step to state 2
} else if ((buttonLevel == _buttonPressed) && ((unsigned long)(now - _startTime) > _pressTicks)) {
_isLongPressed = true; // Keep track of long press state
if (_pressFunc) _pressFunc();
if (_longPressStartFunc) _longPressStartFunc();
if (_duringLongPressFunc) _duringLongPressFunc();
_state = 6; // step to state 6
} else {
// wait. Stay in this state.
} // if
} else if (_state == 2) { // waiting for menu pin being pressed the second time or timeout.
if ((unsigned long)(now - _startTime) > _clickTicks) {
// this was only a single short click
if (_clickFunc) _clickFunc();
_state = 0; // restart.
} else if (buttonLevel == _buttonPressed) {
_state = 3; // step to state 3
} // if
} else if (_state == 3) { // waiting for menu pin being released finally.
// maybe should wait here for debounce ticks
if (now > _startTime + _clickTicks*2) {
if (buttonLevel == _buttonReleased) {
// this was a 2 click sequence.
if (_doubleClickFunc) _doubleClickFunc();
_state = 0; // restart.
} else {
_state = 4;
}
}
} else if (_state == 4) {
if (buttonLevel == _buttonReleased) {
if (_tripleClickFunc) _tripleClickFunc();
_state = 0;
}
} else if (_state == 6) { // waiting for menu pin being release after long press.
if (buttonLevel == _buttonReleased) {
_isLongPressed = false; // Keep track of long press state
if(_longPressStopFunc) _longPressStopFunc();
_state = 0; // restart.
} else {
// button is being long pressed
_isLongPressed = true; // Keep track of long press state
if (_duringLongPressFunc) _duringLongPressFunc();
} // if
} // if
} // XButton.tick()
void XButton::tick(void)
{
// Detect the input information
int buttonLevel = digitalRead(_pin); // current button signal.
unsigned long now = millis(); // current (relative) time in msecs.
// Implementation of the state machine
if (_state == 0) { // waiting for menu pin being pressed.
if (buttonLevel == _buttonPressed) {
_state = 1; // step to state 1
_startTime = now; // remember starting time
} // if
} else if (_state == 1) { // waiting for menu pin being released.
if ((buttonLevel == _buttonReleased) && ((unsigned long)(now - _startTime) < _debounceTicks)) {
// button was released to quickly so I assume some debouncing.
// go back to state 0 without calling a function.
_state = 0;
} else if (buttonLevel == _buttonReleased) {
_state = 2; // step to state 2
} else if ((buttonLevel == _buttonPressed) && ((unsigned long)(now - _startTime) > _pressTicks)) {
_isLongPressed = true; // Keep track of long press state
if (_pressFunc) _pressFunc();
if (_longPressStartFunc) _longPressStartFunc();
if (_duringLongPressFunc) _duringLongPressFunc();
_state = 6; // step to state 6
} else {
// wait. Stay in this state.
} // if
} else if (_state == 2) { // waiting for menu pin being pressed the second time or timeout.
if ((unsigned long)(now - _startTime) > _clickTicks) {
// this was only a single short click
if (_clickFunc) _clickFunc();
_state = 0; // restart.
} else if (buttonLevel == _buttonPressed) {
_state = 3; // step to state 3
} // if
} else if (_state == 3) { // waiting for menu pin being released finally.
// maybe should wait here for debounce ticks
if (now > _startTime + _clickTicks*2) {
if (buttonLevel == _buttonReleased) {
// this was a 2 click sequence.
if (_doubleClickFunc) _doubleClickFunc();
_state = 0; // restart.
} else {
_state = 4;
}
}
} else if (_state == 4) {
if (buttonLevel == _buttonReleased) {
if (_tripleClickFunc) _tripleClickFunc();
_state = 0;
}
} else if (_state == 6) { // waiting for menu pin being release after long press.
if (buttonLevel == _buttonReleased) {
_isLongPressed = false; // Keep track of long press state
if(_longPressStopFunc) _longPressStopFunc();
_state = 0; // restart.
} else {
// button is being long pressed
_isLongPressed = true; // Keep track of long press state
if (_duringLongPressFunc) _duringLongPressFunc();
} // if
} // if
} // XButton.tick()
// end.