-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
30 lines (27 loc) · 875 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
#include "Button.h"
void Button::setPress(Action* onPress) { _onPress = onPress; }
void Button::setRelease(Action* onRelease) { _onRelease = onRelease; }
int Button::getState() { return (digitalRead(_pin) == HIGH? 1 : 0); }
void Button::update() {
int state = this->getState();
if (_state != state) {
Serial << this->getName() << ": " << state << "\n";
if (++_count>=SAMPLES) {
if (_state == 0 && state == 1) {
Serial << this->getName() << "->onPress" << "\n";
(*_onPress)(1);
}
if (_state == 1 && state == 0) {
Serial << this->getName() << "->onRelease" << "\n";
(*_onRelease)(1);
}
_state = state;
_count = 0;
}
} else {
_count = 0;
if (_state == 1) {
}
}
return;
}