aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f3
diff options
context:
space:
mode:
authorCristian Eigel <[email protected]>2022-02-16 21:22:35 +0100
committerCristian Eigel <[email protected]>2022-02-16 21:22:35 +0100
commitf4ac3cf3648494dfbe6c18148bbef55ddc7f9f48 (patch)
tree01372a186cda2dd1603b1f83e8d42087179988a7 /examples/stm32f3
parent59f909e66533097b56093ec55fdd1341dbff6744 (diff)
Improve logic for processing button events
Diffstat (limited to 'examples/stm32f3')
-rw-r--r--examples/stm32f3/src/bin/button_events.rs40
1 files changed, 25 insertions, 15 deletions
diff --git a/examples/stm32f3/src/bin/button_events.rs b/examples/stm32f3/src/bin/button_events.rs
index 1218edd2b..0bd7b342f 100644
--- a/examples/stm32f3/src/bin/button_events.rs
+++ b/examples/stm32f3/src/bin/button_events.rs
@@ -50,11 +50,17 @@ impl<'a> Leds<'a> {
50 } 50 }
51 } 51 }
52 52
53 async fn blink(&mut self) { 53 async fn show(&mut self, queue: &mut Receiver<'static, NoopRawMutex, ButtonEvent, 4>) {
54 self.leds[self.current_led].set_high(); 54 self.leds[self.current_led].set_high();
55 Timer::after(Duration::from_millis(500)).await; 55 if let Ok(new_message) = with_timeout(Duration::from_millis(500), queue.recv()).await {
56 self.leds[self.current_led].set_low(); 56 self.leds[self.current_led].set_low();
57 Timer::after(Duration::from_millis(200)).await; 57 self.process_event(new_message).await;
58 } else {
59 self.leds[self.current_led].set_low();
60 if let Ok(new_message) = with_timeout(Duration::from_millis(200), queue.recv()).await {
61 self.process_event(new_message).await;
62 }
63 }
58 } 64 }
59 65
60 async fn flash(&mut self) { 66 async fn flash(&mut self) {
@@ -69,8 +75,21 @@ impl<'a> Leds<'a> {
69 Timer::after(Duration::from_millis(200)).await; 75 Timer::after(Duration::from_millis(200)).await;
70 } 76 }
71 } 77 }
78
79 async fn process_event(&mut self, event: Option<ButtonEvent>) {
80 match event {
81 Some(ButtonEvent::SingleClick) => self.move_next(),
82 Some(ButtonEvent::DoubleClick) => {
83 self.change_direction();
84 self.move_next()
85 }
86 Some(ButtonEvent::Hold) => self.flash().await,
87 _ => {}
88 }
89 }
72} 90}
73 91
92#[derive(Format)]
74enum ButtonEvent { 93enum ButtonEvent {
75 SingleClick, 94 SingleClick,
76 DoubleClick, 95 DoubleClick,
@@ -105,19 +124,10 @@ async fn main(spawner: Spawner, p: Peripherals) {
105#[embassy::task] 124#[embassy::task]
106async fn led_blinker( 125async fn led_blinker(
107 mut leds: Leds<'static>, 126 mut leds: Leds<'static>,
108 queue: Receiver<'static, NoopRawMutex, ButtonEvent, 4>, 127 mut queue: Receiver<'static, NoopRawMutex, ButtonEvent, 4>,
109) { 128) {
110 loop { 129 loop {
111 leds.blink().await; 130 leds.show(&mut queue).await;
112 match queue.try_recv() {
113 Ok(ButtonEvent::SingleClick) => leds.move_next(),
114 Ok(ButtonEvent::DoubleClick) => {
115 leds.change_direction();
116 leds.move_next()
117 }
118 Ok(ButtonEvent::Hold) => leds.flash().await,
119 _ => {}
120 }
121 } 131 }
122} 132}
123 133