From bf2d32896f1a0305130eb25fb3f03936220ee2d5 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 11 Apr 2020 23:36:17 +0200 Subject: [PATCH] battery percentage display fixes --- include/power.h | 2 +- src/cyclic.cpp | 25 +++++++++++++++---------- src/display.cpp | 10 +++++++--- src/power.cpp | 19 ++++++++++++------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/include/power.h b/include/power.h index a5d1cfc9..f41fced0 100644 --- a/include/power.h +++ b/include/power.h @@ -12,7 +12,7 @@ #define NO_OF_SAMPLES 64 // we do some multisampling to get better values #ifndef BAT_MAX_VOLTAGE -#define BAT_MAX_VOLTAGE 4200 // millivolts +#define BAT_MAX_VOLTAGE 4300 // millivolts #endif #ifndef BAT_MIN_VOLTAGE #define BAT_MIN_VOLTAGE 3100 // millivolts diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 8c5aceaa..425b1210 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -67,10 +67,16 @@ void doHousekeeping() { // read battery voltage into global variable #if (defined BAT_MEASURE_ADC || defined HAS_PMU) batt_level = read_battlevel(); - if (batt_level == MCMD_DEVS_EXT_POWER) + switch (batt_level) { + case MCMD_DEVS_EXT_POWER: ESP_LOGI(TAG, "Battery: external power"); - else + break; + case MCMD_DEVS_BATT_NOINFO : + ESP_LOGI(TAG, "Battery: unknown state"); + break; + default: ESP_LOGI(TAG, "Battery: %d%%", batt_level); + } #ifdef HAS_PMU AXP192_showstatus(); #endif @@ -116,14 +122,13 @@ void doHousekeeping() { #endif #if (HAS_SDS011) - if ( isSDS011Active ) { - ESP_LOGD(TAG, "SDS011: go to sleep"); - sds011_loop(); - } - else { - ESP_LOGD(TAG, "SDS011: wakeup"); - sds011_wakeup(); - } + if (isSDS011Active) { + ESP_LOGD(TAG, "SDS011: go to sleep"); + sds011_loop(); + } else { + ESP_LOGD(TAG, "SDS011: wakeup"); + sds011_wakeup(); + } #endif } // doHousekeeping() diff --git a/src/display.cpp b/src/display.cpp index a630f51a..38266b4b 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -281,12 +281,16 @@ void dp_drawPage(time_t t, bool nextpage) { // line 4: Battery + GPS status + Wifi channel // B:a.bcV Sats:ab ch:ab #if (defined BAT_MEASURE_ADC || defined HAS_PMU) - if (batt_level == MCMD_DEVS_EXT_POWER) + switch (batt_level) { + case MCMD_DEVS_EXT_POWER: dp_printf("USB "); - else if (batt_level == MCMD_DEVS_BATT_NOINFO) + break; + case MCMD_DEVS_BATT_NOINFO: dp_printf("No batt "); - else + break; + default: dp_printf("B:%3d%% ", batt_level); + } #else dp_printf(" "); #endif diff --git a/src/power.cpp b/src/power.cpp index 092f786e..1a290764 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -224,18 +224,19 @@ 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 : 0; + const uint8_t batt_percent = batt_voltage > 0 + ? batt_voltage / batt_voltage_range * 100 + : MCMD_DEVS_BATT_NOINFO; uint8_t lmic_batt_level; #ifdef HAS_PMU - if (batt_percent > 0) + 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; #else - if (batt_percent > 0) + if ((batt_percent > 0) && (batt_percent != MCMD_DEVS_BATT_NOINFO)) lmic_batt_level = batt_percent / 100 * batt_level_range; else lmic_batt_level = MCMD_DEVS_BATT_NOINFO; @@ -243,7 +244,7 @@ uint8_t read_battlevel() { // set battery level value for lmic stack #if (HAS_LORA) - //LMIC_setBattLevel(lmic_batt_level); + // LMIC_setBattLevel(lmic_batt_level); #endif return batt_percent; @@ -252,10 +253,14 @@ uint8_t read_battlevel() { bool batt_sufficient() { #if (defined HAS_PMU || defined BAT_MEASURE_ADC) uint8_t my_batt_level = read_battlevel(); - if (my_batt_level == MCMD_DEVS_EXT_POWER) + switch (my_batt_level) { + case MCMD_DEVS_EXT_POWER: return true; - else + case MCMD_DEVS_BATT_NOINFO: + return true; + default: return (my_batt_level > OTA_MIN_BATT); + } #else return true; // we don't know batt level #endif