forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2_Gadget_Buttons.h
108 lines (94 loc) · 3.54 KB
/
CO2_Gadget_Buttons.h
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
#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
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 {
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
btnUp.loop();
btnDwn.loop();
}
#endif // CO2_Gadget_Buttons_h