Little Help Understanding What's Happening In Code Between Library & Sketch?

Hi guys,

I've been trying my best to understand the interactions between a sketch and library file for the RF24 library and some wifi example sketches I'm using. Basically what I've done is post what I 'think' is happening in the code. I'm sure my assumptions will need some corrections, and appreciate anyone who offers a bit of their time to point me in the right direction.

First, here is what I am using
Example Sketches:
HelloWorld_TX Sketch: RF24Network/examples/helloworld_tx at master · maniacbug/RF24Network · GitHub
HelloWorld_RX Sketch: RF24Network/examples/helloworld_rx at master · maniacbug/RF24Network · GitHub

Libraries:
SPI.h: http://arduino.cc/en/Reference/SPI
RF24.h: GitHub - maniacbug/RF24: Arduino driver for nRF24L01
RF24Network.h: GitHub - maniacbug/RF24Network: Network Layer for nRF24L01(+) Radios

Now for my explanations of what is occuring in the code
Staring in the TX Sketch:

#include <RF24.h>
This code loads the RF24 library, which will allow you to use the functions, keywords, etc built into the RF24 library.

RF24 radio(9,10);
RF24 is a reference to a constructor found in the RF24 library. In the library it states the usage for the constructor as "RF24 (uint8_t _cepin, uint8_t _cspin)". So what this statement does is creates a new object called "radio". This object is assigns the ce pin to pin 9 and the cs pin to pin 10. I'm not sure what the "uint8_t" means? Is this referring to a function within the RF24 library that is called "uint8_t_cepin" to assign the pin within the library to "9"?

My basic understanding is that using the above code, "radio" has now become a reference to calling code from the RF24 library. So I should think of it almost like shorthand for more lengthy/sophisticated code found in the RF24 library?

RF24Network network(radio);
RF24Network is a reference to a constructor found in the RF24Network library. This constructor creates a new object called "network". This object is fed another object called "radio", which is actually the radio drivers (or pins?) to use to build the network of nodes?

So the way I should think of this is a layer of code, that references a more complex layer of code in a library, that references a more complex layer of code in another library...correct?

const uint16_t this_node = 1;
First, const means that this variable is assigned a value of "1" and the const type means that this cannot be changed by outside data? In reading the RF24Network Library, it lists a function as
"void RF24Network::begin(uint8_t_channel, uint16_t_node_address )"

Does this mean that in the previous line of code "RF24Network network(radio)" that the new object "network" has been assigned "1" to the variable "uint16_t_node_address" variable in this function? It's confusing to follow the logic as there doesn't appear to be any direct reference to this function, like "network(channel, node)"?

const uint16_t other_node = 0;
I assume that this is a variable assignment? I don't see any variables named this in the RF24Network Member List (found here: RF24Network: Member List

const unsigned long interval = 2000; //ms
unsigned long last_sent;
unsigned long packets_sent;
I believe these are fairly self explanatory. They are just variable declarations....correct? I'm not sure I understand what an 'unsigned' variable is. Can someone explain it in layman's terms?

struct payload_t
I googled 'struct' because I had no idea what it was, and it appears to be similar to declaring a class in other programming languages. So when we see:
struct payload_t
{
unsigned long ms;
unsigned long counter;
};

'struct' is creating a new 'class' called 'payload_t'. In turn, 'payload_t' has the fields 'ms' and 'counter' within it that are of the variable type 'long'. I still am not sure what 'unsigned' means though.

From what I understand, if I went out and said something like:
payload_t = {123, 456};
then the result would be the variable 'ms' would be set to '123' and the variable 'counter' would be set to '456', correct?

SPI.begin();
This is a direct reference to the 'begin' in the SPI library, but is it a reference to a function or keyword, because it's listed as a keyword in SPI.h? I'm aware only of the Serial.begin() function which set's the data rate in bits per second. What is the function of SPI.begin(), and what does it reference?

radio.begin();
This is a call to the object named 'radio' that was created earlier in our code using the RF24 constructor, correct? In the RF24.h library, there is a 'void begin(void);' function. I'm uncertain as to how this function accomplishes what is states it does which is "start listening on the pipes opened for reading". I assume this means the 'pipes' or 'pins' we assigned earlier of 9 and 10?

network.begin(/channel/ 90, /node address/ this_node);
This is a call to the object named 'network' that was created earlier in our code using the RF24Network constructor, correct? In the RF24Network.cpp file - which brings me to a quick question. When you include a library in arduino code, it references the .h file, not the .cpp file....so which file does the arduino sketch use? I found that the constructor, using the 'begin' function assigns the channel and node address to the object, network. So in this instance, in the RF24Network library where you have the code:
"void RF24Network::begin(uint8_t _channel, uint16_t _node_address )"
this is basically setting the uint8_t_channel = 90 and the uint16_t_node_address = this_node, correct?

network.update();
Similar to the begin function above in the RF24Network library, this calls the update function. In turn, this runs the code from the RF24Network library found in the RF24Network.cpp file to see if any data needs to be updated on the network, correct?

Sorry for the length guys. I know this is a bear to read, but it will really help my understanding of how the arduino code works. TIA :slight_smile:

unsigned means that the value can only take on positive numbers.

For example, a signed int ranges from about -32000 to 32000 while an unsigned int ranges from 0 to 64,000

unsigned ints can give you twice the range at the expense of being only positive. This is why they are used for millis(), for example, since millis() returns a counter that will never go below zero.

For a temperature, you would want a signed int since they can go below zero.

Thanks. :smiley: That makes sense. Now if I could just figure out the rest of this stuff :grin:

I haven't looked at your code but I will try to add a few comments to your questions

jdlev:
Now for my explanations of what is occuring in the code
Staring in the TX Sketch:

#include <RF24.h>
This code loads the RF24 library, which will allow you to use the functions, keywords, etc built into the RF24 library.

RF24 radio(9,10);
RF24 is a reference to a constructor found in the RF24 library. In the library it states the usage for the constructor as "RF24 (uint8_t _cepin, uint8_t _cspin)". So what this statement does is creates a new object called "radio". This object is assigns the ce pin to pin 9 and the cs pin to pin 10. I'm not sure what the "uint8_t" means? Is this referring to a function within the RF24 library that is called "uint8_t_cepin" to assign the pin within the library to "9"?

uint8_t just define the data type for the variable - unsigned integer 8 bits wide - the same as byte.

My basic understanding is that using the above code, "radio" has now become a reference to calling code from the RF24 library. So I should think of it almost like shorthand for more lengthy/sophisticated code found in the RF24 library? yes - strictly speaking "radio" is an instance of the RF24 class. With some libraries - eg. Servo - you can have several instances

RF24Network network(radio);
RF24Network is a reference to a constructor found in the RF24Network library. This constructor creates a new object called "network". This object is fed another object called "radio", which is actually the radio drivers (or pins?) to use to build the network of nodes? Again, "network" is an instance of the RF24Network class. By the way the instances can have any name you wish

So the way I should think of this is a layer of code, that references a more complex layer of code in a library, that references a more complex layer of code in another library...correct?

const uint16_t this_node = 1;
First, const means that this variable is assigned a value of "1" and the const type means that this cannot be changed by outside data? In reading the RF24Network Library, it lists a function as
"void RF24Network::begin(uint8_t_channel, uint16_t_node_address )" You may be making more of this than necessary. the line in bold just defines a variable called "this_node" and gives it the value 1 The variable is probably used later in the code

Does this mean that in the previous line of code "RF24Network network(radio)" that the new object "network" has been assigned "1" to the variable "uint16_t_node_address" variable in this function? It's confusing to follow the logic as there doesn't appear to be any direct reference to this function, like "network(channel, node)"?

const uint16_t other_node = 0;
I assume that this is a variable assignment? I don't see any variables named this in the RF24Network Member List (found here: RF24Network: Member List

const unsigned long interval = 2000; //ms
unsigned long last_sent;
unsigned long packets_sent;
I believe these are fairly self explanatory. They are just variable declarations....correct? I'm not sure I understand what an 'unsigned' variable is. Can someone explain it in layman's terms? I think this was already explained

struct payload_t
I googled 'struct' because I had no idea what it was, and it appears to be similar to declaring a class in other programming languages. So when we see:
struct payload_t
{
unsigned long ms;
unsigned long counter;
};

'struct' is creating a new 'class' called 'payload_t'. In turn, 'payload_t' has the fields 'ms' and 'counter' within it that are of the variable type 'long'. I still am not sure what 'unsigned' means though.

A struct is not a class - it is just a container for data. A struct can contain several different data items with different data types. If you think of a byte as a container for 8 bits of data a struct is just a more complex container.

From what I understand, if I went out and said something like:
payload_t = {123, 456};
then the result would be the variable 'ms' would be set to '123' and the variable 'counter' would be set to '456', correct? yes

SPI.begin();
This is a direct reference to the 'begin' in the SPI library, but is it a reference to a function or keyword, because it's listed as a keyword in SPI.h? I'm aware only of the Serial.begin() function which set's the data rate in bits per second. What is the function of SPI.begin(), and what does it reference? It has much the same purpose for the SPI library as Serial.begin for the Serial library

radio.begin();
This is a call to the object named 'radio' that was created earlier in our code using the RF24 constructor, correct? In the RF24.h library, there is a 'void begin(void);' function. I'm uncertain as to how this function accomplishes what is states it does which is "start listening on the pipes opened for reading". I assume this means the 'pipes' or 'pins' we assigned earlier of 9 and 10? You will need to study all the RF24 code (.h and .cpp files) to figure this out.

network.begin(/channel/ 90, /node address/ this_node);
This is a call to the object named 'network' that was created earlier in our code using the RF24Network constructor, correct? In the RF24Network.cpp file - which brings me to a quick question. When you include a library in arduino code, it references the .h file, not the .cpp file....so which file does the arduino sketch use? BOTH
I found that the constructor, using the 'begin' function assigns the channel and node address to the object, network. So in this instance, in the RF24Network library where you have the code:
"void RF24Network::begin(uint8_t _channel, uint16_t _node_address )"
this is basically setting the uint8_t_channel = 90 and the uint16_t_node_address = this_node, correct? yes - this is using the "this_node variable you queried earlier

network.update();
Similar to the begin function above in the RF24Network library, this calls the update function. In turn, this runs the code from the RF24Network library found in the RF24Network.cpp file to see if any data needs to be updated on the network, correct? probably

...R

Robin2:
A struct is not a class - it is just a container for data. A struct can contain several different data items with different data types. If you think of a byte as a container for 8 bits of data a struct is just a more complex container.

In C++ classes, structs, and unions are all types of classes. The only difference between a class and a struct is the members of a struct are all public.

In fact when writing a class that must be a POD, you can write it easier by using the 'struct' class key rather than 'class'. This is because a POD must be trivial and a standard-layout class, which must have no protected or private members.

Uncomment the 'public:' to allow POD initialization:

struct abc{
  int a;
  int b;
};
class def{
  //public:
    int a;
    int b;
};

void setup(){
  abc a = {1,2};
  def b = {1,2};
}

void loop(){}

pYro_65:

Robin2:
A struct is not a class - it is just a container for data. A struct can contain several different data items with different data types. If you think of a byte as a container for 8 bits of data a struct is just a more complex container.

In C++ classes, structs, and unions are all types of classes.

I apologize for over-simpifying (and my ignorance) but I suspect your explanation might be a bit too advanced in comparison to the OPs questions.

I'm not familiar with the OOP stuff in C/C++ but in Ruby (which is entirely OOP and was never just an add-on for another language) the things such as structs, unions etc would be OBJECTS but they would not be CLASSES.

...R