|
18 | 18 | #include "RF24.h"
|
19 | 19 |
|
20 | 20 | // We will be using the nRF24L01's IRQ pin for this example
|
21 |
| -#define IRQ_PIN 2 // this needs to be a digital input capable pin |
22 |
| -volatile bool got_interrupt = false; // used to signal processing of interrupt |
23 |
| -bool wait_for_event = false; // used to wait for an IRQ event to trigger |
| 21 | +#define IRQ_PIN 2 // this needs to be a digital input capable pin |
| 22 | +volatile bool got_interrupt = false; // used to signal processing of interrupt |
| 23 | +volatile bool wait_for_event = false; // used to wait for an IRQ event to trigger |
24 | 24 |
|
25 | 25 | #define CE_PIN 7
|
26 | 26 | #define CSN_PIN 8
|
@@ -138,44 +138,10 @@ void setup() {
|
138 | 138 |
|
139 | 139 | void loop() {
|
140 | 140 | if (got_interrupt) {
|
141 |
| - // print IRQ status and all masking flags' states |
142 |
| - |
143 |
| - Serial.println(F("\tIRQ pin is actively LOW")); // show that this function was called |
144 |
| - delayMicroseconds(250); |
145 |
| - bool tx_ds, tx_df, rx_dr; // declare variables for IRQ masks |
146 |
| - radio.whatHappened(tx_ds, tx_df, rx_dr); // get values for IRQ masks |
147 |
| - // whatHappened() clears the IRQ masks also. This is required for |
148 |
| - // continued TX operations when a transmission fails. |
149 |
| - // clearing the IRQ masks resets the IRQ pin to its inactive state (HIGH) |
150 |
| - |
151 |
| - Serial.print(F("\tdata_sent: ")); |
152 |
| - Serial.print(tx_ds); // print "data sent" mask state |
153 |
| - Serial.print(F(", data_fail: ")); |
154 |
| - Serial.print(tx_df); // print "data fail" mask state |
155 |
| - Serial.print(F(", data_ready: ")); |
156 |
| - Serial.println(rx_dr); // print "data ready" mask state |
157 |
| - |
158 |
| - if (tx_df) // if TX payload failed |
159 |
| - radio.flush_tx(); // clear all payloads from the TX FIFO |
160 |
| - |
161 |
| - // print if test passed or failed. Unintentional fails mean the RX node was not listening. |
162 |
| - // pl_iterator has already been incremented by now |
163 |
| - if (pl_iterator <= 1) { |
164 |
| - Serial.print(F(" 'Data Ready' event test ")); |
165 |
| - Serial.println(rx_dr ? F("passed") : F("failed")); |
166 |
| - } else if (pl_iterator == 2) { |
167 |
| - Serial.print(F(" 'Data Sent' event test ")); |
168 |
| - Serial.println(tx_ds ? F("passed") : F("failed")); |
169 |
| - } else if (pl_iterator == 4) { |
170 |
| - Serial.print(F(" 'Data Fail' event test ")); |
171 |
| - Serial.println(tx_df ? F("passed") : F("failed")); |
172 |
| - } |
173 |
| - got_interrupt = false; |
174 |
| - wait_for_event = false; |
| 141 | + assessInterruptEvent(); |
175 | 142 | }
|
176 |
| - if (role && !wait_for_event) { |
177 | 143 |
|
178 |
| - // delay(1); // wait for IRQ pin to fully RISE |
| 144 | + if (role && !wait_for_event) { |
179 | 145 |
|
180 | 146 | // This device is a TX node. This if block is only triggered when
|
181 | 147 | // NOT waiting for an IRQ event to happen
|
@@ -255,25 +221,22 @@ void loop() {
|
255 | 221 | pl_iterator++; // proceed from step 3 to last step (stop at step 4 for readability)
|
256 | 222 | }
|
257 | 223 |
|
258 |
| - } else if (!role) { |
259 |
| - // This device is a RX node |
260 |
| - |
261 |
| - if (radio.rxFifoFull()) { |
262 |
| - // wait until RX FIFO is full then stop listening |
| 224 | + } else if (!role && radio.rxFifoFull()) { |
| 225 | + // This device is a RX node: |
| 226 | + // wait until RX FIFO is full then stop listening |
263 | 227 |
|
264 |
| - delay(100); // let ACK payload finish transmitting |
265 |
| - radio.stopListening(); // also discards unused ACK payloads |
266 |
| - printRxFifo(); // flush the RX FIFO |
| 228 | + delay(100); // let ACK payload finish transmitting |
| 229 | + radio.stopListening(); // also discards unused ACK payloads |
| 230 | + printRxFifo(); // flush the RX FIFO |
267 | 231 |
|
268 |
| - // Fill the TX FIFO with 3 ACK payloads for the first 3 received |
269 |
| - // transmissions on pipe 1. |
270 |
| - radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size); |
271 |
| - radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size); |
272 |
| - radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size); |
| 232 | + // Fill the TX FIFO with 3 ACK payloads for the first 3 received |
| 233 | + // transmissions on pipe 1. |
| 234 | + radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size); |
| 235 | + radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size); |
| 236 | + radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size); |
273 | 237 |
|
274 |
| - delay(100); // let TX node finish its role |
275 |
| - radio.startListening(); // We're ready to start over. Begin listening. |
276 |
| - } |
| 238 | + delay(100); // let TX node finish its role |
| 239 | + radio.startListening(); // We're ready to start over. Begin listening. |
277 | 240 |
|
278 | 241 | } // role
|
279 | 242 |
|
@@ -319,13 +282,53 @@ void loop() {
|
319 | 282 |
|
320 | 283 |
|
321 | 284 | /**
|
322 |
| - * when the IRQ pin goes active LOW, call this fuction print out why |
| 285 | + * when the IRQ pin goes active LOW. |
| 286 | + * Here we just tell the main loop() to call `assessInterruptEve4nt()`. |
323 | 287 | */
|
324 | 288 | void interruptHandler() {
|
325 |
| - got_interrupt = true; |
326 |
| - wait_for_event = false; // ready to continue with loop() operations |
327 |
| -} // interruptHandler |
| 289 | + got_interrupt = true; // forward event handling back to main loop() |
| 290 | +} |
328 | 291 |
|
| 292 | +/** |
| 293 | + * Called when an event has been triggered. |
| 294 | + * Here, we want to verify the expected IRQ flag has been asserted. |
| 295 | + */ |
| 296 | +void assessInterruptEvent() { |
| 297 | + // print IRQ status and all masking flags' states |
| 298 | + |
| 299 | + Serial.println(F("\tIRQ pin is actively LOW")); // show that this function was called |
| 300 | + delayMicroseconds(250); |
| 301 | + bool tx_ds, tx_df, rx_dr; // declare variables for IRQ masks |
| 302 | + radio.whatHappened(tx_ds, tx_df, rx_dr); // get values for IRQ masks |
| 303 | + // whatHappened() clears the IRQ masks also. This is required for |
| 304 | + // continued TX operations when a transmission fails. |
| 305 | + // clearing the IRQ masks resets the IRQ pin to its inactive state (HIGH) |
| 306 | + |
| 307 | + Serial.print(F("\tdata_sent: ")); |
| 308 | + Serial.print(tx_ds); // print "data sent" mask state |
| 309 | + Serial.print(F(", data_fail: ")); |
| 310 | + Serial.print(tx_df); // print "data fail" mask state |
| 311 | + Serial.print(F(", data_ready: ")); |
| 312 | + Serial.println(rx_dr); // print "data ready" mask state |
| 313 | + |
| 314 | + if (tx_df) // if TX payload failed |
| 315 | + radio.flush_tx(); // clear all payloads from the TX FIFO |
| 316 | + |
| 317 | + // print if test passed or failed. Unintentional fails mean the RX node was not listening. |
| 318 | + // pl_iterator has already been incremented by now |
| 319 | + if (pl_iterator <= 1) { |
| 320 | + Serial.print(F(" 'Data Ready' event test ")); |
| 321 | + Serial.println(rx_dr ? F("passed") : F("failed")); |
| 322 | + } else if (pl_iterator == 2) { |
| 323 | + Serial.print(F(" 'Data Sent' event test ")); |
| 324 | + Serial.println(tx_ds ? F("passed") : F("failed")); |
| 325 | + } else if (pl_iterator == 4) { |
| 326 | + Serial.print(F(" 'Data Fail' event test ")); |
| 327 | + Serial.println(tx_df ? F("passed") : F("failed")); |
| 328 | + } |
| 329 | + got_interrupt = false; // reset this flag to prevent calling this function from loop() |
| 330 | + wait_for_event = false; // ready to continue with loop() operations |
| 331 | +} |
329 | 332 |
|
330 | 333 | /**
|
331 | 334 | * Print the entire RX FIFO with one buffer. This will also flush the RX FIFO.
|
|
0 commit comments