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