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
#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

View File

@ -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()

View File

@ -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

View File

@ -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