diff --git a/src/ISerial.h b/src/ISerial.h index d086beaf..c8ecacbe 100644 --- a/src/ISerial.h +++ b/src/ISerial.h @@ -15,7 +15,7 @@ */ class ISerial { public: - ISerial(Module* mod); + explicit ISerial(Module* mod); void begin(long); bool listen(); diff --git a/src/Module.cpp b/src/Module.cpp index e24e6424..218be4fa 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -9,6 +9,7 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs _spi = &RADIOLIB_DEFAULT_SPI; _spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0); _initInterface = true; + ModuleSerial = NULL; } Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio) { @@ -20,9 +21,10 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs _spi = &RADIOLIB_DEFAULT_SPI; _spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0); _initInterface = true; + ModuleSerial = NULL; } -Module::Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* useSer, RADIOLIB_PIN_TYPE rst) { +Module::Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial, RADIOLIB_PIN_TYPE rst) { _cs = RADIOLIB_NC; _rx = rx; _tx = tx; @@ -31,10 +33,10 @@ Module::Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* useSe _initInterface = true; #ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED - ModuleSerial = useSer; + ModuleSerial = serial; #else ModuleSerial = new SoftwareSerial(_rx, _tx); - (void)useSer; + (void)serial; #endif } @@ -47,6 +49,7 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs _spi = &spi; _spiSettings = spiSettings; _initInterface = false; + ModuleSerial = NULL; } Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings) { @@ -58,9 +61,10 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs _spi = &spi; _spiSettings = spiSettings; _initInterface = false; + ModuleSerial = NULL; } -Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, SPIClass& spi, SPISettings spiSettings, HardwareSerial* useSer) { +Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, SPIClass& spi, SPISettings spiSettings, HardwareSerial* serial) { _cs = cs; _rx = rx; _tx = tx; @@ -71,10 +75,10 @@ Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rs _initInterface = false; #ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED - ModuleSerial = useSer; + ModuleSerial = serial; #else ModuleSerial = new SoftwareSerial(_rx, _tx); - (void)useSer; + (void)serial; #endif } @@ -259,16 +263,20 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da // send data or get response if(cmd == SPIwriteCommand) { - for(size_t n = 0; n < numBytes; n++) { - _spi->transfer(dataOut[n]); - RADIOLIB_VERBOSE_PRINT(dataOut[n], HEX); - RADIOLIB_VERBOSE_PRINT('\t'); + if(dataOut != NULL) { + for(size_t n = 0; n < numBytes; n++) { + _spi->transfer(dataOut[n]); + RADIOLIB_VERBOSE_PRINT(dataOut[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + } } } else if (cmd == SPIreadCommand) { - for(size_t n = 0; n < numBytes; n++) { - dataIn[n] = _spi->transfer(0x00); - RADIOLIB_VERBOSE_PRINT(dataIn[n], HEX); - RADIOLIB_VERBOSE_PRINT('\t'); + if(dataIn != NULL) { + for(size_t n = 0; n < numBytes; n++) { + dataIn[n] = _spi->transfer(0x00); + RADIOLIB_VERBOSE_PRINT(dataIn[n], HEX); + RADIOLIB_VERBOSE_PRINT('\t'); + } } } RADIOLIB_VERBOSE_PRINTLN(); diff --git a/src/Module.h b/src/Module.h index 2a20b926..ccc571c0 100644 --- a/src/Module.h +++ b/src/Module.h @@ -21,18 +21,18 @@ class Module { /*! \brief UART-based module constructor. - \param tx Arduino pin to be used as Tx pin for SoftwareSerial communication. - \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 tx, RADIOLIB_PIN_TYPE rx, HardwareSerial* serial = &RADIOLIB_HARDWARE_SERIAL_PORT, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); + 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 tx, RADIOLIB_PIN_TYPE rx, HardwareSerial* serial = nullptr, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); + Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = nullptr, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC); #endif /*! @@ -408,18 +408,18 @@ class Module { #ifndef RADIOLIB_GODMODE private: #endif - RADIOLIB_PIN_TYPE _cs; - RADIOLIB_PIN_TYPE _tx; - RADIOLIB_PIN_TYPE _rx; - RADIOLIB_PIN_TYPE _irq; - RADIOLIB_PIN_TYPE _rst; + RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _tx = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _rx = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC; + RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC; - bool _initInterface; - SPIClass* _spi; + bool _initInterface = false; + SPIClass* _spi = NULL; SPISettings _spiSettings; bool _useRfSwitch = false; - RADIOLIB_PIN_TYPE _rxEn, _txEn; + RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC, _txEn = RADIOLIB_NC; uint32_t _ATtimeout = 15000; };