Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 62 additions & 73 deletions ESP32-DIV/ESP32-DIV.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ TFT_eSPI tft = TFT_eSPI();

PCF8574 pcf(PCF8574_I2C_ADDR);

// BUGFIX: Safe button release wait with timeout
// Prevents infinite hang if a button gets stuck (e.g., hardware short, debris)
#define BUTTON_RELEASE_TIMEOUT_MS 5000

void waitForButtonRelease(uint8_t btn) {
unsigned long start = millis();
while (isButtonPressed(btn)) {
if (millis() - start > BUTTON_RELEASE_TIMEOUT_MS) {
Serial.printf("[WARN] Button %d stuck - forcing release after %dms\n", btn, BUTTON_RELEASE_TIMEOUT_MS);
break;
}
delay(10); // Yield to FreeRTOS / watchdog
}
}

void waitForButtonRelease2(uint8_t btn1, uint8_t btn2) {
unsigned long start = millis();
while (isButtonPressed(btn1) || isButtonPressed(btn2)) {
if (millis() - start > BUTTON_RELEASE_TIMEOUT_MS) {
Serial.printf("[WARN] Buttons stuck - forcing release after %dms\n", BUTTON_RELEASE_TIMEOUT_MS);
break;
}
delay(10);
}
}

void setBrightness(uint8_t value) {
ledcWrite(PWM_CHANNEL, value);
}
Expand Down Expand Up @@ -944,8 +970,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -978,8 +1003,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1012,8 +1036,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1046,8 +1069,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1080,8 +1102,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1114,8 +1135,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1147,8 +1167,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1208,8 +1227,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1240,8 +1258,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1272,8 +1289,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1304,8 +1320,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1336,8 +1351,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1368,8 +1382,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1400,8 +1413,7 @@ void handleWiFiSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1473,8 +1485,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1507,8 +1518,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1543,8 +1553,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1579,8 +1588,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1615,8 +1623,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1682,8 +1689,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1714,8 +1720,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1747,8 +1752,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1780,8 +1784,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1813,8 +1816,7 @@ void handleBluetoothSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1889,8 +1891,7 @@ void handleNRFSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1923,8 +1924,7 @@ void handleNRFSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -1984,8 +1984,7 @@ void handleNRFSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2016,8 +2015,7 @@ void handleNRFSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2090,8 +2088,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2125,8 +2122,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2160,8 +2156,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2223,8 +2218,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2256,8 +2250,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2289,8 +2282,7 @@ void handleSubGHzSubmenuButtons() {
feature_exit_requested = false;
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
break;
}
}
Expand Down Expand Up @@ -2328,8 +2320,7 @@ static void runToolsFeatureExitCleanup() {
resetTouchNavHeldState();
displaySubmenu();
delay(200);
while (isButtonPressed(BTN_SELECT)) {
}
waitForButtonRelease(BTN_SELECT);
}

static void runToolsFeature(int idx, void (*setupFn)(), void (*loopFn)()) {
Expand Down Expand Up @@ -2447,9 +2438,7 @@ void handleToolsSubmenuButtons() {

static void otherDismissPlaceholder() {
delay(25);
while (isButtonPressed(BTN_SELECT) || isButtonPressed(BTN_LEFT)) {
delay(5);
}
waitForButtonRelease2(BTN_SELECT, BTN_LEFT);
while (!isButtonPressed(BTN_SELECT) && !isButtonPressed(BTN_LEFT)) {
int x = 0, y = 0;
if (!readTouchXYDismiss(x, y) && !readTouchXY(x, y)) {
Expand Down
Loading