From ce6676d211f21b6710bab266e4fd2fc3252008da Mon Sep 17 00:00:00 2001 From: jpirnay Date: Wed, 18 Mar 2026 12:56:35 +0100 Subject: [PATCH] Add immediate response for usb cable plug in / removal --- lib/hal/HalGPIO.cpp | 9 ++++++++- lib/hal/HalGPIO.h | 6 ++++++ src/main.cpp | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/hal/HalGPIO.cpp b/lib/hal/HalGPIO.cpp index 64a251de..ec0176a2 100644 --- a/lib/hal/HalGPIO.cpp +++ b/lib/hal/HalGPIO.cpp @@ -7,7 +7,14 @@ void HalGPIO::begin() { pinMode(UART0_RXD, INPUT); } -void HalGPIO::update() { inputMgr.update(); } +void HalGPIO::update() { + inputMgr.update(); + const bool connected = isUsbConnected(); + usbStateChanged = (connected != lastUsbConnected); + lastUsbConnected = connected; +} + +bool HalGPIO::wasUsbStateChanged() const { return usbStateChanged; } bool HalGPIO::isPressed(uint8_t buttonIndex) const { return inputMgr.isPressed(buttonIndex); } diff --git a/lib/hal/HalGPIO.h b/lib/hal/HalGPIO.h index 09e1fa52..a283ed60 100644 --- a/lib/hal/HalGPIO.h +++ b/lib/hal/HalGPIO.h @@ -23,6 +23,9 @@ class HalGPIO { InputManager inputMgr; #endif + bool lastUsbConnected = false; + bool usbStateChanged = false; + public: HalGPIO() = default; @@ -41,6 +44,9 @@ class HalGPIO { // Check if USB is connected bool isUsbConnected() const; + // Returns true once per edge (plug or unplug) since the last update() + bool wasUsbStateChanged() const; + enum class WakeupReason { PowerButton, AfterFlash, AfterUSBPower, Other }; WakeupReason getWakeupReason() const; diff --git a/src/main.cpp b/src/main.cpp index 7bd21f37..a9710f7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -347,6 +347,11 @@ void loop() { powerManager.setPowerSaving(false); // Restore normal CPU frequency on user activity } + // Refresh the battery icon immediately when USB is plugged or unplugged + if (gpio.wasUsbStateChanged()) { + activityManager.requestUpdate(); + } + static bool screenshotButtonsReleased = true; if (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.isPressed(HalGPIO::BTN_DOWN)) { if (screenshotButtonsReleased) {