-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackButton.cpp
87 lines (76 loc) · 3.02 KB
/
CallbackButton.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
#include "Arduino.h"
#include "CallbackButton.h"
CallbackButton::CallbackButton(int port)
: CallbackButton(port, [](){}) {
}
CallbackButton::CallbackButton(int port, void (*callbackFunction)(int, int))
: CallbackButton(port, 500, 350, 10, callbackFunction) {
}
CallbackButton::CallbackButton(int port, int longClickTimeout, int doubleClickTimeout, int debounceTimeout)
: CallbackButton(port, longClickTimeout, doubleClickTimeout, debounceTimeout, [](){}) {
}
CallbackButton::CallbackButton(int port, int longClickTimeout, int doubleClickTimeout, int debounceTimeout, void (*callbackFunction)(int, int))
: _port(port), _longClickTimeout(longClickTimeout), _doubleClickTimeout(doubleClickTimeout), _debounceTimeout(debounceTimeout), _callbackFunction(callbackFunction) {
_isButtonPressed = false;
_lastIsButtonPressed = false;
_isLongClickCallbackCalled = false;
_buttonPressStartTime = NULL;
_timeSinceLastShortClick = NULL;
_lastDebounceTime = NULL;
}
void CallbackButton::setCallbackFunction(void (*callbackFunction)(int, int)) {
_callbackFunction = callbackFunction;
}
void CallbackButton::checkButtonState() {
unsigned long currentTime = millis();
bool newIsButtonPressed = digitalRead(_port) == HIGH;
if(_lastDebounceTime == NULL) {
if(newIsButtonPressed != _lastIsButtonPressed) {
_lastDebounceTime = currentTime;
}
} else {
if((currentTime - _lastDebounceTime) > _debounceTimeout) {
_isButtonPressed = newIsButtonPressed;
_lastDebounceTime = NULL;
}
}
if(_buttonPressStartTime == NULL) {
if(_isButtonPressed) {
//Button down
_buttonPressStartTime = currentTime;
}
} else {
if(_isButtonPressed) {
if(!_isLongClickCallbackCalled && (currentTime - _buttonPressStartTime >= _longClickTimeout)) {
//Button is being pressed long enough for a long click
_callbackFunction(_port, CALLBACK_BUTTON_LONG_CLICK);
_timeSinceLastShortClick = NULL;
_isLongClickCallbackCalled = true;
}
} else {
//Button up
if(currentTime - _buttonPressStartTime < _longClickTimeout) {
if(_timeSinceLastShortClick == NULL) {
//_timeSinceLastShortClick is NULL, no previous short clicks were executed
_timeSinceLastShortClick = currentTime;
} else {
//_timeSinceLastShortClick is NOT NULL, a short clicks were executed previously.
//If _doubleClickTimeout has not been reached now, this is a double click.
if(currentTime - _timeSinceLastShortClick < _doubleClickTimeout) {
//DOUBLE_CLICK
_callbackFunction(_port, CALLBACK_BUTTON_DOUBLE_CLICK);
_timeSinceLastShortClick = NULL;
}
}
}
_buttonPressStartTime = NULL;
_isLongClickCallbackCalled = false;
}
}
if(!_isButtonPressed && _timeSinceLastShortClick != NULL && currentTime - _timeSinceLastShortClick >= _doubleClickTimeout) {
//SHORT_CLICK
_callbackFunction(_port, CALLBACK_BUTTON_SHORT_CLICK);
_timeSinceLastShortClick = NULL;
}
_lastIsButtonPressed = _isButtonPressed;
}