From 53be14d8a6e1408a2e05aa5eb4e63e3d5f3456e8 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 14 Apr 2020 10:57:30 +0200 Subject: [PATCH] battery level code switched to callback --- include/lorawan.h | 3 +-- src/lorawan.cpp | 10 ++++++---- src/power.cpp | 7 +------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index cba3de2f..3e54175d 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -33,7 +33,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); +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); @@ -41,7 +41,6 @@ void IRAM_ATTR myTxCallback(void *pUserData, int fSuccess); const char *getSfName(rps_t rps); const char *getBwName(rps_t rps); const char *getCrName(rps_t rps); -// u1_t os_getBattLevel(void); #if (VERBOSE) diff --git a/src/lorawan.cpp b/src/lorawan.cpp index acf14ec0..0d67570e 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -287,8 +287,7 @@ void lora_send(void *pvParameters) { } void lora_stack_reset() { - LMIC_reset(); // reset LMIC MAC - read_battlevel(); // refresh battery value for LMIC MAC + LMIC_reset(); // reset LMIC MAC } esp_err_t lora_stack_init(bool do_join) { @@ -392,6 +391,8 @@ void lmictask(void *pvParameters) { // LMIC_reset() doesn't affect callbacks, so we can do this first. 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. @@ -470,7 +471,7 @@ void myEventCallback(void *pUserData, ev_t ev) { ESP_LOGD(TAG, "%s", lmic_event_msg); } -void lora_setBattLevel(uint8_t batt_percent) { +uint8_t myBattLevelCb(void *pUserData) { // set the battery value to send by LMIC in MAC Command // DevStatusAns. Available defines in lorabase.h: @@ -482,6 +483,7 @@ void lora_setBattLevel(uint8_t batt_percent) { // MCMD_DEVS_BATT_MAX from bat_percent value uint8_t lmic_batt_level; + uint8_t const batt_percent = read_battlevel(); if (batt_percent == 0) lmic_batt_level = MCMD_DEVS_BATT_NOINFO; @@ -499,7 +501,7 @@ void lora_setBattLevel(uint8_t batt_percent) { lmic_batt_level = batt_percent / 100.0 * (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1); - //LMIC_setBattLevel(lmic_batt_level); + return lmic_batt_level; } // event EV_RXCOMPLETE message handler diff --git a/src/power.cpp b/src/power.cpp index 192c2255..bbae6157 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -214,8 +214,8 @@ uint8_t read_battlevel(mapFn_t mapFunction) { // returns the estimated battery level in values 0 ... 100 [percent] - const uint16_t batt_voltage = read_voltage(); uint8_t batt_percent; + const uint16_t batt_voltage = read_voltage(); if (batt_voltage <= BAT_MIN_VOLTAGE) batt_percent = 0; @@ -228,11 +228,6 @@ uint8_t read_battlevel(mapFn_t mapFunction) { ESP_LOGD(TAG, "batt_voltage = %dmV / batt_percent = %d%%", batt_voltage, batt_percent); -#if (HAS_LORA) - // to come with future LMIC version - lora_setBattLevel(batt_percent); -#endif - return batt_percent; }