I am not sure about "Best" practice, but...
Again that library looks like you can hard code it to any specific SPI port, (by changing the code)...
On my install the file is: C:\Users\kurte\Documents\Arduino\libraries\RF24\utility\Teensy\RF24_arch_config.h
Which has a line in it:
Which you can change to SPI1 or SPI2... But than it is hard coded to that one... Which is probably not what you want:
Then it looks like this library is hard coded to use _SPI, like:
Code:
_SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
If it were my library, I would probably do something like:
Probably add a member variable in the RF24 class like:
And change everywhere in the code that uses the _SPI. to _pspi->
Like:
Code:
_pspi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
I would then need to decide when to set the _pspi variable, there could be a few different ways like:
a) Constructor assign it to default or as initial value when defined:
In constructor: _pspi = &_SPI;
or
Code:
/****************************************************************************/
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),
csDelay(5),_pspi(&SPI)
{
pipe0_reading_address[0] = 0;
if(spi_speed <= 35000){ //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
spi_speed = RF24_SPI_SPEED;
}
}
In definition: SPIClass *_pspi = &SPI;
b) could do the assignment probably in the begin method.
But now how to get the specific SPI port set, again a few options...
Add it to the constructor, like:
By reference:
Code:
RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spispeed = RF24_SPI_SPEED, SPIClass &spi=SPI);
By Pointer
Code:
RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spispeed = RF24_SPI_SPEED, SPIClass *pspi=&SPI);
Note: You may want second version of constructor as to make it such that library still works with bitbang version (SOFTWARE SPI)
In both of these cases change constructor implementation to use this than the hard coded one... Like:
Code:
/****************************************************************************/
RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed, SPIClass spi)
: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),_pspi(&spi)
{
pipe0_reading_address[0] = 0;
if(spi_speed <= 35000){ //Handle old BCM2835 speed constants, default to RF24_SPI_SPEED
spi_speed = RF24_SPI_SPEED;
}
}
And/or you could add it as an option parameter to the begin method: where maybe you have option like: radio.begin(SPI1) or radio.begin(&SPI1)
depending on if you want to pass by reference or pointer.
And/Or you could add new method like setSPI which again could be either pass by reference or by pointer: radio.setSPI(SPI1);
Which would need to be called before the begin is called.
-----------
In many cases like this if you can get something working great. Better yet if you can then get the library owner to take the changes in with a Pull Request even better!
Hope that helps