diff --git a/lib/BoardFinder/BoardFinder.cpp b/lib/BoardFinder/BoardFinder.cpp index 5e250382..efb16917 100644 --- a/lib/BoardFinder/BoardFinder.cpp +++ b/lib/BoardFinder/BoardFinder.cpp @@ -7,7 +7,7 @@ OledPins::OledPins(int8_t sda, int8_t scl, int8_t reset, int8_t addr) : Sda(sda), Scl(scl), Reset(reset), Addr(addr) { } -LoraPins::LoraPins(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t reset, int8_t irq) : Sck(sck), Miso(miso), Mosi(mosi), CS(cs), Reset(reset), IRQ(irq) { +LoraPins::LoraPins(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t reset, int8_t irq, LoraModem modem) : Sck(sck), Miso(miso), Mosi(mosi), CS(cs), Reset(reset), IRQ(irq), Modem(modem) { } GpsPins::GpsPins(int8_t rx, int8_t tx) : Rx(rx), Tx(tx) { @@ -30,6 +30,7 @@ BoardConfig const *BoardFinder::searchBoardConfig(logging::Logger &logger) { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "searching for OLED..."); for (BoardConfig const *boardconf : _boardConfigs) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "trying board config: %s", boardconf->Name.c_str()); if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf, logger) == boardconf->powerCheckStatus) { PowerManagement powerManagement; Wire.begin(boardconf->Oled.Sda, boardconf->Oled.Scl); @@ -48,6 +49,7 @@ BoardConfig const *BoardFinder::searchBoardConfig(logging::Logger &logger) { logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "could not find OLED, will search for the modem now..."); for (BoardConfig const *boardconf : _boardConfigs) { + logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, MODULE_NAME, "trying board config: %s", boardconf->Name.c_str()); if (boardconf->needCheckPowerChip && checkPowerConfig(boardconf, logger) == boardconf->powerCheckStatus) { PowerManagement powerManagement; Wire.begin(boardconf->Oled.Sda, boardconf->Oled.Scl); @@ -148,16 +150,16 @@ bool BoardFinder::checkPowerConfig(BoardConfig const *boardConfig, logging::Logg } // clang-format off -BoardConfig TTGO_LORA32_V1 ("TTGO_LORA32_V1", eTTGO_LORA32_V1, OledPins( 4, 15), LoraPins( 5, 19, 27, 18, 14, 26)); -BoardConfig TTGO_LORA32_V2 ("TTGO_LORA32_V2", eTTGO_LORA32_V2, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26)); -BoardConfig TTGO_T_Beam_V0_7 ("TTGO_T_Beam_V0_7", eTTGO_T_Beam_V0_7, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26), GpsPins(15, 12), EthernetPins(), ButtonPins(38), true); -BoardConfig TTGO_T_Beam_V1_0 ("TTGO_T_Beam_V1_0", eTTGO_T_Beam_V1_0, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26), GpsPins(12, 34), EthernetPins(), ButtonPins(38), true, true); -BoardConfig LILYGO_POE_ETH_BOARD ("LILYGO_POE_ETH_BOARD", eLILYGO_POE_ETH_BOARD, OledPins(33, 32), LoraPins(14, 2, 15, 12, 4, 36), GpsPins(), EthernetPins(23, 18, 5, 0, -1, ETH_CLOCK_GPIO17_OUT, ETH_PHY_LAN8720)); -BoardConfig WT32_ETH_BOARD ("WT32_ETH_BOARD", eWT32_ETH_BOARD, OledPins(17, 5), LoraPins( 4, 12, 14, 15, 33, 32), GpsPins(), EthernetPins(23, 18, -1, 1, 16, ETH_CLOCK_GPIO0_IN, ETH_PHY_LAN8720)); -BoardConfig TRACKERD ("TRACKERD", eTRACKERD, OledPins( 5, 4), LoraPins(18, 19, 23, 16, 14, 26)); -BoardConfig HELTEC_WIFI_LORA_32_V1 ("HELTEC_WIFI_LORA_32_V1", eHELTEC_WIFI_LORA_32_V1, OledPins( 4, 15, 16), LoraPins( 5, 19, 27, 18, 14, 26)); -BoardConfig HELTEC_WIFI_LORA_32_V2 ("HELTEC_WIFI_LORA_32_V2", eHELTEC_WIFI_LORA_32_V2, OledPins( 4, 15, 16), LoraPins( 5, 19, 27, 18, 14, 26)); -BoardConfig HELTEC_WIFI_LORA_32_V3 ("HELTEC_WIFI_LORA_32_V3", eHELTEC_WIFI_LORA_32_V3, OledPins( 17, 18, 21), LoraPins( 9, 11, 10, 8, 12, 13)); -BoardConfig GUALTHERIUS_LORAHAM_v100("GUALTHERIUS_LORAHAM_v100", eGUALTHERIUS_LORAHAM_v100, OledPins(17, 16), LoraPins(18, 19, 23, 5, 13, 35)); -BoardConfig GUALTHERIUS_LORAHAM_v106("GUALTHERIUS_LORAHAM_v106", eGUALTHERIUS_LORAHAM_v106, OledPins(17, 16), LoraPins(18, 19, 23, 2, 13, 35)); +BoardConfig TTGO_LORA32_V1 ("TTGO_LORA32_V1", eTTGO_LORA32_V1, OledPins( 4, 15), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278)); +BoardConfig TTGO_LORA32_V2 ("TTGO_LORA32_V2", eTTGO_LORA32_V2, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278)); +BoardConfig TTGO_T_Beam_V0_7 ("TTGO_T_Beam_V0_7", eTTGO_T_Beam_V0_7, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278), GpsPins(15, 12), EthernetPins(), ButtonPins(38), true); +BoardConfig TTGO_T_Beam_V1_0 ("TTGO_T_Beam_V1_0", eTTGO_T_Beam_V1_0, OledPins(21, 22), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278), GpsPins(12, 34), EthernetPins(), ButtonPins(38), true, true); +BoardConfig LILYGO_POE_ETH_BOARD ("LILYGO_POE_ETH_BOARD", eLILYGO_POE_ETH_BOARD, OledPins(33, 32), LoraPins(14, 2, 15, 12, 4, 36, eSX1278), GpsPins(), EthernetPins(23, 18, 5, 0, -1, ETH_CLOCK_GPIO17_OUT, ETH_PHY_LAN8720)); +BoardConfig WT32_ETH_BOARD ("WT32_ETH_BOARD", eWT32_ETH_BOARD, OledPins(17, 5), LoraPins( 4, 12, 14, 15, 33, 32, eSX1278), GpsPins(), EthernetPins(23, 18, -1, 1, 16, ETH_CLOCK_GPIO0_IN, ETH_PHY_LAN8720)); +BoardConfig TRACKERD ("TRACKERD", eTRACKERD, OledPins( 5, 4), LoraPins(18, 19, 23, 16, 14, 26, eSX1278)); +BoardConfig HELTEC_WIFI_LORA_32_V1 ("HELTEC_WIFI_LORA_32_V1", eHELTEC_WIFI_LORA_32_V1, OledPins( 4, 15, 16), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278)); +BoardConfig HELTEC_WIFI_LORA_32_V2 ("HELTEC_WIFI_LORA_32_V2", eHELTEC_WIFI_LORA_32_V2, OledPins( 4, 15, 16), LoraPins( 5, 19, 27, 18, 14, 26, eSX1278)); +BoardConfig HELTEC_WIFI_LORA_32_V3 ("HELTEC_WIFI_LORA_32_V3", eHELTEC_WIFI_LORA_32_V3, OledPins(17, 18, 21), LoraPins( 9, 11, 10, 8, 12, 14, eSX1268)); +BoardConfig GUALTHERIUS_LORAHAM_v100("GUALTHERIUS_LORAHAM_v100", eGUALTHERIUS_LORAHAM_v100, OledPins(17, 16), LoraPins(18, 19, 23, 5, 13, 35, eSX1278)); +BoardConfig GUALTHERIUS_LORAHAM_v106("GUALTHERIUS_LORAHAM_v106", eGUALTHERIUS_LORAHAM_v106, OledPins(17, 16), LoraPins(18, 19, 23, 2, 13, 35, eSX1278)); // clang-format on diff --git a/lib/BoardFinder/BoardFinder.h b/lib/BoardFinder/BoardFinder.h index 50f6eab3..09d4f2d0 100644 --- a/lib/BoardFinder/BoardFinder.h +++ b/lib/BoardFinder/BoardFinder.h @@ -21,16 +21,22 @@ class OledPins { int8_t Addr; }; +enum LoraModem { + eSX1278, + eSX1268, +}; + class LoraPins { public: - explicit LoraPins(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t reset, int8_t irq); - - int8_t Sck; - int8_t Miso; - int8_t Mosi; - int8_t CS; - int8_t Reset; - int8_t IRQ; + explicit LoraPins(int8_t sck, int8_t miso, int8_t mosi, int8_t cs, int8_t reset, int8_t irq, LoraModem modem); + + int8_t Sck; + int8_t Miso; + int8_t Mosi; + int8_t CS; + int8_t Reset; + int8_t IRQ; + LoraModem Modem; }; class GpsPins { diff --git a/src/LoRaModem.cpp b/src/LoRaModem.cpp new file mode 100644 index 00000000..57e4f993 --- /dev/null +++ b/src/LoRaModem.cpp @@ -0,0 +1,133 @@ +#include "LoRaModem.h" + +// SX1278 +Modem_SX1278::Modem_SX1278() : _radio(0) { +} + +int16_t Modem_SX1278::begin(const LoraPins &lora_pins, const Configuration::LoRa &lora_config, const uint16_t preambleLength, void (*setFlag)()) { + float _frequencyRx = (float)lora_config.frequencyRx / 1000000; + float BWkHz = (float)lora_config.signalBandwidth / 1000; + + SPI.begin(lora_pins.Sck, lora_pins.Miso, lora_pins.Mosi, lora_pins.CS); + _module = new Module(lora_pins.CS, lora_pins.IRQ, lora_pins.Reset); + + _radio = new SX1278(_module); + int16_t state = _radio->begin(_frequencyRx, BWkHz, lora_config.spreadingFactor, lora_config.codingRate4, RADIOLIB_SX127X_SYNC_WORD, lora_config.power, preambleLength, lora_config.gainRx); + if (state != RADIOLIB_ERR_NONE) { + return state; + } + + state = _radio->setCRC(true); + if (state != RADIOLIB_ERR_NONE) { + return state; + } + + _radio->setDio0Action(setFlag, RISING); + + if (lora_config.tx_enable && lora_config.power > 17) { + _radio->setCurrentLimit(140); + } + return RADIOLIB_ERR_NONE; +} + +int16_t Modem_SX1278::readData(String &str) { + return _radio->readData(str); +} + +int16_t Modem_SX1278::setFrequency(float freq) { + return _radio->setFrequency(freq); +} + +int16_t Modem_SX1278::startReceive() { + return _radio->startReceive(); +} + +int16_t Modem_SX1278::startTransmit(String &str) { + return _radio->startTransmit(str); +} + +int16_t Modem_SX1278::receive(String &str) { + return _radio->receive(str); +} + +float Modem_SX1278::getRSSI() { + return _radio->getRSSI(); +} + +float Modem_SX1278::getSNR() { + return _radio->getSNR(); +} + +float Modem_SX1278::getFrequencyError() { + return _radio->getFrequencyError(); +} + +uint8_t Modem_SX1278::getModemStatus() { + return _radio->getModemStatus(); +} + +// SX1262 +Modem_SX1268::Modem_SX1268() : _radio(0) { +} + +int16_t Modem_SX1268::begin(const LoraPins &lora_pins, const Configuration::LoRa &lora_config, const uint16_t preambleLength, void (*setFlag)()) { + float _frequencyRx = (float)lora_config.frequencyRx / 1000000; + float BWkHz = (float)lora_config.signalBandwidth / 1000; + + SPI.begin(lora_pins.Sck, lora_pins.Miso, lora_pins.Mosi, lora_pins.CS); + _module = new Module(lora_pins.CS, lora_pins.IRQ, lora_pins.Reset, 13); + + _radio = new SX1262(_module); + int16_t state = _radio->begin(_frequencyRx, BWkHz, lora_config.spreadingFactor, lora_config.codingRate4, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, lora_config.power, preambleLength); + if (state != RADIOLIB_ERR_NONE) { + return state; + } + + state = _radio->setCRC(true); + if (state != RADIOLIB_ERR_NONE) { + return state; + } + + _radio->setDio1Action(setFlag); + + if (lora_config.tx_enable && lora_config.power > 17) { + _radio->setCurrentLimit(140); + } + return RADIOLIB_ERR_NONE; +} + +int16_t Modem_SX1268::readData(String &str) { + return _radio->readData(str); +} + +int16_t Modem_SX1268::setFrequency(float freq) { + return _radio->setFrequency(freq); +} + +int16_t Modem_SX1268::startReceive() { + return _radio->startReceive(); +} + +int16_t Modem_SX1268::startTransmit(String &str) { + return _radio->startTransmit(str); +} + +int16_t Modem_SX1268::receive(String &str) { + return _radio->receive(str); +} + +float Modem_SX1268::getRSSI() { + return _radio->getRSSI(); +} + +float Modem_SX1268::getSNR() { + return _radio->getSNR(); +} + +float Modem_SX1268::getFrequencyError() { + return _radio->getFrequencyError(); +} + +uint8_t Modem_SX1268::getModemStatus() { + return 0; +} diff --git a/src/LoRaModem.h b/src/LoRaModem.h new file mode 100644 index 00000000..bbd35234 --- /dev/null +++ b/src/LoRaModem.h @@ -0,0 +1,82 @@ +#ifndef LORA_MODEM_H_ +#define LORA_MODEM_H_ + +#include + +#include "BoardFinder.h" +#include "project_configuration.h" + +class LoRaModem { +public: + LoRaModem() : _module(0) { + } + + virtual ~LoRaModem() { + } + + virtual int16_t begin(const LoraPins &lora_pins, const Configuration::LoRa &lora_config, const uint16_t preambleLength, void (*setFlag)()) = 0; + + virtual int16_t readData(String &str) = 0; + + virtual int16_t setFrequency(float freq) = 0; + virtual int16_t startReceive() = 0; + virtual int16_t startTransmit(String &str) = 0; + + virtual int16_t receive(String &str) = 0; + + virtual float getRSSI() = 0; + virtual float getSNR() = 0; + virtual float getFrequencyError() = 0; + virtual uint8_t getModemStatus() = 0; + +protected: + Module *_module; +}; + +class Modem_SX1278 : public LoRaModem { +public: + Modem_SX1278(); + + int16_t begin(const LoraPins &lora_pins, const Configuration::LoRa &lora_config, const uint16_t preambleLength, void (*setFlag)()) override; + + int16_t readData(String &str) override; + + int16_t setFrequency(float freq) override; + int16_t startReceive() override; + int16_t startTransmit(String &str) override; + + int16_t receive(String &str) override; + + float getRSSI() override; + float getSNR() override; + float getFrequencyError() override; + uint8_t getModemStatus() override; + +private: + SX1278 *_radio; +}; + +class Modem_SX1268 : public LoRaModem { +public: + Modem_SX1268(); + + int16_t begin(const LoraPins &lora_pins, const Configuration::LoRa &lora_config, const uint16_t preambleLength, void (*setFlag)()) override; + + int16_t readData(String &str) override; + + int16_t setFrequency(float freq) override; + int16_t startReceive() override; + int16_t startTransmit(String &str) override; + + int16_t receive(String &str) override; + + float getRSSI() override; + float getSNR() override; + float getFrequencyError() override; + uint8_t getModemStatus() override; + +private: + SX1262 *_radio; +}; + +#endif diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index b6b1f9d1..4e2c27b3 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -60,16 +60,16 @@ void setup() { std::list boardConfigs; boardConfigs.push_back(&TTGO_LORA32_V1); boardConfigs.push_back(&TTGO_LORA32_V2); - boardConfigs.push_back(&TTGO_T_Beam_V0_7); - boardConfigs.push_back(&TTGO_T_Beam_V1_0); - boardConfigs.push_back(&LILYGO_POE_ETH_BOARD); - boardConfigs.push_back(&WT32_ETH_BOARD); - boardConfigs.push_back(&TRACKERD); boardConfigs.push_back(&HELTEC_WIFI_LORA_32_V1); boardConfigs.push_back(&HELTEC_WIFI_LORA_32_V2); boardConfigs.push_back(&HELTEC_WIFI_LORA_32_V3); + boardConfigs.push_back(&LILYGO_POE_ETH_BOARD); + boardConfigs.push_back(&WT32_ETH_BOARD); + boardConfigs.push_back(&TRACKERD); boardConfigs.push_back(&GUALTHERIUS_LORAHAM_v100); boardConfigs.push_back(&GUALTHERIUS_LORAHAM_v106); + boardConfigs.push_back(&TTGO_T_Beam_V0_7); + boardConfigs.push_back(&TTGO_T_Beam_V1_0); ProjectConfigurationManagement confmg(LoRaSystem.getLogger()); confmg.readConfiguration(LoRaSystem.getLogger(), userConfig); diff --git a/src/TaskRadiolib.cpp b/src/TaskRadiolib.cpp index f52fb61e..4a9fa4f3 100644 --- a/src/TaskRadiolib.cpp +++ b/src/TaskRadiolib.cpp @@ -4,20 +4,15 @@ #include "TaskRadiolib.h" -volatile bool RadiolibTask::_modemInterruptOccured = false; +volatile bool RadiolibTask::_modemInterruptOccurred = false; -RadiolibTask::RadiolibTask(TaskQueue> &fromModem, TaskQueue> &toModem) : Task(TASK_RADIOLIB, TaskRadiolib), _module(0), _radio(0), _rxEnable(false), _txEnable(false), _fromModem(fromModem), _toModem(toModem), _transmitFlag(false), _frequencyTx(0.0), _frequencyRx(0.0), _frequenciesAreSame(false) { +RadiolibTask::RadiolibTask(TaskQueue> &fromModem, TaskQueue> &toModem) : Task(TASK_RADIOLIB, TaskRadiolib), _modem(0), _rxEnable(false), _txEnable(false), _fromModem(fromModem), _toModem(toModem), _transmitFlag(false), _frequencyTx(0.0), _frequencyRx(0.0), _frequenciesAreSame(false) { } RadiolibTask::~RadiolibTask() { - _radio->clearDio0Action(); } bool RadiolibTask::setup(System &system) { - SPI.begin(system.getBoardConfig()->Lora.Sck, system.getBoardConfig()->Lora.Miso, system.getBoardConfig()->Lora.Mosi, system.getBoardConfig()->Lora.CS); - _module = new Module(system.getBoardConfig()->Lora.CS, system.getBoardConfig()->Lora.IRQ, system.getBoardConfig()->Lora.Reset); - _radio = new SX1278(_module); - _rxEnable = true; _txEnable = system.getUserConfig()->lora.tx_enable; @@ -28,30 +23,24 @@ bool RadiolibTask::setup(System &system) { _frequenciesAreSame = true; } - float BWkHz = (float)system.getUserConfig()->lora.signalBandwidth / 1000; - const uint16_t preambleLength = 8; - int16_t state = _radio->begin(_frequencyRx, BWkHz, system.getUserConfig()->lora.spreadingFactor, system.getUserConfig()->lora.codingRate4, RADIOLIB_SX127X_SYNC_WORD, system.getUserConfig()->lora.power, preambleLength, system.getUserConfig()->lora.gainRx); - if (state != RADIOLIB_ERR_NONE) { - decodeError(system, state); + if (system.getBoardConfig()->Lora.Modem == eSX1278) { + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] using SX1278", timeString().c_str()); + _modem = new Modem_SX1278(); + } else if (system.getBoardConfig()->Lora.Modem == eSX1268) { + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] using SX1268", timeString().c_str()); + _modem = new Modem_SX1268(); + } else { + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] Modem not correctly defined!", timeString().c_str()); } - state = _radio->setCRC(true); + int16_t state = _modem->begin(system.getBoardConfig()->Lora, system.getUserConfig()->lora, preambleLength, setFlag); if (state != RADIOLIB_ERR_NONE) { - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] setCRC failed, code %d", timeString().c_str(), state); decodeError(system, state); } - _radio->setDio0Action(setFlag, RISING); - - if (_rxEnable) { - startRX(system); - } - - if (system.getUserConfig()->lora.tx_enable && system.getUserConfig()->lora.power > 17) { - _radio->setCurrentLimit(140); - } + startRX(system); uint32_t preambleDurationMilliSec = ((uint64_t)(preambleLength + 4) << (system.getUserConfig()->lora.spreadingFactor + 10 /* to milli-sec */)) / system.getUserConfig()->lora.signalBandwidth; _txWaitTimer.setTimeout(preambleDurationMilliSec * 2); @@ -61,7 +50,7 @@ bool RadiolibTask::setup(System &system) { } bool RadiolibTask::loop(System &system) { - if (_modemInterruptOccured) { + if (_modemInterruptOccurred) { handleModemInterrupt(system); } else if (_txWaitTimer.check() && !_toModem.empty()) { handleTXing(system); @@ -70,11 +59,11 @@ bool RadiolibTask::loop(System &system) { } void RadiolibTask::setFlag(void) { - _modemInterruptOccured = true; + _modemInterruptOccurred = true; } void RadiolibTask::handleModemInterrupt(System &system) { - _modemInterruptOccured = false; + _modemInterruptOccurred = false; if (_transmitFlag) { // transmitted _transmitFlag = false; @@ -86,20 +75,20 @@ void RadiolibTask::handleModemInterrupt(System &system) { // received String str; - int state = _radio->readData(str); + int state = _modem->readData(str); if (state != RADIOLIB_ERR_NONE) { system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] readData failed, code %d", timeString().c_str(), state); return; } if (str.substring(0, 3) != "<\xff\x01") { - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] Unknown packet '%s' with RSSI %.0fdBm, SNR %.2fdB and FreqErr %fHz", timeString().c_str(), str.c_str(), _radio->getRSSI(), _radio->getSNR(), -_radio->getFrequencyError()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] Unknown packet '%s' with RSSI %.0fdBm, SNR %.2fdB and FreqErr %fHz", timeString().c_str(), str.c_str(), _modem->getRSSI(), _modem->getSNR(), -_modem->getFrequencyError()); return; } std::shared_ptr msg = std::shared_ptr(new APRSMessage()); msg->decode(str.substring(3)); _fromModem.addElement(msg); - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] Received packet '%s' with RSSI %.0fdBm, SNR %.2fdB and FreqErr %fHz", timeString().c_str(), msg->toString().c_str(), _radio->getRSSI(), _radio->getSNR(), -_radio->getFrequencyError()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] Received packet '%s' with RSSI %.0fdBm, SNR %.2fdB and FreqErr %fHz", timeString().c_str(), msg->toString().c_str(), _modem->getRSSI(), _modem->getSNR(), -_modem->getFrequencyError()); system.getDisplay().addFrame(std::shared_ptr(new TextFrame("LoRa", msg->toString().c_str()))); } @@ -121,7 +110,7 @@ void RadiolibTask::handleTXing(System &system) { // we are currently RXing static bool rxsignaldetected_print = false; - if (_frequenciesAreSame && (_radio->getModemStatus() & 0x01) == 0x01) { + if (_frequenciesAreSame && (_modem->getModemStatus() & 0x01) == 0x01) { if (!rxsignaldetected_print) { rxsignaldetected_print = true; system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "[%s] RX signal detected. Waiting TX", timeString().c_str()); @@ -138,7 +127,7 @@ void RadiolibTask::handleTXing(System &system) { void RadiolibTask::startRX(System &system) { if (!_frequenciesAreSame) { - int16_t state = _radio->setFrequency(_frequencyRx); + int16_t state = _modem->setFrequency(_frequencyRx); if (state != RADIOLIB_ERR_NONE) { system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] startRX failed, Freq update, code %d", timeString().c_str(), state); decodeError(system, state); @@ -146,7 +135,7 @@ void RadiolibTask::startRX(System &system) { } } - int16_t state = _radio->startReceive(); + int16_t state = _modem->startReceive(); if (state != RADIOLIB_ERR_NONE) { system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] startRX failed, code %d", timeString().c_str(), state); decodeError(system, state); @@ -155,7 +144,7 @@ void RadiolibTask::startRX(System &system) { void RadiolibTask::startTX(System &system, String &str) { if (!_frequenciesAreSame) { - int16_t state = _radio->setFrequency(_frequencyTx); + int16_t state = _modem->setFrequency(_frequencyTx); if (state != RADIOLIB_ERR_NONE) { system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] startTX failed, Freq update, code %d", timeString().c_str(), state); decodeError(system, state); @@ -164,7 +153,7 @@ void RadiolibTask::startTX(System &system, String &str) { } } - int16_t state = _radio->startTransmit(str); + int16_t state = _modem->startTransmit(str); if (state != RADIOLIB_ERR_NONE) { system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] startTX failed, code %d", timeString().c_str(), state); decodeError(system, state); @@ -177,106 +166,106 @@ void RadiolibTask::startTX(System &system, String &str) { void RadiolibTask::decodeError(System &system, int16_t state) { switch (state) { case RADIOLIB_ERR_UNKNOWN: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 unknown error.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx unknown error.", timeString().c_str()); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_CHIP_NOT_FOUND: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, chip not found.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, chip not found.", timeString().c_str()); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_PACKET_TOO_LONG: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 packet too long.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx packet too long.", timeString().c_str()); break; case RADIOLIB_ERR_TX_TIMEOUT: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 tx timeout.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx tx timeout.", timeString().c_str()); break; case RADIOLIB_ERR_RX_TIMEOUT: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 rx timeout.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx rx timeout.", timeString().c_str()); break; case RADIOLIB_ERR_CRC_MISMATCH: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 crc mismatch.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx crc mismatch.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_BANDWIDTH: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied bandwidth value (%fkHz) is invalid for this module. Should be 7800, 10400, 15600, 20800, 31250, 41700 ,62500, 125000, 250000, 500000.", timeString().c_str(), system.getUserConfig()->lora.signalBandwidth / 1000); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied bandwidth value (%fkHz) is invalid for this module. Should be 7800, 10400, 15600, 20800, 31250, 41700 ,62500, 125000, 250000, 500000.", timeString().c_str(), system.getUserConfig()->lora.signalBandwidth / 1000); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_INVALID_SPREADING_FACTOR: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied spreading factor value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.spreadingFactor); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied spreading factor value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.spreadingFactor); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_INVALID_CODING_RATE: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied coding rate value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.codingRate4); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied coding rate value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.codingRate4); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_INVALID_FREQUENCY: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied frequency value (%fMHz) is invalid for this module.", timeString().c_str(), _frequencyRx); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied frequency value (%fMHz) is invalid for this module.", timeString().c_str(), _frequencyRx); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_INVALID_OUTPUT_POWER: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied output power value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.power); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied output power value (%d) is invalid for this module.", timeString().c_str(), system.getUserConfig()->lora.power); _txEnable = false; break; case RADIOLIB_ERR_INVALID_CURRENT_LIMIT: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied current limit is invalid.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied current limit is invalid.", timeString().c_str()); _txEnable = false; break; case RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied preamble length is invalid.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied preamble length is invalid.", timeString().c_str()); _txEnable = false; break; case RADIOLIB_ERR_INVALID_GAIN: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, The supplied gain value (%d) is invalid.", timeString().c_str(), system.getUserConfig()->lora.gainRx); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, The supplied gain value (%d) is invalid.", timeString().c_str(), system.getUserConfig()->lora.gainRx); _rxEnable = false; break; case RADIOLIB_ERR_WRONG_MODEM: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, wrong modem selected.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, wrong modem selected.", timeString().c_str()); _rxEnable = false; _txEnable = false; break; case RADIOLIB_ERR_INVALID_NUM_SAMPLES: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid number of samples.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid number of samples.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_RSSI_OFFSET: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid RSSI offset.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid RSSI offset.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_ENCODING: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid encoding.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid encoding.", timeString().c_str()); break; case RADIOLIB_ERR_LORA_HEADER_DAMAGED: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 LoRa header damaged.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx LoRa header damaged.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_DIO_PIN: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid DIO pin.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid DIO pin.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_RSSI_THRESHOLD: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid RSSI threshold.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid RSSI threshold.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_BIT_RATE: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid bit rate.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid bit rate.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid frequency deviation.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid frequency deviation.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_RX_BANDWIDTH: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid rx bandwidth.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid rx bandwidth.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_SYNC_WORD: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid sync word.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid sync word.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_DATA_SHAPING: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid data shaping.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid data shaping.", timeString().c_str()); break; case RADIOLIB_ERR_INVALID_MODULATION: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 invalid modulation.", timeString().c_str()); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx invalid modulation.", timeString().c_str()); break; default: - system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX1278 init failed, code %d", timeString().c_str(), state); + system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, getName(), "[%s] SX12xx init failed, code %d", timeString().c_str(), state); _rxEnable = false; _txEnable = false; } diff --git a/src/TaskRadiolib.h b/src/TaskRadiolib.h index f07cb553..4876d204 100644 --- a/src/TaskRadiolib.h +++ b/src/TaskRadiolib.h @@ -4,7 +4,7 @@ #include "project_configuration.h" #include #include -#include +#include #include class RadiolibTask : public Task { @@ -16,8 +16,7 @@ class RadiolibTask : public Task { virtual bool loop(System &system) override; private: - Module *_module; - SX1278 *_radio; + LoRaModem *_modem; bool _rxEnable; bool _txEnable; @@ -25,7 +24,7 @@ class RadiolibTask : public Task { TaskQueue> &_fromModem; TaskQueue> &_toModem; - static volatile bool _modemInterruptOccured; + static volatile bool _modemInterruptOccurred; Timer _txWaitTimer;