From 0f1140e5adc1f3963139a0e85659fa1970955e9c Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 5 Sep 2021 12:00:30 +0200 Subject: [PATCH] Added SerialModule wrapper class (#305) --- keywords.txt | 1 + src/Module.cpp | 31 +++++++++++--------------- src/Module.h | 60 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/keywords.txt b/keywords.txt index d66029ac..0950331b 100644 --- a/keywords.txt +++ b/keywords.txt @@ -9,6 +9,7 @@ RadioLib KEYWORD1 RadioShield KEYWORD1 Module KEYWORD1 +SerialModule KEYWORD1 # modules CC1101 KEYWORD1 diff --git a/src/Module.cpp b/src/Module.cpp index f9b8a9e6..ccf2e8fd 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -1,5 +1,18 @@ #include "Module.h" +SerialModule::SerialModule(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial, RADIOLIB_PIN_TYPE rst): + Module(RADIOLIB_NC, RADIOLIB_NC, (RADIOLIB_PIN_TYPE)rst, (RADIOLIB_PIN_TYPE)rx, (RADIOLIB_PIN_TYPE)tx, RADIOLIB_DEFAULT_SPI, SPISettings(2000000, MSBFIRST, SPI_MODE0), NULL) +{ + _initInterface = true; + +#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED + ModuleSerial = serial; +#else + ModuleSerial = new SoftwareSerial(rx, tx); + (void)serial; +#endif +} + Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst): _cs(cs), _irq(irq), @@ -26,24 +39,6 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs ModuleSerial = NULL; } -Module::Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial, RADIOLIB_PIN_TYPE rst): - _cs(RADIOLIB_NC), - _irq(RADIOLIB_NC), - _rst(rst), - _rx(rx), - _tx(tx), - _spiSettings(SPISettings(2000000, MSBFIRST, SPI_MODE0)) -{ - _initInterface = true; - -#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED - ModuleSerial = serial; -#else - ModuleSerial = new SoftwareSerial(_rx, _tx); - (void)serial; -#endif -} - Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, SPIClass& spi, SPISettings spiSettings): _cs(cs), _irq(irq), diff --git a/src/Module.h b/src/Module.h index 0d28c335..a101fbdd 100644 --- a/src/Module.h +++ b/src/Module.h @@ -11,29 +11,11 @@ /*! \class Module - \brief Implements all common low-level SPI/UART/I2C methods to control the wireless module. + \brief Implements all common low-level SPI/UART methods to control the wireless module. Every module class contains one private instance of this class. */ class Module { public: - - /*! - \brief UART-based module constructor. - - \param rx Arduino pin to be used as Rx pin for SoftwareSerial communication. - - \param tx Arduino pin to be used as Tx pin for SoftwareSerial communication. - - \param serial HardwareSerial to be used on platforms that do not support SoftwareSerial. Defaults to Serial1. - - \param rst Arduino pin to be used as hardware reset for the module. Defaults to NC (unused). - */ -#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED - Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = &RADIOLIB_HARDWARE_SERIAL_PORT, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); -#else - Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = nullptr, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); -#endif - /*! \brief SPI-based module constructor. Will use the default SPI interface automatically initialize it. @@ -481,21 +463,55 @@ class Module { #ifndef RADIOLIB_GODMODE private: #endif + // allow SerialModule class to access private members + friend class SerialModule; + + // whether RadioLib should automatically initalize selected SPI or UART interface + bool _initInterface = false; + + // pins RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC; RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC; RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC; RADIOLIB_PIN_TYPE _rx = RADIOLIB_NC; RADIOLIB_PIN_TYPE _tx = RADIOLIB_NC; + // SPI settings SPISettings _spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0); - - bool _initInterface = false; SPIClass* _spi = NULL; + // RF switch presence and pins bool _useRfSwitch = false; - RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC, _txEn = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _txEn = RADIOLIB_NC; + // AT command timeout in milliseconds uint32_t _ATtimeout = 15000; }; +/*! + \class SerialModule + + \brief Extension of Module class for UART-based modules, only exists to distinguish the UART constructor. +*/ +class SerialModule: public Module { + public: + /*! + \brief UART-based module constructor. + + \param rx Arduino pin to be used as Rx pin for SoftwareSerial communication. + + \param tx Arduino pin to be used as Tx pin for SoftwareSerial communication. + + \param serial HardwareSerial to be used on platforms that do not support SoftwareSerial. Defaults to Serial1. + + \param rst Arduino pin to be used as hardware reset for the module. Defaults to NC (unused). + */ + #ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED + SerialModule(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = &RADIOLIB_HARDWARE_SERIAL_PORT, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); + #else + SerialModule(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = nullptr, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); + #endif +}; + #endif