From 67cfc305884644d084453ff1725a967dc36308ec Mon Sep 17 00:00:00 2001 From: mikecarper Date: Sun, 19 Jul 2026 22:18:26 -0700 Subject: [PATCH] Fix companion display behavior on USB hosts --- examples/companion_radio/AbstractUITask.h | 18 ++++++++++++++++++ examples/companion_radio/main.cpp | 4 +++- examples/companion_radio/ui-new/UITask.cpp | 4 ++-- examples/companion_radio/ui-orig/UITask.cpp | 4 ++-- examples/companion_radio/ui-tiny/UITask.cpp | 4 ++-- src/MeshCore.h | 4 ++++ src/helpers/ESP32Board.h | 19 +++++++++++++++++++ src/helpers/NRF52Board.cpp | 8 ++++++++ src/helpers/NRF52Board.h | 1 + 9 files changed, 59 insertions(+), 7 deletions(-) diff --git a/examples/companion_radio/AbstractUITask.h b/examples/companion_radio/AbstractUITask.h index 0eee45aef..2da5713f1 100644 --- a/examples/companion_radio/AbstractUITask.h +++ b/examples/companion_radio/AbstractUITask.h @@ -32,6 +32,24 @@ protected: _connected = false; } + bool isDisplayAutoOffDue(unsigned long configured_deadline, + unsigned long configured_timeout_millis) const { + unsigned long deadline = configured_deadline; + if (_board->isUsbHostConnected()) { + // The configured deadline already includes the first timeout period. + // Add four more periods for a total of 5x while attached to a computer. + deadline += configured_timeout_millis * 4UL; + } + return static_cast(millis() - deadline) > 0; + } + + bool shouldWakeDisplayForMessage() const { + // Keep the existing BLE-only behavior, where the connected companion is + // expected to surface the notification. A computer attached over USB is + // the exception: show the message on the device even if BLE is connected. + return !hasConnection() || _board->isUsbHostConnected(); + } + public: void setHasConnection(bool connected) { _connected = connected; } bool hasConnection() const { return _connected; } diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index c85e37f2f..ac95ba508 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -529,7 +529,9 @@ void loop() { serial_interface.loop(); #endif - if (!the_mesh.hasPendingWork()) { + // USB power alone (for example, a wall charger) must not disable power + // saving. Stay awake only while a computer has an active USB data session. + if (!board.isUsbHostConnected() && !the_mesh.hasPendingWork()) { #if defined(NRF52_PLATFORM) board.sleep(0); // nrf ignores seconds param, sleeps whenever possible #elif defined(ESP32_PLATFORM) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index a26dc19a2..ca14b7fe0 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -640,7 +640,7 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i setCurrScreen(msg_preview); if (_display != NULL) { - if (!_display->isOn() && !hasConnection()) { + if (!_display->isOn() && shouldWakeDisplayForMessage()) { _display->turnOn(); } if (_display->isOn()) { @@ -827,7 +827,7 @@ void UITask::loop() { _auto_off = millis() + AUTO_OFF_MILLIS; } #endif - if (millis() > _auto_off) { + if (isDisplayAutoOffDue(_auto_off, AUTO_OFF_MILLIS)) { _display->turnOff(); } #endif diff --git a/examples/companion_radio/ui-orig/UITask.cpp b/examples/companion_radio/ui-orig/UITask.cpp index 7afcbf663..783497295 100644 --- a/examples/companion_radio/ui-orig/UITask.cpp +++ b/examples/companion_radio/ui-orig/UITask.cpp @@ -138,7 +138,7 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i StrHelper::strncpy(_msg, text, sizeof(_msg)); if (_display != NULL) { - if (!_display->isOn() && !hasConnection()) { + if (!_display->isOn() && shouldWakeDisplayForMessage()) { _display->turnOn(); } if (_display->isOn()) { @@ -351,7 +351,7 @@ void UITask::loop() { _auto_off = millis() + AUTO_OFF_MILLIS; } #endif - if (millis() > _auto_off) { + if (isDisplayAutoOffDue(_auto_off, AUTO_OFF_MILLIS)) { _display->turnOff(); } } diff --git a/examples/companion_radio/ui-tiny/UITask.cpp b/examples/companion_radio/ui-tiny/UITask.cpp index 452c02d41..bc98a7616 100644 --- a/examples/companion_radio/ui-tiny/UITask.cpp +++ b/examples/companion_radio/ui-tiny/UITask.cpp @@ -509,7 +509,7 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i _msgcount = msgcount; if (_display != NULL) { - if (!_display->isOn() && !hasConnection()) { + if (!_display->isOn() && shouldWakeDisplayForMessage()) { _display->turnOn(); } if (_display->isOn()) { @@ -712,7 +712,7 @@ void UITask::loop() { _auto_off = millis() + AUTO_OFF_MILLIS; } #endif - if (millis() > _auto_off) { + if (isDisplayAutoOffDue(_auto_off, AUTO_OFF_MILLIS)) { _display->turnOff(); } #endif diff --git a/src/MeshCore.h b/src/MeshCore.h index bf3c1fde1..6b93ace98 100644 --- a/src/MeshCore.h +++ b/src/MeshCore.h @@ -101,6 +101,10 @@ public: // Power management interface (boards with power management override these) virtual bool isExternalPowered() { return false; } virtual bool isUsbDataConnected() { return false; } + // True when the device is enumerated by a USB host, even if its serial port + // is not open. Defaults to the stricter data-connection signal on boards + // that cannot distinguish a computer from USB power. + virtual bool isUsbHostConnected() { return isUsbDataConnected(); } virtual uint16_t getBootVoltage() { return 0; } virtual uint32_t getResetReason() const { return 0; } virtual const char* getResetReasonString(uint32_t reason) { return "Not available"; } diff --git a/src/helpers/ESP32Board.h b/src/helpers/ESP32Board.h index 09e45417c..670410a99 100644 --- a/src/helpers/ESP32Board.h +++ b/src/helpers/ESP32Board.h @@ -16,6 +16,11 @@ #include "esp_system.h" #include +#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT && \ + (!defined(ARDUINO_USB_MODE) || !ARDUINO_USB_MODE) +#include +#endif + #if !defined(LIGHTWEIGHT_WIFI_OTA) class AsyncWebServer; #endif @@ -192,6 +197,20 @@ public: #endif } + bool isUsbHostConnected() override { +#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT +#if defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE + return Serial.isPlugged(); +#elif defined(CONFIG_TINYUSB_ENABLED) && CONFIG_TINYUSB_ENABLED + return (bool)USB; +#else + return (bool)Serial; +#endif +#else + return false; +#endif + } + void setInhibitSleep(bool inhibit) { inhibit_sleep = inhibit; } diff --git a/src/helpers/NRF52Board.cpp b/src/helpers/NRF52Board.cpp index 6c4fc217f..e7a99bf10 100644 --- a/src/helpers/NRF52Board.cpp +++ b/src/helpers/NRF52Board.cpp @@ -282,6 +282,14 @@ bool NRF52Board::isUsbDataConnected() { #endif } +bool NRF52Board::isUsbHostConnected() { +#if defined(USE_TINYUSB) + return tud_mounted(); +#else + return false; +#endif +} + void NRF52Board::sleep(uint32_t secs) { // Clear FPU interrupt flags to avoid insomnia // see errata 87 for details https://docs.nordicsemi.com/bundle/errata_nRF52840_Rev3/page/ERR/nRF52840/Rev3/latest/anomaly_840_87.html diff --git a/src/helpers/NRF52Board.h b/src/helpers/NRF52Board.h index 671596cab..ec653e4b7 100644 --- a/src/helpers/NRF52Board.h +++ b/src/helpers/NRF52Board.h @@ -58,6 +58,7 @@ public: virtual void sleep(uint32_t secs) override; bool isExternalPowered() override; bool isUsbDataConnected() override; + bool isUsbHostConnected() override; #ifdef NRF52_POWER_MANAGEMENT uint16_t getBootVoltage() override { return boot_voltage_mv; }