Move default pins over to Getting Started defaults
[RF24-C.git] / examples / starping / starping.pde
blob97b0953567d5d339e0402abd85a9f2e31edd848c
1 /*
2  Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7  */
9 /**
10  * Example RF Radio Ping Star Group
11  *
12  * This sketch is a more complex example of using the RF24 library for Arduino.
13  * Deploy this on up to six nodes.  Set one as the 'pong receiver' by tying the
14  * role_pin low, and the others will be 'ping transmit' units.  The ping units
15  * unit will send out the value of millis() once a second.  The pong unit will
16  * respond back with a copy of the value.  Each ping unit can get that response
17  * back, and determine how long the whole cycle took.
18  *
19  * This example requires a bit more complexity to determine which unit is which.
20  * The pong receiver is identified by having its role_pin tied to ground.
21  * The ping senders are further differentiated by a byte in eeprom.
22  */
24 #include <SPI.h>
25 #include <EEPROM.h>
26 #include "nRF24L01.h"
27 #include "RF24.h"
28 #include "printf.h"
31 // Hardware configuration
34 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
36 RF24 radio(9,10);
38 // sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
39 // Leave open to be the 'pong' receiver.
40 const int role_pin = 7;
43 // Topology
46 // Radio pipe addresses for the nodes to communicate.  Only ping nodes need
47 // dedicated pipes in this topology.  Each ping node has a talking pipe
48 // that it will ping into, and a listening pipe that it will listen for
49 // the pong.  The pong node listens on all the ping node talking pipes
50 // and sends the pong back on the sending node's specific listening pipe.
52 const uint64_t talking_pipes[5] = { 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL };
53 const uint64_t listening_pipes[5] = { 0x3A3A3A3AD2LL, 0x3A3A3A3AC3LL, 0x3A3A3A3AB4LL, 0x3A3A3A3AA5LL, 0x3A3A3A3A96LL };
56 // Role management
58 // Set up role.  This sketch uses the same software for all the nodes
59 // in this system.  Doing so greatly simplifies testing.  The hardware itself specifies
60 // which node it is.
62 // This is done through the role_pin
65 // The various roles supported by this sketch
66 typedef enum { role_invalid = 0, role_ping_out, role_pong_back } role_e;
68 // The debug-friendly names of those roles
69 const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
71 // The role of the current running sketch
72 role_e role;
75 // Address management
78 // Where in EEPROM is the address stored?
79 const uint8_t address_at_eeprom_location = 0;
81 // What is our address (SRAM cache of the address from EEPROM)
82 // Note that zero is an INVALID address.  The pong back unit takes address
83 // 1, and the rest are 2-6
84 uint8_t node_address;
86 void setup(void)
88   //
89   // Role
90   //
92   // set up the role pin
93   pinMode(role_pin, INPUT);
94   digitalWrite(role_pin,HIGH);
95   delay(20); // Just to get a solid reading on the role pin
97   // read the address pin, establish our role
98   if ( digitalRead(role_pin) )
99     role = role_ping_out;
100   else
101     role = role_pong_back;
103   //
104   // Address
105   //
107   if ( role == role_pong_back )
108     node_address = 1;
109   else
110   {
111     // Read the address from EEPROM
112     uint8_t reading = EEPROM.read(address_at_eeprom_location);
114     // If it is in a valid range for node addresses, it is our
115     // address.
116     if ( reading >= 2 && reading <= 6 )
117       node_address = reading;
119     // Otherwise, it is invalid, so set our address AND ROLE to 'invalid'
120     else
121     {
122       node_address = 0;
123       role = role_invalid;
124     }
125   }
127   //
128   // Print preamble
129   //
131   Serial.begin(57600);
132   printf_begin();
133   printf("\n\rRF24/examples/starping/\n\r");
134   printf("ROLE: %s\n\r",role_friendly_name[role]);
135   printf("ADDRESS: %i\n\r",node_address);
137   //
138   // Setup and configure rf radio
139   //
141   radio.begin();
143   //
144   // Open pipes to other nodes for communication
145   //
147   // The pong node listens on all the ping node talking pipes
148   // and sends the pong back on the sending node's specific listening pipe.
149   if ( role == role_pong_back )
150   {
151     radio.openReadingPipe(1,talking_pipes[0]);
152     radio.openReadingPipe(2,talking_pipes[1]);
153     radio.openReadingPipe(3,talking_pipes[2]);
154     radio.openReadingPipe(4,talking_pipes[3]);
155     radio.openReadingPipe(5,talking_pipes[4]);
156   }
158   // Each ping node has a talking pipe that it will ping into, and a listening
159   // pipe that it will listen for the pong.
160   if ( role == role_ping_out )
161   {
162     // Write on our talking pipe
163     radio.openWritingPipe(talking_pipes[node_address-2]);
164     // Listen on our listening pipe
165     radio.openReadingPipe(1,listening_pipes[node_address-2]);
166   }
168   //
169   // Start listening
170   //
172   radio.startListening();
174   //
175   // Dump the configuration of the rf unit for debugging
176   //
178   radio.printDetails();
180   //
181   // Prompt the user to assign a node address if we don't have one
182   //
184   if ( role == role_invalid )
185   {
186     printf("\n\r*** NO NODE ADDRESS ASSIGNED *** Send 1 through 6 to assign an address\n\r");
187   }
190 void loop(void)
192   //
193   // Ping out role.  Repeatedly send the current time
194   //
196   if (role == role_ping_out)
197   {
198     // First, stop listening so we can talk.
199     radio.stopListening();
201     // Take the time, and send it.  This will block until complete
202     unsigned long time = millis();
203     printf("Now sending %lu...",time);
204     radio.write( &time, sizeof(unsigned long) );
206     // Now, continue listening
207     radio.startListening();
209     // Wait here until we get a response, or timeout (250ms)
210     unsigned long started_waiting_at = millis();
211     bool timeout = false;
212     while ( ! radio.available() && ! timeout )
213       if (millis() - started_waiting_at > 250 )
214         timeout = true;
216     // Describe the results
217     if ( timeout )
218     {
219       printf("Failed, response timed out.\n\r");
220     }
221     else
222     {
223       // Grab the response, compare, and send to debugging spew
224       unsigned long got_time;
225       radio.read( &got_time, sizeof(unsigned long) );
227       // Spew it
228       printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
229     }
231     // Try again 1s later
232     delay(1000);
233   }
235   //
236   // Pong back role.  Receive each packet, dump it out, and send it back
237   //
239   if ( role == role_pong_back )
240   {
241     // if there is data ready
242     uint8_t pipe_num;
243     if ( radio.available(&pipe_num) )
244     {
245       // Dump the payloads until we've gotten everything
246       unsigned long got_time;
247       bool done = false;
248       while (!done)
249       {
250         // Fetch the payload, and see if this was the last one.
251         done = radio.read( &got_time, sizeof(unsigned long) );
253         // Spew it
254         printf("Got payload %lu from node %i...",got_time,pipe_num+1);
255       }
257       // First, stop listening so we can talk
258       radio.stopListening();
260       // Open the correct pipe for writing
261       radio.openWritingPipe(listening_pipes[pipe_num-1]);
263       // Retain the low 2 bytes to identify the pipe for the spew
264       uint16_t pipe_id = listening_pipes[pipe_num-1] & 0xffff;
266       // Send the final one back.
267       radio.write( &got_time, sizeof(unsigned long) );
268       printf("Sent response to %04x.\n\r",pipe_id);
270       // Now, resume listening so we catch the next packets.
271       radio.startListening();
272     }
273   }
275   //
276   // Listen for serial input, which is how we set the address
277   //
278   if (Serial.available())
279   {
280     // If the character on serial input is in a valid range...
281     char c = Serial.read();
282     if ( c >= '1' && c <= '6' )
283     {
284       // It is our address
285       EEPROM.write(address_at_eeprom_location,c-'0');
287       // And we are done right now (no easy way to soft reset)
288       printf("\n\rManually reset address to: %c\n\rPress RESET to continue!",c);
289       while(1) ;
290     }
291   }
293 // vim:ai:ci sts=2 sw=2 ft=cpp