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 of using interrupts
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.
22 // Hardware configuration
25 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
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;
37 // Single radio pipe address for the 2 nodes to communicate.
38 const uint64_t pipe = 0xE8E8F0F0E1LL;
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
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
59 // Interrupt handler, check the radio because we got an IRQ
60 void check_radio(void);
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) )
85 printf("\n\rRF24/examples/pingpair_irq/\n\r");
86 printf("ROLE: %s\n\r",role_friendly_name[role]);
89 // Setup and configure rf radio
94 // We will be using the Ack Payload feature, so please enable it
95 radio.enableAckPayload();
98 // Open pipes to other nodes for communication
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 )
106 radio.openWritingPipe(pipe);
110 radio.openReadingPipe(1,pipe);
117 if ( role == role_receiver )
118 radio.startListening();
121 // Dump the configuration of the rf unit for debugging
124 radio.printDetails();
127 // Attach interrupt handler to interrupt #0 (using pin 2)
128 // on BOTH the sender and receiver
131 attachInterrupt(0, check_radio, FALLING);
134 static uint32_t message_count = 0;
139 // Sender role. Repeatedly send the current time
142 if (role == role_sender)
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) );
154 // Receiver role: Does nothing! All the work is in IRQ
159 void check_radio(void)
163 radio.whatHappened(tx,fail,rx);
165 // Have we successfully transmitted?
168 if ( role == role_sender )
169 printf("Send:OK\n\r");
171 if ( role == role_receiver )
172 printf("Ack Payload:Sent\n\r");
175 // Have we failed to transmit?
178 if ( role == role_sender )
179 printf("Send:Failed\n\r");
181 if ( role == role_receiver )
182 printf("Ack Payload:Failed\n\r");
185 // Transmitter can power down for now, because
186 // the transmission is done.
187 if ( ( tx || fail ) && ( role == role_sender ) )
190 // Did we receive a message?
193 // If we're the sender, we've received an ack payload
194 if ( role == role_sender )
196 radio.read(&message_count,sizeof(message_count));
197 printf("Ack:%lu\n\r",message_count);
200 // If we're the receiver, we've received a time message
201 if ( role == role_receiver )
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
210 radio.writeAckPayload( 1, &message_count, sizeof(message_count) );
216 // vim:ai:cin:sts=2 sw=2 ft=cpp