Battery percentage display

This commit is contained in:
Klaus K Wilting 2020-04-11 21:30:09 +02:00
parent 90a004ec79
commit f4e8c67117
6 changed files with 76 additions and 53 deletions

View File

@ -123,8 +123,8 @@ extern std::array<uint64_t, 0xff> beacons;
extern configData_t cfg; // current device configuration
extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer
extern uint8_t volatile channel; // wifi channel rotation counter
extern uint16_t volatile macs_total, macs_wifi, macs_ble,
batt_voltage; // display values
extern uint8_t batt_level; // display value
extern uint16_t volatile macs_total, macs_wifi, macs_ble; // display values
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern timesource_t timeSource;
extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ;

View File

@ -12,10 +12,14 @@
#define NO_OF_SAMPLES 64 // we do some multisampling to get better values
#ifndef BAT_MAX_VOLTAGE
#define BAT_MAX_VOLTAGE 4100 // millivolts
#define BAT_MAX_VOLTAGE 4200 // millivolts
#endif
#ifndef BAT_MIN_VOLTAGE
#define BAT_MIN_VOLTAGE 3100 // millivolts
#endif
uint16_t read_voltage(void);
uint8_t read_battlevel(void);
void calibrate_voltage(void);
bool batt_sufficient(void);

View File

@ -28,7 +28,7 @@ void doHousekeeping() {
if (batt_sufficient()) {
do_reset(true); // warmstart to runmode update
} else {
ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage);
ESP_LOGE(TAG, "Battery level %d%% is too low for OTA", batt_level);
RTC_runmode = RUNMODE_NORMAL; // keep running in normal mode
}
}
@ -66,11 +66,11 @@ void doHousekeeping() {
// read battery voltage into global variable
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
batt_voltage = read_voltage();
if (batt_voltage == 0xffff)
batt_level = read_battlevel();
if (batt_level == MCMD_DEVS_EXT_POWER)
ESP_LOGI(TAG, "Battery: external power");
else
ESP_LOGI(TAG, "Battery: %dmV", batt_voltage);
ESP_LOGI(TAG, "Battery: %d%%", batt_level);
#ifdef HAS_PMU
AXP192_showstatus();
#endif

View File

@ -281,12 +281,12 @@ 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_voltage == 0xffff)
if (batt_level == MCMD_DEVS_EXT_POWER)
dp_printf("USB ");
else if (batt_voltage == 0)
else if (batt_level == MCMD_DEVS_BATT_NOINFO)
dp_printf("No batt ");
else
dp_printf("B:%.2fV ", batt_voltage / 1000.0);
dp_printf("B:%3d%% ", batt_level);
#else
dp_printf(" ");
#endif

View File

@ -79,8 +79,9 @@ triggers pps 1 sec impulse
configData_t cfg; // struct holds current device configuration
char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message
uint8_t volatile channel = 0; // channel rotation counter
uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0,
batt_voltage = 0; // globals for display
uint8_t batt_level = 0; // display value
uint16_t volatile macs_total = 0, macs_wifi = 0,
macs_ble = 0; // globals for display
hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL;
@ -260,7 +261,7 @@ void setup() {
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
strcat_P(features, " BATT");
calibrate_voltage();
batt_voltage = read_voltage();
batt_level = read_battlevel();
#endif
#if (USE_OTA)
@ -328,9 +329,9 @@ void setup() {
#endif
#if (HAS_SDS011)
ESP_LOGI(TAG, "init fine-dust-sensor");
if ( sds011_init() )
strcat_P(features, " SDS");
ESP_LOGI(TAG, "init fine-dust-sensor");
if (sds011_init())
strcat_P(features, " SDS");
#endif
#if (VENDORFILTER)

View File

@ -63,7 +63,7 @@ void AXP192_powerevent_IRQ(void) {
pmu.clearIRQ();
// refresh stored voltage value
read_voltage();
read_battlevel();
}
void AXP192_power(pmu_power_t powerlevel) {
@ -175,21 +175,11 @@ void calibrate_voltage(void) {
#endif
}
bool batt_sufficient() {
#if (defined HAS_PMU || defined BAT_MEASURE_ADC)
uint16_t volts = read_voltage();
return ((volts < 1000) ||
(volts > OTA_MIN_BATT)); // no battery or battery sufficient
#else
return true;
#endif
}
uint16_t read_voltage() {
uint16_t read_voltage(void) {
uint16_t voltage = 0;
#ifdef HAS_PMU
voltage = pmu.isVBUSPlug() ? 0xffff : pmu.getBattVoltage();
voltage = pmu.getBattVoltage();
#else
#ifdef BAT_MEASURE_ADC
@ -217,28 +207,56 @@ uint16_t read_voltage() {
#endif // HAS_PMU
/*
// set battery level value for lmic stack
#if (HAS_LORA)
// Sets the battery level returned in MAC Command DevStatusAns.
// Available defines in lorabase.h:
// MCMD_DEVS_EXT_POWER = 0x00, // external power supply
// MCMD_DEVS_BATT_MIN = 0x01, // min battery value
// MCMD_DEVS_BATT_MAX = 0xFE, // max battery value
// MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level
// When setting the battery level calculate the applicable
// value from MCMD_DEVS_BATT_MIN to MCMD_DEVS_BATT_MAX.
// external power
if (voltage == 0xffff)
LMIC_setBattLevel(MCMD_DEVS_EXT_POWER);
// scale battery millivolts to lmic battlevel
else
LMIC_setBattLevel(voltage / BAT_MAX_VOLTAGE *
(MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1));
#endif // (HAS_LORA)
*/
return voltage;
}
}
uint8_t read_battlevel() {
// return the battery value as sent in MAC Command
// DevStatusAns. Available defines in lorabase.h:
// MCMD_DEVS_EXT_POWER = 0x00, // external power supply
// MCMD_DEVS_BATT_MIN = 0x01, // min battery value
// MCMD_DEVS_BATT_MAX = 0xFE, // max battery value
// MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level
// we calculate the applicable value from MCMD_DEVS_BATT_MIN to
// MCMD_DEVS_BATT_MAX from bat_percent value
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;
uint8_t lmic_batt_level;
#ifdef HAS_PMU
if (batt_percent > 0)
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)
lmic_batt_level = batt_percent / 100 * batt_level_range;
else
lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
#endif // HAS_PMU
// set battery level value for lmic stack
#if (HAS_LORA)
//LMIC_setBattLevel(lmic_batt_level);
#endif
return batt_percent;
}
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)
return true;
else
return (my_batt_level > OTA_MIN_BATT);
#else
return true; // we don't know batt level
#endif
}