diff --git a/include/lorawan.h b/include/lorawan.h index 0804efae..394284be 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -36,7 +36,6 @@ void lora_send(void *pvParameters); void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); uint32_t lora_queuewaiting(void); -uint8_t myBattLevelCb(void *pUserData); 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/lorawan.cpp b/src/lorawan.cpp index f9c4db4b..32f7f1f7 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -262,7 +262,6 @@ esp_err_t lmic_init(void) { LMIC_registerRxMessageCb(myRxCallback, NULL); LMIC_registerEventCb(myEventCallback, NULL); // to come with future LMIC version - // LMIC_registerBattLevelCb(myBattLevelCb, NULL); // Reset the MAC state. Session and pending data transfers will be // discarded. @@ -409,34 +408,6 @@ void myEventCallback(void *pUserData, ev_t ev) { ESP_LOGD(TAG, "%s", lmic_event_msg); } -uint8_t myBattLevelCb(void *pUserData) { - - // 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 const batt_percent = read_battlevel(); - - if (batt_percent == 0) - return MCMD_DEVS_BATT_NOINFO; - else - -#ifdef HAS_PMU - if (pmu.isVBUSPlug()) - return MCMD_DEVS_EXT_POWER; -#elif defined HAS_IP5306 - if (IP5306_GetPowerSource()) - return MCMD_DEVS_EXT_POWER; -#endif // HAS_PMU - - return (batt_percent / 100.0 * (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1)); -} - // 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 e918a350..d9c4cccf 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -258,17 +258,48 @@ uint16_t read_voltage(void) { uint8_t read_battlevel(mapFn_t mapFunction) { // returns the estimated battery level in values 0 ... 100 [percent] + uint8_t batt_percent = 0; #ifdef HAS_IP5306 - return IP5306_GetBatteryLevel(); + batt_percent = IP5306_GetBatteryLevel(); #else const uint16_t batt_voltage = read_voltage(); if (batt_voltage <= BAT_MIN_VOLTAGE) - return 0; + batt_percent = 0; else if (batt_voltage >= BAT_MAX_VOLTAGE) - return 100; + batt_percent = 100; else - return (*mapFunction)(batt_voltage, BAT_MIN_VOLTAGE, BAT_MAX_VOLTAGE); + batt_percent = + (*mapFunction)(batt_voltage, BAT_MIN_VOLTAGE, BAT_MAX_VOLTAGE); #endif + +#if (HAS_LORA) + // set the battery status 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 batt_percent value + + if (batt_percent == 0) + LMIC_setBatteryLevel(MCMD_DEVS_BATT_NOINFO); + else + LMIC_setBatteryLevel(batt_percent / 100.0 * + (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1)); + +// overwrite calculated value if we have external power +#ifdef HAS_PMU + if (pmu.isVBUSPlug()) + LMIC_setBatteryLevel(MCMD_DEVS_EXT_POWER); +#elif defined HAS_IP5306 + if (IP5306_GetPowerSource()) + LMIC_setBatteryLevel(MCMD_DEVS_EXT_POWER); +#endif // HAS_PMU + +#endif // HAS_LORA + + return batt_percent; } bool batt_sufficient() {