-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider abstracting config out of radio instance #5
Comments
Conserving memory footprintIn exploring this idea, I found a handy library called bitfield-struct. It is an interesting way to serialize bits of a register into fields of a struct. I ended up using this to replace magic numbers with // within RF24 struct as private API
// capture and decode the STATUS byte
self._status = StatusFlags::from_bits(self._buf[0]);
// now access each flag as a field
self._status.tx_full() // -> bool
self._status.rx_pipe() // -> u8 ranging [0, 7]
self._status.tx_ds() // -> bool
// ...
// the raw status byte can still be accessed
self._status.into_bits() & 0x70 // isolate IRQ flags I did the same to the internal cached CONFIG register as well... This helped a lot to reduce the memory footprint of a This bitfield-struct lib was so impressive that I came up with the idea to transform enumerations into masks when writing them to registers: CrcLength::Bit16.into_bits() // -> 0x0C Config APIAs proposed, I landed on the following API exposed to end-users: let config = EsbConfig::default() // uses library defaults akin to C++ RF24 lib
.with_auto_ack(0b11) // enable auto-ack on pipes 0 and 1
.with_rx_dr(false) // disable RX data ready event on IRQ pin
.with_ack_payloads(true) // enables dynamic payloads also
.with_crc_length(CrcLength::Bit8)
// set address for RX pipe 1 (and opens the pipe)
.with_rx_address(1, &b"1Node");
// when ready to use the cached config
let mut radio = RF24::new(ce_obj, spi_dev_obj, delay_ns_impl);
radio.init()?;
radio.with_config(&config)?; The
|
This idea will surely consume more memory. The advantage is an easy way to reconfigure the radio using a
Config
object instead of using) multiple configuration functions on the RF24 (radio
) object.Proposal
I'm probably explaining this wrong, so consider the following pseudo code:
This allows the same radio to be easily and quickly reconfigured for different user-defined protocols.
For example, lets say someone wants to switch between different network configurations. Instead of
Users could instead cache a config object for each network config and re-use those objects to switch between the networks:
Additional Context
I already have been using this approach in the CirPy lib using python's
with
statement blocks (AKA context managers). See CirPy lib's context example and tutorial on participating in multiple networks with 1 radio.There's seems to be a convention in rust in which a builder structure is preferred over a multitude of config functions or constructor parameters.
The text was updated successfully, but these errors were encountered: