mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
button controlled example
This commit is contained in:
parent
46fd82d184
commit
63122f6d7e
64
examples/stm32f0/src/bin/button_controlled_blink.rs
Normal file
64
examples/stm32f0/src/bin/button_controlled_blink.rs
Normal file
@ -0,0 +1,64 @@
|
||||
//! This example showcases how to create task
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
static BLINK_MS: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn led_task(led: AnyPin) {
|
||||
// Configure the LED pin as a push pull ouput and obtain handler.
|
||||
// On the Nucleo F091RC theres an on-board LED connected to pin PA5.
|
||||
let mut led = Output::new(led, Level::Low, Speed::Low);
|
||||
|
||||
loop {
|
||||
let del = BLINK_MS.load(Ordering::Relaxed);
|
||||
info!("Value of del is {}", del);
|
||||
Timer::after(Duration::from_millis(del.into())).await;
|
||||
info!("LED toggling");
|
||||
led.toggle();
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
// Initialize and create handle for devicer peripherals
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
// Configure the button pin and obtain handler.
|
||||
// On the Nucleo F091RC there is a button connected to pin PC13.
|
||||
let button = Input::new(p.PC13, Pull::None);
|
||||
let mut button = ExtiInput::new(button, p.EXTI13);
|
||||
|
||||
// Create and initialize a delay variable to manage delay loop
|
||||
let mut del_var = 2000;
|
||||
|
||||
// Blink duration value to global context
|
||||
BLINK_MS.store(del_var, Ordering::Relaxed);
|
||||
|
||||
// Spawn LED blinking task
|
||||
spawner.spawn(led_task(p.PA5.degrade())).unwrap();
|
||||
|
||||
loop {
|
||||
// Check if button got pressed
|
||||
button.wait_for_rising_edge().await;
|
||||
info!("rising_edge");
|
||||
del_var = del_var - 200;
|
||||
// If updated delay value drops below 200 then reset it back to starting value
|
||||
if del_var < 200 {
|
||||
del_var = 2000;
|
||||
}
|
||||
// Updated delay value to global context
|
||||
BLINK_MS.store(del_var, Ordering::Relaxed);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user