diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f0/src/bin/button_controlled_blink.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/stm32f0/src/bin/button_controlled_blink.rs b/examples/stm32f0/src/bin/button_controlled_blink.rs new file mode 100644 index 000000000..e1f223433 --- /dev/null +++ b/examples/stm32f0/src/bin/button_controlled_blink.rs | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | //! This example showcases how to create task | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use core::sync::atomic::{AtomicU32, Ordering}; | ||
| 8 | |||
| 9 | use defmt::info; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::exti::ExtiInput; | ||
| 12 | use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed}; | ||
| 13 | use embassy_time::{Duration, Timer}; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | static BLINK_MS: AtomicU32 = AtomicU32::new(0); | ||
| 17 | |||
| 18 | #[embassy_executor::task] | ||
| 19 | async fn led_task(led: AnyPin) { | ||
| 20 | // Configure the LED pin as a push pull ouput and obtain handler. | ||
| 21 | // On the Nucleo F091RC theres an on-board LED connected to pin PA5. | ||
| 22 | let mut led = Output::new(led, Level::Low, Speed::Low); | ||
| 23 | |||
| 24 | loop { | ||
| 25 | let del = BLINK_MS.load(Ordering::Relaxed); | ||
| 26 | info!("Value of del is {}", del); | ||
| 27 | Timer::after(Duration::from_millis(del.into())).await; | ||
| 28 | info!("LED toggling"); | ||
| 29 | led.toggle(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | #[embassy_executor::main] | ||
| 34 | async fn main(spawner: Spawner) { | ||
| 35 | // Initialize and create handle for devicer peripherals | ||
| 36 | let p = embassy_stm32::init(Default::default()); | ||
| 37 | |||
| 38 | // Configure the button pin and obtain handler. | ||
| 39 | // On the Nucleo F091RC there is a button connected to pin PC13. | ||
| 40 | let button = Input::new(p.PC13, Pull::None); | ||
| 41 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 42 | |||
| 43 | // Create and initialize a delay variable to manage delay loop | ||
| 44 | let mut del_var = 2000; | ||
| 45 | |||
| 46 | // Blink duration value to global context | ||
| 47 | BLINK_MS.store(del_var, Ordering::Relaxed); | ||
| 48 | |||
| 49 | // Spawn LED blinking task | ||
| 50 | spawner.spawn(led_task(p.PA5.degrade())).unwrap(); | ||
| 51 | |||
| 52 | loop { | ||
| 53 | // Check if button got pressed | ||
| 54 | button.wait_for_rising_edge().await; | ||
| 55 | info!("rising_edge"); | ||
| 56 | del_var = del_var - 200; | ||
| 57 | // If updated delay value drops below 200 then reset it back to starting value | ||
| 58 | if del_var < 200 { | ||
| 59 | del_var = 2000; | ||
| 60 | } | ||
| 61 | // Updated delay value to global context | ||
| 62 | BLINK_MS.store(del_var, Ordering::Relaxed); | ||
| 63 | } | ||
| 64 | } | ||
