-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
93 lines (76 loc) · 1.53 KB
/
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
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
#include "Button.h"
/**
* Standard-Constructor
*
*/
Button::Button() {
}
/**
* Standard-Destructor
*
*/
Button::~Button() {
}
void Button::init(byte pin, byte led) {
init(pin);
pinMode(led, OUTPUT);
_led = led;
}
void Button::init(byte pin) {
pinMode(pin, INPUT_PULLUP);
_button = Bounce();
_button.attach(pin);
_button.interval(10);
_previousState = HIGH;
_pin = pin;
}
boolean Button::update() {
boolean change = false;
if(_justPressed || _justReleased) {
change = true;
}
_justReleased = false;
_justPressed = false;
if (_button.update()) {
byte currentState = _button.read();
if(currentState != _previousState) {
if (_pressed && currentState == HIGH) {
_justReleased = true;
}
else if (!_pressed && currentState == LOW) {
_justPressed = true;
}
_pressed = !currentState;
change = true;
}
_previousState = currentState;
}
return change;
}
boolean Button::isPressed() {
return _pressed;
}
boolean Button::isJustPressed() {
return _justPressed;
}
boolean Button::isJustReleased() {
return _justReleased;
}
byte Button::getPin() {
return _pin;
}
void Button::turnLedOn() {
if(_led) {
digitalWrite(_led, HIGH);
_ledOn = true;
}
}
void Button::turnLedOff() {
if(_led) {
digitalWrite(_led, LOW);
_ledOn = false;
}
}
boolean Button::isLedTurnedOn() {
return _ledOn;
}