-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathButton.cpp
70 lines (61 loc) · 1.45 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
#include "Arduino.h"
#include "Button.h"
Button::Button(byte pin) {
_shortClick = 35;
_longClick = 400;
_pin = pin;
pinMode(_pin, INPUT);
_lastEventTime = 0;
_lastEventType = Unknown;
}
void Button::turnOnPullUp() {
digitalWrite(_pin, HIGH);
}
void Button::turnOffPullUp() {
digitalWrite(_pin, LOW);
}
Button::Event Button::getEvent() {
MomentaryState _state = getState();
unsigned long _currentTime = millis();
if (_lastEventType == Unknown || _lastEventType == Off) {
if (_state == Up) {
_lastEventTime = _currentTime;
_lastEventType = Off;
return None;
}
else {
_lastEventTime = _currentTime;
_lastEventType = SingleClickOn;
return None;
}
}
else if (_lastEventType == SingleClickOn) {
if (_state == Down) { // we're still clicking
return None;
}
else { // button has been released
if (_currentTime > _lastEventTime + _longClick) {
_lastEventTime = _currentTime;
_lastEventType = Off;
return LongClick;
}
else if (_currentTime > _lastEventTime + _shortClick) {
_lastEventTime = _currentTime;
_lastEventType = Off;
return SingleClick;
}
else {
_lastEventTime = _currentTime;
_lastEventType = Off;
return None;
}
}
}
}
Button::MomentaryState Button::getState() {
byte _state = digitalRead(_pin);
if (_state == LOW)
return Down;
else
return Up;
}