aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-07-20 08:01:42 +0200
committerGitHub <[email protected]>2021-07-20 08:01:42 +0200
commitb04dc7e7837161bc8d9c6cf3cf40d876f2f82c9a (patch)
tree1bb10d83863fe53961fb43d6c0b4e9b01201a234 /examples
parent17999381877714ccc6bfbb81320a0e51821cdd58 (diff)
parent72d6f79ec731980bf776d35bfc0af848a1d994da (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.rs65
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"]
9mod example_common;
10
11use defmt::panic;
12use embassy::executor::Spawner;
13use embassy::time::{Duration, Timer};
14use embassy::util::mpsc::TryRecvError;
15use embassy::util::{mpsc, Forever};
16use embassy_nrf::gpio::{Level, Output, OutputDrive};
17use embassy_nrf::Peripherals;
18use embedded_hal::digital::v2::OutputPin;
19use mpsc::{Channel, Sender, WithNoThreads};
20
21enum LedState {
22 On,
23 Off,
24}
25
26static CHANNEL: Forever<Channel<WithNoThreads, LedState, 1>> = Forever::new();
27
28#[embassy::task(pool_size = 1)]
29async 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]
39async 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}