From cda046b95cfbae9936390c18e7728c2a1025852d Mon Sep 17 00:00:00 2001 From: kallanreed <3761006+kallanreed@users.noreply.github.com> Date: Thu, 30 Oct 2025 08:23:49 -0700 Subject: [PATCH 1/3] Clean up UI button handling --- examples/companion_radio/ui-new/UITask.cpp | 81 ++++++++++++---------- examples/companion_radio/ui-new/UITask.h | 8 ++- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 7c75a0892..0978d9028 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -4,7 +4,7 @@ #include "target.h" #ifndef AUTO_OFF_MILLIS - #define AUTO_OFF_MILLIS 15000 // 15 seconds + #define AUTO_OFF_MILLIS 15000 // 15 seconds #endif #define BOOT_SCREEN_MILLIS 3000 // 3 seconds @@ -663,52 +663,51 @@ bool UITask::isButtonPressed() const { } void UITask::loop() { - char c = 0; #if UI_HAS_JOYSTICK int ev = user_btn.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_ENTER); + handleSingleClick(KEY_ENTER); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_ENTER); // REVISIT: could be mapped to different key code + handleLongPress(KEY_ENTER); // REVISIT: could be mapped to different key code } ev = joystick_left.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_LEFT); + handleSingleClick(KEY_LEFT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_LEFT); + handleLongPress(KEY_LEFT); } ev = joystick_right.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_RIGHT); + handleSingleClick(KEY_RIGHT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_RIGHT); + handleLongPress(KEY_RIGHT); } ev = back_btn.check(); if (ev == BUTTON_EVENT_TRIPLE_CLICK) { - c = handleTripleClick(KEY_SELECT); + handleTripleClick(KEY_SELECT); } #elif defined(PIN_USER_BTN) int ev = user_btn.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_NEXT); + handleSingleClick(KEY_NEXT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_ENTER); + handleLongPress(KEY_ENTER); } else if (ev == BUTTON_EVENT_DOUBLE_CLICK) { - c = handleDoubleClick(KEY_PREV); + handleDoubleClick(KEY_PREV); } else if (ev == BUTTON_EVENT_TRIPLE_CLICK) { - c = handleTripleClick(KEY_SELECT); + handleTripleClick(KEY_SELECT); } #endif #if defined(PIN_USER_BTN_ANA) ev = analog_btn.check(); if (ev == BUTTON_EVENT_CLICK) { - c = checkDisplayOn(KEY_NEXT); + handleSingleClick(KEY_NEXT); } else if (ev == BUTTON_EVENT_LONG_PRESS) { - c = handleLongPress(KEY_ENTER); + handleLongPress(KEY_ENTER); } else if (ev == BUTTON_EVENT_DOUBLE_CLICK) { - c = handleDoubleClick(KEY_PREV); + handleDoubleClick(KEY_PREV); } else if (ev == BUTTON_EVENT_TRIPLE_CLICK) { - c = handleTripleClick(KEY_SELECT); + handleTripleClick(KEY_SELECT); } #endif #if defined(DISP_BACKLIGHT) && defined(BACKLIGHT_BTN) @@ -719,12 +718,6 @@ void UITask::loop() { } #endif - if (c != 0 && curr) { - curr->handleInput(c); - _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer - _next_refresh = 100; // trigger refresh - } - userLedHandler(); #ifdef PIN_BUZZER @@ -792,35 +785,49 @@ void UITask::loop() { char UITask::checkDisplayOn(char c) { if (_display != NULL) { if (!_display->isOn()) { - _display->turnOn(); // turn display on and consume event + _display->turnOn(); // turn display on and consume event c = 0; } - _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer + _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer _next_refresh = 0; // trigger refresh } return c; } -char UITask::handleLongPress(char c) { - if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue +void UITask::handleLongPress(char c) { + if (millis() - ui_started_at < 8000) { // long press in first 8 seconds since startup -> CLI/rescue the_mesh.enterCLIRescue(); - c = 0; // consume event + } else { + MESH_DEBUG_PRINTLN("UITask: long press triggered"); + uiHandleKey(c); } - return c; } -char UITask::handleDoubleClick(char c) { +void UITask::handleSingleClick(char c) { + MESH_DEBUG_PRINTLN("UITask: single click triggered"); + uiHandleKey(c); +} + +void UITask::handleDoubleClick(char c) { MESH_DEBUG_PRINTLN("UITask: double click triggered"); - checkDisplayOn(c); - return c; + uiHandleKey(c); } -char UITask::handleTripleClick(char c) { +void UITask::handleTripleClick(char c) { MESH_DEBUG_PRINTLN("UITask: triple click triggered"); - checkDisplayOn(c); - toggleBuzzer(); - c = 0; - return c; + if (!uiHandleKey(c)) toggleBuzzer(); +} + +bool UITask::uiHandleKey(char c) { + bool handled = false; + c = checkDisplayOn(c); + + if (c != 0 && curr) { + handled = curr->handleInput(c); + _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer + _next_refresh = 100; // trigger refresh + } + return handled; } bool UITask::getGPSState() { diff --git a/examples/companion_radio/ui-new/UITask.h b/examples/companion_radio/ui-new/UITask.h index c24d33a48..64688e3f8 100644 --- a/examples/companion_radio/ui-new/UITask.h +++ b/examples/companion_radio/ui-new/UITask.h @@ -49,9 +49,11 @@ class UITask : public AbstractUITask { // Button action handlers char checkDisplayOn(char c); - char handleLongPress(char c); - char handleDoubleClick(char c); - char handleTripleClick(char c); + void handleLongPress(char c); + void handleSingleClick(char c); + void handleDoubleClick(char c); + void handleTripleClick(char c); + bool uiHandleKey(char c); void setCurrScreen(UIScreen* c); From 4cc2e74237fadf0940d88b06eb5b878eb4cab722 Mon Sep 17 00:00:00 2001 From: kallanreed <3761006+kallanreed@users.noreply.github.com> Date: Thu, 30 Oct 2025 09:12:59 -0700 Subject: [PATCH 2/3] Simplify checkDispOn --- examples/companion_radio/ui-new/UITask.cpp | 16 ++++++++++------ examples/companion_radio/ui-new/UITask.h | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 0978d9028..d2e0636d2 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -782,16 +782,20 @@ void UITask::loop() { #endif } -char UITask::checkDisplayOn(char c) { +bool UITask::checkDisplayOn() { + // ensures that the display is on and the timer is reset + // returns false if the display was previously off + bool display_on = false; if (_display != NULL) { if (!_display->isOn()) { - _display->turnOn(); // turn display on and consume event - c = 0; + _display->turnOn(); // turn display on + } else { + display_on = true; } _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer _next_refresh = 0; // trigger refresh } - return c; + return display_on; } void UITask::handleLongPress(char c) { @@ -820,9 +824,9 @@ void UITask::handleTripleClick(char c) { bool UITask::uiHandleKey(char c) { bool handled = false; - c = checkDisplayOn(c); + bool display_on = checkDisplayOn(); - if (c != 0 && curr) { + if (c != 0 && display_on && curr) { handled = curr->handleInput(c); _auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer _next_refresh = 100; // trigger refresh diff --git a/examples/companion_radio/ui-new/UITask.h b/examples/companion_radio/ui-new/UITask.h index 64688e3f8..e6287de26 100644 --- a/examples/companion_radio/ui-new/UITask.h +++ b/examples/companion_radio/ui-new/UITask.h @@ -48,7 +48,7 @@ class UITask : public AbstractUITask { void userLedHandler(); // Button action handlers - char checkDisplayOn(char c); + bool checkDisplayOn(); void handleLongPress(char c); void handleSingleClick(char c); void handleDoubleClick(char c); From b9d17817923383492c7b49a956626bb80142c0b9 Mon Sep 17 00:00:00 2001 From: kallanreed <3761006+kallanreed@users.noreply.github.com> Date: Thu, 30 Oct 2025 10:43:35 -0700 Subject: [PATCH 3/3] undo unnecessary WS change --- examples/companion_radio/ui-new/UITask.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index d2e0636d2..5e9e60875 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -4,7 +4,7 @@ #include "target.h" #ifndef AUTO_OFF_MILLIS - #define AUTO_OFF_MILLIS 15000 // 15 seconds + #define AUTO_OFF_MILLIS 15000 // 15 seconds #endif #define BOOT_SCREEN_MILLIS 3000 // 3 seconds