Move default pins over to Getting Started defaults
[RF24-C.git] / examples / pingpair_pl / pingpair_pl.pde
blob94a0da5f33b5c92571de72eb19554ec31fda94f2
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 of using Ack Payloads
11  *
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.
16  */
18 #include <SPI.h>
19 #include "nRF24L01.h"
20 #include "RF24.h"
21 #include "printf.h"
24 // Hardware configuration
27 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
29 RF24 radio(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;
36 // Topology
39 // Single radio pipe address for the 2 nodes to communicate.
40 const uint64_t pipe = 0xE8E8F0F0E1LL;
43 // Role management
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
47 // which node it is.
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
59 role_e role;
61 void setup(void)
63   //
64   // Role
65   //
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) )
74     role = role_sender;
75   else
76     role = role_receiver;
78   //
79   // Print preamble
80   //
82   Serial.begin(57600);
83   printf_begin();
84   printf("\n\rRF24/examples/pingpair_pl/\n\r");
85   printf("ROLE: %s\n\r",role_friendly_name[role]);
87   //
88   // Setup and configure rf radio
89   //
91   radio.begin();
93   // We will be using the Ack Payload feature, so please enable it
94   radio.enableAckPayload();
96   //
97   // Open pipes to other nodes for communication
98   //
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 )
104   {
105     radio.openWritingPipe(pipe);
106   }
107   else
108   {
109     radio.openReadingPipe(1,pipe);
110   }
112   //
113   // Start listening
114   //
116   if ( role == role_receiver )
117     radio.startListening();
119   //
120   // Dump the configuration of the rf unit for debugging
121   //
123   radio.printDetails();
126 void loop(void)
128   static uint32_t message_count = 0;
130   //
131   // Sender role.  Repeatedly send the current time
132   //
134   if (role == role_sender)
135   {
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() )
142     {
143       radio.read(&message_count,sizeof(message_count));
144       printf("Ack: [%lu] ",message_count);
145     }
146     printf("OK\n\r");
148     // Try again soon
149     delay(2000);
150   }
152   //
153   // Receiver role.  Receive each packet, dump it out, add ack payload for next time
154   //
156   if ( role == role_receiver )
157   {
158     // if there is data ready
159     if ( radio.available() )
160     {
161       // Dump the payloads until we've gotten everything
162       static unsigned long got_time;
163       bool done = false;
164       while (!done)
165       {
166         // Fetch the payload, and see if this was the last one.
167         done = radio.read( &got_time, sizeof(unsigned long) );
169         // Spew it
170         printf("Got payload %lu\n",got_time);
171       }
173       // Add an ack packet for the next time around.  This is a simple
174       // packet counter
175       radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
176       ++message_count;
177     }
178   }
180 // vim:ai:cin:sts=2 sw=2 ft=cpp