Skip to content

Commit

Permalink
use enum to return precise FIFO state
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Nov 19, 2024
1 parent 8aa7103 commit fd90bc4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
7 changes: 4 additions & 3 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,16 +1382,17 @@ bool RF24::rxFifoFull()

/****************************************************************************/

uint8_t RF24::isFifo(bool about_tx)
rf24_fifo_state_e RF24::isFifo(bool about_tx)
{
return static_cast<uint8_t>((read_register(FIFO_STATUS) >> (4 * about_tx)) & 3);
uint8_t state = (read_register(FIFO_STATUS) >> (4 * about_tx)) & 3;
return static_cast<rf24_fifo_state_e>(state);
}

/****************************************************************************/

bool RF24::isFifo(bool about_tx, bool check_empty)
{
return static_cast<bool>(isFifo(about_tx) & _BV(!check_empty));
return static_cast<bool>(static_cast<uint8_t>(isFifo(about_tx)) & _BV(!check_empty));
}

/****************************************************************************/
Expand Down
30 changes: 26 additions & 4 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ typedef enum
RF24_CRC_16
} rf24_crclength_e;

/**
* @}
* @defgroup fifoState FIFO state
* The state of a single FIFO (RX or TX).
* Remember, each FIFO has a maximum occupancy of 3 payloads.
* @see RF24::isFifo()
* @{
*/
typedef enum
{
/// @brief The FIFO is not full nor empty, but it is occupied with 1 or 2 payloads.
RF24_FIFO_OCCUPIED,
/// @brief The FIFO is empty.
RF24_FIFO_EMPTY,
/// @brief The FIFO is full.
RF24_FIFO_FULL,
/// @brief Represents corruption of data over SPI (when observed).
RF24_FIFO_INVALID,
} rf24_fifo_state_e;

/**
* @}
* @brief Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver
Expand Down Expand Up @@ -785,13 +805,15 @@ class RF24
/**
* @param about_tx `true` focuses on the TX FIFO, `false` focuses on the RX FIFO
* @return
* - `0` if the specified FIFO is neither full nor empty.
* - `1` if the specified FIFO is empty.
* - `2` if the specified FIFO is full.
* - @ref RF24_FIFO_OCCUPIED (`0`) if the specified FIFO is neither full nor empty.
* - @ref RF24_FIFO_EMPTY (`1`) if the specified FIFO is empty.
* - @ref RF24_FIFO_FULL (`2`) if the specified FIFO is full.
* - @ref RF24_FIFO_INVALID (`3`) if the data fetched over SPI was malformed.
*/
uint8_t isFifo(bool about_tx);
rf24_fifo_state_e isFifo(bool about_tx);

/**
* @deprecated Use RF24::isFifo(bool about_tx) instead.
* @param about_tx `true` focuses on the TX FIFO, `false` focuses on the RX FIFO
* @param check_empty
* - `true` checks if the specified FIFO is empty
Expand Down
9 changes: 8 additions & 1 deletion pyRF24/pyRF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ BOOST_PYTHON_MODULE(RF24)
.value("RF24_PA_ERROR", RF24_PA_ERROR)
.export_values();

bp::enum_<rf24_fifo_state_e>("rf24_fifo_state_e")
.value("RF24_FIFO_OCCUPIED", RF24_FIFO_OCCUPIED)
.value("RF24_FIFO_EMPTY", RF24_FIFO_EMPTY)
.value("RF24_FIFO_FULL", RF24_FIFO_FULL)
.value("RF24_FIFO_INVALID", RF24_FIFO_INVALID)
.export_values();

// ******************** RF24 class **************************
bp::class_<RF24>("RF24", bp::init<uint16_t, uint16_t>((bp::arg("_cepin"), bp::arg("_cspin"))))
#if defined(RF24_LINUX) && !defined(MRAA)
Expand Down Expand Up @@ -320,7 +327,7 @@ BOOST_PYTHON_MODULE(RF24)
.def("reUseTX", &RF24::reUseTX)
.def("read", &read_wrap, (bp::arg("maxlen")))
.def("rxFifoFull", &RF24::rxFifoFull)
.def("isFifo", (uint8_t(::RF24::*)(bool))(&::RF24::isFifo), (bp::arg("about_tx")))
.def("isFifo", (rf24_fifo_state_e(::RF24::*)(bool))(&::RF24::isFifo), (bp::arg("about_tx")))
.def("isFifo", (bool(::RF24::*)(bool, bool))(&::RF24::isFifo), (bp::arg("about_tx"), bp::arg("check_empty")))
.def("setAddressWidth", &RF24::setAddressWidth)
.def("setAutoAck", (void(::RF24::*)(bool))(&::RF24::setAutoAck), (bp::arg("enable")))
Expand Down

0 comments on commit fd90bc4

Please sign in to comment.