Skip to content

fix(basic): fix battery percentage label clipping and redraw on USB plug/unplug - #129

Open
i-oikawa wants to merge 2 commits into
m5stack:masterfrom
i-oikawa:fix/core-gray-battery-label
Open

i-oikawa wants to merge 2 commits into
m5stack:masterfrom
i-oikawa:fix/core-gray-battery-label

Conversation

@i-oikawa

@i-oikawa i-oikawa commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #127

Summary

  • Adjust _battery_label geometry (y=220 + 3, w=32, h=16, keeping x=320 - 56 + 22) so "100%" renders intact with Montserrat12 without triggering _long_wrap() line drops.
  • Repaint _bar_img, _network_img, and _cloud_img when _battery_src changes so the 56x20 charging bolt (x=308..316) is cleared cleanly when switching back to the 44x20 discharging icon.
  • Track refresh_battery so _battery_label is redrawn on top after _battery_img or _bar_img repaints even when the percentage text and _status_bar_src are unchanged.
  • Skip drawing a blank white rectangle (bg_color=0xFEFEFE) over BATTERY_BLACK_IMG when the battery level is <= 0.
  • Preserve and redraw the battery percentage label when _battery_src updates in on_run().
  • Restore the bottom border of the battery icon after drawing the percentage text (_restore_battery_border() via M5.Lcd.drawJpg).

Testing

  • Verified on M5Stack Core Gray (4MB) across boot, USB plug-in, and USB unplug transitions.
  • Verified that the battery percentage text and bottom icon border render cleanly without white-box artifacts at 0% or during battery icon updates.

@m5agent

m5agent commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Reviewed at head 3153517a — mergeable from my side. Base is master 54b1d242 (no drift), one file, +9/−4, and it is exactly the patch we converged on.

Code-level check:

  • Geometry x=286, y=223, w=32, h=16: "100%" measures 30 px, so _long_wrap() takes the early return on textWidth < max_w (label.py#L104-L107) and h never participates. The erase set_text() performs first covers 271..300 × 223..237 (label.py#L47-L56), inside the bar row and the 44 px icon body.
  • refresh_battery: Label.set_text() has no "same text" early return (label.py#L58-L76), so re-calling it with an unchanged "100%" still erases and redraws. refresh_bar remains correct for the bar-driven path.
  • Repaint order: bar → network → cloud → battery icon → label, one iteration (dev.py#L163-L178). The 12 px bolt strip is background again before the 44 px discharge asset draws, so the clear no longer depends on _get_bar_src() changing in the same tick — exactly the case this hunk exists for (cloud disconnected → BAR2_IMG unconditionally). The extra decodes happen once per plug/unplug; fine.

Evidence: code-level verification at head 3153517a / base 54b1d242; your Core Gray v2.5.4 run is the hardware evidence. The reported forward check passes.

NOT RUN here: no Core Gray unit, so no flash/framebuffer. Two things your hardware can still close, neither blocking: (a) one unplug with the cloud disconnected — a unit where the bar source stays constant — to exercise this hunk in isolation; (b) the <20% red / red-charge transition (same 44/56 split, so the same clear should cover it). I reviewed the Basic launcher only.

@i-oikawa

Copy link
Copy Markdown
Author

Thank you for the thorough review!

I have also built and flashed the final patch — including the _bar_img._draw(False), _network_img._draw(False), and _cloud_img._draw(False) repaint calls — onto my M5Stack Core Gray (4MB) and verified the remaining hardware test case:

  • USB unplug with cloud disconnected (static BAR2_IMG): Unplugged the USB cable with Wi-Fi/cloud disconnected so the status bar source remained constant. The blue charging bolt (x=308..316) cleared immediately with no leftover artifacts, and the network/cloud icons along with the "100%" label redrew cleanly on top.

Everything is verified on hardware and ready to merge.

@i-oikawa i-oikawa closed this Nov 16, 2026
@i-oikawa i-oikawa reopened this Nov 16, 2026
@m5agent

m5agent commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks — that closes the one case I left open. Head is still 3153517a (base 54b1d242), so the code-level review above stands unchanged; your Core Gray run is the hardware evidence for the constant-BAR2_IMG unplug path, and I have nothing further on the code.

Two boundaries to keep explicit:

  • The <20% red / red-charge transition (my item b) is still NOT RUN — same 44/56 split, so the same clear should cover it, but I have not seen it observed.
  • I could not reproduce the unplug on a virtual device: basic-v27 models the IP5306 registers only, with no charger/VBUS power path or cell (READ4 reads 0xff), so USB presence cannot be toggled there (record).

No changes requested; merge is the maintainers' call. NOT RUN here: no Core Gray unit, and I reviewed the Basic launcher only.

@i-oikawa

Copy link
Copy Markdown
Author

I have pushed an additional commit to address a few remaining battery indicator edge cases in DevApp (m5stack/modules/startup/basic/apps/dev.py) and updated the PR description above:

  • Avoid drawing a blank white rectangle over BATTERY_BLACK_IMG when the battery level is <= 0.
  • Keep the battery percentage label visible when _battery_src updates in on_run().
  • Restore the bottom border of the battery icon after drawing the percentage text (_restore_battery_border()).

Please review and approve the workflow checks when you have time. Thank you!

@m5agent

m5agent commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Reviewed 45e33560 (+12/−2 on top of 3153517a); the geometry/repaint analysis from the earlier review is unchanged and still applies.

Conclusion: the three new hunks are correct at code level — nothing to change in them. The only open item is workflow approval, which I can't do from this account.

Evidence — code-level verification at 45e33560:

  • Border restore: _restore_battery_border() calls drawJpg(src, 264, 236, maxW=0, maxH=4, offX=0, offY=16) (binding). image_info_t::begin() resolves that to a clip rect of 264,236,44×4 for the 44 px discharge icons (56×4 for the 56 px charge icons), i.e. image rows 16–19 (LGFXBase.cpp). I decoded the assets: the bottom border is rows 16–18 and the 100% erase is 271..300 × 223..237, so rows 236–237 are exactly the part that must be repainted, and nothing outside the icon is touched.
  • No new text clipping: Montserrat12 on this board is LVGL montserrat_12 (line_height=15, base_line=3, font). With top_center at (286,223) the digit and % ink lands on rows 226–234, so the restore starting at row 236 does not touch the glyphs; the original 12 px-in-10 px cause stays fixed.
  • 0% path: _get_battery_text() returns "" for <=0, so skipping set_text() removes the 0xFEFEFE erase over BATTERY_BLACK_IMG, and on_view() draws no label. When a visible percentage becomes empty, the icon source necessarily changes (red/green → black) and Image.set_src() calls _draw(True), repainting over the old text — I couldn't construct a stale-text case.
  • refresh_battery covers the src-changed/text-unchanged tick; draw order stays bar → network → cloud → battery → label.

Workflow approval: both runs for this head are action_required (m5stack port, Check code formatting). I can't approve them — this account has no push access to the repo (/collaborators/m5agent/permission → 403), so a maintainer needs to approve/run them. FYI the first-attempt runs on 3153517a were reported by GitHub as "workflow file issue", i.e. infra rather than this diff.

Nit (non-blocking): dev.py:129-130 has a double blank line. The repo's ruff format config would collapse it, but the current "Check code formatting" job formats C only, so it won't fail on this.

NOT RUN / limits: code-level only; I did not flash this head. Virtual-device validation is still unavailable for the plug/unplug path: basic-v27 exists on the local Device Service, but its IP5306 is register-only — no cell/charger/VBUS path, READ4=0xff — same boundary as before. I reviewed the Basic launcher only.

@i-oikawa

Copy link
Copy Markdown
Author

NOT RUN here: no Core Gray unit, so no flash/framebuffer. Two things your hardware can still close, neither blocking: (a) one unplug with the cloud disconnected — a unit where the bar source stays constant — to exercise this hunk in isolation; (b) the <20% red / red-charge transition (same 44/56 split, so the same clear should cover it). I reviewed the Basic launcher only.

Thanks for the thorough review! I checked both items on my M5Stack Core Gray (M5STACK_Basic_4MB) unit:

  • (a) Unplugging with cloud disconnected (BAR0_IMG): Verified on the default open-source build (without M5Things), where the bar source remains constant. Unplugging the USB cable cleanly clears the charging bolt artifact without leaving any residue.
  • (b) <20% red / red-charge transition: On the Core Gray, the IP5306 PMIC only reports discrete battery levels (-1, 0, 25, 50, 75, 100) and drops directly from 25 to 0 right before power-off, so a <20% state cannot be reproduced on this hardware. As you noted, it shares the exact same 44/56 width split and clear logic.

@m5agent

m5agent commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Conclusion: (a) is exactly the isolation case I asked for — with a constant bar source refresh_bar never becomes true, so the only thing that can clear the 12 px bolt strip is the three unconditional _draw(False) calls in the _battery_src branch. Your run therefore closes the hunk itself, not just the symptom. (b) I am retiring as not applicable to this board rather than NOT RUN: the red branch is unreachable on the IP5306, so there is nothing left to observe. No code change requested; head is still 45e33560.

Evidence — code-level, this head on base 54b1d242:

  • _get_battery_src() only takes the red path for 0 < battery < 20 (dev.py#L326-L338). M5.BOARD.M5Stack without PSRAM is the only route into the basic launcher (startup/init.py#L327-L337), and that board's PMIC is the IP5306, whose getBatteryLevel() decodes READ4 >> 4 as 0x00→100, 0x08→75, 0x0C→50, 0x0E→25, default→0 (-1 on I2C error) (IP5306_Class.cpp#L45-L57, dispatch at Power_Class.cpp#L2294-L2295). No value can land in 1..19; the bottom step yields 0 → BATTERY_BLACK_* with empty text, which the 0% hunk already covers. Your 25 → 0 observation is what the driver allows, not a missing test.

  • Keeping the record accurate: the note says BAR0_IMG, but there is no BAR0 asset — res.py defines BAR1–BAR5, and the no-M5Things branch of _get_bar_src() returns BAR2_IMG (dev.py#L289-L297). A constant BAR2_IMG, as in your earlier run, is what this head produces. It does not change the result — the repaint is unconditional — but the two reports should name the same asset.

NOT RUN: no Core Gray unit, and I did not rebuild/flash this head on the virtual Basic either; (a) rests on your hardware run, (b) on the code above. I reviewed the Basic launcher only.

@i-oikawa

i-oikawa commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thank you for reviewing and merging the PR! Glad to hear it will be included in the next UiFlow2 release.

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.

[Bug] Battery level indicator missing on M5Stack Core Gray (IP5306) in UIFlow2 v2.4.4 and later

3 participants