fix(batt-led): force LED off + disarm on disconnect

The low-battery blink would stay frozen mid-cycle (or briefly resume
during reconnect retries) after a controller died and lost connection,
because the stale-report check stopped toggling but never turned the
LED off, and the cached interrupt_in_data[52] still read low.

New `battery_led_on_disconnect()` clears blink state, forces the LED
off, and zeros last_report_us so the stale-check early-return blocks
any new blink until a fresh 0x31 report arrives on the next connection.
Called from bt.cpp's HCI_EVENT_DISCONNECTION_COMPLETE handler.

Stale-check in the tick also now forces LED off when it fires while a
blink was in progress (defense in depth for unclean disconnects).

Same bug exists in upstream awalol/DS5Dongle (their battery_led.cpp is
byte-identical to ours pre-fix) — Sura Academy reported it in their
Discord. Plan to send this back as a PR after we validate it locally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MarcelineVPQ
2026-05-17 11:19:55 -06:00
co-authored by Claude Opus 4.7
parent 9e6269d413
commit fb68ea5608
3 changed files with 34 additions and 2 deletions
+20 -2
View File
@@ -37,11 +37,29 @@ void battery_led_note_report(void) {
last_report_us = time_us_64();
}
void battery_led_on_disconnect(void) {
// Stop any in-progress blink and force the LED off immediately. Zero
// last_report_us so the tick's stale-check early-returns until a fresh
// 0x31 report arrives on the next connection — prevents the cached
// low-battery byte from re-arming a blink during reconnect retries.
blinking = false;
led_state = false;
last_report_us = 0;
last_toggle_us = 0;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false);
}
void battery_led_tick(void) {
const uint64_t now = time_us_64();
if (last_report_us == 0 || (now - last_report_us) >= REPORT_STALE_US) {
// No fresh data — bt.cpp owns the LED while disconnected.
blinking = false;
// No fresh data — bt.cpp owns the LED while disconnected. If we
// were mid-blink when the report went stale, force the LED off
// so it doesn't freeze in whichever half-cycle it was in.
if (blinking) {
blinking = false;
led_state = false;
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false);
}
return;
}