BayEOS-Arduino  1.8.0_0.0.4
pingpair_pl.pde
1 /*
2  Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3 
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  */
8 
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  */
17 
18 #include <SPI.h>
19 #include "nRF24L01.h"
20 #include "RF24.h"
21 #include "printf.h"
22 
23 //
24 // Hardware configuration
25 //
26 
27 // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
28 
29 RF24 radio(9,10);
30 
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;
34 
35 //
36 // Topology
37 //
38 
39 // Single radio pipe address for the 2 nodes to communicate.
40 const uint64_t pipe = 0xE8E8F0F0E1LL;
41 
42 //
43 // Role management
44 //
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.
48 //
49 // This is done through the role_pin
50 //
51 
52 // The various roles supported by this sketch
53 typedef enum { role_sender = 1, role_receiver } role_e;
54 
55 // The debug-friendly names of those roles
56 const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
57 
58 // The role of the current running sketch
59 role_e role;
60 
61 void setup(void)
62 {
63  //
64  // Role
65  //
66 
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
71 
72  // read the address pin, establish our role
73  if ( digitalRead(role_pin) )
74  role = role_sender;
75  else
76  role = role_receiver;
77 
78  //
79  // Print preamble
80  //
81 
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]);
86 
87  //
88  // Setup and configure rf radio
89  //
90 
91  radio.begin();
92 
93  // We will be using the Ack Payload feature, so please enable it
94  radio.enableAckPayload();
95 
96  //
97  // Open pipes to other nodes for communication
98  //
99 
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.
102 
103  if ( role == role_sender )
104  {
105  radio.openWritingPipe(pipe);
106  }
107  else
108  {
109  radio.openReadingPipe(1,pipe);
110  }
111 
112  //
113  // Start listening
114  //
115 
116  if ( role == role_receiver )
117  radio.startListening();
118 
119  //
120  // Dump the configuration of the rf unit for debugging
121  //
122 
123  radio.printDetails();
124 }
125 
126 void loop(void)
127 {
128  static uint32_t message_count = 0;
129 
130  //
131  // Sender role. Repeatedly send the current time
132  //
133 
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) );
140 
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");
147 
148  // Try again soon
149  delay(2000);
150  }
151 
152  //
153  // Receiver role. Receive each packet, dump it out, add ack payload for next time
154  //
155 
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) );
168 
169  // Spew it
170  printf("Got payload %lu\n",got_time);
171  }
172 
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  }
179 }
180 // vim:ai:cin:sts=2 sw=2 ft=cpp