-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
45 lines (37 loc) · 994 Bytes
/
Button.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
#include "Arduino.h"
#include "Bounce2.h" // Button Debouncer
#include "Button.h"
Button::Button(uint8_t physicalPin) {
_physicalPin = physicalPin;
}
Button::Button(uint8_t physicalPin, uint8_t rfPin) {
_physicalPin = physicalPin;
_rfPin = rfPin;
}
void Button::init() {
if (_physicalPin) {
physicalButton = createBounceInstance(_physicalPin, INPUT_PULLUP, 50);
}
if (_rfPin) {
rfButton = createBounceInstance(_rfPin, INPUT, 0);
}
}
void Button::update() {
physicalButton->update();
if (rfButton) {
rfButton->update();
}
}
boolean Button::justPressed() {
return physicalButton->fell() || (rfButton && rfButton->rose());
}
boolean Button::isHeld() {
return physicalButton->read() == LOW || (rfButton && rfButton->read() == HIGH);
}
Bounce* Button::createBounceInstance(uint8_t pin, uint8_t mode, uint8_t debounceMs) {
Bounce *bouncer = new Bounce();
pinMode(pin, mode);
bouncer->attach(pin);
bouncer->interval(debounceMs);
return bouncer;
}