From 4a1499b2bb82669dbb41935a80bce19151900ef4 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 12 Apr 2020 17:33:13 +0200 Subject: [PATCH] battery percentage display fixes --- include/power.h | 2 +- src/display.cpp | 2 +- src/power.cpp | 25 ++++++++++++------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/include/power.h b/include/power.h index f41fced0..a1033041 100644 --- a/include/power.h +++ b/include/power.h @@ -15,7 +15,7 @@ #define BAT_MAX_VOLTAGE 4300 // millivolts #endif #ifndef BAT_MIN_VOLTAGE -#define BAT_MIN_VOLTAGE 3100 // millivolts +#define BAT_MIN_VOLTAGE 3200 // millivolts #endif uint16_t read_voltage(void); diff --git a/src/display.cpp b/src/display.cpp index 38266b4b..764da7fc 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -283,7 +283,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if (defined BAT_MEASURE_ADC || defined HAS_PMU) switch (batt_level) { case MCMD_DEVS_EXT_POWER: - dp_printf("USB "); + dp_printf("ext.Pwr "); break; case MCMD_DEVS_BATT_NOINFO: dp_printf("No batt "); diff --git a/src/power.cpp b/src/power.cpp index 1a290764..4ab413a1 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -223,24 +223,24 @@ uint8_t read_battlevel() { const uint16_t batt_voltage_range = BAT_MAX_VOLTAGE - BAT_MIN_VOLTAGE; const uint8_t batt_level_range = MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1; - const uint16_t batt_voltage = read_voltage() - BAT_MIN_VOLTAGE; - const uint8_t batt_percent = batt_voltage > 0 - ? batt_voltage / batt_voltage_range * 100 + const int batt_voltage = read_voltage() - BAT_MIN_VOLTAGE; + const uint8_t batt_percent = (batt_voltage > 0) + ? (float)batt_voltage / (float)batt_voltage_range * 100.0 : MCMD_DEVS_BATT_NOINFO; uint8_t lmic_batt_level; + ESP_LOGD(TAG, "batt_voltage = %d mV / batt_level = %u%%", batt_voltage, + batt_percent); + + if (batt_percent != MCMD_DEVS_BATT_NOINFO) #ifdef HAS_PMU - if ((batt_percent > 0) && (batt_percent != MCMD_DEVS_BATT_NOINFO)) lmic_batt_level = pmu.isVBUSPlug() ? MCMD_DEVS_EXT_POWER - : batt_percent / 100 * batt_level_range; - else - lmic_batt_level = MCMD_DEVS_BATT_NOINFO; + : (float)batt_percent / (float)batt_level_range * 100.0; #else - if ((batt_percent > 0) && (batt_percent != MCMD_DEVS_BATT_NOINFO)) - lmic_batt_level = batt_percent / 100 * batt_level_range; + lmic_batt_level = (float)batt_percent / (float)batt_level_range * 100.0; +#endif // HAS_PMU else lmic_batt_level = MCMD_DEVS_BATT_NOINFO; -#endif // HAS_PMU // set battery level value for lmic stack #if (HAS_LORA) @@ -252,14 +252,13 @@ uint8_t read_battlevel() { bool batt_sufficient() { #if (defined HAS_PMU || defined BAT_MEASURE_ADC) - uint8_t my_batt_level = read_battlevel(); - switch (my_batt_level) { + switch (batt_level) { case MCMD_DEVS_EXT_POWER: return true; case MCMD_DEVS_BATT_NOINFO: return true; default: - return (my_batt_level > OTA_MIN_BATT); + return (batt_level > OTA_MIN_BATT); } #else return true; // we don't know batt level