Simplified example to use less calls.
Defaults now to 32-byte payloads, but you can call in with any size under that. Added example to docs.
This commit is contained in:
@@ -29,13 +29,9 @@
|
||||
|
||||
RF24 radio(8,9);
|
||||
|
||||
// sets the address (and therefore the role of operation) of this unit.
|
||||
// lo = node0, hi = node1
|
||||
const int addr_pin = 7;
|
||||
|
||||
// The actual value of the node's address will be filled in by the sketch
|
||||
// when it reads the addr_pin
|
||||
int node_address;
|
||||
// sets the role of this unit in hardware. Connect to GND to be the 'ping' sender.
|
||||
// Connect to +5V to be the 'pong' receiver.
|
||||
const int role_pin = 7;
|
||||
|
||||
//
|
||||
// Topology
|
||||
@@ -47,21 +43,18 @@ const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
|
||||
//
|
||||
// Role management
|
||||
//
|
||||
// Set up address & role. This sketch uses the same software for all the nodes
|
||||
// Set up role. This sketch uses the same software for all the nodes
|
||||
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
|
||||
// which node it is.
|
||||
//
|
||||
// This is done through the addr_pin. Set it low for address #0, high for #1.
|
||||
// This is done through the role_pin
|
||||
//
|
||||
|
||||
// The various roles supported by this sketch
|
||||
typedef enum { role_rx = 1, role_tx1, role_end } role_e;
|
||||
typedef enum { role_ping_out = 1, role_pong_back } role_e;
|
||||
|
||||
// The debug-friendly names of those roles
|
||||
const char* role_friendly_name[] = { "invalid", "Receive", "Transmit"};
|
||||
|
||||
// Which role is assumed by each of the possible hardware addresses
|
||||
const role_e role_map[2] = { role_rx, role_tx1 };
|
||||
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
|
||||
|
||||
// The role of the current running sketch
|
||||
role_e role;
|
||||
@@ -69,17 +62,19 @@ role_e role;
|
||||
void setup(void)
|
||||
{
|
||||
//
|
||||
// Address & Role
|
||||
// Role
|
||||
//
|
||||
|
||||
// set up the address pin
|
||||
pinMode(addr_pin, INPUT);
|
||||
digitalWrite(addr_pin,HIGH);
|
||||
delay(20); // Just to get a solid reading on the addr pin
|
||||
// set up the role pin
|
||||
pinMode(role_pin, INPUT);
|
||||
digitalWrite(role_pin,HIGH);
|
||||
delay(20); // Just to get a solid reading on the role pin
|
||||
|
||||
// read the address pin, establish our address and role
|
||||
node_address = digitalRead(addr_pin) ? 0 : 1;
|
||||
role = role_map[node_address];
|
||||
// read the address pin, establish our role
|
||||
if ( digitalRead(role_pin) )
|
||||
role = role_pong_back;
|
||||
else
|
||||
role = role_ping_out;
|
||||
|
||||
//
|
||||
// Print preamble
|
||||
@@ -87,8 +82,7 @@ void setup(void)
|
||||
|
||||
Serial.begin(9600);
|
||||
printf_begin();
|
||||
printf("\n\rRF24 pingpair example\n\r");
|
||||
printf("ADDRESS: %x\n\r",node_address);
|
||||
printf("\n\rRF24/examples/pingpair/\n\r");
|
||||
printf("ROLE: %s\n\r",role_friendly_name[role]);
|
||||
|
||||
//
|
||||
@@ -97,32 +91,26 @@ void setup(void)
|
||||
|
||||
radio.begin();
|
||||
|
||||
// Set channel (optional)
|
||||
radio.setChannel(1);
|
||||
|
||||
// Set size of payload (optional, but recommended)
|
||||
// The library uses a fixed-size payload, so if you don't set one, it will pick
|
||||
// one for you!
|
||||
radio.setPayloadSize(sizeof(unsigned long));
|
||||
|
||||
//
|
||||
// Open pipes to other nodes for communication (required)
|
||||
// Open pipes to other nodes for communication
|
||||
//
|
||||
|
||||
// This simple sketch opens two pipes for these two nodes to communicate
|
||||
// back and forth.
|
||||
// Open 'our' pipe for writing
|
||||
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
|
||||
|
||||
// We will open 'our' pipe for writing
|
||||
radio.openWritingPipe(pipes[node_address]);
|
||||
|
||||
// We open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
|
||||
int other_node_address;
|
||||
if (node_address == 0)
|
||||
other_node_address = 1;
|
||||
if ( role == role_ping_out )
|
||||
{
|
||||
radio.openWritingPipe(pipes[0]);
|
||||
radio.openReadingPipe(1,pipes[1]);
|
||||
}
|
||||
else
|
||||
other_node_address = 0;
|
||||
radio.openReadingPipe(1,pipes[other_node_address]);
|
||||
|
||||
{
|
||||
radio.openWritingPipe(pipes[1]);
|
||||
radio.openReadingPipe(1,pipes[0]);
|
||||
}
|
||||
|
||||
//
|
||||
// Start listening
|
||||
//
|
||||
@@ -139,10 +127,10 @@ void setup(void)
|
||||
void loop(void)
|
||||
{
|
||||
//
|
||||
// Transmitter role. Repeatedly send the current time
|
||||
// Ping out role. Repeatedly send the current time
|
||||
//
|
||||
|
||||
if (role == role_tx1)
|
||||
if (role == role_ping_out)
|
||||
{
|
||||
// First, stop listening so we can talk.
|
||||
radio.stopListening();
|
||||
@@ -150,7 +138,7 @@ void loop(void)
|
||||
// Take the time, and send it. This will block until complete
|
||||
unsigned long time = millis();
|
||||
printf("Now sending %lu...",time);
|
||||
bool ok = radio.write( &time );
|
||||
bool ok = radio.write( &time, sizeof(unsigned long) );
|
||||
|
||||
// Now, continue listening
|
||||
radio.startListening();
|
||||
@@ -171,7 +159,7 @@ void loop(void)
|
||||
{
|
||||
// Grab the response, compare, and send to debugging spew
|
||||
unsigned long got_time;
|
||||
radio.read( &got_time );
|
||||
radio.read( &got_time, sizeof(unsigned long) );
|
||||
|
||||
// Spew it
|
||||
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
|
||||
@@ -182,10 +170,10 @@ void loop(void)
|
||||
}
|
||||
|
||||
//
|
||||
// Receiver role. Receive each packet, dump it out, and send it back to the transmitter
|
||||
// Pong back role. Receive each packet, dump it out, and send it back
|
||||
//
|
||||
|
||||
if ( role == role_rx )
|
||||
if ( role == role_pong_back )
|
||||
{
|
||||
// if there is data ready
|
||||
if ( radio.available() )
|
||||
@@ -196,7 +184,7 @@ void loop(void)
|
||||
while (!done)
|
||||
{
|
||||
// Fetch the payload, and see if this was the last one.
|
||||
done = radio.read( &got_time );
|
||||
done = radio.read( &got_time, sizeof(unsigned long) );
|
||||
|
||||
// Spew it
|
||||
printf("Got payload %lu...",got_time);
|
||||
@@ -206,7 +194,7 @@ void loop(void)
|
||||
radio.stopListening();
|
||||
|
||||
// Send the final one back.
|
||||
radio.write( &got_time );
|
||||
radio.write( &got_time, sizeof(unsigned long) );
|
||||
printf("Sent response.\n\r");
|
||||
|
||||
// Now, resume listening so we catch the next packets.
|
||||
@@ -214,3 +202,4 @@ void loop(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
// vim:ci sts=2 sw=2 ft=cpp
|
||||
|
||||
Reference in New Issue
Block a user