Factored out status-checking from write() into a public method

This commit is contained in:
maniacbug 2011-07-06 19:51:30 -07:00
parent c62224ff86
commit fbae441249
2 changed files with 33 additions and 13 deletions

View File

@ -391,28 +391,20 @@ boolean RF24::write( const void* buf, uint8_t len )
// and then call this when you got an interrupt // and then call this when you got an interrupt
// ------------ // ------------
// Read the status // Call this when you get an interrupt
status = get_status();
// Reset the status
write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
// The status tells us three things // The status tells us three things
// * The send was successful (TX_DS) // * The send was successful (TX_DS)
// * The send failed, too many retries (MAX_RT) // * The send failed, too many retries (MAX_RT)
// * There is an ack packet waiting (RX_DR) // * 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? result = tx_ok;
if ( status & _BV(TX_DS) ) IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed"));
result = true;
IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed"); if ( status & _BV(MAX_RT) ) Serial.print(" too many retries"));
// Handle the ack packet // Handle the ack packet
ack_payload_available = ( status & _BV(RX_DR) );
if ( ack_payload_available ) if ( ack_payload_available )
{ {
write_register(STATUS,_BV(RX_DR) );
ack_payload_length = read_payload_length(); ack_payload_length = read_payload_length();
IF_SERIAL_DEBUG(Serial.print("[AckPacket]/")); IF_SERIAL_DEBUG(Serial.print("[AckPacket]/"));
IF_SERIAL_DEBUG(Serial.println(ack_payload_length,DEC)); 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) void RF24::openWritingPipe(uint64_t value)
{ {
// Note that AVR 8-bit uC's store this LSB first, and the NRF24L01 // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01

12
RF24.h
View File

@ -444,6 +444,18 @@ public:
*/ */
boolean isAckPayloadAvailable(void); 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 * Enable or disable auto-acknowlede packets
* *