Skip to content

fix: Use HalPowerManager for battery percentage#1005

Merged
ngxson merged 2 commits intocrosspoint-reader:masterfrom
vjapolitzer:fix/battery-read
Feb 19, 2026
Merged

fix: Use HalPowerManager for battery percentage#1005
ngxson merged 2 commits intocrosspoint-reader:masterfrom
vjapolitzer:fix/battery-read

Conversation

@vjapolitzer
Copy link
Contributor

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 **

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

No actionable comments were generated in the recent review. 🎉

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 41c02eb and c162112.

📒 Files selected for processing (2)
  • lib/hal/HalPowerManager.cpp
  • lib/hal/HalPowerManager.h
🧰 Additional context used
🧬 Code graph analysis (1)
lib/hal/HalPowerManager.h (1)
lib/hal/HalPowerManager.cpp (2)
  • getBatteryPercentage (67-70)
  • getBatteryPercentage (67-67)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: cppcheck
  • GitHub Check: build
🔇 Additional comments (2)
lib/hal/HalPowerManager.h (1)

36-37: LGTM — return type change is appropriate for non-negative percentage.

The change to uint16_t is suitable for the documented 0-100 range. Consider verifying that BatteryMonitor::readPercentage() doesn't return negative values (e.g., -1 for error conditions), as these would wrap to large positive values when returned as uint16_t.

lib/hal/HalPowerManager.cpp (1)

67-70: Verify BatteryMonitor::readPercentage() return type compatibility.

The method is documented to return battery percentage (range 0-100) as uint16_t. However, the underlying BatteryMonitor::readPercentage() return type must be verified. If it returns a signed type and can yield negative values for error conditions, implicit conversion to uint16_t would produce unexpected wraparound behavior. Confirm that BatteryMonitor::readPercentage() either returns an unsigned type or is guaranteed to only yield values in the range 0-100.


📝 Walkthrough

Walkthrough

The PR removes public Battery.h contents and replaces direct BatteryMonitor usage with HalPowerManager across several files; it updates HalPowerManager.getBatteryPercentage() signature to return uint16_t and updates callers to use powerManager.getBatteryPercentage().

Changes

Cohort / File(s) Summary
Battery Header Removal
src/Battery.h
Erased public header content: removed include guard/pragmas, #include "BatteryMonitor.h", #define BAT_GPIO0 0, and static BatteryMonitor battery(BAT_GPIO0);.
Include Cleanup
src/activities/home/HomeActivity.cpp, src/main.cpp
Removed #include "Battery.h" from these compilation units.
Theme Battery Source Migration
src/components/themes/BaseTheme.cpp, src/components/themes/lyra/LyraTheme.cpp
Replaced #include "Battery.h" with #include "HalPowerManager.h" and changed battery.readPercentage() calls to powerManager.getBatteryPercentage() in drawBatteryLeft/drawBatteryRight.
HAL API update
lib/hal/HalPowerManager.h, lib/hal/HalPowerManager.cpp
Changed getBatteryPercentage() return type from int to uint16_t (signature update in header and implementation).

Sequence Diagram(s)

sequenceDiagram
  participant Theme as "UI Theme\n(drawBattery)"
  participant Power as "HalPowerManager"
  participant Monitor as "BatteryMonitor"
  Theme->>Power: getBatteryPercentage()
  Note right of Power: HalPowerManager reads battery
  Power->>Monitor: readPercentage() / underlying read
  Monitor-->>Power: percentage (uint16_t)
  Power-->>Theme: percentage (uint16_t)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • jdk2pq
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and clearly describes the main change: replacing deprecated BatteryMonitor usage with HalPowerManager for battery percentage retrieval across the codebase.
Description check ✅ Passed The description clearly explains the context and rationale for the changes, detailing how BatteryMonitor was moved and why it needs to be replaced with HalPowerManager.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d461d93 and 41c02eb.

📒 Files selected for processing (5)
  • src/Battery.h
  • src/activities/home/HomeActivity.cpp
  • src/components/themes/BaseTheme.cpp
  • src/components/themes/lyra/LyraTheme.cpp
  • src/main.cpp
💤 Files with no reviewable changes (3)
  • src/activities/home/HomeActivity.cpp
  • src/main.cpp
  • src/Battery.h
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: cppcheck
  • GitHub Check: build
🔇 Additional comments (3)
src/components/themes/BaseTheme.cpp (1)

3-5: Include update looks good.
HalPowerManager include is aligned with the new battery API usage.

src/components/themes/lyra/LyraTheme.cpp (2)

3-5: Include update looks good.
HalPowerManager include matches the new battery percentage API usage.


86-89: LGTM for the battery percentage swap.
This mirrors the BaseTheme update and keeps the rendering logic intact.

Also applies to: 123-126

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/themes/BaseTheme.cpp`:
- Around line 48-50: The local variable in BaseTheme::drawBatteryLeft uses
uint16_t but powerManager.getBatteryPercentage() returns int, causing an
implicit narrowing/consistency mismatch; update the local to int percentage (in
BaseTheme::drawBatteryLeft and any other places such as the similar variable in
drawBatteryRight/drawBatteryIcon usages) so the variable type matches
getBatteryPercentage(), or alternatively change
HalPowerManager::getBatteryPercentage() to return uint16_t across the
codebase—but prefer the minimal change: replace uint16_t percentage with int
percentage in BaseTheme::drawBatteryLeft and related battery-drawing methods to
keep types consistent.

@ngxson ngxson merged commit cabbfcf into crosspoint-reader:master Feb 19, 2026
6 checks passed
lukestein pushed a commit to lukestein/crosspoint-reader that referenced this pull request Feb 20, 2026
## 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 **_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants