Move default pins over to Getting Started defaults
[RF24-C.git] / examples / pingpair_irq / pingpair_irq.pde
blob2c4f5831374fbbc163c3e3ca636d1cf9dc45553e
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 interrupts
11  *
12  * This is an example of how to user interrupts to interact with the radio.
13  * It builds on the pingpair_pl example, and uses ack payloads.
14  */
16 #include <SPI.h>
17 #include "nRF24L01.h"
18 #include "RF24.h"
19 #include "printf.h"
22 // Hardware configuration
25 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
27 RF24 radio(9,10);
29 // sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
30 // Leave open to be the 'ping' transmitter
31 const short role_pin = 7;
34 // Topology
37 // Single radio pipe address for the 2 nodes to communicate.
38 const uint64_t pipe = 0xE8E8F0F0E1LL;
41 // Role management
43 // Set up role.  This sketch uses the same software for all the nodes in this
44 // system.  Doing so greatly simplifies testing.  The hardware itself specifies
45 // which node it is.
47 // This is done through the role_pin
50 // The various roles supported by this sketch
51 typedef enum { role_sender = 1, role_receiver } role_e;
53 // The debug-friendly names of those roles
54 const char* role_friendly_name[] = { "invalid", "Sender", "Receiver"};
56 // The role of the current running sketch
57 role_e role;
59 // Interrupt handler, check the radio because we got an IRQ
60 void check_radio(void);
62 void setup(void)
64   //
65   // Role
66   //
68   // set up the role pin
69   pinMode(role_pin, INPUT);
70   digitalWrite(role_pin,HIGH);
71   delay(20); // Just to get a solid reading on the role pin
73   // read the address pin, establish our role
74   if ( digitalRead(role_pin) )
75     role = role_sender;
76   else
77     role = role_receiver;
79   //
80   // Print preamble
81   //
83   Serial.begin(57600);
84   printf_begin();
85   printf("\n\rRF24/examples/pingpair_irq/\n\r");
86   printf("ROLE: %s\n\r",role_friendly_name[role]);
88   //
89   // Setup and configure rf radio
90   //
92   radio.begin();
94   // We will be using the Ack Payload feature, so please enable it
95   radio.enableAckPayload();
97   //
98   // Open pipes to other nodes for communication
99   //
101   // This simple sketch opens a single pipe for these two nodes to communicate
102   // back and forth.  One listens on it, the other talks to it.
104   if ( role == role_sender )
105   {
106     radio.openWritingPipe(pipe);
107   }
108   else
109   {
110     radio.openReadingPipe(1,pipe);
111   }
113   //
114   // Start listening
115   //
117   if ( role == role_receiver )
118     radio.startListening();
120   //
121   // Dump the configuration of the rf unit for debugging
122   //
124   radio.printDetails();
126   //
127   // Attach interrupt handler to interrupt #0 (using pin 2)
128   // on BOTH the sender and receiver
129   //
131   attachInterrupt(0, check_radio, FALLING);
134 static uint32_t message_count = 0;
136 void loop(void)
138   //
139   // Sender role.  Repeatedly send the current time
140   //
142   if (role == role_sender)
143   {
144     // Take the time, and send it.
145     unsigned long time = millis();
146     printf("Now sending %lu\n\r",time);
147     radio.startWrite( &time, sizeof(unsigned long) );
149     // Try again soon
150     delay(2000);
151   }
153   //
154   // Receiver role: Does nothing!  All the work is in IRQ
155   //
159 void check_radio(void)
161   // What happened?
162   bool tx,fail,rx;
163   radio.whatHappened(tx,fail,rx);
165   // Have we successfully transmitted?
166   if ( tx )
167   {
168     if ( role == role_sender )
169       printf("Send:OK\n\r");
171     if ( role == role_receiver )
172       printf("Ack Payload:Sent\n\r");
173   }
175   // Have we failed to transmit?
176   if ( fail )
177   {
178     if ( role == role_sender )
179       printf("Send:Failed\n\r");
181     if ( role == role_receiver )
182       printf("Ack Payload:Failed\n\r");
183   }
185   // Transmitter can power down for now, because
186   // the transmission is done.
187   if ( ( tx || fail ) && ( role == role_sender ) )
188     radio.powerDown();
190   // Did we receive a message?
191   if ( rx )
192   {
193     // If we're the sender, we've received an ack payload
194     if ( role == role_sender )
195     {
196       radio.read(&message_count,sizeof(message_count));
197       printf("Ack:%lu\n\r",message_count);
198     }
200     // If we're the receiver, we've received a time message
201     if ( role == role_receiver )
202     {
203       // Get this payload and dump it
204       static unsigned long got_time;
205       radio.read( &got_time, sizeof(got_time) );
206       printf("Got payload %lu\n\r",got_time);
208       // Add an ack packet for the next time around.  This is a simple
209       // packet counter
210       radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
211       ++message_count;
212     }
213   }
216 // vim:ai:cin:sts=2 sw=2 ft=cpp