From 6d01f9aa891b0262fdf64547b3275ab5ce0f093a Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Wed, 20 Apr 2022 20:04:44 +0200 Subject: [PATCH] Add isChannelActive() function to radio interface --- src/mesh/RF95Interface.cpp | 19 +++++++++++++++++++ src/mesh/RF95Interface.h | 3 +++ src/mesh/SX126xInterface.cpp | 17 +++++++++++++++++ src/mesh/SX126xInterface.h | 3 +++ 4 files changed, 42 insertions(+) diff --git a/src/mesh/RF95Interface.cpp b/src/mesh/RF95Interface.cpp index adaeead46..a96b95100 100644 --- a/src/mesh/RF95Interface.cpp +++ b/src/mesh/RF95Interface.cpp @@ -176,6 +176,25 @@ void RF95Interface::startReceive() enableInterrupt(isrRxLevel0); } +bool RF95Interface::isChannelActive() +{ + // check if we can detect a LoRa preamble on the current channel + int16_t result; + setTransmitEnable(false); + setStandby(); // needed for smooth transition + result = lora->scanChannel(); + + if (result == PREAMBLE_DETECTED) { + // DEBUG_MSG("Channel is busy!\n"); + return true; + } + + assert(result != ERR_WRONG_MODEM); + + // DEBUG_MSG("Channel is free!\n"); + return false; +} + /** Could we send right now (i.e. either not actively receving or transmitting)? */ bool RF95Interface::isActivelyReceiving() { diff --git a/src/mesh/RF95Interface.h b/src/mesh/RF95Interface.h index f62195a26..5e666ae8b 100644 --- a/src/mesh/RF95Interface.h +++ b/src/mesh/RF95Interface.h @@ -40,6 +40,9 @@ class RF95Interface : public RadioLibInterface */ virtual void enableInterrupt(void (*callback)()) { lora->setDio0Action(callback); } + /** can we detect a LoRa preamble on the current channel? */ + virtual bool isChannelActive() override; + /** are we actively receiving a packet (only called during receiving state) */ virtual bool isActivelyReceiving() override; diff --git a/src/mesh/SX126xInterface.cpp b/src/mesh/SX126xInterface.cpp index b0c48d55a..ac96aa70b 100644 --- a/src/mesh/SX126xInterface.cpp +++ b/src/mesh/SX126xInterface.cpp @@ -226,6 +226,23 @@ void SX126xInterface::startReceive() #endif } +/** Could we send right now (i.e. either not actively receving or transmitting)? */ +template +bool SX126xInterface::isChannelActive() +{ + // check if we can detect a LoRa preamble on the current channel + int16_t result; + + setStandby(); + result = lora.scanChannel(); + if (result == PREAMBLE_DETECTED) + return true; + + assert(result != ERR_WRONG_MODEM); + + return false; +} + /** Could we send right now (i.e. either not actively receving or transmitting)? */ template bool SX126xInterface::isActivelyReceiving() diff --git a/src/mesh/SX126xInterface.h b/src/mesh/SX126xInterface.h index 5168313e2..b0e5d9a32 100644 --- a/src/mesh/SX126xInterface.h +++ b/src/mesh/SX126xInterface.h @@ -46,6 +46,9 @@ class SX126xInterface : public RadioLibInterface */ virtual void enableInterrupt(void (*callback)()) { lora.setDio1Action(callback); } + /** can we detect a LoRa preamble on the current channel? */ + virtual bool isChannelActive() override; + /** are we actively receiving a packet (only called during receiving state) */ virtual bool isActivelyReceiving() override;