summaryrefslogtreecommitdiff
path: root/examples/src/example_common.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-14 17:47:43 +0200
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-14 17:47:43 +0200
commit1d840cf4b190397f5944ed1fb7e9007e5a2d82dc (patch)
tree5e259e4ee5234c6a9b5a8e382ef19b943c867fcf /examples/src/example_common.rs
parentcb6771aceb53281797e578256a661ecbb1286f87 (diff)
downloadnrf-softdevice-1d840cf4b190397f5944ed1fb7e9007e5a2d82dc.zip
example -> examples
Diffstat (limited to 'examples/src/example_common.rs')
-rw-r--r--examples/src/example_common.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs
new file mode 100644
index 0000000..e991915
--- /dev/null
+++ b/examples/src/example_common.rs
@@ -0,0 +1,68 @@
+#![macro_use]
+
+use defmt_rtt as _; // global logger
+use nrf52840_hal as _;
+use panic_probe as _;
+use static_executor_cortex_m as _;
+
+pub use defmt::{info, intern};
+
+use core::sync::atomic::{AtomicUsize, Ordering};
+
+#[defmt::timestamp]
+fn timestamp() -> u64 {
+ static COUNT: AtomicUsize = AtomicUsize::new(0);
+ // NOTE(no-CAS) `timestamps` runs with interrupts disabled
+ let n = COUNT.load(Ordering::Relaxed);
+ COUNT.store(n + 1, Ordering::Relaxed);
+ n as u64
+}
+
+macro_rules! depanic {
+ ($( $i:expr ),*) => {
+ {
+ defmt::error!($( $i ),*);
+ panic!();
+ }
+ }
+}
+
+pub trait Dewrap<T> {
+ /// dewrap = defmt unwrap
+ fn dewrap(self) -> T;
+
+ /// dexpect = defmt expect
+ fn dexpect<M: defmt::Format>(self, msg: M) -> T;
+}
+
+impl<T> Dewrap<T> for Option<T> {
+ fn dewrap(self) -> T {
+ match self {
+ Some(t) => t,
+ None => depanic!("Dewrap failed: enum is none"),
+ }
+ }
+
+ fn dexpect<M: defmt::Format>(self, msg: M) -> T {
+ match self {
+ Some(t) => t,
+ None => depanic!("Unexpected None: {:?}", msg),
+ }
+ }
+}
+
+impl<T, E: defmt::Format> Dewrap<T> for Result<T, E> {
+ fn dewrap(self) -> T {
+ match self {
+ Ok(t) => t,
+ Err(e) => depanic!("Dewrap failed: {:?}", e),
+ }
+ }
+
+ fn dexpect<M: defmt::Format>(self, msg: M) -> T {
+ match self {
+ Ok(t) => t,
+ Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e),
+ }
+ }
+}