-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushButton.cpp
37 lines (31 loc) · 948 Bytes
/
PushButton.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
#include "PushButton.h"
PushButton::PushButton(byte pin, bool pullup, Callback press_callback = NULL, int debounce_delay = 50) {
pinMode(pin, (pullup ? INPUT_PULLUP : INPUT));
this->pin = pin;
this->pressed_mode = pullup;
this->debounce_delay = debounce_delay;
this->press_callback = press_callback;
}
void PushButton::update() {
bool state = is_pressed();
unsigned long t = millis();
if (last_state != state) {
last_debounce = t;
last_state = state;
}
if ((t - last_debounce) >= debounce_delay) {
/// button state has actually changed
if (state != debounced_state) {
debounced_state = state;
if (state) {
press_callback();
}
}
}
}
bool PushButton::is_pressed() {
return digitalRead(pin) ^ pressed_mode;
}
void PushButton::set_press_callback(Callback callback) {
this->press_callback = callback;
}