ESP: first implementation; compiles fine; not tested with MCU
This commit is contained in:
1 parent
5bdcb51bff
commit
6bda641ce8
36 files changed
+6234
No files matched your search
@@ -0,0 +1,47 @@
|
||||
// battery.h — supply / battery voltage sensing.
|
||||
//
|
||||
// WIRING (nothing is wired by default; reads report invalid until it is)
|
||||
// The XIAO ESP32S3 does not bring the battery rail out to an ADC pin you can
|
||||
// simply read — the documented approaches involve soldering, and the divider
|
||||
// differs between board revisions. So this driver assumes an *external*
|
||||
// divider you add yourself:
|
||||
//
|
||||
// BAT+ ──[ R1 ]──┬──[ R2 ]── GND
|
||||
// └── PIN_BATTERY_ADC (default D3 / GPIO4)
|
||||
//
|
||||
// Two 1 MΩ resistors give a 2.00:1 ratio and ~2 µA of continuous drain, which
|
||||
// is negligible on mains and tolerable on battery. If you care about the last
|
||||
// microamp, gate the low side with a MOSFET on PIN_BATTERY_EN — SPEC §9 is
|
||||
// right that an always-connected divider is a permanent load.
|
||||
//
|
||||
// Set `battery_divider_x100` to your actual ratio: measure the real resistors
|
||||
// rather than trusting their printed value, then trim with
|
||||
// `battery_offset_mv` against a multimeter. The ESP32 ADC is not linear and
|
||||
// `analogReadMilliVolts()` only corrects for the part of that Espressif
|
||||
// characterised at the factory.
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct BatteryReading {
|
||||
bool valid; // false when no divider is configured or the read failed
|
||||
uint16_t mv; // battery terminal voltage in millivolts
|
||||
uint8_t pct; // 0..100, 0 when chemistry is BATT_NONE
|
||||
uint16_t raw_mv; // at the pin, before the divider ratio is applied
|
||||
};
|
||||
|
||||
void batteryBegin();
|
||||
|
||||
// Averaged, calibrated read. Powers the divider for the measurement if
|
||||
// PIN_BATTERY_EN is wired, then turns it off again.
|
||||
BatteryReading batteryRead();
|
||||
|
||||
// Last reading without touching the ADC. Cheap; use this in status payloads
|
||||
// that are built more often than the voltage can meaningfully change.
|
||||
BatteryReading batteryLast();
|
||||
|
||||
// Refresh the cached reading. Called on a timer from the main loop.
|
||||
void batteryPoll();
|
||||
|
||||
// SPEC C6 gate, registered with otaSetBatteryCheck().
|
||||
bool batteryAboveOtaThreshold(uint8_t min_pct);
|
||||
Reference in new issue
Block a user