aboutsummaryrefslogtreecommitdiff
path: root/examples/src/example_common.rs
diff options
context:
space:
mode:
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 000000000..e9919153c
--- /dev/null
+++ b/examples/src/example_common.rs
@@ -0,0 +1,68 @@
1#![macro_use]
2
3use defmt_rtt as _; // global logger
4use nrf52840_hal as _;
5use panic_probe as _;
6use static_executor_cortex_m as _;
7
8pub use defmt::{info, intern};
9
10use core::sync::atomic::{AtomicUsize, Ordering};
11
12#[defmt::timestamp]
13fn timestamp() -> u64 {
14 static COUNT: AtomicUsize = AtomicUsize::new(0);
15 // NOTE(no-CAS) `timestamps` runs with interrupts disabled
16 let n = COUNT.load(Ordering::Relaxed);
17 COUNT.store(n + 1, Ordering::Relaxed);
18 n as u64
19}
20
21macro_rules! depanic {
22 ($( $i:expr ),*) => {
23 {
24 defmt::error!($( $i ),*);
25 panic!();
26 }
27 }
28}
29
30pub trait Dewrap<T> {
31 /// dewrap = defmt unwrap
32 fn dewrap(self) -> T;
33
34 /// dexpect = defmt expect
35 fn dexpect<M: defmt::Format>(self, msg: M) -> T;
36}
37
38impl<T> Dewrap<T> for Option<T> {
39 fn dewrap(self) -> T {
40 match self {
41 Some(t) => t,
42 None => depanic!("Dewrap failed: enum is none"),
43 }
44 }
45
46 fn dexpect<M: defmt::Format>(self, msg: M) -> T {
47 match self {
48 Some(t) => t,
49 None => depanic!("Unexpected None: {:?}", msg),
50 }
51 }
52}
53
54impl<T, E: defmt::Format> Dewrap<T> for Result<T, E> {
55 fn dewrap(self) -> T {
56 match self {
57 Ok(t) => t,
58 Err(e) => depanic!("Dewrap failed: {:?}", e),
59 }
60 }
61
62 fn dexpect<M: defmt::Format>(self, msg: M) -> T {
63 match self {
64 Ok(t) => t,
65 Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e),
66 }
67 }
68}