From 781b4f5a33728080b0ea524cdc51fbc81d30b700 Mon Sep 17 00:00:00 2001 From: liquidraver <504870+liquidraver@users.noreply.github.com> Date: Wed, 20 May 2026 22:27:32 +0200 Subject: [PATCH] fixups for battery reading --- .../helpers/ui-joystick/joystick_ui_task.cpp | 1 + zephcore/helpers/ui/ui_common.c | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/zephcore/helpers/ui-joystick/joystick_ui_task.cpp b/zephcore/helpers/ui-joystick/joystick_ui_task.cpp index b5ac80f..5a2416c 100644 --- a/zephcore/helpers/ui-joystick/joystick_ui_task.cpp +++ b/zephcore/helpers/ui-joystick/joystick_ui_task.cpp @@ -483,6 +483,7 @@ do_render: } else #endif { + ui_refresh_battery(); _display.startFrame(); int delay_ms; if (_locked) { diff --git a/zephcore/helpers/ui/ui_common.c b/zephcore/helpers/ui/ui_common.c index 567a34d..1db0f4c 100644 --- a/zephcore/helpers/ui/ui_common.c +++ b/zephcore/helpers/ui/ui_common.c @@ -194,3 +194,33 @@ void ui_led_flash_msg(void) } #endif } + +/* ========== Battery refresh ========== + * Lazy: render path calls ui_refresh_battery(); ADC only fires when the + * cached reading is older than UI_BATT_REFRESH_MS. Telemetry / stats paths + * read fresh directly via their own callbacks — this gate only governs the + * local display. */ +#define UI_BATT_REFRESH_MS 30000 + +static uint16_t (*s_batt_provider)(void); +static uint32_t s_batt_last_read_ms; +static bool s_batt_ever_read; + +void ui_set_battery_provider(uint16_t (*provider)(void)) +{ + s_batt_provider = provider; +} + +void ui_refresh_battery(void) +{ + if (!s_batt_provider) { + return; + } + uint32_t now = k_uptime_get_32(); + if (s_batt_ever_read && (now - s_batt_last_read_ms) < UI_BATT_REFRESH_MS) { + return; + } + ui_set_battery(s_batt_provider(), 0); + s_batt_last_read_ms = k_uptime_get_32(); + s_batt_ever_read = true; +}