FIx battery quirks

This commit is contained in:
2026-03-19 22:38:04 +01:00
parent 395fd9b839
commit 1486fe13f2
2 changed files with 9 additions and 11 deletions
+3 -2
View File
@@ -36,14 +36,15 @@ void updateBattery() {
ChargeStatus status = chg ? (pct >= 99 ? CHGSTAT_FULL : CHGSTAT_CHARGING) : CHGSTAT_DISCHARGING; ChargeStatus status = chg ? (pct >= 99 ? CHGSTAT_FULL : CHGSTAT_CHARGING) : CHGSTAT_DISCHARGING;
// Only write BLE Battery Service when connected - blebas.write() blocks on the // 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. // 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; lastChargeStatus = status;
#ifdef FEATURE_TELEMETRY #ifdef FEATURE_TELEMETRY
telem.chargeStatus = (uint8_t)status; telem.chargeStatus = (uint8_t)status;
#endif #endif
const char* st[] = {"discharging","charging","full"}; const char* st[] = {"discharging","charging","full"};
Serial.print("[BATT] "); Serial.print(v,2); Serial.print("V "); 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. // Critical battery alert - only blink when not connected to avoid blocking BLE scheduler.
// 6 × 160ms = 960ms hard block; skip during active connection. // 6 × 160ms = 960ms hard block; skip during active connection.
if (status == CHGSTAT_DISCHARGING && v < BATT_CRITICAL && !Bluefruit.connected()) if (status == CHGSTAT_DISCHARGING && v < BATT_CRITICAL && !Bluefruit.connected())
+6 -9
View File
@@ -137,14 +137,11 @@ async function discoverServices() {
await checkHashMatch(); await checkHashMatch();
// Telemetry notify (1 Hz) - also carries chargeStatus // Telemetry notify (1 Hz) - also carries chargeStatus
chars.telemetry.addEventListener('characteristicvaluechanged', e => parseTelemetry(e.target.value)); chars.telemetry.addEventListener('characteristicvaluechanged', e => {
console.log('[TELEM] subscribing to notifications'); const val = e.target.value; // capture immediately — Chrome reuses the DataView buffer on next notify
parseTelemetry(val);
});
await chars.telemetry.startNotifications(); 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()); parseTelemetry(await chars.telemetry.readValue());
updateChargeUI(); updateChargeUI();
@@ -447,16 +444,16 @@ function parseTelemetry(dv) {
const tEl = document.getElementById('telTemp'); const tEl = document.getElementById('telTemp');
tEl.className = 'telem-val '+(temp>40?'warn':'accent'); tEl.className = 'telem-val '+(temp>40?'warn':'accent');
// chargeStatus is now delivered via telemetry (no separate characteristic)
if (chargeStatus !== currentChargeStatus) { if (chargeStatus !== currentChargeStatus) {
currentChargeStatus = chargeStatus; currentChargeStatus = chargeStatus;
console.log('[TELEM] charge status:', ['discharging','charging','full'][chargeStatus] ?? chargeStatus);
updateChargeUI(); updateChargeUI();
} }
if (battVoltage !== null) { if (battVoltage !== null) {
currentBattVoltage = battVoltage; currentBattVoltage = battVoltage;
document.getElementById('ciVolt').textContent = battVoltage.toFixed(2) + 'V'; 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) { function formatUptime(s) {