diff --git a/src/block.rs b/src/block.rs new file mode 100644 index 0000000000000000000000000000000000000000..cfe5a42efa87bf010c7fdfdd528c246626adb5dd --- /dev/null +++ b/src/block.rs @@ -0,0 +1,33 @@ +/// Turns the non-blocking expression `$e` into a blocking expression, with a maximum number of +/// retries `max`. +/// +/// Just like [`nb::block`](https://docs.rs/nb/latest/nb/macro.block.html), `$e` is evaluated in a +/// loop, except now if the loop does more than `max` iterations, `None` is returned, and `Some(_)` +/// is returned instead of `_`. +#[macro_export] +macro_rules! block_retry { + ($e:expr, $max:expr) => { + { + let mut i = $max; + loop { + #[allow(unreachable_patterns)] + match $e { + Err($crate::nb::Error::Other(e)) => + { + #[allow(unreachable_code)] + break Err(e) + } + //wb @ Err($crate::nb::Error::WouldBlock) => { + Err($crate::nb::Error::WouldBlock) => { + if i == 0 { + break Ok(None) + } else { + i-=1; + } + } + Ok(x) => break Ok(Some(x)), + } + } + } + }; +} diff --git a/src/lib.rs b/src/lib.rs index d02fbaca8fdfb3f67205b791112d365ab0524c07..8ba108deb743fb158b4e31c03804547a7345ffc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,8 @@ pub extern crate nrf51_hal; pub use cortex_m_rt::entry; /// reexport of the `nb::block` macro. pub use nb::block; +/// reexport of the `nb` crate. +pub use nb; /// reexport of the `cortex_m` crate pub use cortex_m; @@ -41,6 +43,9 @@ pub use nrf51_pac; /// reexport of the `ringbuffer` crate pub use ringbuffer; +/// Blocking macros with a maximum retry counter +pub mod block; + /// Utilities to read out the barometer pub mod barometer;