battery level code switched to callback

This commit is contained in:
Klaus K Wilting 2020-04-14 10:57:30 +02:00
parent 0531a3fda8
commit 53be14d8a6
3 changed files with 8 additions and 12 deletions

View File

@ -33,7 +33,7 @@ void os_getDevEui(u1_t *buf);
void lora_send(void *pvParameters); void lora_send(void *pvParameters);
void lora_enqueuedata(MessageBuffer_t *message); void lora_enqueuedata(MessageBuffer_t *message);
void lora_queuereset(void); 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 myEventCallback(void *pUserData, ev_t ev);
void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, void IRAM_ATTR myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
size_t nMsg); size_t nMsg);
@ -41,7 +41,6 @@ void IRAM_ATTR myTxCallback(void *pUserData, int fSuccess);
const char *getSfName(rps_t rps); const char *getSfName(rps_t rps);
const char *getBwName(rps_t rps); const char *getBwName(rps_t rps);
const char *getCrName(rps_t rps); const char *getCrName(rps_t rps);
// u1_t os_getBattLevel(void);
#if (VERBOSE) #if (VERBOSE)

View File

@ -287,8 +287,7 @@ void lora_send(void *pvParameters) {
} }
void lora_stack_reset() { void lora_stack_reset() {
LMIC_reset(); // reset LMIC MAC LMIC_reset(); // reset LMIC MAC
read_battlevel(); // refresh battery value for LMIC MAC
} }
esp_err_t lora_stack_init(bool do_join) { 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_reset() doesn't affect callbacks, so we can do this first.
LMIC_registerRxMessageCb(myRxCallback, NULL); LMIC_registerRxMessageCb(myRxCallback, NULL);
LMIC_registerEventCb(myEventCallback, 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 // Reset the MAC state. Session and pending data transfers will be
// discarded. // discarded.
@ -470,7 +471,7 @@ void myEventCallback(void *pUserData, ev_t ev) {
ESP_LOGD(TAG, "%s", lmic_event_msg); 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 // set the battery value to send by LMIC in MAC Command
// DevStatusAns. Available defines in lorabase.h: // 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 // MCMD_DEVS_BATT_MAX from bat_percent value
uint8_t lmic_batt_level; uint8_t lmic_batt_level;
uint8_t const batt_percent = read_battlevel();
if (batt_percent == 0) if (batt_percent == 0)
lmic_batt_level = MCMD_DEVS_BATT_NOINFO; lmic_batt_level = MCMD_DEVS_BATT_NOINFO;
@ -499,7 +501,7 @@ void lora_setBattLevel(uint8_t batt_percent) {
lmic_batt_level = lmic_batt_level =
batt_percent / 100.0 * (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1); 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 // event EV_RXCOMPLETE message handler

View File

@ -214,8 +214,8 @@ uint8_t read_battlevel(mapFn_t mapFunction) {
// returns the estimated battery level in values 0 ... 100 [percent] // returns the estimated battery level in values 0 ... 100 [percent]
const uint16_t batt_voltage = read_voltage();
uint8_t batt_percent; uint8_t batt_percent;
const uint16_t batt_voltage = read_voltage();
if (batt_voltage <= BAT_MIN_VOLTAGE) if (batt_voltage <= BAT_MIN_VOLTAGE)
batt_percent = 0; 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, ESP_LOGD(TAG, "batt_voltage = %dmV / batt_percent = %d%%", batt_voltage,
batt_percent); batt_percent);
#if (HAS_LORA)
// to come with future LMIC version
lora_setBattLevel(batt_percent);
#endif
return batt_percent; return batt_percent;
} }