feat: battery monitoring, external RTC persistence, SD used-space fix

- Add BQ27220 fuel gauge: reads SOC percentage every 5s
- Add BQ25896 PMIC: detects charging status (pre-charge/fast/done)
- Add MAX31329 external RTC: reads time on boot, syncs from NTP
- Battery UI: icon + percentage in header, cyan when charging
- Fix SD used bytes: fallback to recursive dir walk when usedBytes() returns 0
- Libraries: PMIC_BQ25896, kode_bq27220, kode_MAX31329
This commit is contained in:
Lago
2026-04-03 16:25:55 +02:00
parent 44b6a89b57
commit b31e80a71b
4 changed files with 216 additions and 0 deletions
+149
View File
@@ -9,6 +9,7 @@
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <lvgl.h>
#include <kodedot/display_manager.h>
#include <kodedot/pin_config.h>
@@ -19,6 +20,9 @@
#include <ArduinoJson.h>
#include <SimpleFTPServer.h>
#include <time.h>
#include <PMIC_BQ25896.h>
#include <BQ27220.h>
#include <kode_MAX31329.h>
#include "ui.h"
// ---------------------------------------------------------------------------
@@ -37,6 +41,15 @@ static Adafruit_NeoPixel pixel(NEO_PIXEL_COUNT, NEO_PIXEL_PIN, NEO_GRB + NEO_KHZ
// FTP server
FtpServer ftpSrv;
// Battery fuel gauge (BQ27220 @ 0x55)
static BQ27220 fuelGauge;
// PMIC / Charger (BQ25896 @ 0x6B)
static PMIC_BQ25896 pmic;
// External RTC (MAX31329 @ 0xD0)
static MAX31329 rtc;
// ---------------------------------------------------------------------------
// LED colour definitions
// ---------------------------------------------------------------------------
@@ -53,7 +66,11 @@ static bool sd_ok = false;
static bool wifi_connected = false;
static bool ftp_client_on = false;
static bool ftp_transfer = false;
static bool rtc_ok = false;
static bool gauge_ok = false;
static bool pmic_ok = false;
static unsigned long last_time_update = 0;
static unsigned long last_batt_update = 0;
// ---------------------------------------------------------------------------
// NeoPixel helpers
@@ -84,10 +101,30 @@ static void updateLed() {
// ---------------------------------------------------------------------------
/** Get used / total space on SD card in MiB and push to UI */
static uint64_t calcDirSize(fs::FS &fs, const char *dirname) {
uint64_t total = 0;
File root = fs.open(dirname);
if (!root || !root.isDirectory()) return 0;
File f = root.openNextFile();
while (f) {
if (f.isDirectory()) {
total += calcDirSize(fs, f.path());
} else {
total += f.size();
}
f = root.openNextFile();
}
return total;
}
static void updateSdInfo() {
if (!sd_ok) return;
uint64_t total = SD_MMC.totalBytes() / (1024ULL * 1024ULL);
uint64_t used = SD_MMC.usedBytes() / (1024ULL * 1024ULL);
/* SD_MMC.usedBytes() returns 0 on exFAT / large cards — walk filesystem */
if (used == 0 && total > 0) {
used = calcDirSize(SD_MMC, "/") / (1024ULL * 1024ULL);
}
ui_set_sd(used, total);
}
@@ -157,6 +194,36 @@ void ftpTransferCallback(FtpTransferOperation ftpOperation, const char *name, ui
updateLed();
}
// ---------------------------------------------------------------------------
// Battery monitoring
// ---------------------------------------------------------------------------
/** Update battery percentage and charging status every 5 seconds */
static void updateBattery() {
unsigned long now = millis();
if (now - last_batt_update < 5000) return;
last_batt_update = now;
int soc = -1;
bool charging = false;
/* Read state-of-charge from fuel gauge */
if (gauge_ok) {
soc = fuelGauge.readStateOfChargePercent();
if (soc < 0 || soc > 100) soc = -1; // sanity check
}
/* Read charging status from PMIC */
if (pmic_ok) {
pmic.setCONV_START(true); // trigger ADC conversion
uint8_t chrg_stat = pmic.get_VBUS_STAT_reg().chrg_stat;
// chrg_stat: 0=Not Charging, 1=Pre-charge, 2=Fast Charging, 3=Charge Done
charging = (chrg_stat == 1 || chrg_stat == 2);
}
ui_set_battery(soc, charging);
}
// ---------------------------------------------------------------------------
// Time display
// ---------------------------------------------------------------------------
@@ -269,6 +336,82 @@ static bool initWiFi() {
return false;
}
/** Initialise I2C bus and battery / RTC peripherals */
static void initPeripherals() {
Wire.begin(48, 47); // SDA=48, SCL=47
/* ── Fuel Gauge (BQ27220 @ 0x55) ──────────────────────────── */
gauge_ok = fuelGauge.begin();
if (gauge_ok) {
int soc = fuelGauge.readStateOfChargePercent();
int mv = fuelGauge.readVoltageMillivolts();
Serial.printf("[BAT] Fuel gauge ready — SOC: %d%% Voltage: %d mV\n", soc, mv);
} else {
Serial.println("[BAT] Warning: BQ27220 fuel gauge not found");
}
/* ── PMIC (BQ25896 @ 0x6B) ───────────────────────────────── */
pmic.begin();
pmic_ok = pmic.isConnected();
if (pmic_ok) {
pmic.setCONV_START(true); // trigger first ADC conversion
Serial.printf("[BAT] PMIC ready — VBUS: %d mV VBAT: %d mV\n",
pmic.getVBUSV(), pmic.getBATV());
} else {
Serial.println("[BAT] Warning: BQ25896 PMIC not found");
}
/* ── External RTC (MAX31329 @ 0xD0) ──────────────────────── */
rtc.begin();
/* Try reading time — if year is valid (>2020), set ESP32 system clock */
if (rtc.readTime()) {
if (rtc.t.year >= 2020 && rtc.t.year <= 2099) {
rtc_ok = true;
struct tm t = {};
t.tm_year = rtc.t.year - 1900;
t.tm_mon = rtc.t.month - 1;
t.tm_mday = rtc.t.day;
t.tm_hour = rtc.t.hour;
t.tm_min = rtc.t.minute;
t.tm_sec = rtc.t.second;
struct timeval tv = { .tv_sec = mktime(&t), .tv_usec = 0 };
settimeofday(&tv, NULL);
Serial.printf("[RTC] External RTC time: %04d-%02d-%02d %02d:%02d:%02d\n",
rtc.t.year, rtc.t.month, rtc.t.day,
rtc.t.hour, rtc.t.minute, rtc.t.second);
} else {
Serial.printf("[RTC] External RTC has invalid year (%d) — will set from NTP\n", rtc.t.year);
}
} else {
Serial.println("[RTC] Warning: MAX31329 external RTC not responding");
}
}
/** Write current system time to external RTC (call after NTP sync) */
static void syncRtcFromSystem() {
if (!rtc_ok && !gauge_ok) return; // RTC not available
struct tm timeinfo;
if (!getLocalTime(&timeinfo, 0)) return;
rtc.t.year = timeinfo.tm_year + 1900;
rtc.t.month = timeinfo.tm_mon + 1;
rtc.t.day = timeinfo.tm_mday;
rtc.t.hour = timeinfo.tm_hour;
rtc.t.minute = timeinfo.tm_min;
rtc.t.second = timeinfo.tm_sec;
rtc.t.dayOfWeek = timeinfo.tm_wday;
if (rtc.writeTime()) {
rtc_ok = true;
Serial.printf("[RTC] Updated external RTC: %04d-%02d-%02d %02d:%02d:%02d\n",
rtc.t.year, rtc.t.month, rtc.t.day,
rtc.t.hour, rtc.t.minute, rtc.t.second);
} else {
Serial.println("[RTC] Warning: Failed to write time to external RTC");
}
}
/** Configure NTP with Vienna timezone and wait briefly for sync */
static void initNtp() {
Serial.println("[NTP] Configuring time (Vienna CET/CEST)...");
@@ -280,6 +423,8 @@ static void initNtp() {
char buf[32];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &timeinfo);
Serial.printf("[NTP] Time synced: %s\n", buf);
/* Write NTP time to external RTC for persistence */
syncRtcFromSystem();
return;
}
}
@@ -336,6 +481,9 @@ void setup() {
// Create the LVGL status UI
ui_init();
// Initialise I2C peripherals (battery, RTC)
initPeripherals();
// IO Expander — check SD card presence
if (!checkSdPresence()) {
Serial.println("[SD] WARNING: SD card not detected by IO expander");
@@ -388,5 +536,6 @@ void loop() {
ftpSrv.handleFTP();
display.update();
updateTimeDisplay();
updateBattery();
delay(5);
}