-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
88 lines (81 loc) · 1.97 KB
/
Button.cpp
File metadata and controls
88 lines (81 loc) · 1.97 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
78
79
80
81
82
83
84
85
86
87
88
//
//
//
#include "Button.h"
void Button::Begin(ButtonName n, int p) {
name = n;
pin = p;
}
void IRAM_ATTR Button::SetInterrupted() {
interrupted = 1;
}
void Button::Loop()
{
if (shortPress == 1)
{
if (shortPressNotified == 0)
{
ObserverManager::notify(&ButtonObserver::ShortPress, name);
shortPressNotified == 1;
shortPress = 0;
}
}
if (longPress == 1 && longPressInProgess == 1)
{
if (longPressNotified == 0)
{
ObserverManager::notify(&ButtonObserver::LongPress, name);
longPressNotified = 1;
}
}
if (longPress == 0 && longPressInProgess == 0)
{
if (longPressReleasedNotified == 0)
{
ObserverManager::notify(&ButtonObserver::LongPressReleased);
longPressReleasedNotified = 1;
longPress = 0;
}
}
if (interrupted == 1)
{
if (millis() - previousMillis > Button::debounceInterval)
{
previousMillis = millis();
current = digitalRead(pin);
// CASE: button press (low to high)
if (current == HIGH && previous == LOW)
{
longPress = longPressMillis = 0;
shortPress = 1;
}
// CASE: button release (high to low)
else if (current == LOW && previous == HIGH)
{
shortPress = shortPressNotified = 0;
longPress = longPressNotified = longPressInProgess = interrupted = 0;
}
// CASE: long pressed still held
else if (current == HIGH && longPressInProgess == 1)
{
return; // do nothing
}
// CASE: long press
else if (current == HIGH && longPress == 0)
{
if (longPressMillis == 0) longPressMillis = millis();
//Serial.println("lpc:" + String(millis() - longPressMillis));
if (millis() - longPressMillis >= Button::longPressMillisMax)
{
longPress = 1;
longPressInProgess = 1;
longPressReleasedNotified = 0;
longPressMillis = 0;
//Serial.println("LONG PRESS " + String((int)name));
}
}
// remember button state for next loop
previous = current;
}
}
}