-
Notifications
You must be signed in to change notification settings - Fork 19
/
OneButton.cpp
333 lines (261 loc) · 8.79 KB
/
OneButton.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
/**
* @file OneButton.cpp
*
* @brief Library for detecting button clicks, doubleclicks and long press
* pattern on a single button.
*
* @author Matthias Hertel, https://www.mathertel.de
* @Copyright Copyright (c) by Matthias Hertel, https://www.mathertel.de.
*
* This work is licensed under a BSD style license. See
* http://www.mathertel.de/License.aspx
*
* More information on: https://www.mathertel.de/Arduino/OneButtonLibrary.aspx
*
* Changelog: see OneButton.h
*/
#include "OneButton.h"
// ----- Initialization and Default Values -----
/**
* @brief Construct a new OneButton object but not (yet) initialize the IO pin.
*/
OneButton::OneButton()
{
_pin = -1;
// further initialization has moved to OneButton.h
}
/**
* Initialize the OneButton library.
* @param pin The pin to be used for input from a momentary button.
* @param activeLow Set to true when the input level is LOW when the button is pressed, Default is true.
* @param pullupActive Activate the internal pullup when available. Default is true.
*/
OneButton::OneButton(const int pin, const boolean activeLow, const bool pullupActive)
{
// OneButton();
_pin = pin;
if (activeLow) {
// the button connects the input pin to GND when pressed.
_buttonPressed = LOW;
} else {
// the button connects the input pin to VCC when pressed.
_buttonPressed = HIGH;
} // if
if (pullupActive) {
// use the given pin as input and activate internal PULLUP resistor.
pinMode(pin, INPUT_PULLUP);
} else {
// use the given pin as input
pinMode(pin, INPUT);
} // if
} // OneButton
// explicitly set the number of millisec that have to pass by before a click is assumed stable.
void OneButton::setDebounceTicks(const int ticks)
{
_debounceTicks = ticks;
} // setDebounceTicks
// explicitly set the number of millisec that have to pass by before a click is detected.
void OneButton::setClickTicks(const int ticks)
{
_clickTicks = ticks;
} // setClickTicks
// explicitly set the number of millisec that have to pass by before a long button press is detected.
void OneButton::setPressTicks(const int ticks)
{
_pressTicks = ticks;
} // setPressTicks
// save function for click event
void OneButton::attachClick(callbackFunction newFunction)
{
_clickFunc = newFunction;
} // attachClick
// save function for parameterized click event
void OneButton::attachClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramClickFunc = newFunction;
_clickFuncParam = parameter;
} // attachClick
// save function for doubleClick event
void OneButton::attachDoubleClick(callbackFunction newFunction)
{
_doubleClickFunc = newFunction;
_maxClicks = max(_maxClicks, 2);
} // attachDoubleClick
// save function for parameterized doubleClick event
void OneButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramDoubleClickFunc = newFunction;
_doubleClickFuncParam = parameter;
_maxClicks = max(_maxClicks, 2);
} // attachDoubleClick
// save function for multiClick event
void OneButton::attachMultiClick(callbackFunction newFunction)
{
_multiClickFunc = newFunction;
_maxClicks = max(_maxClicks, 100);
} // attachMultiClick
// save function for parameterized MultiClick event
void OneButton::attachMultiClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramMultiClickFunc = newFunction;
_multiClickFuncParam = parameter;
_maxClicks = max(_maxClicks, 100);
} // attachMultiClick
// save function for longPressStart event
void OneButton::attachLongPressStart(callbackFunction newFunction)
{
_longPressStartFunc = newFunction;
} // attachLongPressStart
// save function for parameterized longPressStart event
void OneButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramLongPressStartFunc = newFunction;
_longPressStartFuncParam = parameter;
} // attachLongPressStart
// save function for longPressStop event
void OneButton::attachLongPressStop(callbackFunction newFunction)
{
_longPressStopFunc = newFunction;
} // attachLongPressStop
// save function for parameterized longPressStop event
void OneButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramLongPressStopFunc = newFunction;
_longPressStopFuncParam = parameter;
} // attachLongPressStop
// save function for during longPress event
void OneButton::attachDuringLongPress(callbackFunction newFunction)
{
_duringLongPressFunc = newFunction;
} // attachDuringLongPress
// save function for parameterized during longPress event
void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramDuringLongPressFunc = newFunction;
_duringLongPressFuncParam = parameter;
} // attachDuringLongPress
void OneButton::reset(void)
{
_state = OneButton::OCS_INIT;
_lastState = OneButton::OCS_INIT;
_nClicks = 0;
_startTime = 0;
}
// ShaggyDog ---- return number of clicks in any case: single or multiple clicks
int OneButton::getNumberClicks(void)
{
return _nClicks;
}
/**
* @brief Check input of the configured pin and then advance the finite state
* machine (FSM).
*/
void OneButton::tick(void)
{
if (_pin >= 0) {
tick(digitalRead(_pin) == _buttonPressed);
}
}
/**
* @brief Advance to a new state and save the last one to come back in cas of bouncing detection.
*/
void OneButton::_newState(stateMachine_t nextState)
{
_lastState = _state;
_state = nextState;
} // _newState()
/**
* @brief Run the finite state machine (FSM) using the given level.
*/
void OneButton::tick(bool activeLevel)
{
unsigned long now = millis(); // current (relative) time in msecs.
unsigned long waitTime = (now - _startTime);
// Implementation of the state machine
switch (_state) {
case OneButton::OCS_INIT:
// waiting for level to become active.
if (activeLevel) {
_newState(OneButton::OCS_DOWN);
_startTime = now; // remember starting time
_nClicks = 0;
} // if
break;
case OneButton::OCS_DOWN:
// waiting for level to become inactive.
if ((!activeLevel) && (waitTime < _debounceTicks)) {
// button was released to quickly so I assume some bouncing.
_newState(_lastState);
} else if (!activeLevel) {
_newState(OneButton::OCS_UP);
_startTime = now; // remember starting time
} else if ((activeLevel) && (waitTime > _pressTicks)) {
if (_longPressStartFunc) _longPressStartFunc();
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
_newState(OneButton::OCS_PRESS);
} // if
break;
case OneButton::OCS_UP:
// level is inactive
if ((activeLevel) && (waitTime < _debounceTicks)) {
// button was pressed to quickly so I assume some bouncing.
_newState(_lastState); // go back
} else if (waitTime >= _debounceTicks) {
// count as a short button down
_nClicks++;
_newState(OneButton::OCS_COUNT);
} // if
break;
case OneButton::OCS_COUNT:
// dobounce time is over, count clicks
if (activeLevel) {
// button is down again
_newState(OneButton::OCS_DOWN);
_startTime = now; // remember starting time
} else if ((waitTime > _clickTicks) || (_nClicks == _maxClicks)) {
// now we know how many clicks have been made.
if (_nClicks == 1) {
// this was 1 click only.
if (_clickFunc) _clickFunc();
if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
} else if (_nClicks == 2) {
// this was a 2 click sequence.
if (_doubleClickFunc) _doubleClickFunc();
if (_paramDoubleClickFunc) _paramDoubleClickFunc(_doubleClickFuncParam);
} else {
// this was a multi click sequence.
if (_multiClickFunc) _multiClickFunc();
if (_paramMultiClickFunc) _paramMultiClickFunc(_multiClickFuncParam);
} // if
reset();
} // if
break;
case OneButton::OCS_PRESS:
// waiting for menu pin being release after long press.
if (!activeLevel) {
_newState(OneButton::OCS_PRESSEND);
_startTime = now;
} else {
// still the button is pressed
if (_duringLongPressFunc) _duringLongPressFunc();
if (_paramDuringLongPressFunc) _paramDuringLongPressFunc(_duringLongPressFuncParam);
} // if
break;
case OneButton::OCS_PRESSEND:
// button was released.
if ((activeLevel) && (waitTime < _debounceTicks)) {
// button was released to quickly so I assume some bouncing.
_newState(_lastState); // go back
} else if (waitTime >= _debounceTicks) {
if (_longPressStopFunc) _longPressStopFunc();
if (_paramLongPressStopFunc) _paramLongPressStopFunc(_longPressStopFuncParam);
reset();
}
break;
default:
// unknown state detected -> reset state machine
_newState(OneButton::OCS_INIT);
break;
} // if
} // OneButton.tick()
// end.