summaryrefslogtreecommitdiff
path: root/examples/stm32f3/src/bin
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2022-04-06 00:00:29 +0200
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2022-04-06 01:34:08 +0200
commit27a1b0ea7316be4687e7173a73861d276974d502 (patch)
tree6edd9fd7c2d69a5ad130dc13ae7c0bbed442e640 /examples/stm32f3/src/bin
parentaee19185b7cf34466f7941784b55e639c925fae4 (diff)
downloadembassy-27a1b0ea7316be4687e7173a73861d276974d502.zip
Simpler Channel.
- Allow initializing in a static, without Forever. - Remove ability to close, since in embedded enviromnents channels usually live forever and don't get closed. - Remove MPSC restriction, it's MPMC now. Rename "mpsc" to "channel". - `Sender` and `Receiver` are still available if you want to enforce a piece of code only has send/receive access, but are optional: you can send/receive directly into the Channel if you want.
Diffstat (limited to 'examples/stm32f3/src/bin')
-rw-r--r--examples/stm32f3/src/bin/button_events.rs59
1 files changed, 24 insertions, 35 deletions
diff --git a/examples/stm32f3/src/bin/button_events.rs b/examples/stm32f3/src/bin/button_events.rs
index 99aab302..06e8eec1 100644
--- a/examples/stm32f3/src/bin/button_events.rs
+++ b/examples/stm32f3/src/bin/button_events.rs
@@ -11,11 +11,10 @@
#![feature(type_alias_impl_trait)]
use defmt::*;
-use embassy::blocking_mutex::raw::NoopRawMutex;
-use embassy::channel::mpsc::{self, Channel, Receiver, Sender};
+use embassy::blocking_mutex::raw::ThreadModeRawMutex;
+use embassy::channel::channel::Channel;
use embassy::executor::Spawner;
use embassy::time::{with_timeout, Duration, Timer};
-use embassy::util::Forever;
use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::peripherals::PA0;
@@ -51,14 +50,15 @@ impl<'a> Leds<'a> {
}
}
- async fn show(&mut self, queue: &mut Receiver<'static, NoopRawMutex, ButtonEvent, 4>) {
+ async fn show(&mut self) {
self.leds[self.current_led].set_high();
- if let Ok(new_message) = with_timeout(Duration::from_millis(500), queue.recv()).await {
+ if let Ok(new_message) = with_timeout(Duration::from_millis(500), CHANNEL.recv()).await {
self.leds[self.current_led].set_low();
self.process_event(new_message).await;
} else {
self.leds[self.current_led].set_low();
- if let Ok(new_message) = with_timeout(Duration::from_millis(200), queue.recv()).await {
+ if let Ok(new_message) = with_timeout(Duration::from_millis(200), CHANNEL.recv()).await
+ {
self.process_event(new_message).await;
}
}
@@ -77,15 +77,18 @@ impl<'a> Leds<'a> {
}
}
- async fn process_event(&mut self, event: Option<ButtonEvent>) {
+ async fn process_event(&mut self, event: ButtonEvent) {
match event {
- Some(ButtonEvent::SingleClick) => self.move_next(),
- Some(ButtonEvent::DoubleClick) => {
+ ButtonEvent::SingleClick => {
+ self.move_next();
+ }
+ ButtonEvent::DoubleClick => {
self.change_direction();
- self.move_next()
+ self.move_next();
+ }
+ ButtonEvent::Hold => {
+ self.flash().await;
}
- Some(ButtonEvent::Hold) => self.flash().await,
- _ => {}
}
}
}
@@ -97,7 +100,7 @@ enum ButtonEvent {
Hold,
}
-static BUTTON_EVENTS_QUEUE: Forever<Channel<NoopRawMutex, ButtonEvent, 4>> = Forever::new();
+static CHANNEL: Channel<ThreadModeRawMutex, ButtonEvent, 4> = Channel::new();
#[embassy::main]
async fn main(spawner: Spawner, p: Peripherals) {
@@ -116,27 +119,19 @@ async fn main(spawner: Spawner, p: Peripherals) {
];
let leds = Leds::new(leds);
- let buttons_queue = BUTTON_EVENTS_QUEUE.put(Channel::new());
- let (sender, receiver) = mpsc::split(buttons_queue);
- spawner.spawn(button_waiter(button, sender)).unwrap();
- spawner.spawn(led_blinker(leds, receiver)).unwrap();
+ spawner.spawn(button_waiter(button)).unwrap();
+ spawner.spawn(led_blinker(leds)).unwrap();
}
#[embassy::task]
-async fn led_blinker(
- mut leds: Leds<'static>,
- mut queue: Receiver<'static, NoopRawMutex, ButtonEvent, 4>,
-) {
+async fn led_blinker(mut leds: Leds<'static>) {
loop {
- leds.show(&mut queue).await;
+ leds.show().await;
}
}
#[embassy::task]
-async fn button_waiter(
- mut button: ExtiInput<'static, PA0>,
- queue: Sender<'static, NoopRawMutex, ButtonEvent, 4>,
-) {
+async fn button_waiter(mut button: ExtiInput<'static, PA0>) {
const DOUBLE_CLICK_DELAY: u64 = 250;
const HOLD_DELAY: u64 = 1000;
@@ -150,9 +145,7 @@ async fn button_waiter(
.is_err()
{
info!("Hold");
- if queue.send(ButtonEvent::Hold).await.is_err() {
- break;
- }
+ CHANNEL.send(ButtonEvent::Hold).await;
button.wait_for_falling_edge().await;
} else if with_timeout(
Duration::from_millis(DOUBLE_CLICK_DELAY),
@@ -161,15 +154,11 @@ async fn button_waiter(
.await
.is_err()
{
- if queue.send(ButtonEvent::SingleClick).await.is_err() {
- break;
- }
info!("Single click");
+ CHANNEL.send(ButtonEvent::SingleClick).await;
} else {
info!("Double click");
- if queue.send(ButtonEvent::DoubleClick).await.is_err() {
- break;
- }
+ CHANNEL.send(ButtonEvent::DoubleClick).await;
button.wait_for_falling_edge().await;
}
button.wait_for_rising_edge().await;