-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoButton.cpp
More file actions
114 lines (94 loc) · 3.7 KB
/
ArduinoButton.cpp
File metadata and controls
114 lines (94 loc) · 3.7 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
/*
ArduinoButton.cpp - Library for facilitating interfacing with buttons.
Created by JMax, October 14, 2023.
Released into the public domain.
*/
#include "Arduino.h"
#include "ArduinoButton.h"
ArduinoButton::ArduinoButton(int buttonPin, bool isActiveLow) {
setButtonPin(buttonPin);
keyUpCallback = NULL;
keyDownCallback = NULL;
doubleClickCallback = NULL;
prevState = digitalRead(buttonPin);
setLastKeyDown(millis());
setDoubleClickDelay(0);
setDebounceDelay(20);
setLastDebounceTime(millis());
setIsActiveLow(isActiveLow);
setLongKeyDownLastReading(millis());
longKeyDownIsHeld = false;
}
void ArduinoButton::loop() {
if(keyUpCallback == NULL && keyDownCallback == NULL && doubleClickCallback == NULL) return;
bool val = digitalRead(buttonPin);
if(val != lastReading) {
lastReading = val;
setLastDebounceTime(millis());
return;
}
if(val == lastReading && millis() - lastDebounceTime >= debounceDelay) {
if(keyUpCallback != NULL && val == normalizeButtonState(LOW) && prevState == normalizeButtonState(HIGH)) keyUpCallback();
if(keyDownCallback != NULL && val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(LOW)) keyDownCallback();
if(doubleClickCallback != NULL && val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(LOW) && millis() - lastKeyDown <= doubleClickDelay) doubleClickCallback();
else if (doubleClickCallback != NULL && val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(LOW)) lastKeyDown = millis();
if(longKeyDownCallback != NULL) {
if(val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(LOW) && !longKeyDownIsHeld) setLongKeyDownLastReading(millis());
if(val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(HIGH) && !longKeyDownIsHeld && millis() - longKeyDownLastReading >= longKeyDownInitialDelay) {
setLongKeyDownLastReading(millis());
longKeyDownIsHeld = true;
longKeyDownCallback();
} else if (val == normalizeButtonState(HIGH) && prevState == normalizeButtonState(HIGH) && longKeyDownIsHeld && millis() - longKeyDownLastReading >= longKeyDownDelay) {
setLongKeyDownLastReading(millis());
longKeyDownCallback();
} else if (val == normalizeButtonState(LOW) && prevState == normalizeButtonState(HIGH) && longKeyDownIsHeld) {
setLongKeyDownLastReading(millis());
longKeyDownIsHeld = false;
}
}
prevState = val;
}
else return;
}
void ArduinoButton::onKeyUp(void (*cb)()) {
keyUpCallback = cb;
}
void ArduinoButton::onKeyDown(void (*cb)()) {
keyDownCallback = cb;
}
void ArduinoButton::onDoubleClick(void (*cb)(), int delay = 500) {
doubleClickCallback = cb;
setDoubleClickDelay(delay);
}
void ArduinoButton::onLongKeyDown(void (*cb)(), int initialDelay = 1000, int delay = 500) {
longKeyDownCallback = cb;
longKeyDownInitialDelay = initialDelay;
setLongKeyDownDelay(delay);
}
void ArduinoButton::setButtonPin(int pin) {
buttonPin = pin;
}
void ArduinoButton::setLastKeyDown(unsigned long num) {
lastKeyDown = num;
}
void ArduinoButton::setDoubleClickDelay(int delay) {
doubleClickDelay = delay;
}
void ArduinoButton::setDebounceDelay(int delay) {
debounceDelay = delay;
}
void ArduinoButton::setLastDebounceTime(unsigned long time) {
lastDebounceTime = time;
}
void ArduinoButton::setIsActiveLow(bool activeLow) {
isActiveLow = activeLow;
}
bool ArduinoButton::normalizeButtonState(bool state) {
return isActiveLow ? (state == HIGH ? LOW : HIGH) : state;
}
void ArduinoButton::setLongKeyDownDelay(int delay) {
longKeyDownDelay = delay;
}
void ArduinoButton::setLongKeyDownLastReading(unsigned long time) {
longKeyDownLastReading = time;
}