From c1ab0de85fa7baaaf81fa4aa3a9b9383607b1662 Mon Sep 17 00:00:00 2001 From: Kevin Le Date: Sat, 17 Oct 2026 23:01:09 +0700 Subject: [PATCH] Added isReadBusy() for Serial interfaces --- src/helpers/ArduinoSerialInterface.cpp | 5 +++++ src/helpers/ArduinoSerialInterface.h | 3 +++ src/helpers/BaseSerialInterface.h | 3 +++ src/helpers/MultiSerialInterface.h | 17 +++++++++++++++++ src/helpers/esp32/SerialBLEInterface.cpp | 5 +++++ src/helpers/esp32/SerialBLEInterface.h | 3 +++ src/helpers/nrf52/SerialBLEInterface.cpp | 5 +++++ src/helpers/nrf52/SerialBLEInterface.h | 1 + src/helpers/wifi/SerialWifiInterface.cpp | 5 +++++ src/helpers/wifi/SerialWifiInterface.h | 4 ++++ 10 files changed, 51 insertions(+) diff --git a/src/helpers/ArduinoSerialInterface.cpp b/src/helpers/ArduinoSerialInterface.cpp index a01fa5866..afd7525d4 100644 --- a/src/helpers/ArduinoSerialInterface.cpp +++ b/src/helpers/ArduinoSerialInterface.cpp @@ -17,6 +17,11 @@ bool ArduinoSerialInterface::isConnected() const { return true; // no way of knowing, so assume yes } +// PowerSaving variant +bool ArduinoSerialInterface::isReadBusy() const { + return false; +} + bool ArduinoSerialInterface::isWriteBusy() const { return false; } diff --git a/src/helpers/ArduinoSerialInterface.h b/src/helpers/ArduinoSerialInterface.h index c4086353a..78155672d 100644 --- a/src/helpers/ArduinoSerialInterface.h +++ b/src/helpers/ArduinoSerialInterface.h @@ -28,6 +28,9 @@ public: bool isConnected() const override; + // PowerSaving variant + bool isReadBusy() const override; + bool isWriteBusy() const override; size_t writeFrame(const uint8_t src[], size_t len) override; size_t checkRecvFrame(uint8_t dest[]) override; diff --git a/src/helpers/BaseSerialInterface.h b/src/helpers/BaseSerialInterface.h index 23933fcb4..5431bac15 100644 --- a/src/helpers/BaseSerialInterface.h +++ b/src/helpers/BaseSerialInterface.h @@ -16,6 +16,9 @@ public: virtual bool isConnected() const = 0; virtual void loop() {}; + // PowerSaving variant + virtual bool isReadBusy() const = 0; + virtual bool isWriteBusy() const = 0; virtual size_t writeFrame(const uint8_t src[], size_t len) = 0; virtual size_t checkRecvFrame(uint8_t dest[]) = 0; diff --git a/src/helpers/MultiSerialInterface.h b/src/helpers/MultiSerialInterface.h index f7742b24c..7bb827c18 100644 --- a/src/helpers/MultiSerialInterface.h +++ b/src/helpers/MultiSerialInterface.h @@ -140,6 +140,23 @@ public: } } + bool isReadBusy() const override { + // not busy when disabled + if (!_enabled) { + return false; + } + + // check if any interface is busy + for (auto iface : _interfaces) { + if (iface.instance && iface.instance->isEnabled() && iface.instance->isReadBusy()) { + return true; + } + } + + // nothing busy + return false; + } + bool isWriteBusy() const override { // not busy when disabled if(!_enabled){ diff --git a/src/helpers/esp32/SerialBLEInterface.cpp b/src/helpers/esp32/SerialBLEInterface.cpp index 6371cf335..176fd988f 100644 --- a/src/helpers/esp32/SerialBLEInterface.cpp +++ b/src/helpers/esp32/SerialBLEInterface.cpp @@ -188,6 +188,11 @@ size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) { #define BLE_WRITE_MIN_INTERVAL 60 +// PowerSaving variant +bool SerialBLEInterface::isReadBusy() const { + return uxQueueMessagesWaiting(recv_queue) > 0; +} + bool SerialBLEInterface::isWriteBusy() const { return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write? } diff --git a/src/helpers/esp32/SerialBLEInterface.h b/src/helpers/esp32/SerialBLEInterface.h index 9fb6adba9..063d382c6 100644 --- a/src/helpers/esp32/SerialBLEInterface.h +++ b/src/helpers/esp32/SerialBLEInterface.h @@ -82,6 +82,9 @@ public: bool isConnected() const override; + // PowerSaving variant + bool isReadBusy() const override; + bool isWriteBusy() const override; size_t writeFrame(const uint8_t src[], size_t len) override; size_t checkRecvFrame(uint8_t dest[]) override; diff --git a/src/helpers/nrf52/SerialBLEInterface.cpp b/src/helpers/nrf52/SerialBLEInterface.cpp index 6ed16bc86..46d4a6772 100644 --- a/src/helpers/nrf52/SerialBLEInterface.cpp +++ b/src/helpers/nrf52/SerialBLEInterface.cpp @@ -405,6 +405,11 @@ bool SerialBLEInterface::isConnected() const { return _isDeviceConnected && Bluefruit.connected() > 0; } +// PowerSaving variant +bool SerialBLEInterface::isReadBusy() const { + return (recv_queue_len > 0); +} + bool SerialBLEInterface::isWriteBusy() const { return send_queue_len >= (FRAME_QUEUE_SIZE * 2 / 3); } diff --git a/src/helpers/nrf52/SerialBLEInterface.h b/src/helpers/nrf52/SerialBLEInterface.h index de1030548..d3cc50551 100644 --- a/src/helpers/nrf52/SerialBLEInterface.h +++ b/src/helpers/nrf52/SerialBLEInterface.h @@ -66,6 +66,7 @@ public: void disable() override; bool isEnabled() const override { return _isEnabled; } bool isConnected() const override; + bool isReadBusy() const override; bool isWriteBusy() const override; size_t writeFrame(const uint8_t src[], size_t len) override; size_t checkRecvFrame(uint8_t dest[]) override; diff --git a/src/helpers/wifi/SerialWifiInterface.cpp b/src/helpers/wifi/SerialWifiInterface.cpp index 462e3ecc3..61afe77ff 100644 --- a/src/helpers/wifi/SerialWifiInterface.cpp +++ b/src/helpers/wifi/SerialWifiInterface.cpp @@ -39,6 +39,11 @@ size_t SerialWifiInterface::writeFrame(const uint8_t src[], size_t len) { return 0; } +// PowerSaving variant +bool SerialWifiInterface::isReadBusy() const { + return false; +} + bool SerialWifiInterface::isWriteBusy() const { return false; } diff --git a/src/helpers/wifi/SerialWifiInterface.h b/src/helpers/wifi/SerialWifiInterface.h index 19291497f..0ead41574 100644 --- a/src/helpers/wifi/SerialWifiInterface.h +++ b/src/helpers/wifi/SerialWifiInterface.h @@ -52,6 +52,10 @@ public: bool isEnabled() const override { return _isEnabled; } bool isConnected() const override; + + // PowerSaving variant + bool isReadBusy() const override; + bool isWriteBusy() const override; size_t writeFrame(const uint8_t src[], size_t len) override;