Battery: use the standard BLE charging flag when a device exposes it
The companion protocol reports only a battery voltage, so charging is a voltage-trend inference. Some devices could instead expose the standard GATT Battery Service (0x180F) whose Battery Level Status characteristic (0x2BED, Battery Service 1.1) carries a real, firmware-reported charging flag. Dumping the GATT table shows no current MeshCore build implements it — the BLE profile is just the Nordic UART pipe plus DFU — but wire it up now so it lights up automatically the day one does. get_hw_charging() reads 0x2BED best-effort over BLE (reaching the bleak client the forced-teardown path already uses) and prefers its flag; any failure — service absent, read refused, short value, non-BLE transport — is a quiet None, so the poller falls back to the inference and every current device behaves exactly as before. Verified against a live device: the read resolves and returns a clean, steady None. The 0x2BED bitfield decode is a pure function unit-tested against the spec's Charge State enum so the parse is correct ahead of any hardware that populates it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
e73cea9f42
commit
6aecb58e59
3 files changed
+159
-8
No files matched your search
@@ -13,14 +13,17 @@ Two device facts shape what it can report:
|
||||
the reading is turned into a state-of-charge estimate against a single-cell LiPo
|
||||
discharge curve (:func:`battery_percent`). Devices with no battery gauge answer with no
|
||||
usable level; those are reported as *absent* so the header shows nothing for them.
|
||||
* Firmware exposes **no charging flag at all**. Charging is therefore *inferred* from the
|
||||
terminal voltage — but a single-cell LiPo sags tens of millivolts under each LoRa
|
||||
* The companion protocol exposes **no charging flag at all**. Charging is therefore *inferred*
|
||||
from the terminal voltage — but a single-cell LiPo sags tens of millivolts under each LoRa
|
||||
transmission and springs back when the radio idles, so the raw sample trend is mostly load
|
||||
noise, not charge. The inference reads it robustly (the median of the sample window's older
|
||||
half against its newer half, which discards those transient spikes) and calls charge only
|
||||
on a large, *sustained* rise, dropping straight back the moment the pack goes flat. It is
|
||||
still an estimate — the best the firmware allows — but one a discharging or resting pack no
|
||||
longer trips.
|
||||
still an estimate — the best the companion protocol allows — but one a discharging or
|
||||
resting pack no longer trips. Where a device *does* expose a firmware charging flag over the
|
||||
standard BLE Battery Level Status characteristic (no MeshCore build does today), that ground
|
||||
truth is preferred and the inference only fills in for the rest — see
|
||||
:meth:`~meshterm.core.connection.Device.get_hw_charging`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -197,10 +200,15 @@ class BatteryService:
|
||||
return
|
||||
now = time.monotonic()
|
||||
self._history.append((now, mv))
|
||||
# Prefer a firmware-reported charging flag when the device exposes one (the standard
|
||||
# BLE Battery Level Status characteristic); only where it can't — every MeshCore device
|
||||
# today — fall back to inferring charge from the voltage trend.
|
||||
hw_charging = await device.get_hw_charging()
|
||||
charging = hw_charging if hw_charging is not None else self._charging(now)
|
||||
self._reading = BatteryReading(
|
||||
millivolts=mv,
|
||||
percent=battery_percent(mv),
|
||||
charging=self._charging(now),
|
||||
charging=charging,
|
||||
)
|
||||
|
||||
def _charging(self, now: float) -> bool:
|
||||
|
||||
Reference in new issue
Block a user