Fix companion display behavior on USB hosts

This commit is contained in:
mikecarper committed 2026-07-19 22:18:26 -07:00
1 parent ccce0c8d75
commit 67cfc30588
9 files changed
+59 -7

No files matched your search

+18
View File
@@ -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<int32_t>(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; }
+3 -1
View File
@@ -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)
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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();
}
}
+2 -2
View File
@@ -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
+4
View File
@@ -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"; }
+19
View File
@@ -16,6 +16,11 @@
#include "esp_system.h"
#include <driver/rtc_io.h>
#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT && \
(!defined(ARDUINO_USB_MODE) || !ARDUINO_USB_MODE)
#include <USB.h>
#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;
}
+8
View File
@@ -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
+1
View File
@@ -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; }