-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCO2_Gadget_Buttons.h
More file actions
146 lines (126 loc) · 4.75 KB
/
Copy pathCO2_Gadget_Buttons.h
File metadata and controls
146 lines (126 loc) · 4.75 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef CO2_Gadget_Buttons_h
#define CO2_Gadget_Buttons_h
#include "Button2.h"
#undef LONGCLICK_TIME_MS
#define LONGCLICK_TIME_MS 300 // https://github.com/LennartHennigs/Button2/issues/10
Button2 btnUp(BTN_UP); // Initialize the up button
Button2 btnDwn(BTN_DWN); // Initialize the down button
void IRAM_ATTR buttonUpISR() {
if (actualDisplayBrightness == 0) { // Turn on the display only if it's OFF
shouldWakeUpDisplay = true;
lastTimeButtonPressed = millis();
}
if (!menuInitialized) { // If the menu is not initialized yet, initialize it now (disable Improv WiFi functionality)
mustInitMenu = true;
publishMQTTLogData("-->[BUTT] mustInitMenu: " + String(mustInitMenu));
}
}
void IRAM_ATTR buttonDownISR() {
if (actualDisplayBrightness == 0) { // Turn on the display only if it's OFF
shouldWakeUpDisplay = true;
lastTimeButtonPressed = millis();
}
if (!menuInitialized) { // If the menu is not initialized yet, initialize it now (disable Improv WiFi functionality)
mustInitMenu = true;
publishMQTTLogData("-->[BUTT] mustInitMenu: " + String(mustInitMenu));
}
}
// void doubleClick(Button2& btn) {
// Serial.println("-->[BUTT] Test double click...");
// displayNotification("Test functionality", "double click", notifyInfo);
// }
void initButtons() {
// Set Interrupt Service Routine to turn on the display on button UP or DOWN press (nothing to do with the button functionality for the menu itself)
#if BTN_UP != -1
attachInterrupt(BTN_UP, buttonUpISR, RISING);
#endif
#if BTN_DWN != -1
// attachInterrupt(BTN_DWN, buttonUpISR, RISING); // Conflicts with Improv because of GPIO 0
#endif
btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
if (displayReverse) {
reverseButtons(true);
}
// btnDwn.setDoubleClickHandler(doubleClick);
}
void reverseButtons(bool reversed) {
if (reversed) {
// Interrupt Service Routine to turn on the display on button UP press
if ((BTN_UP >= 0) && (BTN_DWN >= 0)) {
attachInterrupt(BTN_UP, buttonUpISR, RISING);
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});
btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
}
} else {
if ((BTN_UP >= 0) && (BTN_DWN >= 0)) {
attachInterrupt(BTN_DWN, buttonUpISR, RISING);
btnUp.setLongClickTime(LONGCLICK_TIME_MS);
btnUp.setLongClickHandler([](Button2 &b) { nav.doNav(enterCmd); });
btnUp.setClickHandler([](Button2 &b) {
// Up
nav.doNav(downCmd);
});
btnDwn.setLongClickTime(LONGCLICK_TIME_MS);
btnDwn.setLongClickHandler([](Button2 &b) { nav.doNav(escCmd); });
btnDwn.setClickHandler([](Button2 &b) {
// Down
nav.doNav(upCmd);
});
}
}
}
void buttonsLoop() {
// Check for button presses
if (BTN_UP >= 0) {
btnUp.loop();
}
if (BTN_DWN >= 0) {
btnDwn.loop();
}
}
bool isButtonPressedOnWakeUp() {
unsigned long buttonPressStartTime = millis();
bool buttonState = digitalRead(BTN_DWN); // Initial button state
// If the button is not pressed, return false immediately
if (buttonState == HIGH) {
return false;
}
// Wait for the button to be released or for LONGCLICK_TIME_MS
while (buttonState == LOW && (millis() - buttonPressStartTime < LONGCLICK_TIME_MS)) {
delay(10); // Debouncing delay
buttonState = digitalRead(BTN_DWN);
// Check if the button is released
if (buttonState == HIGH) {
// Button was released before LONGCLICK_TIME_MS
return false;
}
}
// Check if the button is still pressed after waiting
if (buttonState == LOW && (millis() - buttonPressStartTime >= LONGCLICK_TIME_MS)) {
// Button was pressed and held for LONGCLICK_TIME_MS
return true;
}
return false;
}
#endif // CO2_Gadget_Buttons_h