From 9e50470a51e7b4470f2507170098d53fe386e768 Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Sat, 26 Sep 2026 09:14:58 +0200 Subject: [PATCH] fix(wio-tracker-l2): keep the last good battery reading when the ADC read fails A failed ADS1115 read returned 0 mV, so the status bar showed 0 % (and the low-battery shutdown could see an empty battery) until a reboot. Retry once, then keep the last good value. Co-Authored-By: Claude Opus 5.5 --- variants/wio-tracker-l2/WioTrackerL2Board.cpp | 15 +++++++++++---- variants/wio-tracker-l2/WioTrackerL2Board.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/variants/wio-tracker-l2/WioTrackerL2Board.cpp b/variants/wio-tracker-l2/WioTrackerL2Board.cpp index edeaacce..ecdc25a7 100644 --- a/variants/wio-tracker-l2/WioTrackerL2Board.cpp +++ b/variants/wio-tracker-l2/WioTrackerL2Board.cpp @@ -194,11 +194,18 @@ int16_t WioTrackerL2Board::adsReadRaw() { return raw < 0 ? 0 : raw; } +// A failed or zero ADS1115 read (the I2C bus busy or the ADC not answering) +// keeps the last good value instead of reporting 0 mV -- that read as 0 % in +// the status bar and could trip the low-battery shutdown. Retried once first. uint16_t WioTrackerL2Board::getBattMilliVolts() { if (!expander_ok) return 0; // BAT_ADC_EN rail never came up - int16_t raw = adsReadRaw(); - if (raw < 0) return 0; - - return (uint16_t)(raw * ADS_MV_PER_LSB); + for (int attempt = 0; attempt < 2; attempt++) { + int16_t raw = adsReadRaw(); + if (raw > 0) { + batt_mv_last = (uint16_t)(raw * ADS_MV_PER_LSB); + return batt_mv_last; + } + } + return batt_mv_last; } diff --git a/variants/wio-tracker-l2/WioTrackerL2Board.h b/variants/wio-tracker-l2/WioTrackerL2Board.h index c5644e2f..47ce561f 100644 --- a/variants/wio-tracker-l2/WioTrackerL2Board.h +++ b/variants/wio-tracker-l2/WioTrackerL2Board.h @@ -93,6 +93,7 @@ private: bool expander_ok = false; bool aw_ok = false; // AW35615 USB-C controller responded at probe uint8_t wake_btn_baseline = 0; // idle level of P00, captured at init + uint16_t batt_mv_last = 0; // last good battery reading (a failed read keeps it) int expReadInputs(); // 16-bit input register pair, -1 on error