628: Improve logic for processing button events r=Dirbaio a=ceigel

Allow blinking to be interrupted by button presses in this sample.

Co-authored-by: Cristian Eigel <cristian.eigel@esrlabs.com>
This commit is contained in:
bors[bot] 2022-02-23 02:50:12 +00:00 committed by GitHub
commit 78795d6f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,11 +50,17 @@ impl<'a> Leds<'a> {
} }
} }
async fn blink(&mut self) { async fn show(&mut self, queue: &mut Receiver<'static, NoopRawMutex, ButtonEvent, 4>) {
self.leds[self.current_led].set_high(); self.leds[self.current_led].set_high();
Timer::after(Duration::from_millis(500)).await; if let Ok(new_message) = with_timeout(Duration::from_millis(500), queue.recv()).await {
self.leds[self.current_led].set_low(); self.leds[self.current_led].set_low();
Timer::after(Duration::from_millis(200)).await; 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 {
self.process_event(new_message).await;
}
}
} }
async fn flash(&mut self) { async fn flash(&mut self) {
@ -69,8 +75,21 @@ impl<'a> Leds<'a> {
Timer::after(Duration::from_millis(200)).await; Timer::after(Duration::from_millis(200)).await;
} }
} }
async fn process_event(&mut self, event: Option<ButtonEvent>) {
match event {
Some(ButtonEvent::SingleClick) => self.move_next(),
Some(ButtonEvent::DoubleClick) => {
self.change_direction();
self.move_next()
}
Some(ButtonEvent::Hold) => self.flash().await,
_ => {}
}
}
} }
#[derive(Format)]
enum ButtonEvent { enum ButtonEvent {
SingleClick, SingleClick,
DoubleClick, DoubleClick,
@ -105,19 +124,10 @@ async fn main(spawner: Spawner, p: Peripherals) {
#[embassy::task] #[embassy::task]
async fn led_blinker( async fn led_blinker(
mut leds: Leds<'static>, mut leds: Leds<'static>,
queue: Receiver<'static, NoopRawMutex, ButtonEvent, 4>, mut queue: Receiver<'static, NoopRawMutex, ButtonEvent, 4>,
) { ) {
loop { loop {
leds.blink().await; leds.show(&mut queue).await;
match queue.try_recv() {
Ok(ButtonEvent::SingleClick) => leds.move_next(),
Ok(ButtonEvent::DoubleClick) => {
leds.change_direction();
leds.move_next()
}
Ok(ButtonEvent::Hold) => leds.flash().await,
_ => {}
}
} }
} }