Move default pins over to Getting Started defaults
[RF24-C.git] / examples / pingpair_dyn / pingpair_dyn.pde
blobcea56ee1c8d0af197eb0c518713705d06f18765b
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 using Dynamic Payloads 
11  *
12  * This is an example of how to use payloads of a varying (dynamic) size. 
13  */
15 #include <SPI.h>
16 #include "nRF24L01.h"
17 #include "RF24.h"
18 #include "printf.h"
21 // Hardware configuration
24 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
26 RF24 radio(9,10);
28 // sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
29 // Leave open to be the 'ping' transmitter
30 const int role_pin = 7;
33 // Topology
36 // Radio pipe addresses for the 2 nodes to communicate.
37 const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
40 // Role management
42 // Set up role.  This sketch uses the same software for all the nodes
43 // in this system.  Doing so greatly simplifies testing.  The hardware itself specifies
44 // which node it is.
46 // This is done through the role_pin
49 // The various roles supported by this sketch
50 typedef enum { role_ping_out = 1, role_pong_back } role_e;
52 // The debug-friendly names of those roles
53 const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
55 // The role of the current running sketch
56 role_e role;
59 // Payload
62 const int min_payload_size = 4;
63 const int max_payload_size = 32;
64 const int payload_size_increments_by = 2;
65 int next_payload_size = min_payload_size;
67 char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
69 void setup(void)
71   //
72   // Role
73   //
75   // set up the role pin
76   pinMode(role_pin, INPUT);
77   digitalWrite(role_pin,HIGH);
78   delay(20); // Just to get a solid reading on the role pin
80   // read the address pin, establish our role
81   if ( digitalRead(role_pin) )
82     role = role_ping_out;
83   else
84     role = role_pong_back;
86   //
87   // Print preamble
88   //
90   Serial.begin(57600);
91   printf_begin();
92   printf("\n\rRF24/examples/pingpair_dyn/\n\r");
93   printf("ROLE: %s\n\r",role_friendly_name[role]);
95   //
96   // Setup and configure rf radio
97   //
99   radio.begin();
101   // enable dynamic payloads
102   radio.enableDynamicPayloads();
104   // optionally, increase the delay between retries & # of retries
105   radio.setRetries(15,15);
107   //
108   // Open pipes to other nodes for communication
109   //
111   // This simple sketch opens two pipes for these two nodes to communicate
112   // back and forth.
113   // Open 'our' pipe for writing
114   // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
116   if ( role == role_ping_out )
117   {
118     radio.openWritingPipe(pipes[0]);
119     radio.openReadingPipe(1,pipes[1]);
120   }
121   else
122   {
123     radio.openWritingPipe(pipes[1]);
124     radio.openReadingPipe(1,pipes[0]);
125   }
127   //
128   // Start listening
129   //
131   radio.startListening();
133   //
134   // Dump the configuration of the rf unit for debugging
135   //
137   radio.printDetails();
140 void loop(void)
142   //
143   // Ping out role.  Repeatedly send the current time
144   //
146   if (role == role_ping_out)
147   {
148     // The payload will always be the same, what will change is how much of it we send.
149     static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
151     // First, stop listening so we can talk.
152     radio.stopListening();
154     // Take the time, and send it.  This will block until complete
155     printf("Now sending length %i...",next_payload_size);
156     radio.write( send_payload, next_payload_size );
158     // Now, continue listening
159     radio.startListening();
161     // Wait here until we get a response, or timeout
162     unsigned long started_waiting_at = millis();
163     bool timeout = false;
164     while ( ! radio.available() && ! timeout )
165       if (millis() - started_waiting_at > 500 )
166         timeout = true;
168     // Describe the results
169     if ( timeout )
170     {
171       printf("Failed, response timed out.\n\r");
172     }
173     else
174     {
175       // Grab the response, compare, and send to debugging spew
176       uint8_t len = radio.getDynamicPayloadSize();
177       radio.read( receive_payload, len );
179       // Put a zero at the end for easy printing
180       receive_payload[len] = 0;
182       // Spew it
183       printf("Got response size=%i value=%s\n\r",len,receive_payload);
184     }
185     
186     // Update size for next time.
187     next_payload_size += payload_size_increments_by;
188     if ( next_payload_size > max_payload_size )
189       next_payload_size = min_payload_size;
191     // Try again 1s later
192     delay(1000);
193   }
195   //
196   // Pong back role.  Receive each packet, dump it out, and send it back
197   //
199   if ( role == role_pong_back )
200   {
201     // if there is data ready
202     if ( radio.available() )
203     {
204       // Dump the payloads until we've gotten everything
205       uint8_t len;
206       bool done = false;
207       while (!done)
208       {
209         // Fetch the payload, and see if this was the last one.
210         len = radio.getDynamicPayloadSize();
211         done = radio.read( receive_payload, len );
213         // Put a zero at the end for easy printing
214         receive_payload[len] = 0;
216         // Spew it
217         printf("Got payload size=%i value=%s\n\r",len,receive_payload);
218       }
220       // First, stop listening so we can talk
221       radio.stopListening();
223       // Send the final one back.
224       radio.write( receive_payload, len );
225       printf("Sent response.\n\r");
227       // Now, resume listening so we catch the next packets.
228       radio.startListening();
229     }
230   }
232 // vim:cin:ai:sts=2 sw=2 ft=cpp