Battery: the sweep is the charging state, so it runs everywhere

The gauge says charging by sweeping its fill bottom-to-full, holding the
charge colour. That is the state itself, not decoration, so gating it on
Platform.effects left the PicoCalc with no way to say it at all — and,
worse, frozen on the sweep's empty frame. Only the low-battery blink is
decoration over a charge the cell already shows; that alone stays behind
`effects`, and `animate` now says so.

The sweep steps on the platform's own idle repaint interval rather than a
fixed second, so each repaint advances it one clean step instead of
aliasing across skipped frames: 1 s on the desktop, 2 s on the handheld.

A pack at 100% draws as not charging whichever way the flag reads — a full
cell resting full is the honest picture, and a charger left plugged in
shouldn't leave the gauge sweeping forever.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5 committed 2026-08-13 23:16:09 -04:00
1 parent 88b2d347dd
commit 2731af12cb
4 files changed
+63 -61

No files matched your search

+17 -13
View File
@@ -213,11 +213,13 @@ def _header(ctx: AppContext, cache: dict, width: int) -> Text:
return header
#: Seconds per animation step of the header battery gauge (the charging sweep and the
#: low-battery blink). Matched to the session's ~1 Hz idle repaint (see the app's
#: ``refresh_interval``) so each repaint advances the animation by one clean step rather
#: than aliasing across skipped frames.
_BATTERY_ANIM_S = 1.0
#: Floor on the seconds per animation step of the header battery gauge (the charging sweep
#: and the low-battery blink). The step is the platform's own idle repaint interval
#: (``Platform.tick_s``, the app's ``refresh_interval``) so each repaint advances the
#: animation by one clean step rather than aliasing across skipped frames — 1 s on the
#: desktop, one every 2 s on the PicoCalc's slower tick, where a full sweep therefore takes
#: :data:`~meshterm.ui.widgets._CHARGE_FRAMES` × 2 s to climb.
_BATTERY_ANIM_MIN_S = 1.0
def _battery_segment(ctx: AppContext) -> Text:
@@ -238,15 +240,17 @@ def _battery_segment(ctx: AppContext) -> Text:
reading = ctx.battery.reading()
if reading is None:
return Text()
# A platform without effects draws the gauge's resting state instead: the true fill,
# unblinking, with charging said by a static mark rather than the sweep. The blink and
# the sweep both exist to catch the eye, and neither is worth a forced repaint (nor, on
# a 16-slot console, a colour swap) on hardware where the cells are dear — but charging
# is a *fact about the pack*, not decoration, so it still has to show at rest.
effects = get_platform().effects
frame = int(time.monotonic() / _BATTERY_ANIM_S) if effects else 0
platform = get_platform()
# The sweep is how the gauge says "charging" at all, so its clock runs on every platform;
# only the step widens to whatever that platform actually repaints at. The low-battery
# blink is decoration over a charge the cell already shows, so it stays behind `effects`
# — not worth a forced repaint, nor a colour swap on a 16-slot console.
frame = int(time.monotonic() / max(_BATTERY_ANIM_MIN_S, platform.tick_s))
return battery_cell(
reading.percent, charging=reading.charging, frame=frame, animate=effects
reading.percent,
charging=reading.charging,
frame=frame,
animate=platform.effects,
)
+14 -24
View File
@@ -1361,12 +1361,6 @@ _BATTERY_CRITICAL = 10
#: Frames in the charging sweep: empty → four fills → round again (bottom-to-full loop).
_CHARGE_FRAMES = 5
#: The charging mark for a platform that can't animate (``Platform.effects`` off): one arrow
#: beside the true fill, saying charge is going *in*. The sweep is the desktop's way of
#: telling the same thing over time; where time isn't available the glyph has to say it at
#: rest, and it must stay inside the console font's inventory (see ``ui/fontset.py``).
_CHARGE_MARK = "↑"
def _braille_fill(rows: int) -> str:
"""A single braille cell with its bottom ``rows`` (0–4) dot rows lit."""
@@ -1401,39 +1395,35 @@ def battery_cell(
* **Critically low** (≤ :data:`_BATTERY_CRITICAL`%, not charging) blinks the red cell to a
dim slate on alternate frames — an unmissable pulse in the corner.
Both are *animations*, and a platform without effects (the PicoCalc — see
``Platform.effects``) has no frames to spend on them, so it passes ``animate=False``.
The blink simply doesn't run there, but charging still has to be legible at rest: the
sweep would otherwise freeze on whichever fill its held frame happened to land on and
lie about the charge. So the static gauge keeps the **true** fill and says charging with
:data:`_CHARGE_MARK` beside it instead.
The two differ in what they're *for*, which is what ``animate`` sorts out. The sweep is
the only thing that says charging at all, so it runs everywhere — a platform whose frames
are dear (the PicoCalc: see ``Platform.effects``) just steps it on that platform's own
slower repaint cadence. The blink is an alarm laid over a charge the gauge already shows,
so it is genuinely decorative and ``animate=False`` drops it, leaving the red cell steady.
A pack at 100% is drawn as **not charging** whichever way the flag reads: a full cell
resting full is the honest picture, and a topped-off charger left plugged in shouldn't
leave the gauge sweeping forever.
Args:
percent: State of charge, 0–100 (clamped).
charging: Whether the pack is taking charge (drives the fill sweep, or the static
mark where nothing animates).
charging: Whether the pack is taking charge (drives the fill sweep). Ignored at 100%.
frame: A monotonically advancing tick; only its phase is read, so any
steadily-incrementing integer animates the two live states.
animate: Whether the caller repaints often enough for animation. ``False`` draws the
gauge's resting state — true fill, no blink — and marks charging statically.
animate: Whether decorative animation runs. ``False`` holds the low-battery blink at
its resting frame; the charging sweep runs regardless, being the state itself.
Returns:
A Rich :class:`Text`: the coloured glyph (plus the charge mark, when charging with
no animation), a space, and ``NN%`` in muted text.
A Rich :class:`Text`: the coloured glyph, a space, and ``NN%`` in muted text.
"""
pct = max(0, min(100, int(percent)))
rows, color = _battery_tier(pct)
mark = ""
if charging and animate:
if charging and pct < 100:
# The charge colour is held; only the fill sweeps, so it reads as "filling", not
# as the charge itself jumping around.
rows = frame % _CHARGE_FRAMES
elif charging:
mark = _CHARGE_MARK
elif animate and pct <= _BATTERY_CRITICAL and frame % 2:
color = "batt.dim"
out = Text(_braille_fill(rows), style=color)
if mark:
out.append(mark, style=color)
out.append(f" {pct}%", style="muted")
return out
+13 -17
View File
@@ -19,8 +19,7 @@ from meshterm.services.battery_service import (
BatteryService,
battery_percent,
)
from meshterm.ui.fontset import in_font
from meshterm.ui.widgets import _BATTERY_CRITICAL, _CHARGE_MARK, battery_cell
from meshterm.ui.widgets import _BATTERY_CRITICAL, battery_cell
# --- the LiPo state-of-charge curve -------------------------------------------------------
@@ -86,24 +85,21 @@ def test_battery_cell_charging_sweeps_bottom_to_full_holding_the_percent() -> No
assert {str(battery_cell(20, charging=True, frame=f).style) for f in range(6)} == {"batt.low"}
def test_battery_cell_marks_charging_statically_when_nothing_animates() -> None:
"""With animation off the fill stays true and a mark — not the sweep — says charging."""
still = battery_cell(20, charging=True, frame=0, animate=False)
# The frozen sweep used to draw an *empty* cell here, under-reading the pack outright.
assert still.plain == chr(0x2800 | 0xC0) + _CHARGE_MARK + " 20%"
assert still.plain[:1] == battery_cell(20).plain[:1] # the same fill as at rest
# It says the same thing whatever the frame — there are no frames to read.
assert {battery_cell(20, charging=True, frame=f, animate=False).plain for f in range(6)} == {
still.plain
}
# Not charging draws no mark, and the low-battery blink stays still too.
assert battery_cell(20, animate=False).plain == chr(0x2800 | 0xC0) + " 20%"
def test_battery_cell_sweeps_even_where_decoration_is_switched_off() -> None:
"""``animate=False`` silences the blink, never the sweep — the sweep *is* the state."""
fills = [battery_cell(20, charging=True, frame=f, animate=False).plain[0] for f in range(5)]
assert fills == [battery_cell(20, charging=True, frame=f).plain[0] for f in range(5)]
assert len(set(fills)) == 5 # it really climbs, rather than holding one frame
# The blink, being decoration over a charge the cell already shows, does hold still.
assert {str(battery_cell(5, frame=f, animate=False).style) for f in range(4)} == {"batt.low"}
def test_charge_mark_is_drawable_on_the_console_font() -> None:
"""The static mark is only useful on the platform whose font has to have it."""
assert in_font(_CHARGE_MARK)
def test_battery_cell_at_full_rests_full_however_the_charging_flag_reads() -> None:
"""A topped-off pack draws as not charging, so a plugged-in charger stops the sweep."""
assert battery_cell(100, charging=True, frame=0).plain == "⣿ 100%"
assert {battery_cell(100, charging=True, frame=f).plain for f in range(6)} == {"⣿ 100%"}
# One percent short is still filling, and still sweeps.
assert battery_cell(99, charging=True, frame=0).plain[0] == chr(0x2800)
# --- the poller ---------------------------------------------------------------------------
+19 -7
View File
@@ -194,8 +194,9 @@ def test_an_explicit_spinner_cycle_still_wins() -> None:
assert Spinner("ab").frames == "ab"
def test_battery_gauge_holds_still_where_effects_are_off() -> None:
"""No charging sweep and no low-battery blink: the gauge draws its resting frame."""
def test_battery_gauge_steps_the_sweep_at_the_platforms_own_cadence(monkeypatch) -> None:
"""The charging sweep runs on the PicoCalc too — a step per repaint, not per second."""
from meshterm.ui import menu
from meshterm.services.battery_service import BatteryReading
from meshterm.ui.menu import _battery_segment
@@ -206,13 +207,24 @@ def test_battery_gauge_holds_still_where_effects_are_off() -> None:
return BatteryReading(millivolts=3300, percent=5, charging=True)
ctx = _Ctx()
clock = 0.0
monkeypatch.setattr(menu.time, "monotonic", lambda: clock)
def _sweep(seconds: float) -> list[str]:
"""The gauge's fills over five ticks of ``seconds`` each."""
nonlocal clock
out = []
for step in range(5):
clock = step * seconds
out.append(_battery_segment(ctx).plain[0]) # type: ignore[arg-type]
return out
set_platform(PICOCALC)
# Frame 0 of a charging sweep is the empty cell; any later frame differs. Two calls at
# different wall-clock instants must agree, which they only can if the clock isn't read.
first = _battery_segment(ctx).plain # type: ignore[arg-type]
second = _battery_segment(ctx).plain # type: ignore[arg-type]
assert first == second
# A step per 2 s idle repaint: the sweep climbs, and does not alias across skipped frames.
assert len(set(_sweep(PICOCALC.tick_s))) == 5
assert len(set(_sweep(PICOCALC.tick_s / 2))) < 5 # half a tick apart, some frames repeat
set_platform(REGULAR)
assert len(set(_sweep(REGULAR.tick_s))) == 5
assert _battery_segment(ctx).plain.endswith("5%") # type: ignore[arg-type]