diff --git a/include/lorawan.h b/include/lorawan.h index 91305048..a731edfe 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -32,6 +32,7 @@ void os_getDevEui(u1_t *buf); void lora_send(void *pvParameters); void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); +void lora_setBattLevel(uint8_t batt_percent); void IRAM_ATTR myEventCallback(void *pUserData, ev_t ev); void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, size_t nMsg); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 425b1210..92d98acf 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -67,16 +67,11 @@ void doHousekeeping() { // read battery voltage into global variable #if (defined BAT_MEASURE_ADC || defined HAS_PMU) batt_level = read_battlevel(); - switch (batt_level) { - case MCMD_DEVS_EXT_POWER: - ESP_LOGI(TAG, "Battery: external power"); - 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); +#if (HAS_LORA) + // to come with future LMIC version + // lora_setBattLevel(batt_level); +#endif #ifdef HAS_PMU AXP192_showstatus(); #endif diff --git a/src/display.cpp b/src/display.cpp index 764da7fc..f340e6c9 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -281,16 +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) - switch (batt_level) { - case MCMD_DEVS_EXT_POWER: - dp_printf("ext.Pwr "); - break; - case MCMD_DEVS_BATT_NOINFO: + if (batt_level == 0) dp_printf("No batt "); - break; - default: + else if (batt_level < 100) dp_printf("B:%3d%% ", batt_level); - } + else + dp_printf("ext.Pwr "); #else dp_printf(" "); #endif diff --git a/src/lorawan.cpp b/src/lorawan.cpp index fd0d5fb8..6a033580 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -459,6 +459,40 @@ void myEventCallback(void *pUserData, ev_t ev) { ESP_LOGD(TAG, "%s", lmic_event_msg); } +void lora_setBattLevel(uint8_t batt_percent) { + + // set the battery value to send by LMIC 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 + + uint8_t lmic_batt_level; + + if (batt_percent == 0) + lmic_batt_level = MCMD_DEVS_BATT_NOINFO; + else + +#ifdef HAS_PMU + if (pmu.isVBUSPlug()) + lmic_batt_level = MCMD_DEVS_EXT_POWER; +#else + if (batt_percent > 100) + lmic_batt_level = MCMD_DEVS_EXT_POWER; +#endif // HAS_PMU + + else + lmic_batt_level = static_cast( + (float)batt_percent / + (float)(MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1) * 100.0f); + + LMIC_setBattLevel(lmic_batt_level); + ESP_LOGD(TAG, "lmic_batt_level = %d", lmic_batt_level); +} + // event EV_RXCOMPLETE message handler void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, size_t nMsg) { diff --git a/src/power.cpp b/src/power.cpp index 4d1920c5..47e4fb0a 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -212,56 +212,23 @@ uint16_t read_voltage(void) { 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 + // return the battery level in values 0 ... 255 [percent], + // values > 100 probably mean external power, depending on hardware - 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; - uint8_t lmic_batt_level; + const uint16_t batt_voltage = read_voltage(); + float batt_percent_fl = (float)(batt_voltage - BAT_MIN_VOLTAGE) / + (float)(BAT_MAX_VOLTAGE - BAT_MIN_VOLTAGE) * 100.0f; + const uint8_t batt_percent = static_cast(batt_percent_fl); - 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; -#else - lmic_batt_level = (float)batt_percent / (float)batt_level_range * 100.0; -#endif // HAS_PMU - else - lmic_batt_level = MCMD_DEVS_BATT_NOINFO; - -// set battery level value for lmic stack -#if (HAS_LORA) - // LMIC_setBattLevel(lmic_batt_level); -#endif + ESP_LOGD(TAG, "batt_voltage = %dmV / batt_percent = %u%%", batt_voltage, + batt_percent); return batt_percent; } bool batt_sufficient() { #if (defined HAS_PMU || defined BAT_MEASURE_ADC) - switch (batt_level) { - case MCMD_DEVS_EXT_POWER: - return true; - case MCMD_DEVS_BATT_NOINFO: - return true; - default: - return (batt_level > OTA_MIN_BATT); - } + return (batt_level > OTA_MIN_BATT); #else return true; // we don't know batt level #endif