-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTheButton.cpp
More file actions
42 lines (38 loc) · 877 Bytes
/
TheButton.cpp
File metadata and controls
42 lines (38 loc) · 877 Bytes
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
#include "TheButton.h"
void TheButton::Setup(uint8_t pin) {
pinMode(pin, INPUT_PULLUP);
_Debouncer.attach(pin);
_Debouncer.interval(5);
}
void TheButton::Loop() {
unsigned long t = millis();
if ( _Debouncer.update() ) {
// Get the update value
int value = _Debouncer.read();
if ( value == HIGH) { // Release
if (_ticks > 0) { // Long press
if (_tickUseCallback)
(*_tickUseCallback)(_ticks);
}
else { // short press
if (_clickCallback) {
(*_clickCallback)();
}
}
_pressed = false;
_ticks = 0;
} else { // Press
_pressed = true;
_pressedAt = t;
_tickAt = t;
}
}
if ( _pressed ) { // Long press
if ( t - _tickAt >= _tickInterval ) {
_tickAt = t;
_ticks++;
if (_tickCallback)
(*_tickCallback)(_ticks);
}
}
}