This commit is contained in:
MickiusMousius 2026-01-29 13:01:51 -06:00 committed by GitHub
commit da3f257edd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 9 deletions

View File

@ -29,15 +29,37 @@ int battery_channel_get(const struct battery_value *value, enum sensor_channel c
return 0;
}
uint8_t lithium_ion_mv_to_pct(int16_t bat_mv) {
// Simple linear approximation of a battery based off adafruit's discharge graph:
// https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages
uint8_t lithium_ion_mv_to_pct(int16_t batt_mv) {
// Lookup table of slope formulas for calculating remaining battery capacity.
//
// The current set of values acheives the same as: bat_mv * 2 / 15 - 459;
//
// Future updates sould include more complete lookup tables for different
// battery types slectable through config flags.
struct lookup_point {
int16_t millivolts;
int16_t percent;
};
if (bat_mv >= 4200) {
return 100;
} else if (bat_mv <= 3450) {
return 0;
static const struct lookup_point battery_lookup[] = {
{.millivolts = 4200, .percent = 100},
{.millivolts = 3450, .percent = 0},
};
if (batt_mv > battery_lookup[0].millivolts) {
return battery_lookup[0].percent;
}
return bat_mv * 2 / 15 - 459;
}
for (int i = 1; i < ARRAY_SIZE(battery_lookup); i++) {
struct lookup_point one = battery_lookup[i - 1];
struct lookup_point two = battery_lookup[i];
if (batt_mv >= two.millivolts) {
const int t = batt_mv - one.millivolts;
const int dx = two.millivolts - one.millivolts;
const int dy = two.percent - one.percent;
return one.percent + dy * t / dx;
}
}
return battery_lookup[ARRAY_SIZE(battery_lookup) - 1].percent;
}