From 1bad6a498930991fcdc7454ad38e371d0966087d Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Fri, 3 Jun 2022 19:47:17 +0200 Subject: [PATCH] Add example for using a Signal. --- examples/stm32h7/src/bin/signal.rs | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/stm32h7/src/bin/signal.rs diff --git a/examples/stm32h7/src/bin/signal.rs b/examples/stm32h7/src/bin/signal.rs new file mode 100644 index 000000000..d2531b2ba --- /dev/null +++ b/examples/stm32h7/src/bin/signal.rs @@ -0,0 +1,41 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +// global logger +use defmt::{info, unwrap}; +use defmt_rtt as _; + +use panic_probe as _; + +use embassy::channel::Signal; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; + +use embassy_stm32::Peripherals; + +static SIGNAL: Signal = Signal::new(); + +#[embassy::task] +async fn my_sending_task() { + let mut counter: u32 = 0; + + loop { + Timer::after(Duration::from_secs(1)).await; + + SIGNAL.signal(counter); + + counter = counter.wrapping_add(1); + } +} + +#[embassy::main] +async fn main(spawner: Spawner, _p: Peripherals) { + unwrap!(spawner.spawn(my_sending_task())); + + loop { + let received_counter = SIGNAL.wait().await; + + info!("signalled, counter: {}", received_counter); + } +}