From 90a004ec7958a87f5f21e9f30b1e1b76dee9c8c2 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 10 Apr 2020 23:26:29 +0200 Subject: [PATCH] prepare LMIC battlevel MAC request --- include/power.h | 4 ++++ src/lmic_config.h | 3 +++ src/lorawan.cpp | 26 -------------------------- src/power.cpp | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/include/power.h b/include/power.h index 5e0ee363..dbe3cfca 100644 --- a/include/power.h +++ b/include/power.h @@ -11,6 +11,10 @@ #define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate #define NO_OF_SAMPLES 64 // we do some multisampling to get better values +#ifndef BAT_MAX_VOLTAGE +#define BAT_MAX_VOLTAGE 4100 // millivolts +#endif + uint16_t read_voltage(void); void calibrate_voltage(void); bool batt_sufficient(void); diff --git a/src/lmic_config.h b/src/lmic_config.h index 9a955dc1..00df54d5 100644 --- a/src/lmic_config.h +++ b/src/lmic_config.h @@ -104,3 +104,6 @@ #define USE_IDEETRON_AES // //#define USE_MBEDTLS_AES + +// Define this for devices with external power. +//#define LMIC_MCMD_DEVS_BATT_DEFAULT MCMD_DEVS_EXT_POWER \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 6178580d..fd0d5fb8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -538,32 +538,6 @@ const char *getCrName(rps_t rps) { return t[getCr(rps)]; } -/* -u1_t os_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() -*/ - #if (VERBOSE) // decode LORAWAN MAC message void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, bool is_down) { diff --git a/src/power.cpp b/src/power.cpp index 80d961cd..6fdaaac6 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -217,5 +217,28 @@ uint16_t read_voltage() { #endif // HAS_PMU + /* + // set battery level value for lmic stack + #if (HAS_LORA) + // Sets the battery level returned 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 + // When setting the battery level calculate the applicable + // value from MCMD_DEVS_BATT_MIN to MCMD_DEVS_BATT_MAX. + + // external power + if (voltage == 0xffff) + LMIC_setBattLevel(MCMD_DEVS_EXT_POWER); + // scale battery millivolts to lmic battlevel + else + LMIC_setBattLevel(voltage / BAT_MAX_VOLTAGE * + (MCMD_DEVS_BATT_MAX - MCMD_DEVS_BATT_MIN + 1)); + +#endif // (HAS_LORA) +*/ + return voltage; } \ No newline at end of file