-
Notifications
You must be signed in to change notification settings - Fork 3
/
ButtonKing.cpp
285 lines (245 loc) · 7.83 KB
/
ButtonKing.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
/**
* @file ButtonKing.cpp
*
* @brief Library for detecting button clicks, doubleclicks and simple long
* press and double Long press pattern on a single button.
*
* @author Tanyanat Pichitwong
* @Copyright Copyright (c) by Tanyanat Pichitwong
*
* Changelog: see ButtonKing.h
*/
#include "ButtonKing.h"
// ----- Initialization and Default Values -----
/**
* Construct a new ButtonKing object but not initialize the IO pin.
*/
ButtonKing::ButtonKing()
{
_pin = -1;
}
ButtonKing::ButtonKing(int pin, int activeLow, bool pullupActive)
{
// ButtonKing();
_pin = pin;
if (activeLow) {
// the button connects the input pin to GND when pressed.
_intButtonStage = HIGH;
} else {
// the button connects the input pin to VCC when pressed.
_intButtonStage = LOW;
} // 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
} // ButtonKing
// explicitly set the number of millisec that have to pass by before a click is
// assumed as safe.
void ButtonKing::setTimeDebounce(int ticks)
{
_debounceTime = ticks;
} // setDebounceTime
// explicitly set the number of millisec that have to wait before counting the
// button-ticks was ended.
void ButtonKing::setTimeCount(int ticks)
{
_waitTime = ticks;
} // setTimeCount
/////////////////////////////////////////////////////////////////////////////
// save function for click event
void ButtonKing::setClick(callbackFunction newFunction)
{
_ClickFunc = newFunction;
} // setClick
// save function for parameterized click event
void ButtonKing::setClick(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramClickFunc = newFunction;
_ClickFuncParam = parameter;
} // setClick
// save function for press event
void ButtonKing::setPress(callbackFunction newFunction)
{
_PressFunc = newFunction;
} // setPress
// save function for parameterized press event
void ButtonKing::setPress(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramPressFunc = newFunction;
_PressFuncParam = parameter;
} // setPress
// save function for release event
void ButtonKing::setRelease(callbackFunction newFunction)
{
_ReleaseFunc = newFunction;
} // setRelease
// save function for parameterized release event
void ButtonKing::setRelease(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramReleaseFunc = newFunction;
_ReleaseFuncParam = parameter;
} // setRelease
// save function for doubleclick event
void ButtonKing::setDoubleClick(callbackFunction newFunction)
{
_DoubleClickFunc = newFunction;
} // setDoubleClick
// save function for parameterized doubleclick event
void ButtonKing::setDoubleClick(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramDoubleClickFunc = newFunction;
_DoubleClickFuncParam = parameter;
} // setDoubleClick
// save function for doublepress event
void ButtonKing::setDoublePress(callbackFunction newFunction)
{
_DoublePressFunc = newFunction;
} // setDoublePress
// save function for parameterized doublepress event
void ButtonKing::setDoublePress(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramDoublePressFunc = newFunction;
_DoublePressFuncParam = parameter;
} // setDoublePress
// save function for tripleclick event
void ButtonKing::setTripleClick(callbackFunction newFunction)
{
_TripleClickFunc = newFunction;
} // setTripleClick
// save function for parameterized tripleclick event
void ButtonKing::setTripleClick(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramTripleClickFunc = newFunction;
_TripleClickFuncParam = parameter;
} // setTripleClick
// save function for triplepress event
void ButtonKing::setTriplePress(callbackFunction newFunction)
{
_TriplePressFunc = newFunction;
} // setTriplePress
// save function for parameterized triplepress event
void ButtonKing::setTriplePress(parameterizedCallbackFunction newFunction, void* parameter)
{
_paramTriplePressFunc = newFunction;
_TriplePressFuncParam = parameter;
} // setTriplePress
int ButtonKing::getPressedTimer(){
return _stopPressedTimer - _pressedTimer;
}
/**
* @brief Check input of the configured pin and then advance the finite state
* machine (FSM).
*/
void ButtonKing::isClick(void)
{
if (_pin >= 0) {
isClick(digitalRead(_pin) != _intButtonStage);
}
}
/**
* Advance the finite state machine (FSM) using the given level.
*/
void ButtonKing::isClick(bool activeLevel)
{
unsigned long now = millis(); // current (relative) time in msecs.
if((_buttonCount != 0) || (_buttonCount == 0 && _debounceState == 2)){ // this is not the 1st times.
if(now - _waitingTimer >= _waitTime){ // complete.
if(!activeLevel){
if (_buttonCount == 1) {
// Click the button.
if (_ClickFunc)
_ClickFunc();
if (_paramClickFunc)
_paramClickFunc(_ClickFuncParam);
} else if (_buttonCount == 2) {
// DoubleClick the button.
if (_DoubleClickFunc)
_DoubleClickFunc();
if (_paramDoubleClickFunc)
_paramDoubleClickFunc(_DoubleClickFuncParam);
} else if (_buttonCount == 3) {
// TripleClick the button.
if (_TripleClickFunc)
_TripleClickFunc();
if (_paramTripleClickFunc)
_paramTripleClickFunc(_TripleClickFuncParam);
}
} else {
if (_buttonCount == 0) {
// Pressing the button.
if (_PressFunc)
_PressFunc();
if (_paramPressFunc)
_paramPressFunc(_PressFuncParam);
} else if (_buttonCount == 1) {
// DoublePressing the button.
if (_DoublePressFunc)
_DoublePressFunc();
if (_paramDoublePressFunc)
_paramDoublePressFunc(_DoublePressFuncParam);
} else if (_buttonCount == 2) {
// TriplePressing the button.
if (_TriplePressFunc)
_TriplePressFunc();
if (_paramTriplePressFunc)
_paramTriplePressFunc(_TriplePressFuncParam);
}
_pressedTimer = millis(); // remember the time that button started to pressed.
_debounceState = 3;
}
// RESET //
_buttonCount = 0;
_debounceTimer = 0;
_waitingTimer = 0;
}
}
// Implementation of the state machine
if (_debounceState == 0) { // waiting for pressing
if(activeLevel && !_buttonFlag){
_debounceTimer = now;
if(_buttonCount == 0){ // for the 1st time.
_waitingTimer = now;
}
_buttonFlag = true;
_debounceState = 1;
}
} else if (_debounceState == 1) { // wait for debounce timer.
if(now - _debounceTimer >= _debounceTime){
if(activeLevel){
_debounceState = 2;
}
}
} else if (_debounceState == 2) { // wait for release the button.
if(!activeLevel){
_buttonCount++;
_buttonFlag = false;
_debounceState = 0;
}
} else if (_debounceState == 3) { //wait for released after time-out.
if(!activeLevel){
_buttonFlag = false;
_debounceState = 0;
_stopPressedTimer = millis();
// Stop Pressing the button.
if (_ReleaseFunc)
_ReleaseFunc();
if (_paramReleaseFunc)
_paramReleaseFunc(_ReleaseFuncParam);
}
}
} // ButtonKing
void ButtonKing::reset(void)
{
_debounceState = 0;
_buttonCount = 0;
_buttonFlag = false;
_waitingTimer = 0;
_debounceTimer = 0;
_pressedTimer = 0;
_stopPressedTimer = 0;
}
// end.