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.