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.
10 * Example using Dynamic Payloads
12 * This is an example of how to use payloads of a varying (dynamic) size.
21 // Hardware configuration
24 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
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;
36 // Radio pipe addresses for the 2 nodes to communicate.
37 const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
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
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
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
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) )
84 role = role_pong_back;
92 printf("\n\rRF24/examples/pingpair_dyn/\n\r");
93 printf("ROLE: %s\n\r",role_friendly_name[role]);
96 // Setup and configure rf radio
101 // enable dynamic payloads
102 radio.enableDynamicPayloads();
104 // optionally, increase the delay between retries & # of retries
105 radio.setRetries(15,15);
108 // Open pipes to other nodes for communication
111 // This simple sketch opens two pipes for these two nodes to communicate
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 )
118 radio.openWritingPipe(pipes[0]);
119 radio.openReadingPipe(1,pipes[1]);
123 radio.openWritingPipe(pipes[1]);
124 radio.openReadingPipe(1,pipes[0]);
131 radio.startListening();
134 // Dump the configuration of the rf unit for debugging
137 radio.printDetails();
143 // Ping out role. Repeatedly send the current time
146 if (role == role_ping_out)
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 )
168 // Describe the results
171 printf("Failed, response timed out.\n\r");
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;
183 printf("Got response size=%i value=%s\n\r",len,receive_payload);
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
196 // Pong back role. Receive each packet, dump it out, and send it back
199 if ( role == role_pong_back )
201 // if there is data ready
202 if ( radio.available() )
204 // Dump the payloads until we've gotten everything
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;
217 printf("Got payload size=%i value=%s\n\r",len,receive_payload);
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();
232 // vim:cin:ai:sts=2 sw=2 ft=cpp