From fd3b165bf0a13ef3c3adf17757e6eca40802eac5 Mon Sep 17 00:00:00 2001 From: OBones Date: Tue, 23 Aug 2022 17:41:37 +0200 Subject: [PATCH 1/4] Introduce setRSSIThreshold on RF69 modules --- src/TypeDef.h | 5 +++++ src/modules/RF69/RF69.cpp | 7 +++++++ src/modules/RF69/RF69.h | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/src/TypeDef.h b/src/TypeDef.h index 0ac5d2ef..49a29f78 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -213,6 +213,11 @@ */ #define RADIOLIB_ERR_INVALID_DIO_PIN (-26) +/*! + \brief The supplied RSSI threshold is invalid. +*/ +#define RADIOLIB_ERR_INVALID_RSSI_THRESHOLD (-27) + // RF69-specific status codes /*! diff --git a/src/modules/RF69/RF69.cpp b/src/modules/RF69/RF69.cpp index 9fc69ef2..db39ff70 100644 --- a/src/modules/RF69/RF69.cpp +++ b/src/modules/RF69/RF69.cpp @@ -872,6 +872,13 @@ float RF69::getRSSI() { return(-1.0 * (_mod->SPIgetRegValue(RADIOLIB_RF69_REG_RSSI_VALUE)/2.0)); } +int16_t RF69::setRSSIThreshold(float value) { + if (value < 127.5 || value > 0) + return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; + + return _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RSSI_THRESH, (uint8_t)(-2.0 * value), 7, 0); +} + void RF69::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) { _mod->setRfSwitchPins(rxEn, txEn); } diff --git a/src/modules/RF69/RF69.h b/src/modules/RF69/RF69.h index ef9969e2..cb81cc4e 100644 --- a/src/modules/RF69/RF69.h +++ b/src/modules/RF69/RF69.h @@ -961,6 +961,15 @@ class RF69: public PhysicalLayer { */ float getRSSI(); + /*! + \brief Sets the RSSI value above which the RSSI interrupt is signaled + + \param value A value between -127.5 and 0 inclusive + + \returns \ref status_codes + */ + int16_t setRSSIThreshold(float value); + /*! \brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state. When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch! From 9a76aa1c841203a1a528e13b47e69ebd1bc25ddd Mon Sep 17 00:00:00 2001 From: obones Date: Wed, 24 Aug 2022 15:05:20 +0200 Subject: [PATCH 2/4] Introduce setRSSIThreshold on SX127x modules --- src/modules/SX127x/SX127x.cpp | 7 +++++++ src/modules/SX127x/SX127x.h | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 29980071..2b2be9c2 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1184,6 +1184,13 @@ int16_t SX127x::setCrcFiltering(bool crcOn) { } } +int16_t SX127x::setRSSIThreshold(float value) { + if (value < 127.5 || value > 0) + return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; + + return _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_THRESH, (uint8_t)(-2.0 * value), 7, 0); +} + int16_t SX127x::setRSSIConfig(uint8_t smoothingSamples, int8_t offset) { // check active modem if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) { diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index 996776c9..b322df57 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -1227,6 +1227,15 @@ class SX127x: public PhysicalLayer { */ int16_t setDIOPreambleDetect(bool usePreambleDetect); + /*! + \brief Sets the RSSI value above which the RSSI interrupt is signaled + + \param value A value between -127.5 and 0 inclusive + + \returns \ref status_codes + */ + int16_t setRSSIThreshold(float value); + #if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL) protected: #endif From a7b42b61b23546f6455c1cac75c619c2fc4b80d9 Mon Sep 17 00:00:00 2001 From: obones Date: Wed, 24 Aug 2022 15:38:12 +0200 Subject: [PATCH 3/4] Use a better name for the parameter so that it is clear that it's dBm that are expected here --- src/modules/RF69/RF69.cpp | 6 +++--- src/modules/RF69/RF69.h | 4 ++-- src/modules/SX127x/SX127x.cpp | 6 +++--- src/modules/SX127x/SX127x.h | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/modules/RF69/RF69.cpp b/src/modules/RF69/RF69.cpp index db39ff70..25818a4f 100644 --- a/src/modules/RF69/RF69.cpp +++ b/src/modules/RF69/RF69.cpp @@ -872,11 +872,11 @@ float RF69::getRSSI() { return(-1.0 * (_mod->SPIgetRegValue(RADIOLIB_RF69_REG_RSSI_VALUE)/2.0)); } -int16_t RF69::setRSSIThreshold(float value) { - if (value < 127.5 || value > 0) +int16_t RF69::setRSSIThreshold(float dbm) { + if (dbm < 127.5 || dbm > 0) return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; - return _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RSSI_THRESH, (uint8_t)(-2.0 * value), 7, 0); + return _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RSSI_THRESH, (uint8_t)(-2.0 * dbm), 7, 0); } void RF69::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) { diff --git a/src/modules/RF69/RF69.h b/src/modules/RF69/RF69.h index cb81cc4e..e1d3acf7 100644 --- a/src/modules/RF69/RF69.h +++ b/src/modules/RF69/RF69.h @@ -964,11 +964,11 @@ class RF69: public PhysicalLayer { /*! \brief Sets the RSSI value above which the RSSI interrupt is signaled - \param value A value between -127.5 and 0 inclusive + \param dbm A dBm value between -127.5 and 0 inclusive \returns \ref status_codes */ - int16_t setRSSIThreshold(float value); + int16_t setRSSIThreshold(float dbm); /*! \brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state. diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 2b2be9c2..1adf22b8 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1184,11 +1184,11 @@ int16_t SX127x::setCrcFiltering(bool crcOn) { } } -int16_t SX127x::setRSSIThreshold(float value) { - if (value < 127.5 || value > 0) +int16_t SX127x::setRSSIThreshold(float dbm) { + if (dbm < 127.5 || dbm > 0) return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; - return _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_THRESH, (uint8_t)(-2.0 * value), 7, 0); + return _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_THRESH, (uint8_t)(-2.0 * dbm), 7, 0); } int16_t SX127x::setRSSIConfig(uint8_t smoothingSamples, int8_t offset) { diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index b322df57..87be55e6 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -1230,11 +1230,11 @@ class SX127x: public PhysicalLayer { /*! \brief Sets the RSSI value above which the RSSI interrupt is signaled - \param value A value between -127.5 and 0 inclusive + \param dbm A dBm value between -127.5 and 0 inclusive \returns \ref status_codes */ - int16_t setRSSIThreshold(float value); + int16_t setRSSIThreshold(float dbm); #if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL) protected: From 40599baef1c70db2d992bb6a6394f822af3ac658 Mon Sep 17 00:00:00 2001 From: obones Date: Thu, 25 Aug 2022 10:39:21 +0200 Subject: [PATCH 4/4] Use the RADIOLIB_CHECK_RANGE macro along with proper lower value (it has to be negative) --- src/modules/RF69/RF69.cpp | 3 +-- src/modules/SX127x/SX127x.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/modules/RF69/RF69.cpp b/src/modules/RF69/RF69.cpp index 25818a4f..9867b576 100644 --- a/src/modules/RF69/RF69.cpp +++ b/src/modules/RF69/RF69.cpp @@ -873,8 +873,7 @@ float RF69::getRSSI() { } int16_t RF69::setRSSIThreshold(float dbm) { - if (dbm < 127.5 || dbm > 0) - return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; + RADIOLIB_CHECK_RANGE(dbm, -127.5, 0, RADIOLIB_ERR_INVALID_RSSI_THRESHOLD); return _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RSSI_THRESH, (uint8_t)(-2.0 * dbm), 7, 0); } diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 1adf22b8..6734b1b5 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1185,8 +1185,7 @@ int16_t SX127x::setCrcFiltering(bool crcOn) { } int16_t SX127x::setRSSIThreshold(float dbm) { - if (dbm < 127.5 || dbm > 0) - return RADIOLIB_ERR_INVALID_RSSI_THRESHOLD; + RADIOLIB_CHECK_RANGE(dbm, -127.5, 0, RADIOLIB_ERR_INVALID_RSSI_THRESHOLD); return _mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_THRESH, (uint8_t)(-2.0 * dbm), 7, 0); }