Hello,
I am trying to turn my arduino sketch into the library. But I need to use object of another library inside. I need to build my own library where I include RF24 library in it and where I will work with this object. Currently I have following code but still with problems:
FLNRF24.cpp
#include "Arduino.h"
#include "FLNRF24.h"
#include "SPI.h"
#include "nRF24L01.h"
#include "RF24.h"
FLNRF24::FLNRF24() : radio (9,10)
{
radio.begin(); // NRF24L01+
// 4 values to set for long range distance radio
// Max power
radio.setPALevel( RF24_PA_MAX ) ;
// Min speed
radio.setDataRate( RF24_250KBPS ) ;
// 8 bits CRC
radio.setCRCLength( RF24_CRC_8 ) ;
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(32);
radio.openWritingPipe(_writingPipes[0]);
radio.openReadingPipe(1,_readingPipes[0]);
radio.openReadingPipe(2,_readingPipes[1]);
radio.openReadingPipe(3,_readingPipes[2]);
radio.openReadingPipe(4,_readingPipes[3]);
radio.openReadingPipe(5,_readingPipes[4]);
radio.startListening();
}
unsigned long FLNRF24::listenRadio()
{
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &PACKET, sizeof(unsigned long) );
return PACKET;
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
}
}
Name of the base class is RF24. There is created object in the FLNRF24.h file in private part by following line: RF24 radio; you can see it in the code I pasted in the first post.
By the way here is the tutorial which helped me to create it:
So I think the problem is how to handel the object within the class (to keep the object alive for whole time of the arduino sketch, or how to access this object within the FLNRF24.cpp file).
CPP file (now 'radio' is declared locally in each function):
#include "FLNRF24.h"
uint8_t FLNRF24::cepin=0;
uint8_t FLNRF24::cspin=0;
FLNRF24::FLNRF24(uint8_t ce, uint8_t cs)
{
cepin = ce;
cspin = cs;
RF24 radio(cepin, cspin);
radio.begin(); // NRF24L01+
// 4 values to set for long range distance radio
// Max power
radio.setPALevel( RF24_PA_MAX ) ;
// Min speed
radio.setDataRate( RF24_250KBPS ) ;
// 8 bits CRC
radio.setCRCLength( RF24_CRC_8 ) ;
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(32);
radio.openWritingPipe(_writingPipes[0]);
radio.openReadingPipe(1,_readingPipes[0]);
radio.openReadingPipe(2,_readingPipes[1]);
radio.openReadingPipe(3,_readingPipes[2]);
radio.openReadingPipe(4,_readingPipes[3]);
radio.openReadingPipe(5,_readingPipes[4]);
radio.startListening();
}
unsigned long FLNRF24::listenRadio()
{
RF24 radio(cepin, cspin);
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &PACKET, sizeof(unsigned long) );
return PACKET;
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
}
}
example skech:
#include <SoftwareSerial.h>
#include "FLSigfox.h"
#include "FLNRF24.h"
#include "SPI.h"
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
unsigned long PACKET;
FLNRF24 nrf(9,10); //now specifying the pins to use here instread on in the library
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(nrf.listenRadio());
}
I'm thinking you can't call the constructor like that. It looks like you want your class to contain a member that's an object of type RH24 rather than inheriting the RH24 class. I'd use a pointer. I can't compile / test this because I don't have the library / hardware. But, something like this:
#include "Arduino.h"
#include "FLNRF24.h"
#include "SPI.h"
#include "nRF24L01.h"
#include "RF24.h"
FLNRF24::FLNRF24() : radio (9,10)
{
radio = new RF24(9,10);
radio->begin(); // NRF24L01+
// 4 values to set for long range distance radio
// Max power
radio->setPALevel( RF24_PA_MAX ) ;
// Min speed
radio->setDataRate( RF24_250KBPS ) ;
// 8 bits CRC
radio->setCRCLength( RF24_CRC_8 ) ;
// optionally, increase the delay between retries & # of retries
radio->setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio->setPayloadSize(32);
radio->openWritingPipe(_writingPipes[0]);
radio->openReadingPipe(1,_readingPipes[0]);
radio->openReadingPipe(2,_readingPipes[1]);
radio->openReadingPipe(3,_readingPipes[2]);
radio->openReadingPipe(4,_readingPipes[3]);
radio->openReadingPipe(5,_readingPipes[4]);
radio->startListening();
}
unsigned long FLNRF24::listenRadio()
{
if ( radio->available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio->read( &PACKET, sizeof(unsigned long) );
return PACKET;
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
}
}
I tried your suggested code. It compile without problems but not doing what is expected. It event do not reach the setup part in the sketch file. :o( It is caused by the FLNRF24 class as when I remove it from the arduino sketch it works ok.
boylucky:
I tried your suggested code. It compile without problems but not doing what is expected. It event do not reach the setup part in the sketch file. :o( It is caused by the FLNRF24 class as when I remove it from the arduino sketch it works ok.
I guess the question is “what do you want to do?”
Are you wanting to inherit from RF24 and add some methods, or are you trying to do something else?
Is there also a way that I would not want to add some other functions to existing class, but I would just use that class within my own class? I mean I would define object of RF24 class within my own class where I want to use it and do same operations as in the normal sketch? To be able to manipulate with this object normaly as it would be in the sketch.
For example I want to do SoftwareSerial in my new class. I would like to do it the way that I create object from SoftwareSerial inside the new class and will use it in some functions within my new class.
boylucky:
ok, thanks. I am checking the page if I succeed.
Is there also a way that I would not want to add some other functions to existing class, but I would just use that class within my own class? I mean I would define object of RF24 class within my own class where I want to use it and do same operations as in the normal sketch? To be able to manipulate with this object normaly as it would be in the sketch.
Is your own class functionally a radio object or does it do something else? If you plan to use any methods of the base class in addition to the methods of your own class, then use inheritance instead. It will be more straightforward in the long run.
Think in terms of an "is a" relationship. If your created class "is a" kind of Software serial, then use inheritance. If it's a "something that uses" Software serial, then no.
In this case it is something that only use software serial. I just want to print with SoftwareSerial inside my class. For example function which send commands to SoftwareSerial:
boylucky:
ok, thanks. I am checking the page if I succeed.
Is there also a way that I would not want to add some other functions to existing class, but I would just use that class within my own class? I mean I would define object of RF24 class within my own class where I want to use it and do same operations as in the normal sketch? To be able to manipulate with this object normaly as it would be in the sketch.
of course a class can contain classes as members:
class Aclass{
public:
doSomething(){Serial.println(F("test"));}
private:
doSomethingElse(){Serial.println(F("test2"));}
};
class Bclass{
public:
giddyUp(){a.doSomething();}
// doPrivate(){a.doSomethingElse();} // int Aclass::doSomethingElse()' is private
private:
Aclass a;
};
Bclass b;
void setup()
{
Serial.begin(9600);
b.giddyUp();
}
void loop()
{
}