battery level display fixes

This commit is contained in:
Klaus K Wilting 2020-04-12 22:12:13 +02:00
parent 9e52874d4b
commit 60391792d1
2 changed files with 10 additions and 8 deletions

View File

@ -12,10 +12,10 @@
#define NO_OF_SAMPLES 64 // we do some multisampling to get better values
#ifndef BAT_MAX_VOLTAGE
#define BAT_MAX_VOLTAGE 4300 // millivolts
#define BAT_MAX_VOLTAGE 4200 // millivolts
#endif
#ifndef BAT_MIN_VOLTAGE
#define BAT_MIN_VOLTAGE 3200 // millivolts
#define BAT_MIN_VOLTAGE 2800 // millivolts
#endif
uint16_t read_voltage(void);

View File

@ -224,18 +224,20 @@ 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 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;
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);
ESP_LOGD(TAG, "batt_voltage = %dmV / batt_level = %u%%",
batt_voltage + BAT_MIN_VOLTAGE, batt_percent);
if (batt_percent != MCMD_DEVS_BATT_NOINFO)
#ifdef HAS_PMU
lmic_batt_level = pmu.isVBUSPlug() ? MCMD_DEVS_EXT_POWER
: (float)batt_percent / (float)batt_level_range * 100.0;
: (float)batt_percent /
(float)batt_level_range * 100.0;
#else
lmic_batt_level = (float)batt_percent / (float)batt_level_range * 100.0;
#endif // HAS_PMU