Go Down

Topic: Using the NRF24 class in multiple files (Read 526 times) previous topic - next topic

hazza_ob

I'm trying to access the functions within the nRF24 library whilst having my own sketch broken into .ino, .h and .cpp.

However if I don't include
Code: [Select]
RF24 radio(3, 4);
in both the .cpp and .ino file, the class functions aren't recognised.

however if i do include
Code: [Select]
RF24 radio(3, 4);
in both files, a load of errors come up about things being already initialised.

How can i fix this? i.e. have a subroutines.h/.cpp file that can work with the radio class as well as a node.ino file that can access the commands?

The rest of the code is below if necessary:
node.ino
Code: [Select]

#include "radioInterface.h"

RF24Data volatile iFile;

RF24 radio(3, 4);
void setup() {

  setupRadio();
  radio.printDetails();
}

void loop() {
  // Do nothing for now

}


radioInterface.h
Code: [Select]

//Prototype function definition
void setupRadio(void);


radioInterface.cpp
Code: [Select]

#include "Arduino.h"
#include "radioInterface.h"

void setupRadio(void) {

setupRadio();
  radio.begin();                           // Setup and configure rf radio
  radio.setChannel(74);                    // Set the channel
  radio.setPALevel(RF24_PA_HIGH);          // Set PA LOW for this demonstration. We want the radio to be as lossy as possible for this example.
  radio.setDataRate(RF24_1MBPS);           // Raise the data rate to reduce transmission distance and increase lossiness
  radio.enableDynamicPayloads();           // Enable Dynamic Payloads
  radio.setAutoAck(1);                     // Ensure autoACK is enabled
  radio.setRetries(2, 15);                 // Optionally, increase the delay between retries. Want the number of auto-retries as high as possible (15)
  radio.setCRCLength(RF24_CRC_16);         // Set CRC length to 16-bit to ensure quality of data
  radio.maskIRQ(1, 1, 0);                  // Only recieve notifications for data recieved;
 
  radio.openWritingPipe(pipes[0]);         // Open the default reading and writing pipe
  radio.openReadingPipe(1, pipes[1]);
 
  radio.powerUp();                         //Power up the radio
  delay(5);
 
  radio.startListening();                  // Start listening
}

septillion

Damnit forum, just wrote a long answer but the forum gave me an error.

Short answer, only declare it where you need it. If you only need it in your files, only declare it there. Don't forget ti include the library there and not in the ino.

Or, if you want to use it in both, just declare it in the .ino and pass it as parameter you functions. If you only want to do that once (for example in a begin() call, make a pointer.

Last option would be to declare it as a extern but I think that's the ugly way to do it.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Robin2

Create all the functions that use the nRf24 in one file and then you can call those functions from other files.

I have a project with a .h file for all my wireless code and another .h file for all my motor control code. When it feels like it the motor control code calls the appropriate wireless function. The .ino file is really just there to include the other files.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

hazza_ob

Damnit forum, just wrote a long answer but the forum gave me an error.

Short answer, only declare it where you need it. If you only need it in your files, only declare it there. Don't forget ti include the library there and not in the ino.

Or, if you want to use it in both, just declare it in the .ino and pass it as parameter you functions. If you only want to do that once (for example in a begin() call, make a pointer.

Last option would be to declare it as a extern but I think that's the ugly way to do it.

Can you give a code example for how you're using it in both files?
Cheers - H

hazza_ob

Create all the functions that use the nRf24 in one file and then you can call those functions from other files.

I have a project with a .h file for all my wireless code and another .h file for all my motor control code. When it feels like it the motor control code calls the appropriate wireless function. The .ino file is really just there to include the other files.

...R
That's actually a pretty good idea, thanks! However if i was going to using the functions in both files, how would i do this?

Thanks, -H

Robin2

#5
Jul 26, 2017, 10:38 am Last Edit: Jul 26, 2017, 10:41 am by Robin2
That's actually a pretty good idea, thanks! However if i was going to using the functions in both files, how would i do this?
I have never experienced your problem so I am not sure how to answer that.

My .ino file has this
Code: [Select]
#include "./LocoSlaveBaseBFiles/LocSlvRadBTin1634.h"
#include "./LocoSlaveBaseBFiles/LocSlvSoftStart.h"

and in setup() it has
Code: [Select]
setupRadio();
setupMotorControl();

which are calls to functions in each of those files.

In the ...SoftStart.h file there is a call
Code: [Select]
doRadioStuff();
which is a function in the other file.


Ahhhh - I wonder is your problem due to the need to declare function prototypes at the top oh each .h file

This is at the top of the ...SoftStart.h file
Code: [Select]
    // function prototypes
void showDebugData(int interval) ;
void doRadioStuff();
void revDetectorISR();
void applyAcceleration();
void checkForStopCommand();
void checkForStall();
void checkForSteps();
void updatePWM();
long calcPWM(long errVal, byte kFactor);
void outputMotorPwr();


These functions are all within that file. And there is a similar piece at the top of the other file for its function prototypes.

With .ino files the Arduino system automatically creates the function prototypes.

...R

PS. Just to avoid confusion, the Arduino IDE does not recognize relative file references of the type '" ./LocoSlav...". I have another Python program that converts them to absolute references.
Two or three hours spent thinking and reading documentation solves most programming problems.

septillion

Uncompiled, just from the top of my head :D

If you want to define it in the ino

.ino
Code: [Select]

#include "RF24.h"

RF radio(3,4);

void setup(){
  myThingsBegin(radio);
}


.h
Code: [Select]

class RF;

static RF* myRadio;


.cpp
Code: [Select]

#include "myThingsBegin.h"
#include "RF24.h"

void myThingsBegin(RF24& radio){
  myRadio = &radio;
  myRadio->someFunction();
}


Or the extern route, but that will fail if you rename it and doesn't lend itself to OOP that much
.ino
Code: [Select]

#include "RF24.h"

RF radio(3,4);

void setup(){
  myThingsBegin(radio);
}


.h
Code: [Select]

class RF;

extern RF radio;


.cpp
Code: [Select]

#include "myThingsBegin.h"
#include "RF24.h"

void myThingsBegin(){
  radio.someFunction();
}
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Robin2

Uncompiled, just from the top of my head :D
C/C++ really pisses me off when it is necessary to do all that stuff (not your fault, of course),

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up