-
Notifications
You must be signed in to change notification settings - Fork 5
/
MomentaryButton.cpp
61 lines (51 loc) · 983 Bytes
/
MomentaryButton.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
#include "MomentaryButton.h"
MomentaryButton::MomentaryButton(int inputPin)
: wasClosed(false), pin(inputPin), holdReported(false)
{}
void MomentaryButton::setup()
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
void MomentaryButton::check()
{
unsigned long currentTimeMillis = millis();
boolean isClosed = (digitalRead(pin) == LOW);
if (isClosed != wasClosed)
{
if (isClosed)
{
closeTimeMillis = currentTimeMillis;
}
else
{
holdReported = false;
}
}
clicked = held = false;
boolean overHoldThreshold =
(currentTimeMillis - closeTimeMillis) >= HOLD_THRESHOLD;
if (isClosed && overHoldThreshold)
{
held = true;
}
else if (!isClosed && wasClosed && !overHoldThreshold)
{
clicked = true;
}
wasClosed = isClosed;
}
boolean MomentaryButton::wasClicked() {
return clicked;
}
boolean MomentaryButton::wasHeld() {
if (held && !holdReported)
{
holdReported = true;
return held;
}
else
{
return false;
}
}