2 Copyright (C) 2011 J. Coliz <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 of using Ack Payloads
12 * This is an example of how to do two-way communication without changing
13 * transmit/receive modes. Here, a payload is set to the transmitter within
14 * the Ack packet of each transmission. Note that the payload is set BEFORE
15 * the sender's message arrives.
24 // Hardware configuration
27 // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
31 // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
32 // Leave open to be the 'ping' transmitter
33 const short role_pin = 7;
39 // Single radio pipe address for the 2 nodes to communicate.
40 const uint64_t pipe = 0xE8E8F0F0E1LL;
45 // Set up role. This sketch uses the same software for all the nodes in this
46 // system. Doing so greatly simplifies testing. The hardware itself specifies
49 // This is done through the role_pin
52 // The various roles supported by this sketch
53 typedef enum { role_sender = 1, role_receiver } role_e;
55 // The debug-friendly names of those roles
56 const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
58 // The role of the current running sketch
67 // set up the role pin
68 pinMode(role_pin, INPUT);
69 digitalWrite(role_pin,HIGH);
70 delay(20); // Just to get a solid reading on the role pin
72 // read the address pin, establish our role
73 if ( digitalRead(role_pin) )
84 printf("\n\rRF24/examples/pingpair_pl/\n\r");
85 printf("ROLE: %s\n\r",role_friendly_name[role]);
88 // Setup and configure rf radio
93 // We will be using the Ack Payload feature, so please enable it
94 radio.enableAckPayload();
97 // Open pipes to other nodes for communication
100 // This simple sketch opens a single pipes for these two nodes to communicate
101 // back and forth. One listens on it, the other talks to it.
103 if ( role == role_sender )
105 radio.openWritingPipe(pipe);
109 radio.openReadingPipe(1,pipe);
116 if ( role == role_receiver )
117 radio.startListening();
120 // Dump the configuration of the rf unit for debugging
123 radio.printDetails();
128 static uint32_t message_count = 0;
131 // Sender role. Repeatedly send the current time
134 if (role == role_sender)
136 // Take the time, and send it. This will block until complete
137 unsigned long time = millis();
138 printf("Now sending %lu...",time);
139 radio.write( &time, sizeof(unsigned long) );
141 if ( radio.isAckPayloadAvailable() )
143 radio.read(&message_count,sizeof(message_count));
144 printf("Ack: [%lu] ",message_count);
153 // Receiver role. Receive each packet, dump it out, add ack payload for next time
156 if ( role == role_receiver )
158 // if there is data ready
159 if ( radio.available() )
161 // Dump the payloads until we've gotten everything
162 static unsigned long got_time;
166 // Fetch the payload, and see if this was the last one.
167 done = radio.read( &got_time, sizeof(unsigned long) );
170 printf("Got payload %lu\n",got_time);
173 // Add an ack packet for the next time around. This is a simple
175 radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
180 // vim:ai:cin:sts=2 sw=2 ft=cpp