diff options
| author | Cristian Eigel <[email protected]> | 2022-02-11 22:00:31 +0100 |
|---|---|---|
| committer | Cristian Eigel <[email protected]> | 2022-02-11 22:10:47 +0100 |
| commit | 0e0e23c5be6c14a8f0034f886d729c7c8daa4337 (patch) | |
| tree | c0419d853a9b9414df8bf5e95176bef7582a31d7 | |
| parent | 37bd796fb3090236a9569db7f7fc2bf7b6e8e2b2 (diff) | |
Add button_events example for stm32f3
| -rw-r--r-- | examples/stm32f3/src/bin/button_events.rs | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/examples/stm32f3/src/bin/button_events.rs b/examples/stm32f3/src/bin/button_events.rs new file mode 100644 index 000000000..720ed9d11 --- /dev/null +++ b/examples/stm32f3/src/bin/button_events.rs | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | //! This example showcases channels and timing utilities of embassy. | ||
| 2 | //! | ||
| 3 | //! This example works best on STM32F3DISCOVERY board. It flashes a single LED, then if the USER | ||
| 4 | //! button is pressed the next LED is flashed (in clockwise fashion). If the USER button is double | ||
| 5 | //! clicked, then the direction changes. If the USER button is pressed for 1 second, then all the | ||
| 6 | //! LEDS flash 3 times. | ||
| 7 | //! | ||
| 8 | |||
| 9 | #![no_std] | ||
| 10 | #![no_main] | ||
| 11 | #![feature(type_alias_impl_trait)] | ||
| 12 | |||
| 13 | #[path = "../example_common.rs"] | ||
| 14 | mod example_common; | ||
| 15 | use embassy::blocking_mutex::kind::Noop; | ||
| 16 | use embassy::channel::mpsc::{self, Channel, Receiver, Sender}; | ||
| 17 | use embassy::executor::Spawner; | ||
| 18 | use embassy::time::{with_timeout, Duration, Timer}; | ||
| 19 | use embassy::util::Forever; | ||
| 20 | use embassy_stm32::exti::ExtiInput; | ||
| 21 | use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed}; | ||
| 22 | use embassy_stm32::peripherals::PA0; | ||
| 23 | use embassy_stm32::Peripherals; | ||
| 24 | use example_common::*; | ||
| 25 | |||
| 26 | struct Leds<'a> { | ||
| 27 | leds: [Output<'a, AnyPin>; 8], | ||
| 28 | direction: i8, | ||
| 29 | current_led: usize, | ||
| 30 | } | ||
| 31 | |||
| 32 | impl<'a> Leds<'a> { | ||
| 33 | fn new(pins: [Output<'a, AnyPin>; 8]) -> Self { | ||
| 34 | Self { | ||
| 35 | leds: pins, | ||
| 36 | direction: 1, | ||
| 37 | current_led: 0, | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | fn change_direction(&mut self) { | ||
| 42 | self.direction *= -1; | ||
| 43 | } | ||
| 44 | |||
| 45 | fn move_next(&mut self) { | ||
| 46 | if self.direction > 0 { | ||
| 47 | self.current_led = (self.current_led + 1) & 7; | ||
| 48 | } else { | ||
| 49 | self.current_led = (8 + self.current_led - 1) & 7; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | async fn blink(&mut self) { | ||
| 54 | self.leds[self.current_led].set_high(); | ||
| 55 | Timer::after(Duration::from_millis(500)).await; | ||
| 56 | self.leds[self.current_led].set_low(); | ||
| 57 | Timer::after(Duration::from_millis(200)).await; | ||
| 58 | } | ||
| 59 | |||
| 60 | async fn flash(&mut self) { | ||
| 61 | for _ in 0..3 { | ||
| 62 | for led in &mut self.leds { | ||
| 63 | led.set_high(); | ||
| 64 | } | ||
| 65 | Timer::after(Duration::from_millis(500)).await; | ||
| 66 | for led in &mut self.leds { | ||
| 67 | led.set_low(); | ||
| 68 | } | ||
| 69 | Timer::after(Duration::from_millis(200)).await; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | enum ButtonEvent { | ||
| 75 | SingleClick, | ||
| 76 | DoubleClick, | ||
| 77 | Hold, | ||
| 78 | } | ||
| 79 | |||
| 80 | static BUTTON_EVENTS_QUEUE: Forever<Channel<Noop, ButtonEvent, 4>> = Forever::new(); | ||
| 81 | |||
| 82 | #[embassy::main] | ||
| 83 | async fn main(spawner: Spawner, p: Peripherals) { | ||
| 84 | let button = Input::new(p.PA0, Pull::Down); | ||
| 85 | let button = ExtiInput::new(button, p.EXTI0); | ||
| 86 | info!("Press the USER button..."); | ||
| 87 | let leds = [ | ||
| 88 | Output::new(p.PE9.degrade(), Level::Low, Speed::Low), | ||
| 89 | Output::new(p.PE10.degrade(), Level::Low, Speed::Low), | ||
| 90 | Output::new(p.PE11.degrade(), Level::Low, Speed::Low), | ||
| 91 | Output::new(p.PE12.degrade(), Level::Low, Speed::Low), | ||
| 92 | Output::new(p.PE13.degrade(), Level::Low, Speed::Low), | ||
| 93 | Output::new(p.PE14.degrade(), Level::Low, Speed::Low), | ||
| 94 | Output::new(p.PE15.degrade(), Level::Low, Speed::Low), | ||
| 95 | Output::new(p.PE8.degrade(), Level::Low, Speed::Low), | ||
| 96 | ]; | ||
| 97 | let leds = Leds::new(leds); | ||
| 98 | |||
| 99 | let buttons_queue = BUTTON_EVENTS_QUEUE.put(Channel::new()); | ||
| 100 | let (sender, receiver) = mpsc::split(buttons_queue); | ||
| 101 | spawner.spawn(button_waiter(button, sender)).unwrap(); | ||
| 102 | spawner.spawn(led_blinker(leds, receiver)).unwrap(); | ||
| 103 | } | ||
| 104 | |||
| 105 | #[embassy::task] | ||
| 106 | async fn led_blinker(mut leds: Leds<'static>, queue: Receiver<'static, Noop, ButtonEvent, 4>) { | ||
| 107 | loop { | ||
| 108 | leds.blink().await; | ||
| 109 | match queue.try_recv() { | ||
| 110 | Ok(ButtonEvent::SingleClick) => leds.move_next(), | ||
| 111 | Ok(ButtonEvent::DoubleClick) => { | ||
| 112 | leds.change_direction(); | ||
| 113 | leds.move_next() | ||
| 114 | } | ||
| 115 | Ok(ButtonEvent::Hold) => leds.flash().await, | ||
| 116 | _ => {} | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | #[embassy::task] | ||
| 122 | async fn button_waiter( | ||
| 123 | mut button: ExtiInput<'static, PA0>, | ||
| 124 | queue: Sender<'static, Noop, ButtonEvent, 4>, | ||
| 125 | ) { | ||
| 126 | const DOUBLE_CLICK_DELAY: u64 = 250; | ||
| 127 | const HOLD_DELAY: u64 = 1000; | ||
| 128 | |||
| 129 | button.wait_for_rising_edge().await; | ||
| 130 | loop { | ||
| 131 | if with_timeout( | ||
| 132 | Duration::from_millis(HOLD_DELAY), | ||
| 133 | button.wait_for_falling_edge(), | ||
| 134 | ) | ||
| 135 | .await | ||
| 136 | .is_err() | ||
| 137 | { | ||
| 138 | info!("Hold"); | ||
| 139 | if queue.send(ButtonEvent::Hold).await.is_err() { | ||
| 140 | break; | ||
| 141 | } | ||
| 142 | button.wait_for_falling_edge().await; | ||
| 143 | } else if with_timeout( | ||
| 144 | Duration::from_millis(DOUBLE_CLICK_DELAY), | ||
| 145 | button.wait_for_rising_edge(), | ||
| 146 | ) | ||
| 147 | .await | ||
| 148 | .is_err() | ||
| 149 | { | ||
| 150 | if queue.send(ButtonEvent::SingleClick).await.is_err() { | ||
| 151 | break; | ||
| 152 | } | ||
| 153 | info!("Single click"); | ||
| 154 | } else { | ||
| 155 | info!("Double click"); | ||
| 156 | if queue.send(ButtonEvent::DoubleClick).await.is_err() { | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | button.wait_for_falling_edge().await; | ||
| 160 | } | ||
| 161 | button.wait_for_rising_edge().await; | ||
| 162 | } | ||
| 163 | } | ||
