0

I'm a beginner in creating my own libraries so the following might contain some stupid stuff.

Anyways, I'm using NRF24L01+ modules for wireless communication and I've been utilizing the magnificent RF24 library (https://github.com/TMRh20/RF24) to operate these modules using Arduino. I wanted to clean up my code and decided to write my own library that uses the R24 library. My own library would contain all the subroutines that handle TX and RX functions.

Problem is that the RF24 library is initialized using e.g.

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);

To begin using the NRF24L01+ one would the use the RF24 library like for example:

byte addresses[][6] = {"1Node","2Node"}; // Radio pipe addresses for the 2 nodes to communicate.
Serial.begin(57600);
printf_begin();
radio.begin();

radio.setPALevel(RF24_PA_HIGH);
radio.setCRCLength(RF24_CRC_16);

radio.setRetries(2,15);
radio.setChannel(RFChannel); 
if(radioNumber){
radio.openWritingPipe(addresses[1]); // Both radios listen on the same pipes by default, but opposite addresses
radio.openReadingPipe(1,addresses[0]); // Open a reading pipe on address 0, pipe 1
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}

if(radioNumber == 1){
radio.startListening(); // Start listening
}
radio.printDetails(); 

Well, now I would like to make a library that would allow me just to call setup to handle all of the above. My library would be called RadioComm so I would call in my main code:

RadioComm rComm();

setup() {
rComm.setup();
}

The problem is that how do I use the RF24 library and all its functions in my library? To initialize the RF24 library in my own library I would have to call somehow this

RF24 radio(7,8);

and use all the radio. commands. How do I tell my own library that everything starting with "radio." is related to RF24 library because right now I'm getting this:

RadioComm.cpp: In member function 'void RadioComm::setup()':
RadioComm.cpp:20:18: error: 'radio' was not declared in this scope
radio(pin1,pin2); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10

Below are my .h .cpp and .ino files

Lib_test.ino

#include <RadioComm.h>
#include <RF24.h>
#include <SPI.h>

RadioComm rComm;

void setup() {

rComm.setup();

}

void loop() {

}

RadioComm.h

#ifndef RadioComm_h
#define RadioComm_h

#include "Arduino.h"
#include <RF24.h>


struct dataStruct{
unsigned long id;
float value1;
float value2;
float value3;
float value4;
};


class RadioComm
{

public:

RadioComm();
void setup(void);

private:
int pin1;
int pin2;


};

#endif

RadioComm.cpp

#include "Arduino.h"
#include "RadioComm.h"
#include <RF24.h>
#include "printf.h"


RadioComm::RadioComm(void)
{}


void RadioComm::setup()
{
byte addresses[][6] = {"1Node","2Node"}; // Radio pipe addresses for the 2 nodes to communicate.
radio(pin1,pin2); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
Serial.begin(57600);
printf_begin();
radio.begin();
radio.setPALevel(RF24_PA_HIGH);
radio.setCRCLength(RF24_CRC_16);


radio.setRetries(2,15);
radio.setChannel(RFChannel); 
if(radioNumber){
radio.openWritingPipe(addresses[1]); // Both radios listen on the same pipes by default, but opposite addresses
radio.openReadingPipe(1,addresses[0]); // Open a reading pipe on address 0, pipe 1
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}

if(radioNumber == 1){
radio.startListening(); // Start listening
}
radio.printDetails(); 
}

Any help is appreciated.

Thanks.

1

Firstly, to address your compilation error:

RadioComm rComm();

That is not a constructor, that is a function prototype. When you have no parameters to a constructor you must not include the brackets or it confuses the poor little compiler.

RadioComm rComm;

Now - to address your main question. Well, there's many ways of doing it, but the way I always tackle this is to have your sketch define the RF24 object and pass it to the library for later use.

In the class definition, in the private section, add:

RF24 *radio;

Then change the constructor to have:

RadioComm(RF24 &r);

The constructor itself would then read:

RadioComm::RadioComm(RF24 &r) {
    radio = &r;
}

Now throughout your library, instead of working with radio.whatever you need to work with radio->whatever since you are now using a pointer to an object not the object itself.

Then in your sketch you need to adjust your constructor to be:

RF24 myRadio(7, 8);
RadioComm rComm(myRadio);

The beauty of this is it decouples the configuration of the RF24 class from the RadioComm class, so you can use the same library in a different situation on a different board with different pins without having to change the library - just change the RF24 constructor in your sketch.

It also gives you direct access to the RF24 class in your sketch so you can do any advanced manual tweaks outside the library should you wish to.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.