Skip to content

Commit

Permalink
implement #539
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Apr 11, 2021
1 parent 55cc2dd commit 0c433f3
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 14 deletions.
64 changes: 57 additions & 7 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,24 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)
/****************************************************************************/

RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed)
:ce_pin(_cepin), csn_pin(_cspin),spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
:ce_pin(_cepin), csn_pin(_cspin), spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
csDelay(5)
{
_init_obj();
}

/****************************************************************************/

RF24::RF24(uint32_t _spi_speed = RF24_SPI_SPEED)
:ce_pin(0xFFFF), csn_pin(0xFFFF), spi_speed(_spi_speed), payload_size(32), dynamic_payloads_enabled(true), addr_width(5), _is_p_variant(false),
csDelay(5)
{
_init_obj();
}

/****************************************************************************/

void RF24::_init_obj()
{
// Use a pointer on the Arduino platform
#if defined (RF24_SPI_PTR)
Expand All @@ -502,7 +518,7 @@ RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed)

pipe0_reading_address[0] = 0;
if(spi_speed <= 35000){ //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
spi_speed = RF24_SPI_SPEED;
spi_speed = RF24_SPI_SPEED;
}
}

Expand Down Expand Up @@ -742,14 +758,33 @@ void RF24::printPrettyDetails(void) {
bool RF24::begin(_SPI* spiBus)
{
_spi = spiBus;
_init_pins();
return _init_radio();
if (_init_pins())
return _init_radio();
return false;
}

/****************************************************************************/

bool RF24::begin(_SPI* spiBus, uint16_t _cepin, uint16_t _cspin)
{
ce_pin = _cepin;
csn_pin = _cspin;
return begin();
}

#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)

/****************************************************************************/

bool RF24::begin(uint16_t _cepin, uint16_t _cspin)
{
ce_pin = _cepin;
csn_pin = _cspin;
return begin();
}

/****************************************************************************/

bool RF24::begin(void)
{

Expand Down Expand Up @@ -783,14 +818,20 @@ bool RF24::begin(void)

#endif // !defined(XMEGA_D3) && !defined(RF24_LINUX)

_init_pins();
return _init_radio();
if (_init_pins())
return _init_radio();
return false;
}

/****************************************************************************/

void RF24::_init_pins()
bool RF24::_init_pins()
{
if (!isValid()) {
// didn't specify the CSN & CE pins to c'tor nor begin()
return false;
}

#if defined (RF24_LINUX)

#if defined (MRAA)
Expand Down Expand Up @@ -829,6 +870,8 @@ void RF24::_init_pins()
delay(100);
#endif
#endif // !defined(XMEGA_D3) && !defined(LITTLEWIRE) && !defined(RF24_LINUX)

return true; // assuming pins are connected properly
}

/****************************************************************************/
Expand Down Expand Up @@ -917,6 +960,13 @@ bool RF24::isChipConnected()

/****************************************************************************/

bool RF24::isValid()
{
return ce_pin != 0xFFFF && csn_pin != 0xFFFF;
}

/****************************************************************************/

void RF24::startListening(void)
{
#if !defined(RF24_TINY) && !defined(LITTLEWIRE)
Expand Down
60 changes: 53 additions & 7 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,19 @@ class RF24 {
* - Older/Unsupported Arduino devices will use a default clock divider & settings configuration
* - For Linux: The old way of setting SPI speeds using BCM2835 driver enums has been removed as of v1.3.7
*/
RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spispeed = RF24_SPI_SPEED);
RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed = RF24_SPI_SPEED);

/**
* A constructor for initializing the radio's hardware dynamically
* @warning You MUST use begin(uint16_t, uint16_t) or begin(_SPI*, uint16_t, uint16_t) to pass both the digital output pin
* numbers connected to the radio's CE and CSN pins.
* @param _spispeed The SPI speed in Hz ie: 1000000 == 1Mhz <br><br>Users can specify default SPI speed by modifying
* `#define RF24_SPI_SPEED` in RF24_config.h
* - For Arduino, the default SPI speed will only be properly configured this way on devices supporting SPI TRANSACTIONS
* - Older/Unsupported Arduino devices will use a default clock divider & settings configuration
* - For Linux: The old way of setting SPI speeds using BCM2835 driver enums has been removed as of v1.3.7
*/
RF24(uint32_t _spi_speed = RF24_SPI_SPEED);

#if defined (RF24_LINUX)
virtual ~RF24() {};
Expand Down Expand Up @@ -220,8 +232,40 @@ class RF24 {
* @return same result as begin()
*/
bool begin(_SPI* spiBus);

/**
* Same as begin(), but allows dynamically specifying a SPI bus, CE pin,
* and CSN pin to use.
* @note This function assumes the `SPI::begin()` method was called before to
* calling this function.
*
* @warning This function is for the Arduino platform only
*
* @param spiBus A pointer or reference to an instantiated SPI bus object.
* @param _cepin The pin attached to Chip Enable on the RF module
* @param _cspin The pin attached to Chip Select (often labeled CSN) on the radio module.
* <br><br>For the Arduino Due board, the [Arduino Due extended SPI feature](https://www.arduino.cc/en/Reference/DueExtendedSPI)
* is not supported. This means that the Due's pins 4, 10, or 52 are not mandated options (can use any digital output pin) for the radio's CSN pin.
*
* @note The _SPI datatype is a "wrapped" definition that will represent
* various SPI implementations based on the specified platform (or SoftSPI).
* @see Review the [Arduino support page](md_docs_arduino.html).
*
* @return same result as begin()
*/
bool begin(_SPI* spiBus, uint16_t _cepin, uint16_t _cspin);
#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)

/**
* Same as begin(), but allows dynamically specifying a CE pin
* and CSN pin to use.
* @param _cepin The pin attached to Chip Enable on the RF module
* @param _cspin The pin attached to Chip Select (often labeled CSN) on the radio module.
* <br><br>For the Arduino Due board, the [Arduino Due extended SPI feature](https://www.arduino.cc/en/Reference/DueExtendedSPI)
* is not supported. This means that the Due's pins 4, 10, or 52 are not mandated options (can use any digital output pin) for the radio's CSN pin.
*/
bool begin(uint16_t _cepin, uint16_t _cspin);

/**
* Checks if the chip is connected to the SPI bus
*/
Expand Down Expand Up @@ -1079,10 +1123,7 @@ class RF24 {
*
* @return true if this is a legitimate radio
*/
bool isValid()
{
return ce_pin != 0xff && csn_pin != 0xff;
}
bool isValid();

/**
* Close a pipe after it has been previously opened.
Expand Down Expand Up @@ -1579,7 +1620,6 @@ class RF24 {
*/
/**@{*/


/**
* Open a pipe for reading
* @deprecated For compatibility with old code only, see newer function
Expand Down Expand Up @@ -1638,6 +1678,12 @@ class RF24 {
*/
/**@{*/

/**
* initializing function specific to all constructors
* (regardless of constructor parameters)
*/
void _init_obj();

/**
* initialize radio by performing a soft reset.
* @warning This function assumes the SPI bus object's begin() method has been
Expand All @@ -1648,7 +1694,7 @@ class RF24 {
/**
* initialize the GPIO pins
*/
void _init_pins();
bool _init_pins();

/**
* Set chip select pin
Expand Down

0 comments on commit 0c433f3

Please sign in to comment.