Skip to content

Commit 0c5463e

Browse files
committed
use enum constants for setters
1 parent d3c2bcd commit 0c5463e

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

RF24.cpp

+5-36
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,6 @@ bool StatusFlags::rx_dr() const
5656

5757
/****************************************************************************/
5858

59-
void StatusFlags::set_tx_df(bool enable)
60-
{
61-
if (enable)
62-
value |= _BV(MAX_RT);
63-
else
64-
value &= ~_BV(MAX_RT);
65-
}
66-
67-
/****************************************************************************/
68-
69-
void StatusFlags::set_tx_ds(bool enable)
70-
{
71-
if (enable)
72-
value |= _BV(TX_DS);
73-
else
74-
value &= ~_BV(TX_DS);
75-
}
76-
77-
/****************************************************************************/
78-
79-
void StatusFlags::set_rx_dr(bool enable)
80-
{
81-
if (enable)
82-
value |= _BV(RX_DR);
83-
else
84-
value &= ~_BV(RX_DR);
85-
}
86-
87-
/****************************************************************************/
88-
8959
void RF24::csn(bool mode)
9060
{
9161
#if defined(RF24_TINY)
@@ -1635,26 +1605,25 @@ void RF24::whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready)
16351605

16361606
StatusFlags RF24::clearStatusFlags()
16371607
{
1638-
write_register(NRF_STATUS, 0x70);
1639-
StatusFlags flags;
1640-
memcpy((void*)(&flags), &status, 1);
1608+
write_register(NRF_STATUS, RF24_IRQ_ALL);
1609+
StatusFlags flags(status);
16411610
return flags;
16421611
}
16431612

16441613
/****************************************************************************/
16451614

16461615
void RF24::clearStatusFlags(StatusFlags& flags)
16471616
{
1648-
write_register(NRF_STATUS, flags.value & 0x70);
1649-
flags = StatusFlags(status);
1617+
write_register(NRF_STATUS, flags.value & RF24_IRQ_ALL);
1618+
flags.value = status;
16501619
}
16511620

16521621
/****************************************************************************/
16531622

16541623
void RF24::setStatusFlags(StatusFlags flags)
16551624
{
16561625
// flip the flags to translate from "human understanding"
1657-
write_register(NRF_CONFIG, ~flags.value & 0x70);
1626+
write_register(NRF_CONFIG, ~flags.value & RF24_IRQ_ALL);
16581627
}
16591628

16601629
/****************************************************************************/

RF24.h

+31-7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ typedef enum
134134
* @{
135135
*/
136136

137+
/**
138+
* @brief An enumeration of constants used to configure @ref StatusFlags
139+
*/
140+
typedef enum
141+
{
142+
#include "nRF24L01.h"
143+
/// Represents an event where TX Data Failed to send.
144+
RF24_TX_DF = 1 << MASK_MAX_RT,
145+
/// Represents an event where TX Data Sent successfully.
146+
RF24_TX_DS = 1 << TX_DS,
147+
/// Represents an event where RX Data is Ready to `RF24::read()`.
148+
RF24_RX_DR = 1 << RX_DR,
149+
/// Equivalent to `RF24_RX_DR | RF24_TX_DS | RF24_TX_DF`.
150+
RF24_IRQ_ALL = (1 << MASK_MAX_RT) | (1 << TX_DS) | (1 << RX_DR),
151+
} rf24_irq_flags_e;
152+
137153
/**
138154
* @brief A bit-field struct to represent the radio's STATUS byte.
139155
*/
@@ -152,13 +168,6 @@ struct StatusFlags
152168
/// Represents an event where RX Data is Ready to `RF24::read()`.
153169
bool rx_dr() const;
154170

155-
/// Configure the event where TX Data Failed to send.
156-
void set_tx_df(bool enable);
157-
/// Configure the event where TX Data Sent successfully.
158-
void set_tx_ds(bool enable);
159-
/// Configure the event where RX Data is Ready to `RF24::read()`.
160-
void set_rx_dr(bool enable);
161-
162171
/**
163172
* @brief Convert this struct to a human understandable string.
164173
*
@@ -181,6 +190,21 @@ struct StatusFlags
181190
/// The default initializer constructor.
182191
/// @see Details about `StatusFlags::toString()` show an example output of default values.
183192
StatusFlags() : value(0x0E) {};
193+
194+
/**
195+
* @brief Set the StatusFlags using constants defined by @ref rf24_irq_flags_e
196+
*
197+
* @param bits This value shall be a value of @ref rf24_irq_flags_e.
198+
* Enabling multiple flags can be done with bitwise OR operator (`|`).
199+
*
200+
* ```cpp
201+
* // only enable the "RX Data Ready" event
202+
* StatusFlags flags(RF24_RX_DR);
203+
*
204+
* // only enable the "TX Data Sent" and "TX Data Fail" events
205+
* flags = StatusFlags(RF24_TX_DF | RF24_TX_DS);
206+
* ```
207+
*/
184208
StatusFlags(uint8_t bits) : value(bits) {};
185209

186210
private:

pyRF24/pyRF24.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,18 @@ BOOST_PYTHON_MODULE(RF24)
305305
.value("RF24_FIFO_INVALID", RF24_FIFO_INVALID)
306306
.export_values();
307307

308+
bp::enum_<rf24_irq_flags_e>("rf24_irq_flags_e")
309+
.value("RF24_TX_DF", RF24_TX_DF)
310+
.value("RF24_TX_DS", RF24_TX_DS)
311+
.value("RF24_RX_DR", RF24_RX_DR)
312+
.value("RF24_IRQ_ALL", RF24_IRQ_ALL)
313+
.export_values();
314+
308315
bp::class_<StatusFlags>("StatusFlags", bp::init<>())
309-
.add_property("rx_dr", &StatusFlags::rx_dr, &StatusFlags::set_rx_dr)
310-
.add_property("tx_ds", &StatusFlags::tx_ds, &StatusFlags::set_tx_ds)
311-
.add_property("tx_df", &StatusFlags::tx_df, &StatusFlags::set_tx_df)
316+
.def(bp::init<uint8_t>((bp::arg("bits"))))
317+
.add_property("rx_dr", &StatusFlags::rx_dr)
318+
.add_property("tx_ds", &StatusFlags::tx_ds)
319+
.add_property("tx_df", &StatusFlags::tx_df)
312320
.add_property("tx_full", &StatusFlags::tx_full)
313321
.add_property("rx_pipe", &StatusFlags::rx_pipe)
314322
.def("__repr__", &reprStatusFlags);

0 commit comments

Comments
 (0)