From d9c61535597bcc1583068a232998f3d514d526f4 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Fri, 20 Sep 2019 09:04:24 +0200 Subject: [PATCH] bugfix getBattLevel() (issue #444) --- include/lorawan.h | 1 + include/power.h | 2 +- src/lorawan.cpp | 26 ++++++++++++++++++++++++++ src/ota.cpp | 2 +- src/power.cpp | 30 +++++++----------------------- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index 74a67c04..5c5c67cb 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -49,6 +49,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, void myTxCallback(void *pUserData, int fSuccess); void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], const uint8_t tablesize); +uint8_t getBattLevel(void); #if (TIME_SYNC_LORAWAN) void user_request_network_time_callback(void *pVoidUserUTCTime, diff --git a/include/power.h b/include/power.h index 3622f643..c27daa6c 100644 --- a/include/power.h +++ b/include/power.h @@ -19,6 +19,6 @@ void AXP192_init(void); void AXP192_displaypower(void); uint16_t read_voltage(void); void calibrate_voltage(void); -uint8_t getBattLevel(void); +bool batt_sufficient(void); #endif \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index bfcd7c75..bd5284f8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -666,4 +666,30 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], } // mac_decode() +uint8_t getBattLevel() { + /* + return values: + 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 + */ +#if (defined HAS_PMU || defined BAT_MEASURE_ADC) + uint16_t voltage = read_voltage(); + + switch (voltage) { + case 0: + return MCMD_DEVS_BATT_NOINFO; + case 0xffff: + return MCMD_DEVS_EXT_POWER; + default: + return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN); + } +#else // we don't have any info on battery level + return MCMD_DEVS_BATT_NOINFO; +#endif +} // getBattLevel() + +//u1_t os_getBattLevel(void) { return getBattLevel(); }; + #endif // HAS_LORA \ No newline at end of file diff --git a/src/ota.cpp b/src/ota.cpp index 1a1e9052..18eaa84e 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -41,7 +41,7 @@ inline String getHeaderValue(String header, String headerName) { void start_ota_update() { // check battery status if we can before doing ota - if (getBattLevel() == MCMD_DEVS_BATT_MIN) { + if (!batt_sufficient()) { ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage); return; } diff --git a/src/power.cpp b/src/power.cpp index 2cb0e1ff..877fc953 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -167,31 +167,15 @@ void calibrate_voltage(void) { #endif } -uint8_t getBattLevel() { - /* - return values: - 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 - */ +bool batt_sufficient() { #if (defined HAS_PMU || defined BAT_MEASURE_ADC) - uint16_t voltage = read_voltage(); - - switch (voltage) { - case 0: - return MCMD_DEVS_BATT_NOINFO; - case 0xffff: - return MCMD_DEVS_EXT_POWER; - default: - return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN); - } -#else // we don't have any info on battery level - return MCMD_DEVS_BATT_NOINFO; + uint16_t volts = read_voltage(); + return ((volts < 1000) || + (volts > OTA_MIN_BATT)); // no battery or battery sufficient +#else + return true; #endif -} // getBattLevel() - -// u1_t os_getBattLevel(void) { return getBattLevel(); }; +} uint16_t read_voltage() {