battery percentage display fixes

This commit is contained in:
Klaus K Wilting 2020-04-11 23:36:17 +02:00
parent f4e8c67117
commit bf2d32896f
4 changed files with 35 additions and 21 deletions

View File

@ -12,7 +12,7 @@
#define NO_OF_SAMPLES 64 // we do some multisampling to get better values #define NO_OF_SAMPLES 64 // we do some multisampling to get better values
#ifndef BAT_MAX_VOLTAGE #ifndef BAT_MAX_VOLTAGE
#define BAT_MAX_VOLTAGE 4200 // millivolts #define BAT_MAX_VOLTAGE 4300 // millivolts
#endif #endif
#ifndef BAT_MIN_VOLTAGE #ifndef BAT_MIN_VOLTAGE
#define BAT_MIN_VOLTAGE 3100 // millivolts #define BAT_MIN_VOLTAGE 3100 // millivolts

View File

@ -67,10 +67,16 @@ void doHousekeeping() {
// read battery voltage into global variable // read battery voltage into global variable
#if (defined BAT_MEASURE_ADC || defined HAS_PMU) #if (defined BAT_MEASURE_ADC || defined HAS_PMU)
batt_level = read_battlevel(); 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"); 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); ESP_LOGI(TAG, "Battery: %d%%", batt_level);
}
#ifdef HAS_PMU #ifdef HAS_PMU
AXP192_showstatus(); AXP192_showstatus();
#endif #endif
@ -119,8 +125,7 @@ void doHousekeeping() {
if (isSDS011Active) { if (isSDS011Active) {
ESP_LOGD(TAG, "SDS011: go to sleep"); ESP_LOGD(TAG, "SDS011: go to sleep");
sds011_loop(); sds011_loop();
} } else {
else {
ESP_LOGD(TAG, "SDS011: wakeup"); ESP_LOGD(TAG, "SDS011: wakeup");
sds011_wakeup(); sds011_wakeup();
} }

View File

@ -281,12 +281,16 @@ void dp_drawPage(time_t t, bool nextpage) {
// line 4: Battery + GPS status + Wifi channel // line 4: Battery + GPS status + Wifi channel
// B:a.bcV Sats:ab ch:ab // B:a.bcV Sats:ab ch:ab
#if (defined BAT_MEASURE_ADC || defined HAS_PMU) #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 "); dp_printf("USB ");
else if (batt_level == MCMD_DEVS_BATT_NOINFO) break;
case MCMD_DEVS_BATT_NOINFO:
dp_printf("No batt "); dp_printf("No batt ");
else break;
default:
dp_printf("B:%3d%% ", batt_level); dp_printf("B:%3d%% ", batt_level);
}
#else #else
dp_printf(" "); dp_printf(" ");
#endif #endif

View File

@ -224,18 +224,19 @@ uint8_t read_battlevel() {
const uint16_t batt_voltage_range = BAT_MAX_VOLTAGE - BAT_MIN_VOLTAGE; 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 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 uint16_t batt_voltage = read_voltage() - BAT_MIN_VOLTAGE;
const uint8_t batt_percent = const uint8_t batt_percent = batt_voltage > 0
batt_voltage > 0 ? batt_voltage / batt_voltage_range * 100 : 0; ? batt_voltage / batt_voltage_range * 100
: MCMD_DEVS_BATT_NOINFO;
uint8_t lmic_batt_level; uint8_t lmic_batt_level;
#ifdef HAS_PMU #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 lmic_batt_level = pmu.isVBUSPlug() ? MCMD_DEVS_EXT_POWER
: batt_percent / 100 * batt_level_range; : batt_percent / 100 * batt_level_range;
else else
lmic_batt_level = MCMD_DEVS_BATT_NOINFO; lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
#else #else
if (batt_percent > 0) if ((batt_percent > 0) && (batt_percent != MCMD_DEVS_BATT_NOINFO))
lmic_batt_level = batt_percent / 100 * batt_level_range; lmic_batt_level = batt_percent / 100 * batt_level_range;
else else
lmic_batt_level = MCMD_DEVS_BATT_NOINFO; lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
@ -252,10 +253,14 @@ uint8_t read_battlevel() {
bool batt_sufficient() { bool batt_sufficient() {
#if (defined HAS_PMU || defined BAT_MEASURE_ADC) #if (defined HAS_PMU || defined BAT_MEASURE_ADC)
uint8_t my_batt_level = read_battlevel(); 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; return true;
else case MCMD_DEVS_BATT_NOINFO:
return true;
default:
return (my_batt_level > OTA_MIN_BATT); return (my_batt_level > OTA_MIN_BATT);
}
#else #else
return true; // we don't know batt level return true; // we don't know batt level
#endif #endif