6
6
*
7
7
* @author Matthias Hertel, https://www.mathertel.de
8
8
* @Copyright Copyright (c) by Matthias Hertel, https://www.mathertel.de.
9
+ * Ihor Nehrutsa, [email protected]
9
10
*
10
11
* This work is licensed under a BSD style license. See
11
12
* http://www.mathertel.de/License.aspx
@@ -36,7 +37,6 @@ OneButton::OneButton()
36
37
*/
37
38
OneButton::OneButton (const int pin, const boolean activeLow, const bool pullupActive)
38
39
{
39
- // OneButton();
40
40
_pin = pin;
41
41
42
42
if (activeLow) {
@@ -46,36 +46,36 @@ OneButton::OneButton(const int pin, const boolean activeLow, const bool pullupAc
46
46
} else {
47
47
// the button connects the input pin to VCC when pressed.
48
48
_buttonPressed = HIGH;
49
- } // if
49
+ }
50
50
51
51
if (pullupActive) {
52
52
// use the given pin as input and activate internal PULLUP resistor.
53
53
pinMode (pin, INPUT_PULLUP);
54
54
} else {
55
55
// use the given pin as input
56
56
pinMode (pin, INPUT);
57
- } // if
57
+ }
58
58
} // OneButton
59
59
60
60
61
61
// explicitly set the number of millisec that have to pass by before a click is assumed stable.
62
- void OneButton::setDebounceTicks (const int ticks )
62
+ void OneButton::setDebounceTicks (const unsigned int ms )
63
63
{
64
- _debounceTicks = ticks ;
64
+ _debounce_ms = ms ;
65
65
} // setDebounceTicks
66
66
67
67
68
68
// explicitly set the number of millisec that have to pass by before a click is detected.
69
- void OneButton::setClickTicks (const int ticks )
69
+ void OneButton::setClickTicks (const unsigned int ms )
70
70
{
71
- _clickTicks = ticks ;
71
+ _click_ms = ms ;
72
72
} // setClickTicks
73
73
74
74
75
75
// explicitly set the number of millisec that have to pass by before a long button press is detected.
76
- void OneButton::setPressTicks (const int ticks )
76
+ void OneButton::setPressTicks (const unsigned int ms )
77
77
{
78
- _pressTicks = ticks ;
78
+ _press_ms = ms ;
79
79
} // setPressTicks
80
80
81
81
@@ -176,7 +176,6 @@ void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction,
176
176
void OneButton::reset (void )
177
177
{
178
178
_state = OneButton::OCS_INIT;
179
- _lastState = OneButton::OCS_INIT;
180
179
_nClicks = 0 ;
181
180
_startTime = 0 ;
182
181
}
@@ -190,23 +189,32 @@ int OneButton::getNumberClicks(void)
190
189
191
190
192
191
/* *
193
- * @brief Check input of the configured pin and then advance the finite state
194
- * machine (FSM).
192
+ * @brief Check input of the configured pin,
193
+ * debounce input pin level and then
194
+ * advance the finite state machine (FSM).
195
195
*/
196
196
void OneButton::tick (void )
197
197
{
198
198
if (_pin >= 0 ) {
199
- tick (digitalRead (_pin) == _buttonPressed);
199
+ int pinLevel = digitalRead (_pin);
200
+ now = millis (); // current (relative) time in msecs.
201
+ if (_lastDebouncePinLevel == pinLevel) {
202
+ if ((now - _lastDebounceTime) >= _debounce_ms) {
203
+ tick (pinLevel == _buttonPressed); // pinLevel is debounced here
204
+ }
205
+ } else {
206
+ _lastDebouncePinLevel = pinLevel;
207
+ _lastDebounceTime = now;
208
+ }
200
209
}
201
- }
210
+ } // tick()
202
211
203
212
204
213
/* *
205
214
* @brief Advance to a new state and save the last one to come back in cas of bouncing detection.
206
215
*/
207
216
void OneButton::_newState (stateMachine_t nextState)
208
217
{
209
- _lastState = _state;
210
218
_state = nextState;
211
219
} // _newState()
212
220
@@ -216,7 +224,6 @@ void OneButton::_newState(stateMachine_t nextState)
216
224
*/
217
225
void OneButton::tick (bool activeLevel)
218
226
{
219
- unsigned long now = millis (); // current (relative) time in msecs.
220
227
unsigned long waitTime = (now - _startTime);
221
228
222
229
// Implementation of the state machine
@@ -233,15 +240,11 @@ void OneButton::tick(bool activeLevel)
233
240
case OneButton::OCS_DOWN:
234
241
// waiting for level to become inactive.
235
242
236
- if ((!activeLevel) && (waitTime < _debounceTicks)) {
237
- // button was released to quickly so I assume some bouncing.
238
- _newState (_lastState);
239
-
240
- } else if (!activeLevel) {
243
+ if (!activeLevel) {
241
244
_newState (OneButton::OCS_UP);
242
245
_startTime = now; // remember starting time
243
246
244
- } else if ((activeLevel) && (waitTime > _pressTicks )) {
247
+ } else if ((activeLevel) && (waitTime > _press_ms )) {
245
248
if (_longPressStartFunc) _longPressStartFunc ();
246
249
if (_paramLongPressStartFunc) _paramLongPressStartFunc (_longPressStartFuncParam);
247
250
_newState (OneButton::OCS_PRESS);
@@ -251,15 +254,9 @@ void OneButton::tick(bool activeLevel)
251
254
case OneButton::OCS_UP:
252
255
// level is inactive
253
256
254
- if ((activeLevel) && (waitTime < _debounceTicks)) {
255
- // button was pressed to quickly so I assume some bouncing.
256
- _newState (_lastState); // go back
257
-
258
- } else if (waitTime >= _debounceTicks) {
259
257
// count as a short button down
260
258
_nClicks++;
261
259
_newState (OneButton::OCS_COUNT);
262
- } // if
263
260
break ;
264
261
265
262
case OneButton::OCS_COUNT:
@@ -270,7 +267,7 @@ void OneButton::tick(bool activeLevel)
270
267
_newState (OneButton::OCS_DOWN);
271
268
_startTime = now; // remember starting time
272
269
273
- } else if ((waitTime > _clickTicks ) || (_nClicks == _maxClicks)) {
270
+ } else if ((waitTime >= _click_ms ) || (_nClicks == _maxClicks)) {
274
271
// now we know how many clicks have been made.
275
272
276
273
if (_nClicks == 1 ) {
@@ -294,7 +291,7 @@ void OneButton::tick(bool activeLevel)
294
291
break ;
295
292
296
293
case OneButton::OCS_PRESS:
297
- // waiting for menu pin being release after long press.
294
+ // waiting for pin being release after long press.
298
295
299
296
if (!activeLevel) {
300
297
_newState (OneButton::OCS_PRESSEND);
@@ -310,15 +307,9 @@ void OneButton::tick(bool activeLevel)
310
307
case OneButton::OCS_PRESSEND:
311
308
// button was released.
312
309
313
- if ((activeLevel) && (waitTime < _debounceTicks)) {
314
- // button was released to quickly so I assume some bouncing.
315
- _newState (_lastState); // go back
316
-
317
- } else if (waitTime >= _debounceTicks) {
318
310
if (_longPressStopFunc) _longPressStopFunc ();
319
311
if (_paramLongPressStopFunc) _paramLongPressStopFunc (_longPressStopFuncParam);
320
312
reset ();
321
- }
322
313
break ;
323
314
324
315
default :
0 commit comments