feat(battery): round bat_v and derive bat_pct locally

This commit is contained in:
2026-01-20 22:58:56 +01:00
parent cfbab84f97
commit 8c9520f7e5
6 changed files with 53 additions and 16 deletions

View File

@@ -32,14 +32,18 @@ void read_battery(MeterData &data) {
float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL;
data.battery_voltage_v = v;
float pct = (v - 3.0f) / (4.2f - 3.0f) * 100.0f;
data.battery_percent = battery_percent_from_voltage(v);
}
uint8_t battery_percent_from_voltage(float voltage_v) {
float pct = (voltage_v - 3.0f) / (4.2f - 3.0f) * 100.0f;
if (pct < 0.0f) {
pct = 0.0f;
}
if (pct > 100.0f) {
pct = 100.0f;
}
data.battery_percent = static_cast<uint8_t>(pct + 0.5f);
return static_cast<uint8_t>(pct + 0.5f);
}
void go_to_deep_sleep(uint32_t seconds) {