From 0a892f2dade6c3e17d2c79809b0614bb75e821a3 Mon Sep 17 00:00:00 2001 From: pelgraine <140762863+pelgraine@users.noreply.github.com> Date: Sat, 14 Feb 2026 10:41:03 +1100 Subject: [PATCH] changed to hybrid render battery indicator method --- examples/companion_radio/ui-new/UITask.cpp | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 8d28c8a0..4ea97ffe 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -110,17 +110,22 @@ class HomeScreen : public UIScreen { AdvertPath recent[UI_RECENT_LIST_SIZE]; -void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts) { +void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts, int* outIconX = nullptr) { // Use the BQ27220 fuel gauge SOC register for accurate percentage. - // Falls back to voltage estimation if the fuel gauge returns 0. + // Falls back to voltage estimation if the fuel gauge is uncalibrated. uint8_t batteryPercentage = board.getBatteryPercent(); - if (batteryPercentage == 0 && batteryMilliVolts > 0) { - const int minMilliVolts = 3000; - const int maxMilliVolts = 4200; - int pct = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts); - if (pct < 0) pct = 0; - if (pct > 100) pct = 100; - batteryPercentage = (uint8_t)pct; + + // Sanity check: if voltage says full but gauge disagrees significantly, + // the gauge hasn't calibrated yet — fall back to voltage estimate + int voltagePct = 0; + if (batteryMilliVolts > 0) { + voltagePct = ((batteryMilliVolts - 3000) * 100) / (4200 - 3000); + if (voltagePct < 0) voltagePct = 0; + if (voltagePct > 100) voltagePct = 100; + } + + if (batteryPercentage == 0 || abs((int)batteryPercentage - voltagePct) > 30) { + batteryPercentage = (uint8_t)voltagePct; } display.setColor(DisplayDriver::GREEN);