Files
poorBird/firmware/src/battery.cpp
T

116 lines
3.7 KiB
C++

#include "battery.h"
#include "app_config.h"
#include "board_pins.h"
static BatteryReading s_last = {};
// Discharge curves, millivolts at 100/90/…/0 %. Both are open-circuit
// approximations at moderate load and room temperature; they are good enough
// to drive a "low battery" banner and the OTA gate, and not good enough to
// promise a runtime estimate.
static const uint16_t kLiIon[11] = {
4200, 4060, 3980, 3920, 3870, 3820, 3790, 3700, 3620, 3500, 3300
};
// LiFePO4 sits on a plateau near 3.2 V for most of its discharge. Percentage
// from voltage is genuinely unreliable here — treat anything between 20 % and
// 90 % as "somewhere in the middle" (SPEC §9 suggests this chemistry).
static const uint16_t kLiFePO4[11] = {
3650, 3350, 3320, 3300, 3280, 3260, 3240, 3220, 3200, 3100, 2800
};
static uint8_t pctFromCurve(const uint16_t* curve, uint16_t mv) {
if (mv >= curve[0]) return 100;
if (mv <= curve[10]) return 0;
for (int i = 0; i < 10; i++) {
if (mv <= curve[i] && mv > curve[i + 1]) {
const uint16_t hi = curve[i], lo = curve[i + 1];
const int top = 100 - i * 10;
return (uint8_t)(top - (10 * (hi - mv)) / (hi - lo));
}
}
return 0;
}
void batteryBegin() {
#if PIN_BATTERY_ADC >= 0
analogSetPinAttenuation(PIN_BATTERY_ADC, ADC_11db); // full ~0-3.1 V range
#if PIN_BATTERY_EN >= 0
pinMode(PIN_BATTERY_EN, OUTPUT);
digitalWrite(PIN_BATTERY_EN, LOW); // divider off by default
#endif
log_i("battery: sensing on GPIO%d, divider %.2f:1",
PIN_BATTERY_ADC, g_cfg.battery_divider_x100 / 100.0f);
batteryPoll();
#else
log_i("battery: no sense pin configured, voltage will report as invalid");
#endif
}
BatteryReading batteryRead() {
BatteryReading r = {};
#if PIN_BATTERY_ADC < 0
return r; // not wired; valid stays false
#else
if (g_cfg.battery_divider_x100 == 0) return r;
#if PIN_BATTERY_EN >= 0
digitalWrite(PIN_BATTERY_EN, HIGH);
delayMicroseconds(200); // let the divider settle
#endif
// Average several samples: a single ESP32 ADC read is noisy enough to move
// the reported percentage by a point or two between calls.
uint32_t acc = 0;
const uint8_t samples = 16;
for (uint8_t i = 0; i < samples; i++) acc += analogReadMilliVolts(PIN_BATTERY_ADC);
#if PIN_BATTERY_EN >= 0
digitalWrite(PIN_BATTERY_EN, LOW);
#endif
r.raw_mv = (uint16_t)(acc / samples);
int32_t mv = (int32_t)r.raw_mv * g_cfg.battery_divider_x100 / 100;
mv += g_cfg.battery_offset_mv;
if (mv < 0) mv = 0;
r.mv = (uint16_t)mv;
r.valid = true;
switch (g_cfg.battery_chemistry) {
case BATT_LIION: r.pct = pctFromCurve(kLiIon, r.mv); break;
case BATT_LIFEPO4: r.pct = pctFromCurve(kLiFePO4, r.mv); break;
default: r.pct = 0; break; // BATT_NONE: voltage only
}
return r;
#endif
}
void batteryPoll() {
BatteryReading r = batteryRead();
if (r.valid) s_last = r;
}
BatteryReading batteryLast() { return s_last; }
bool batteryAboveOtaThreshold(uint8_t min_pct) {
BatteryReading r = batteryRead();
// No sensing hardware, or no chemistry configured, means we cannot judge.
// Allow the flash: this build is mains-powered by default, and refusing
// every OTA because an optional sensor is absent would be worse than the
// risk it guards against. Wire the divider if you want the gate to bite.
if (!r.valid || g_cfg.battery_chemistry == BATT_NONE) {
log_w("battery: no reading, OTA battery gate not enforced");
return true;
}
if (r.pct < min_pct) {
log_w("battery: %u%% (%umV) is below the %u%% OTA threshold",
r.pct, r.mv, min_pct);
return false;
}
return true;
}