From cabbfcfd7e77409ca0d7a571bb80c502e8def322 Mon Sep 17 00:00:00 2001 From: Vincent Politzer Date: Thu, 19 Feb 2026 15:05:35 -0800 Subject: [PATCH] fix: Use HalPowerManager for battery percentage (#1005) ## Summary The introduction of `HalGPIO` moved the `BatteryMonitor battery` object into the member function `HalGPIO::getBatteryPercentage()`. Then, with the introduction of `HalPowerManager`, this function was moved to `HalPowerManager::getBatteryPercentage()`. However, the original `BatteryMonitor battery` object is still utilized by themes for displaying the battery percentage. This PR replaces these deprecated uses of `BatteryMonitor battery` with the new `HalPowerManager::getBatteryPercentage()` function. --- ### 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? _**NO **_ --- lib/hal/HalPowerManager.cpp | 2 +- lib/hal/HalPowerManager.h | 2 +- src/Battery.h | 6 ------ src/activities/home/HomeActivity.cpp | 1 - src/components/themes/BaseTheme.cpp | 6 +++--- src/components/themes/lyra/LyraTheme.cpp | 6 +++--- src/main.cpp | 1 - 7 files changed, 8 insertions(+), 16 deletions(-) delete mode 100644 src/Battery.h diff --git a/lib/hal/HalPowerManager.cpp b/lib/hal/HalPowerManager.cpp index b4d8d05c..3b4eaf9c 100644 --- a/lib/hal/HalPowerManager.cpp +++ b/lib/hal/HalPowerManager.cpp @@ -64,7 +64,7 @@ void HalPowerManager::startDeepSleep(HalGPIO& gpio) const { esp_deep_sleep_start(); } -int HalPowerManager::getBatteryPercentage() const { +uint16_t HalPowerManager::getBatteryPercentage() const { static const BatteryMonitor battery = BatteryMonitor(BAT_GPIO0); return battery.readPercentage(); } diff --git a/lib/hal/HalPowerManager.h b/lib/hal/HalPowerManager.h index 80f667d2..74cff1bf 100644 --- a/lib/hal/HalPowerManager.h +++ b/lib/hal/HalPowerManager.h @@ -34,7 +34,7 @@ class HalPowerManager { void startDeepSleep(HalGPIO& gpio) const; // Get battery percentage (range 0-100) - int getBatteryPercentage() const; + uint16_t getBatteryPercentage() const; // RAII helper class to manage power saving locks // Usage: create an instance of Lock in a scope to disable power saving, for example when running a task that needs diff --git a/src/Battery.h b/src/Battery.h deleted file mode 100644 index dcfcbf79..00000000 --- a/src/Battery.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -#include - -#define BAT_GPIO0 0 // Battery voltage - -static BatteryMonitor battery(BAT_GPIO0); diff --git a/src/activities/home/HomeActivity.cpp b/src/activities/home/HomeActivity.cpp index d4e11509..273f366b 100644 --- a/src/activities/home/HomeActivity.cpp +++ b/src/activities/home/HomeActivity.cpp @@ -11,7 +11,6 @@ #include #include -#include "Battery.h" #include "CrossPointSettings.h" #include "CrossPointState.h" #include "MappedInputManager.h" diff --git a/src/components/themes/BaseTheme.cpp b/src/components/themes/BaseTheme.cpp index 950b79f3..63427ac2 100644 --- a/src/components/themes/BaseTheme.cpp +++ b/src/components/themes/BaseTheme.cpp @@ -1,6 +1,7 @@ #include "BaseTheme.h" #include +#include #include #include #include @@ -8,7 +9,6 @@ #include #include -#include "Battery.h" #include "I18n.h" #include "RecentBooksStore.h" #include "components/UITheme.h" @@ -47,7 +47,7 @@ void drawBatteryIcon(const GfxRenderer& renderer, int x, int y, int battWidth, i void BaseTheme::drawBatteryLeft(const GfxRenderer& renderer, Rect rect, const bool showPercentage) const { // Left aligned: icon on left, percentage on right (reader mode) - const uint16_t percentage = battery.readPercentage(); + const uint16_t percentage = powerManager.getBatteryPercentage(); const int y = rect.y + 6; if (showPercentage) { @@ -62,7 +62,7 @@ void BaseTheme::drawBatteryLeft(const GfxRenderer& renderer, Rect rect, const bo void BaseTheme::drawBatteryRight(const GfxRenderer& renderer, Rect rect, const bool showPercentage) const { // Right aligned: percentage on left, icon on right (UI headers) // rect.x is already positioned for the icon (drawHeader calculated it) - const uint16_t percentage = battery.readPercentage(); + const uint16_t percentage = powerManager.getBatteryPercentage(); const int y = rect.y + 6; if (showPercentage) { diff --git a/src/components/themes/lyra/LyraTheme.cpp b/src/components/themes/lyra/LyraTheme.cpp index 38a8aff2..d9950fa0 100644 --- a/src/components/themes/lyra/LyraTheme.cpp +++ b/src/components/themes/lyra/LyraTheme.cpp @@ -1,13 +1,13 @@ #include "LyraTheme.h" #include +#include #include #include #include #include -#include "Battery.h" #include "RecentBooksStore.h" #include "components/UITheme.h" #include "components/icons/book.h" @@ -85,7 +85,7 @@ const uint8_t* iconForName(UIIcon icon, int size) { void LyraTheme::drawBatteryLeft(const GfxRenderer& renderer, Rect rect, const bool showPercentage) const { // Left aligned: icon on left, percentage on right (reader mode) - const uint16_t percentage = battery.readPercentage(); + const uint16_t percentage = powerManager.getBatteryPercentage(); const int y = rect.y + 6; const int battWidth = LyraMetrics::values.batteryWidth; @@ -122,7 +122,7 @@ void LyraTheme::drawBatteryLeft(const GfxRenderer& renderer, Rect rect, const bo void LyraTheme::drawBatteryRight(const GfxRenderer& renderer, Rect rect, const bool showPercentage) const { // Right aligned: percentage on left, icon on right (UI headers) - const uint16_t percentage = battery.readPercentage(); + const uint16_t percentage = powerManager.getBatteryPercentage(); const int y = rect.y + 6; const int battWidth = LyraMetrics::values.batteryWidth; diff --git a/src/main.cpp b/src/main.cpp index 6e145d10..2fa20bed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,6 @@ #include -#include "Battery.h" #include "CrossPointSettings.h" #include "CrossPointState.h" #include "KOReaderCredentialStore.h"