-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinterface.cpp
More file actions
160 lines (140 loc) · 5.41 KB
/
interface.cpp
File metadata and controls
160 lines (140 loc) · 5.41 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "core/powerSave.h"
#include "core/utils.h"
#include <Button.h>
#include <globals.h>
#include <interface.h>
#include "core/display.h"
volatile bool nxtPress = false;
volatile bool prvPress = false;
volatile bool ecPress = false;
volatile bool slPress = false;
static void onButtonSingleClickCb1(void *button_handle, void *usr_data) { nxtPress = true; }
static void onButtonDoubleClickCb1(void *button_handle, void *usr_data) { slPress = true; }
static void onButtonHoldCb1(void *button_handle, void *usr_data) { slPress = true; }
static void onButtonSingleClickCb2(void *button_handle, void *usr_data) { prvPress = true; }
static void onButtonDoubleClickCb2(void *button_handle, void *usr_data) { ecPress = true; }
static void onButtonHoldCb2(void *button_handle, void *usr_data) { ecPress = true; }
Button *btn1;
Button *btn2;
/***************************************************************************************
** Function name: _setup_gpio()
** Description: initial setup for the device
***************************************************************************************/
void _setup_gpio() {
// setup buttons
pinMode(DW_BTN, INPUT_PULLUP);
pinMode(UP_BTN, INPUT_PULLUP);
button_config_t bt1 = {
.type = BUTTON_TYPE_GPIO,
.long_press_time = 600,
.short_press_time = 120,
.gpio_button_config = {
.gpio_num = DW_BTN,
.active_level = 0,
},
};
button_config_t bt2 = {
.type = BUTTON_TYPE_GPIO,
.long_press_time = 600,
.short_press_time = 120,
.gpio_button_config = {
.gpio_num = UP_BTN,
.active_level = 0,
},
};
btn1 = new Button(bt1);
btn1->attachSingleClickEventCb(&onButtonSingleClickCb1, NULL);
btn1->attachDoubleClickEventCb(&onButtonDoubleClickCb1, NULL);
btn1->attachLongPressStartEventCb(&onButtonHoldCb1, NULL);
btn2 = new Button(bt2);
btn2->attachSingleClickEventCb(&onButtonSingleClickCb2, NULL);
btn2->attachDoubleClickEventCb(&onButtonDoubleClickCb2, NULL);
btn2->attachLongPressStartEventCb(&onButtonHoldCb2, NULL);
// setup POWER pin required by the vendor
pinMode(ADC_EN, OUTPUT);
digitalWrite(ADC_EN, HIGH);
// Start with default IR, RF and RFID Configs, replace old
bruceConfigPins.rfModule = CC1101_SPI_MODULE;
bruceConfigPins.rfidModule = PN532_I2C_MODULE;
bruceConfigPins.irRx = RXLED;
bruceConfigPins.irTx = TXLED;
Serial.begin(115200);
}
/*********************************************************************
** Function: setBrightness
** set brightness value
**********************************************************************/
void _setBrightness(uint8_t brightval) {
if (brightval == 0) {
analogWrite(TFT_BL, brightval);
} else {
int bl = MINBRIGHT + round(((255 - MINBRIGHT) * brightval / 100));
analogWrite(TFT_BL, bl);
}
}
/*********************************************************************
** Function: InputHandler
** Handles the variables PrevPress, NextPress, SelPress, AnyKeyPress and EscPress
**********************************************************************/
void InputHandler(void) {
static unsigned long tm = 0;
static bool btn_pressed = false;
if (nxtPress || prvPress || ecPress || slPress) btn_pressed = true;
if (millis() - tm > 200 || LongPress) {
if (btn_pressed) {
btn_pressed = false;
tm = millis();
if (!wakeUpScreen()) AnyKeyPress = true;
else return;
SelPress = slPress;
EscPress = ecPress;
NextPress = nxtPress;
PrevPress = prvPress;
nxtPress = false;
prvPress = false;
ecPress = false;
slPress = false;
}
}
// New logic for Shutdown
static unsigned long upBtnHoldStart = 0;
static bool upBtnHeldState = false;
static int lastShutdownCountdown = 0;
// Check UP_BTN (GPIO 35)
if (digitalRead(UP_BTN) == LOW) { // Pressed
if (menuOptionType == MENU_TYPE_MAIN) {
if (!upBtnHeldState) {
upBtnHoldStart = millis();
upBtnHeldState = true;
} else {
unsigned long elapsed = millis() - upBtnHoldStart;
int remaining = 5 - (elapsed / 1000);
if (remaining <= 0) {
powerOff();
} else if (remaining != lastShutdownCountdown) {
// Draw countdown
tft.setTextColor(TFT_RED, bruceConfig.bgColor);
String msg = "OFF: " + String(remaining);
tft.drawCentreString(msg.c_str(), tftWidth / 2, tftHeight / 2, 4);
lastShutdownCountdown = remaining;
}
}
}
} else {
if (upBtnHeldState) {
upBtnHeldState = false;
// Clear countdown
if (lastShutdownCountdown > 0) {
tft.fillRect(0, tftHeight / 2, tftWidth, 40, bruceConfig.bgColor);
lastShutdownCountdown = 0;
}
}
}
}
void powerOff() {
tft.fillScreen(bruceConfig.bgColor);
digitalWrite(TFT_BL, LOW);
tft.writecommand(0x10);
esp_sleep_enable_ext0_wakeup((gpio_num_t)DW_BTN, BTN_ACT);
esp_deep_sleep_start();
}