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 configData_t cfg; // current device configuration
extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer
extern uint8_t volatile channel; // wifi channel rotation counter extern uint8_t volatile channel; // wifi channel rotation counter
extern uint16_t volatile macs_total, macs_wifi, macs_ble, extern uint8_t batt_level; // display value
batt_voltage; // display values 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 bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern timesource_t timeSource; extern timesource_t timeSource;
extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ; 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 #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 4100 // millivolts #define BAT_MAX_VOLTAGE 4200 // millivolts
#endif
#ifndef BAT_MIN_VOLTAGE
#define BAT_MIN_VOLTAGE 3100 // millivolts
#endif #endif
uint16_t read_voltage(void); uint16_t read_voltage(void);
uint8_t read_battlevel(void);
void calibrate_voltage(void); void calibrate_voltage(void);
bool batt_sufficient(void); bool batt_sufficient(void);

View File

@ -28,7 +28,7 @@ void doHousekeeping() {
if (batt_sufficient()) { if (batt_sufficient()) {
do_reset(true); // warmstart to runmode update do_reset(true); // warmstart to runmode update
} else { } 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 RTC_runmode = RUNMODE_NORMAL; // keep running in normal mode
} }
} }
@ -66,11 +66,11 @@ 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_voltage = read_voltage(); batt_level = read_battlevel();
if (batt_voltage == 0xffff) if (batt_level == MCMD_DEVS_EXT_POWER)
ESP_LOGI(TAG, "Battery: external power"); ESP_LOGI(TAG, "Battery: external power");
else else
ESP_LOGI(TAG, "Battery: %dmV", batt_voltage); ESP_LOGI(TAG, "Battery: %d%%", batt_level);
#ifdef HAS_PMU #ifdef HAS_PMU
AXP192_showstatus(); AXP192_showstatus();
#endif #endif

View File

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

View File

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

View File

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