battery percentage calculation improved
This commit is contained in:
1 parent
e40693ec88
commit
316517337a
4 files changed
+86
-17
No files matched your search
@@ -68,10 +68,6 @@ void doHousekeeping() {
|
||||
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
|
||||
batt_level = read_battlevel();
|
||||
ESP_LOGI(TAG, "Battery: %d%%", batt_level);
|
||||
#if (HAS_LORA)
|
||||
// to come with future LMIC version
|
||||
// lora_setBattLevel(batt_level);
|
||||
#endif
|
||||
#ifdef HAS_PMU
|
||||
AXP192_showstatus();
|
||||
#endif
|
||||
|
||||
+2
-4
@@ -485,12 +485,10 @@ void lora_setBattLevel(uint8_t batt_percent) {
|
||||
#endif // HAS_PMU
|
||||
|
||||
else
|
||||
lmic_batt_level = static_cast<uint8_t>(
|
||||
(float)batt_percent /
|
||||
(float)(MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1) * 100.0f);
|
||||
lmic_batt_level =
|
||||
batt_percent / 100.0 * (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1);
|
||||
|
||||
LMIC_setBattLevel(lmic_batt_level);
|
||||
ESP_LOGD(TAG, "lmic_batt_level = %d", lmic_batt_level);
|
||||
}
|
||||
|
||||
// event EV_RXCOMPLETE message handler
|
||||
|
||||
+17
-7
@@ -210,19 +210,29 @@ uint16_t read_voltage(void) {
|
||||
return voltage;
|
||||
}
|
||||
|
||||
uint8_t read_battlevel() {
|
||||
uint8_t read_battlevel(mapFn_t mapFunction) {
|
||||
|
||||
// return the battery level in values 0 ... 255 [percent],
|
||||
// values > 100 probably mean external power, depending on hardware
|
||||
// returns the estimated battery level in values 0 ... 100 [percent],
|
||||
|
||||
const uint16_t batt_voltage = read_voltage();
|
||||
float batt_percent_fl = (float)(batt_voltage - BAT_MIN_VOLTAGE) /
|
||||
(float)(BAT_MAX_VOLTAGE - BAT_MIN_VOLTAGE) * 100.0f;
|
||||
const uint8_t batt_percent = static_cast<uint8_t>(batt_percent_fl);
|
||||
uint8_t batt_percent;
|
||||
|
||||
ESP_LOGD(TAG, "batt_voltage = %dmV / batt_percent = %u%%", batt_voltage,
|
||||
if (batt_voltage <= BAT_MIN_VOLTAGE)
|
||||
batt_percent = 0;
|
||||
else if (batt_voltage >= BAT_MAX_VOLTAGE)
|
||||
batt_percent = 100;
|
||||
else
|
||||
batt_percent =
|
||||
(*mapFunction)(batt_voltage, BAT_MIN_VOLTAGE, BAT_MAX_VOLTAGE);
|
||||
|
||||
ESP_LOGD(TAG, "batt_voltage = %dmV / batt_percent = %d%%", batt_voltage,
|
||||
batt_percent);
|
||||
|
||||
#if (HAS_LORA)
|
||||
// to come with future LMIC version
|
||||
// lora_setBattLevel(batt_percent);
|
||||
#endif
|
||||
|
||||
return batt_percent;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user