Changed to new approach to pin handling

This commit is contained in:
jgromes 2019-12-27 13:15:33 +01:00
parent a1f94d9f16
commit 606e317958
4 changed files with 99 additions and 99 deletions

View file

@ -171,6 +171,8 @@ shutdown KEYWORD2
# Constants (LITERAL1)
#######################################
RADIOLIB_PIN_UNUSED LITERAL1
ERR_NONE LITERAL1
ERR_UNKNOWN LITERAL1

View file

@ -1,11 +1,11 @@
#include "Module.h"
Module::Module(int rx, int tx, HardwareSerial* useSer) {
_cs = -1;
Module::Module(int16_t rx, int16_t tx, HardwareSerial* useSer, int16_t rst) {
_cs = RADIOLIB_PIN_UNUSED;
_rx = rx;
_tx = tx;
_int0 = -1;
_int1 = -1;
_irq = RADIOLIB_PIN_UNUSED;
_rst = rst;
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
ModuleSerial = useSer;
@ -15,22 +15,32 @@ Module::Module(int rx, int tx, HardwareSerial* useSer) {
#endif
}
Module::Module(int cs, int int0, int int1, SPIClass& spi, SPISettings spiSettings) {
Module::Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi, SPISettings spiSettings) {
_cs = cs;
_rx = -1;
_tx = -1;
_int0 = int0;
_int1 = int1;
_rx = RADIOLIB_PIN_UNUSED;
_tx = RADIOLIB_PIN_UNUSED;
_irq = irq;
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
}
Module::Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi, SPISettings spiSettings, HardwareSerial* useSer) {
Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi, SPISettings spiSettings) {
_cs = cs;
_rx = gpio;
_tx = RADIOLIB_PIN_UNUSED;
_irq = irq;
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
}
Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t rx, int16_t tx, SPIClass& spi, SPISettings spiSettings, HardwareSerial* useSer) {
_cs = cs;
_rx = rx;
_tx = tx;
_int0 = int0;
_int1 = int1;
_irq = irq;
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
@ -42,22 +52,12 @@ Module::Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi, SPISet
#endif
}
Module::Module(int cs, int int0, int int1, int int2, SPIClass& spi, SPISettings spiSettings) {
_cs = cs;
_rx = int2;
_tx = -1;
_int0 = int0;
_int1 = int1;
_spi = &spi;
_spiSettings = spiSettings;
}
void Module::init(uint8_t interface, uint8_t gpio) {
void Module::init(uint8_t interface) {
// select interface
switch(interface) {
case RADIOLIB_USE_SPI:
setPin(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
Module::pinMode(_cs, OUTPUT);
Module::digitalWrite(_cs, HIGH);
_spi->begin();
break;
case RADIOLIB_USE_UART:
@ -70,22 +70,6 @@ void Module::init(uint8_t interface, uint8_t gpio) {
case RADIOLIB_USE_I2C:
break;
}
// select GPIO
switch(gpio) {
case RADIOLIB_INT_NONE:
break;
case RADIOLIB_INT_0:
setPin(_int0, INPUT);
break;
case RADIOLIB_INT_1:
setPin(_int1, INPUT);
break;
case RADIOLIB_INT_BOTH:
setPin(_int0, INPUT);
setPin(_int1, INPUT);
break;
}
}
void Module::term() {
@ -217,7 +201,7 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da
_spi->beginTransaction(_spiSettings);
// pull CS low
digitalWrite(_cs, LOW);
Module::digitalWrite(_cs, LOW);
// send SPI register address with access command
_spi->transfer(reg | cmd);
@ -247,14 +231,20 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da
RADIOLIB_VERBOSE_PRINTLN();
// release CS
digitalWrite(_cs, HIGH);
Module::digitalWrite(_cs, HIGH);
// end SPI transaction
_spi->endTransaction();
}
void Module::setPin(int16_t pin, uint8_t mode) {
if(pin != -1) {
pinMode(pin, mode);
void Module::pinMode(int16_t pin, uint8_t mode) {
if(pin != RADIOLIB_PIN_UNUSED) {
::pinMode(pin, mode);
}
}
void Module::digitalWrite(int16_t pin, uint8_t value) {
if(pin != RADIOLIB_PIN_UNUSED) {
::digitalWrite(pin, value);
}
}

View file

@ -25,12 +25,14 @@ class Module {
\param rx Arduino pin to be used as Rx pin for SoftwareSerial communication.
\param serial HardwareSerial to be used on ESP32 and SAMD. Defaults to 1
\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 -1 (unused).
*/
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
Module(int tx, int rx, HardwareSerial* useSer = &Serial1);
Module(int16_t tx, int16_t rx, HardwareSerial* serial = &Serial1, int16_t rst = -1);
#else
Module(int tx, int rx, HardwareSerial* useSer = nullptr);
Module(int16_t tx, int16_t rx, HardwareSerial* serial = nullptr, int16_t rst = -1);
#endif
/*!
@ -38,41 +40,41 @@ class Module {
\param cs Arduino pin to be used as chip select.
\param int0 Arduino pin to be used as interrupt/GPIO 0.
\param irq Arduino pin to be used as interrupt/GPIO.
\param int1 Arduino pin to be used as interrupt/GPIO 1.
\param rst Arduino pin to be used as hardware reset for the module.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
*/
Module(int cs, int int0, int int1, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
/*!
\brief Extended SPI-based module constructor.
\param cs Arduino pin to be used as chip select.
\param int0 Arduino pin to be used as interrupt/GPIO 0.
\param irq Arduino pin to be used as interrupt/GPIO.
\param int1 Arduino pin to be used as interrupt/GPIO 1.
\param rst Arduino pin to be used as hardware reset for the module.
\param int2 Arduino pin to be used as interrupt/GPIO 2.
\param gpio Arduino pin to be used as additional interrupt/GPIO.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
*/
Module(int cs, int int0, int int1, int int2, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
/*!
\brief Generic module constructor.
\param cs Arduino pin to be used as chip select.
\param int0 Arduino pin to be used as interrupt/GPIO 0.
\param irq Arduino pin to be used as interrupt/GPIO.
\param int1 Arduino pin to be used as interrupt/GPIO 1.
\param rst Arduino pin to be used as hardware reset for the module.
\param tx Arduino pin to be used as Tx pin for SoftwareSerial communication.
@ -85,9 +87,9 @@ class Module {
\param serial HardwareSerial to be used on ESP32 and SAMD. Defaults to 1
*/
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* useSer = &Serial1);
Module(int16_t cs, int16_t irq, int16_t rst, int16_t rx, int16_t tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* serial = &Serial1);
#else
Module(int cs, int int0, int int1, int rx, int tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* useSer = nullptr);
Module(int16_t cs, int16_t irq, int16_t rst, int16_t rx, int16_t tx, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* serial = nullptr);
#endif
@ -128,10 +130,8 @@ class Module {
\brief Initialize low-level module control.
\param interface Interface to be used on the module. See \ref shield_config for details.
\param gpio GPIO/interrupt pins to be used on the module. See \ref uart_config for details.
*/
void init(uint8_t interface, uint8_t gpio);
void init(uint8_t interface);
/*!
\brief Terminate low-level module control.
@ -266,35 +266,42 @@ class Module {
\returns Pin number of SPI chip select configured in the constructor.
*/
int getCs() const { return(_cs); }
int16_t getCs() const { return(_cs); }
/*!
\brief Access method to get the pin number of interrupt/GPIO 0.
\brief Access method to get the pin number of interrupt/GPIO.
\returns Pin number of interrupt/GPIO 0 configured in the constructor.
\returns Pin number of interrupt/GPIO configured in the constructor.
*/
int getInt0() const { return(_int0); }
int16_t getIrq() const { return(_irq); }
/*!
\brief Access method to get the pin number of interrupt/GPIO 1.
\brief Access method to get the pin number of hardware reset pin.
\returns Pin number of interrupt/GPIO 1 configured in the constructor.
\returns Pin number of hardware reset pin configured in the constructor.
*/
int getInt1() const { return(_int1); }
int16_t getRst() const { return(_rst); }
/*!
\brief Access method to get the pin number of second interrupt/GPIO.
\returns Pin number of second interrupt/GPIO configured in the constructor.
*/
int16_t getGpio() const { return(_rx); }
/*!
\brief Access method to get the pin number of UART Rx.
\returns Pin number of UART Rx configured in the constructor.
*/
int getRx() const { return(_rx); }
int16_t getRx() const { return(_rx); }
/*!
\brief Access method to get the pin number of UART Rx.
\returns Pin number of UART Rx configured in the constructor.
*/
int getTx() const { return(_tx); }
int16_t getTx() const { return(_tx); }
/*!
\brief Access method to get the SPI interface.
@ -310,21 +317,37 @@ class Module {
*/
SPISettings getSpiSettings() const { return(_spiSettings); }
/*!
\brief Arduino core pinMode override that checks -1 as alias for unused pin.
\param pin Pin to change the mode of.
\param mode Which mode to set.
*/
static void pinMode(int16_t pin, uint8_t mode);
/*!
\brief Arduino core digitalWrite override that checks -1 as alias for unused pin.
\param pin Pin to write to.
\param value Whether to set the pin high or low.
*/
static void digitalWrite(int16_t pin, uint8_t value);
#ifndef RADIOLIB_GODMODE
private:
#endif
int _cs;
int _tx;
int _rx;
int _int0;
int _int1;
int16_t _cs;
int16_t _tx;
int16_t _rx;
int16_t _irq;
int16_t _rst;
SPIClass* _spi;
SPISettings _spiSettings;
uint32_t _ATtimeout = 15000;
void setPin(int16_t pin, uint8_t mode);
};
#endif

View file

@ -65,6 +65,11 @@
#define RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
#endif
/*!
\brief Alias for unused pin.
*/
#define RADIOLIB_PIN_UNUSED (-1)
/*!
\defgroup shield_config Shield Configuration
@ -86,26 +91,6 @@
*/
#define RADIOLIB_USE_I2C 0x02
/*!
\brief Do not use any interrupts/GPIOs.
*/
#define RADIOLIB_INT_NONE 0x00
/*!
\brief Use interrupt/GPIO 0.
*/
#define RADIOLIB_INT_0 0x01
/*!
\brief Use interrupt/GPIO 1.
*/
#define RADIOLIB_INT_1 0x02
/*!
\brief Use both interrupts/GPIOs.
*/
#define RADIOLIB_INT_BOTH 0x03
/*!
\}
*/