From 81ae9dd7797b998644cfdf54f08d687bb54c0e64 Mon Sep 17 00:00:00 2001 From: Jon Vexler Date: Thu, 16 Apr 2026 01:28:43 -0400 Subject: [PATCH] feat: smooth battery percentage for x4 (#1635) ## Summary Battery percentage is calculated from the voltage, which is not totally stable. We smooth the battery percentage using a moving average. ## Additional Context issue discussion: https://github.com/crosspoint-reader/crosspoint-reader/issues/1444 --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? PARTIALLY --- lib/hal/HalPowerManager.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/hal/HalPowerManager.cpp b/lib/hal/HalPowerManager.cpp index a40c81edd..790c7c052 100644 --- a/lib/hal/HalPowerManager.cpp +++ b/lib/hal/HalPowerManager.cpp @@ -113,8 +113,14 @@ uint16_t HalPowerManager::getBatteryPercentage() const { return _batteryCachedPercent; } static const BatteryMonitor battery = BatteryMonitor(BAT_GPIO0); - _batteryCachedPercent = battery.readPercentage(); - return _batteryCachedPercent; + + // smooth the battery %. + if (_batteryCachedPercent == 0) { + _batteryCachedPercent = 10 * battery.readPercentage(); + } else { + _batteryCachedPercent = (_batteryCachedPercent * 9 + battery.readPercentage() * 10) / 10; + } + return _batteryCachedPercent / 10; } HalPowerManager::Lock::Lock() {