From cc4801b32b1c3bbfffeb5ed7870e5c5d65730c81 Mon Sep 17 00:00:00 2001 From: P4tches Date: Wed, 9 Sep 2026 23:01:26 -0700 Subject: [PATCH] Add multiple battery low voltage readings to prevent early shutdown (especially during TX spikes) (cherry picked from commit 2f79beacc1d23dfe8c4f0b5bb4e8e20638799e2f) --- examples/companion_radio/AbstractUITask.h | 4 ++++ examples/companion_radio/ui-new/UITask.cpp | 7 +++++-- examples/companion_radio/ui-new/UITask.h | 2 ++ examples/companion_radio/ui-tiny/UITask.cpp | 21 ++++++++++++--------- examples/companion_radio/ui-tiny/UITask.h | 2 ++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/examples/companion_radio/AbstractUITask.h b/examples/companion_radio/AbstractUITask.h index d927c1a3fa..7df9ec7cd3 100644 --- a/examples/companion_radio/AbstractUITask.h +++ b/examples/companion_radio/AbstractUITask.h @@ -14,6 +14,10 @@ #include "NodePrefs.h" #include "MyMesh.h" +#ifndef AUTO_SHUTDOWN_LOW_READINGS + #define AUTO_SHUTDOWN_LOW_READINGS 3 +#endif + enum class UIEventType { none, contactMessage, diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 181bc8328b..997984aa57 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -1055,8 +1055,9 @@ void UITask::loop() { #ifdef AUTO_SHUTDOWN_MILLIVOLTS if (millis() > next_batt_chck) { uint16_t milliVolts = getBattMilliVolts(); - if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS) { - if(!board.isExternalPowered()) { + if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS && !board.isExternalPowered()) { + _low_batt_count++; + if (_low_batt_count >= AUTO_SHUTDOWN_LOW_READINGS) { if (_display != NULL) { _display->startFrame(); _display->setTextSize(2); @@ -1068,6 +1069,8 @@ void UITask::loop() { } shutdown(); } + } else { + _low_batt_count = 0; } next_batt_chck = millis() + 8000; } diff --git a/examples/companion_radio/ui-new/UITask.h b/examples/companion_radio/ui-new/UITask.h index 361b04d012..af5e6723ef 100644 --- a/examples/companion_radio/ui-new/UITask.h +++ b/examples/companion_radio/ui-new/UITask.h @@ -61,6 +61,7 @@ class UITask : public AbstractUITask { unsigned long _alert_expiry; int _msgcount; unsigned long ui_started_at, next_batt_chck; + uint8_t _low_batt_count; int next_backlight_btn_check = 0; #ifdef PIN_STATUS_LED int led_state = 0; @@ -91,6 +92,7 @@ class UITask : public AbstractUITask { UITask(mesh::MainBoard* board, MultiSerialInterface* serial) : AbstractUITask(board, serial), _display(NULL), _sensors(NULL) { next_batt_chck = _next_refresh = 0; + _low_batt_count = 0; ui_started_at = 0; curr = NULL; } diff --git a/examples/companion_radio/ui-tiny/UITask.cpp b/examples/companion_radio/ui-tiny/UITask.cpp index 7b70f83686..33b9e61f14 100644 --- a/examples/companion_radio/ui-tiny/UITask.cpp +++ b/examples/companion_radio/ui-tiny/UITask.cpp @@ -755,19 +755,22 @@ void UITask::loop() { #ifdef AUTO_SHUTDOWN_MILLIVOLTS if (millis() > next_batt_chck) { _cached_batt_mv = getBattMilliVolts(); - if (_cached_batt_mv > 0 && _cached_batt_mv < AUTO_SHUTDOWN_MILLIVOLTS) { - if(!board.isExternalPowered()) { + if (_cached_batt_mv > 0 && _cached_batt_mv < AUTO_SHUTDOWN_MILLIVOLTS && !board.isExternalPowered()) { + _low_batt_count++; + if (_low_batt_count >= AUTO_SHUTDOWN_LOW_READINGS) { if (_display != NULL) { - _display->startFrame(); - _display->setTextSize(2); - _display->drawTextCentered(_display->width() / 2, 6, "Low battery!"); - _display->setTextSize(1); - _display->drawTextCentered(_display->width() / 2, 18, "Shutting down!"); - _display->endFrame(); - if (_display->isEink() == false) { delay(3000); } + _display->startFrame(); + _display->setTextSize(2); + _display->drawTextCentered(_display->width() / 2, 6, "Low battery!"); + _display->setTextSize(1); + _display->drawTextCentered(_display->width() / 2, 18, "Shutting down!"); + _display->endFrame(); + if (_display->isEink() == false) { delay(3000); } } shutdown(); } + } else { + _low_batt_count = 0; } next_batt_chck = millis() + 8000; } diff --git a/examples/companion_radio/ui-tiny/UITask.h b/examples/companion_radio/ui-tiny/UITask.h index 084919aa6c..95eedf3d8c 100644 --- a/examples/companion_radio/ui-tiny/UITask.h +++ b/examples/companion_radio/ui-tiny/UITask.h @@ -43,6 +43,7 @@ class UITask : public AbstractUITask { unsigned long ui_started_at, next_batt_chck; int next_backlight_btn_check = 0; uint16_t _cached_batt_mv; + uint8_t _low_batt_count; #ifdef PIN_STATUS_LED int led_state = 0; int next_led_change = 0; @@ -74,6 +75,7 @@ class UITask : public AbstractUITask { UITask(mesh::MainBoard* board, MultiSerialInterface* serial) : AbstractUITask(board, serial), _display(NULL), _sensors(NULL) { next_batt_chck = _next_refresh = 0; _cached_batt_mv = 0; + _low_batt_count = 0; ui_started_at = 0; curr = NULL; }