From 842c54849da573e041192db5bb2032f32f7e9ac3 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 13 Jan 2024 13:39:51 +0100 Subject: [PATCH] [SX127x] Added setLowBatteryThreshold (#925) --- keywords.txt | 1 + src/modules/SX127x/SX127x.cpp | 22 ++++++++++++++++++++++ src/modules/SX127x/SX127x.h | 26 ++++++++++++++++++-------- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/keywords.txt b/keywords.txt index 065991a09..c68cc5bd0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -182,6 +182,7 @@ setFifoFullAction KEYWORD2 clearFifoFullAction KEYWORD2 fifoAdd KEYWORD2 fifoGet KEYWORD2 +setLowBatteryThreshold KEYWORD2 # RF69-specific setAESKey KEYWORD2 diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 38953a145..cdf68ad32 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1714,4 +1714,26 @@ float SX127x::getRSSI(bool packet, bool skipReceive, int16_t offset) { } } +int16_t SX127x::setLowBatteryThreshold(int8_t level, uint32_t pin) { + // check disable + if(level < 0) { + return(this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LOW_BAT, RADIOLIB_SX127X_LOW_BAT_OFF, 3, 3)); + } + + // enable detector and set the threshold + int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_LOW_BAT, RADIOLIB_SX127X_LOW_BAT_ON | level, 3, 0); + RADIOLIB_ASSERT(state); + + // set DIO mapping + switch(pin) { + case(0): + return(this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_1, RADIOLIB_SX127X_DIO0_PACK_TEMP_CHANGE_LOW_BAT, 7, 6)); + case(3): + return(this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_1, RADIOLIB_SX127X_DIO3_CONT_TEMP_CHANGE_LOW_BAT, 1, 0)); + case(4): + return(this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_2, RADIOLIB_SX127X_DIO4_PACK_TEMP_CHANGE_LOW_BAT, 7, 6)); + } + return(RADIOLIB_ERR_INVALID_DIO_PIN); +} + #endif diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index bf09b925c..fd3d4a94a 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -472,14 +472,14 @@ // RADIOLIB_SX127X_REG_LOW_BAT #define RADIOLIB_SX127X_LOW_BAT_OFF 0b00000000 // 3 3 low battery detector disabled #define RADIOLIB_SX127X_LOW_BAT_ON 0b00001000 // 3 3 low battery detector enabled -#define RADIOLIB_SX127X_LOW_BAT_TRIM_1_695_V 0b00000000 // 2 0 battery voltage threshold: 1.695 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_1_764_V 0b00000001 // 2 0 1.764 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_1_835_V 0b00000010 // 2 0 1.835 V (default) -#define RADIOLIB_SX127X_LOW_BAT_TRIM_1_905_V 0b00000011 // 2 0 1.905 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_1_976_V 0b00000100 // 2 0 1.976 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_2_045_V 0b00000101 // 2 0 2.045 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_2_116_V 0b00000110 // 2 0 2.116 V -#define RADIOLIB_SX127X_LOW_BAT_TRIM_2_185_V 0b00000111 // 2 0 2.185 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_1_695_V 0b00000000 // 2 0 battery voltage threshold: 1.695 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_1_764_V 0b00000001 // 2 0 1.764 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_1_835_V 0b00000010 // 2 0 1.835 V (default) +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_1_905_V 0b00000011 // 2 0 1.905 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_1_976_V 0b00000100 // 2 0 1.976 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_2_045_V 0b00000101 // 2 0 2.045 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_2_116_V 0b00000110 // 2 0 2.116 V +#define RADIOLIB_SX127X_LOW_BAT_THRESHOLD_2_185_V 0b00000111 // 2 0 2.185 V // RADIOLIB_SX127X_REG_IRQ_FLAGS_1 #define RADIOLIB_SX127X_FLAG_MODE_READY 0b10000000 // 7 7 requested mode is ready @@ -1216,6 +1216,16 @@ class SX127x: public PhysicalLayer { */ int16_t setRSSIThreshold(float dbm); + /*! + \brief Set low battery indicator threshold. + \param level Battery threshold level (one of RADIOLIB_SX127X_LOW_BAT_THRESHOLD_*), + or -1 to disable the detector. Disabled by default. Note that this will not attach any interrupts! + \param pin DIO pin number which will be used to signal low battery. Only DIO0/4 can be used + (in packet mode) or DIO3/4 (in continuous mode). Ignored when disabling the detector. + \returns \ref status_codes + */ + int16_t setLowBatteryThreshold(int8_t level, uint32_t pin = RADIOLIB_NC); + #if !RADIOLIB_GODMODE && !RADIOLIB_LOW_LEVEL protected: #endif