Type casting ...

I’m using the MiRF library to make several nRF24L01+ modules talk to one another. One of them is designated a “master” in that it tells the others what they should be doing. Sending data out is done with the following call:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

void transmit(const int seq) {
  byte c;
  c = seq;
  Mirf.send(&c);
  while (Mirf.isSending());
}

void setup() {
  Mirf.cePin = 7;
  Mirf.csnPin = 8;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.payload = 2;
  Mirf.channel = 5;
  Mirf.config();
  Mirf.configRegister(RF_SETUP,0x06);
}

void loop() {
  // I'm only sending out numbers
  for (int data = 0; data < 5; data++) {
    Mirf.setTADDR((byte*)"RC_01");
    transmit(data);
  }
}

My problem is that I have several “RC_” modules, each one has a different number (from 01 to 13) …

How can I construct that string piece “RC_” with the number behind and pass that to the Mirf call? Using the String() object doesn’t work because of different types. For example:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

String rcvrString;
String rcvrName;

void transmit(const int seq) {
  byte c;
  c = seq;
  Mirf.send(&c);
  while (Mirf.isSending());
}

void setup() {
  Mirf.cePin = 7;
  Mirf.csnPin = 8;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.payload = 2;
  Mirf.channel = 5;
  Mirf.config();
  Mirf.configRegister(RF_SETUP,0x06);

  rcvrString = String("RC_");
  rcvrName = String();
}

void loop() {
  // I'm only sending out numbers
  for (int data = 0; data < 5; data++) {
    // cycle through all receivers
    for (int rcvr = 0; rcvr < 15; rcvr++) {
      rcvrName = rcvrString + String(rcvr);  // this does create the correct information in rcvrName
      Mirf.setTADDR((byte*)rcvrName);        // this fails with 'invalid cast from type 'String' to type 'byte*'
      transmit(data);
    }
  }
}

I was kinda expecting that error since I’m working with the String() object, and the .setTADDR() command is doing a byte cast. Question is, how do I do this properly?

  rcvrString = String("RC_");
  rcvrName = String();

Directly invoking the constructor is unnecessary.

  rcvrString = "RC_";
  rcvrName = "";
      Mirf.setTADDR((byte*)rcvrName);        // this fails with 'invalid cast from type 'String' to type 'byte*'

Of course it does, because rcvrName is not meaningfully converted to an array of bytes. The character string that the String class wraps is, though.

    char theName[6];
    rcvrName.toCharArray(theName, 6);
    Mirf.setTADDR((byte*)theName);

PaulS:   rcvrString = String("RC_");   rcvrName = String();

Directly invoking the constructor is unnecessary.

That was taken from the example on the StringAdditionOperator page ...

PaulS:     char theName[6];     rcvrName.toCharArray(theName, 6);     Mirf.setTADDR((byte*)theName);

Ahhh, so I needed to run it through .toCharArray(). Gotcha. Duh, that makes sense now that I think about it.

Thanks!

That was taken from the example on the StringAdditionOperator page ...

Crappy documentation to go with a crappy implementation does not surprise me.

LOL! I suppose the same can be said about those examples with this in them:int r=0, g=0, b=0;

I suppose the same can be said about those examples with this in them:

No, that is perfectly valid C code. Not my style, usually, but it is perfectly valid, and uses no more memory than

int r = 0;
int g = 0;
int b = 0;

Creating a useless instance of the String class, on the other hand, does use (read that as waste) more memory.