battery.h (1122B)
1 // battery.h — LiPo fuel gauge for the FireBeetle 2 ESP32-E. 2 // 3 // The board exposes battery voltage through an onboard 1:2 divider on GPIO34. 4 // This module reads it (factory-calibrated, via analogReadMilliVolts), smooths 5 // it with an EMA so a beacon-TX voltage dip doesn't make the gauge twitch, and 6 // maps it to a coarse 0–100% "charge tonight?" estimate. 7 // 8 // Mapping is LINEAR on voltage (VBAT_EMPTY_MV..VBAT_FULL_MV). LiPo discharge is 9 // actually nonlinear (flat in the middle), so this reads slightly pessimistic 10 // mid-range — the safe direction for a low-battery warning. 11 // 12 #pragma once 13 #include <stdint.h> 14 15 // Configure the ADC pin/attenuation. Call once from setup(). 16 void battery_init(); 17 18 // Take a throttled, EMA-smoothed sample. Call every loop(); it self-limits to 19 // one real reading per VBAT_SAMPLE_MS. 20 void battery_sample(uint32_t now); 21 22 // Latest smoothed battery voltage, in millivolts (already divider-corrected). 23 uint16_t battery_mv(); 24 25 // Smoothed charge estimate, 0..100. Clamped. 26 uint8_t battery_pct(); 27 28 // True once the smoothed voltage is at/below VBAT_LOW_MV. 29 bool battery_is_low();