From fbae441249f11a3f15b856881c71899182b42ca3 Mon Sep 17 00:00:00 2001 From: maniacbug Date: Wed, 6 Jul 2011 19:51:30 -0700 Subject: [PATCH] Factored out status-checking from write() into a public method --- RF24.cpp | 34 +++++++++++++++++++++------------- RF24.h | 12 ++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/RF24.cpp b/RF24.cpp index 6d5e6c3..cb3d640 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -391,28 +391,20 @@ boolean RF24::write( const void* buf, uint8_t len ) // and then call this when you got an interrupt // ------------ - // Read the status - status = get_status(); - - // Reset the status - write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); - + // Call this when you get an interrupt // The status tells us three things // * The send was successful (TX_DS) // * The send failed, too many retries (MAX_RT) // * There is an ack packet waiting (RX_DR) + bool tx_ok, tx_fail, ack_payload_available; + whatHappened(tx_ok,tx_fail,ack_payload_available); - // What was the result of the send? - if ( status & _BV(TX_DS) ) - result = true; - - IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed"); if ( status & _BV(MAX_RT) ) Serial.print(" too many retries")); + result = tx_ok; + IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed")); // Handle the ack packet - ack_payload_available = ( status & _BV(RX_DR) ); if ( ack_payload_available ) { - write_register(STATUS,_BV(RX_DR) ); ack_payload_length = read_payload_length(); IF_SERIAL_DEBUG(Serial.print("[AckPacket]/")); IF_SERIAL_DEBUG(Serial.println(ack_payload_length,DEC)); @@ -503,6 +495,22 @@ boolean RF24::read( void* buf, uint8_t len ) /******************************************************************/ +void RF24::whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready) +{ + // Read the status + uint8_t status = get_status(); + + // Reset the status + write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); + + // Report to the user what happened + tx_ok = status & _BV(TX_DS); + tx_fail = status & _BV(MAX_RT); + rx_ready = status & _BV(RX_DR); +} + +/******************************************************************/ + void RF24::openWritingPipe(uint64_t value) { // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01 diff --git a/RF24.h b/RF24.h index d9f844a..eafadaf 100644 --- a/RF24.h +++ b/RF24.h @@ -444,6 +444,18 @@ public: */ boolean isAckPayloadAvailable(void); + /** + * Call this when you get an interrupt to find out why + * + * Tells you what caused the interrupt, and clears the state of + * interrupts. + * + * @param[out] tx_ok The send was successful (TX_DS) + * @param[out] tx_fail The send failed, too many retries (MAX_RT) + * @param[out] rx_ready There is a message waiting to be read (RX_DS) + */ + void whatHappened(bool& tx_ok,bool& tx_fail,bool& rx); + /** * Enable or disable auto-acknowlede packets *