-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiButton.cpp
More file actions
77 lines (74 loc) · 2.22 KB
/
Copy pathMultiButton.cpp
File metadata and controls
77 lines (74 loc) · 2.22 KB
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
//=================================================
// MULTI-CLICK: One Button, Multiple Events
// from: http://forum.arduino.cc/index.php?topic=14479.0
// original: http://jmsarduino.blogspot.com/2009/10/4-way-button-click-double-click-hold.html
#include "Arduino.h"
#include "MultiButton.h"
void MultiButton::setup(byte buttonPin) {
this->gpioPin = buttonPin;
pinMode(buttonPin, INPUT_PULLUP);
}
byte MultiButton::check() {
byte event = 0;
buttonVal = digitalRead(gpioPin);
// Button pressed down
if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
{
downTime = millis();
ignoreUp = false;
waitForUp = false;
singleOK = true;
holdEventPast = false;
longHoldEventPast = false;
if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
else DConUp = false;
DCwaiting = false;
}
// Button released
else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
{
if (not ignoreUp)
{
upTime = millis();
if (DConUp == false) DCwaiting = true;
else
{
event = BTN_DOUBLE_CLICK;
DConUp = false;
DCwaiting = false;
singleOK = false;
}
}
}
// Test for normal click event: DCgap expired
if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
{
event = BTN_SINGLE_CLICK;
DCwaiting = false;
}
// Test for hold
if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
// Trigger "normal" hold
if (not holdEventPast)
{
event = BTN_LONG_PRESS;
waitForUp = true;
ignoreUp = true;
DConUp = false;
DCwaiting = false;
//downTime = millis();
holdEventPast = true;
}
/* Trigger "long" hold
if ((millis() - downTime) >= longHoldTime)
{
if (not longHoldEventPast)
{
event = 4;
longHoldEventPast = true;
}
}*/
}
buttonLast = buttonVal;
return event;
}