diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-07-20 08:01:42 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-07-20 08:01:42 +0200 |
| commit | b04dc7e7837161bc8d9c6cf3cf40d876f2f82c9a (patch) | |
| tree | 1bb10d83863fe53961fb43d6c0b4e9b01201a234 /examples | |
| parent | 17999381877714ccc6bfbb81320a0e51821cdd58 (diff) | |
| parent | 72d6f79ec731980bf776d35bfc0af848a1d994da (diff) | |
Merge pull request #226 from huntc/mpsc
Multi Producer Single Consumer channel
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/mpsc.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/mpsc.rs b/examples/nrf/src/bin/mpsc.rs new file mode 100644 index 000000000..443955239 --- /dev/null +++ b/examples/nrf/src/bin/mpsc.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(min_type_alias_impl_trait)] | ||
| 4 | #![feature(impl_trait_in_bindings)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | #![allow(incomplete_features)] | ||
| 7 | |||
| 8 | #[path = "../example_common.rs"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use defmt::panic; | ||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy::time::{Duration, Timer}; | ||
| 14 | use embassy::util::mpsc::TryRecvError; | ||
| 15 | use embassy::util::{mpsc, Forever}; | ||
| 16 | use embassy_nrf::gpio::{Level, Output, OutputDrive}; | ||
| 17 | use embassy_nrf::Peripherals; | ||
| 18 | use embedded_hal::digital::v2::OutputPin; | ||
| 19 | use mpsc::{Channel, Sender, WithNoThreads}; | ||
| 20 | |||
| 21 | enum LedState { | ||
| 22 | On, | ||
| 23 | Off, | ||
| 24 | } | ||
| 25 | |||
| 26 | static CHANNEL: Forever<Channel<WithNoThreads, LedState, 1>> = Forever::new(); | ||
| 27 | |||
| 28 | #[embassy::task(pool_size = 1)] | ||
| 29 | async fn my_task(sender: Sender<'static, WithNoThreads, LedState, 1>) { | ||
| 30 | loop { | ||
| 31 | let _ = sender.send(LedState::On).await; | ||
| 32 | Timer::after(Duration::from_secs(1)).await; | ||
| 33 | let _ = sender.send(LedState::Off).await; | ||
| 34 | Timer::after(Duration::from_secs(1)).await; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | #[embassy::main] | ||
| 39 | async fn main(spawner: Spawner, p: Peripherals) { | ||
| 40 | |||
| 41 | let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); | ||
| 42 | |||
| 43 | let channel = CHANNEL.put(Channel::new()); | ||
| 44 | let (sender, mut receiver) = mpsc::split(channel); | ||
| 45 | |||
| 46 | spawner.spawn(my_task(sender)).unwrap(); | ||
| 47 | |||
| 48 | // We could just loop on `receiver.recv()` for simplicity. The code below | ||
| 49 | // is optimized to drain the queue as fast as possible in the spirit of | ||
| 50 | // handling events as fast as possible. This optimization is benign when in | ||
| 51 | // thread mode, but can be useful when interrupts are sending messages | ||
| 52 | // with the channel having been created via with_critical_sections. | ||
| 53 | loop { | ||
| 54 | let maybe_message = match receiver.try_recv() { | ||
| 55 | m @ Ok(..) => m.ok(), | ||
| 56 | Err(TryRecvError::Empty) => receiver.recv().await, | ||
| 57 | Err(TryRecvError::Closed) => break, | ||
| 58 | }; | ||
| 59 | match maybe_message { | ||
| 60 | Some(LedState::On) => led.set_high().unwrap(), | ||
| 61 | Some(LedState::Off) => led.set_low().unwrap(), | ||
| 62 | _ => (), | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
