Go Down

Topic: Encapsulating RF24 into my own library (Read 290 times) previous topic - next topic

JohnHoward

Goal:  Moving all the details of RF24 comms out of my mainline code into a library.

I have been successful in building NRF24L01 data links using the ManiacBug examples and libraries

What I'd like to do is move all the logic into my own library and inherit the RF24 library into my class(es) before I start building all sorts of message functions on top of that.


  • Is this even a good idea with respect the way RF24 is architected?
    Is my stab at coding (below) appropriate (from an arduino and a C language perspective)?  I'm not that well-versed in C and this is as much for improving my skills as it is for creating a working end product.




One thing that puzzles me is how the RF24 class initializes with cepin and cspin parameters.  I put what I originally coded inline in my simple testing sketches into the .begin() of my AlarmReceiver class.  Is that the correct way to initialize the RF24 object so my class can use it?

Try to overlook the fact I have not properly declared variables such as payload, _cepin and _cspin, as I'm at the stage where I'm just trying to establish how the library pieces will fit together, not getting something that will necessarily please the compiler.  I saw the 'Hierarchy' tutorial but it is far, far from being complete so I still have a number of blind spots to work through.

Code: [Select]

#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "AlarmReceiver.h"


AlarmReceiver::AlarmReceiver(cepin, cspin) : public RF24 {

_cepin = cepin;
_cspin = cspin;
}

AlarmReceiver::begin() {

RF24 _radio(_cepin, _cspin);

}

void AlarmReceiver::open(uint64_t pipe)
{
uint64_t _pipe = pipe;

_radio.begin();
_radio.setRetries(15,15);
_radio.setPayloadSize(8);
_radio.openReadingPipe(1,_pipe);
_radio.startListening();
}

unsigned long AlarmReceiver::listen() {

    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( &payload, sizeof(unsigned long) );

      }
}

return payload;
}

PeterH

I'd have thought that the cleanest interface would be to have the RF24 instance passed in to your library via a constructor argument or argument to a begin() method.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up