From 1486fe13f2a59ee96bec744032d1ec62a656b79c Mon Sep 17 00:00:00 2001 From: Nik Rozman Date: Thu, 19 Mar 2026 22:38:04 +0100 Subject: [PATCH] FIx battery quirks --- source/battery.cpp | 5 +++-- web/app.js | 15 ++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/source/battery.cpp b/source/battery.cpp index 931507e..0b66d0b 100644 --- a/source/battery.cpp +++ b/source/battery.cpp @@ -36,14 +36,15 @@ void updateBattery() { ChargeStatus status = chg ? (pct >= 99 ? CHGSTAT_FULL : CHGSTAT_CHARGING) : CHGSTAT_DISCHARGING; // Only write BLE Battery Service when connected - blebas.write() blocks on the // SoftDevice ATT layer and causes 30-40ms loop stalls when called during advertising. - if (Bluefruit.connected()) blebas.write(pct); + if (Bluefruit.connected()) blebas.notify(pct); lastChargeStatus = status; #ifdef FEATURE_TELEMETRY telem.chargeStatus = (uint8_t)status; #endif const char* st[] = {"discharging","charging","full"}; Serial.print("[BATT] "); Serial.print(v,2); Serial.print("V "); - Serial.print(pct); Serial.print("% "); Serial.println(st[status]); + Serial.print(pct); Serial.print("% "); Serial.print(st[status]); + Serial.print(" (PIN_CHG="); Serial.print(digitalRead(PIN_CHG)); Serial.println(")"); // Critical battery alert - only blink when not connected to avoid blocking BLE scheduler. // 6 × 160ms = 960ms hard block; skip during active connection. if (status == CHGSTAT_DISCHARGING && v < BATT_CRITICAL && !Bluefruit.connected()) diff --git a/web/app.js b/web/app.js index 0f46706..77b16cd 100644 --- a/web/app.js +++ b/web/app.js @@ -137,14 +137,11 @@ async function discoverServices() { await checkHashMatch(); // Telemetry notify (1 Hz) - also carries chargeStatus - chars.telemetry.addEventListener('characteristicvaluechanged', e => parseTelemetry(e.target.value)); - console.log('[TELEM] subscribing to notifications'); + chars.telemetry.addEventListener('characteristicvaluechanged', e => { + const val = e.target.value; // capture immediately — Chrome reuses the DataView buffer on next notify + parseTelemetry(val); + }); await chars.telemetry.startNotifications(); - console.log('[TELEM] subscribed, reading initial value'); - // Initial read so values show immediately. Also force updateChargeUI() here - // because parseTelemetry() only calls it on a *change*, and currentChargeStatus - // starts at 0 (discharging) - so a discharging device would never trigger the - // update and ciStatus would stay at '--'. parseTelemetry(await chars.telemetry.readValue()); updateChargeUI(); @@ -447,16 +444,16 @@ function parseTelemetry(dv) { const tEl = document.getElementById('telTemp'); tEl.className = 'telem-val '+(temp>40?'warn':'accent'); - // chargeStatus is now delivered via telemetry (no separate characteristic) if (chargeStatus !== currentChargeStatus) { currentChargeStatus = chargeStatus; - console.log('[TELEM] charge status:', ['discharging','charging','full'][chargeStatus] ?? chargeStatus); updateChargeUI(); } if (battVoltage !== null) { currentBattVoltage = battVoltage; document.getElementById('ciVolt').textContent = battVoltage.toFixed(2) + 'V'; + const pct = Math.round(Math.min(100, Math.max(0, (battVoltage - 3.00) / (4.20 - 3.00) * 100))); + if (pct !== currentBattPct) { currentBattPct = pct; updateBatteryBar(pct, currentChargeStatus); } } } function formatUptime(s) {