2022-02-11 22:00:31 +01:00
|
|
|
//! This example showcases channels and timing utilities of embassy.
|
|
|
|
//!
|
|
|
|
//! This example works best on STM32F3DISCOVERY board. It flashes a single LED, then if the USER
|
|
|
|
//! button is pressed the next LED is flashed (in clockwise fashion). If the USER button is double
|
|
|
|
//! clicked, then the direction changes. If the USER button is pressed for 1 second, then all the
|
|
|
|
//! LEDS flash 3 times.
|
|
|
|
//!
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-02-11 22:00:31 +01:00
|
|
|
use embassy_stm32::exti::ExtiInput;
|
|
|
|
use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed};
|
|
|
|
use embassy_stm32::peripherals::PA0;
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
|
2022-08-22 22:00:06 +02:00
|
|
|
use embassy_sync::channel::Channel;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{with_timeout, Duration, Timer};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-02-11 22:00:31 +01:00
|
|
|
|
|
|
|
struct Leds<'a> {
|
|
|
|
leds: [Output<'a, AnyPin>; 8],
|
|
|
|
direction: i8,
|
|
|
|
current_led: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Leds<'a> {
|
|
|
|
fn new(pins: [Output<'a, AnyPin>; 8]) -> Self {
|
|
|
|
Self {
|
|
|
|
leds: pins,
|
|
|
|
direction: 1,
|
|
|
|
current_led: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_direction(&mut self) {
|
|
|
|
self.direction *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn move_next(&mut self) {
|
|
|
|
if self.direction > 0 {
|
|
|
|
self.current_led = (self.current_led + 1) & 7;
|
|
|
|
} else {
|
|
|
|
self.current_led = (8 + self.current_led - 1) & 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:00:29 +02:00
|
|
|
async fn show(&mut self) {
|
2022-02-11 22:00:31 +01:00
|
|
|
self.leds[self.current_led].set_high();
|
2023-08-11 11:58:22 +02:00
|
|
|
if let Ok(new_message) = with_timeout(Duration::from_millis(500), CHANNEL.receive()).await {
|
2022-02-16 21:22:35 +01:00
|
|
|
self.leds[self.current_led].set_low();
|
|
|
|
self.process_event(new_message).await;
|
|
|
|
} else {
|
|
|
|
self.leds[self.current_led].set_low();
|
2023-08-11 11:58:22 +02:00
|
|
|
if let Ok(new_message) = with_timeout(Duration::from_millis(200), CHANNEL.receive()).await {
|
2022-02-16 21:22:35 +01:00
|
|
|
self.process_event(new_message).await;
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 22:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn flash(&mut self) {
|
|
|
|
for _ in 0..3 {
|
|
|
|
for led in &mut self.leds {
|
|
|
|
led.set_high();
|
|
|
|
}
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(500).await;
|
2022-02-11 22:00:31 +01:00
|
|
|
for led in &mut self.leds {
|
|
|
|
led.set_low();
|
|
|
|
}
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(200).await;
|
2022-02-11 22:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 21:22:35 +01:00
|
|
|
|
2022-04-06 00:00:29 +02:00
|
|
|
async fn process_event(&mut self, event: ButtonEvent) {
|
2022-02-16 21:22:35 +01:00
|
|
|
match event {
|
2022-04-06 00:00:29 +02:00
|
|
|
ButtonEvent::SingleClick => {
|
|
|
|
self.move_next();
|
|
|
|
}
|
|
|
|
ButtonEvent::DoubleClick => {
|
2022-02-16 21:22:35 +01:00
|
|
|
self.change_direction();
|
2022-04-06 00:00:29 +02:00
|
|
|
self.move_next();
|
|
|
|
}
|
|
|
|
ButtonEvent::Hold => {
|
|
|
|
self.flash().await;
|
2022-02-16 21:22:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 22:00:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 21:22:35 +01:00
|
|
|
#[derive(Format)]
|
2022-02-11 22:00:31 +01:00
|
|
|
enum ButtonEvent {
|
|
|
|
SingleClick,
|
|
|
|
DoubleClick,
|
|
|
|
Hold,
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:00:29 +02:00
|
|
|
static CHANNEL: Channel<ThreadModeRawMutex, ButtonEvent, 4> = Channel::new();
|
2022-02-11 22:00:31 +01:00
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
let p = embassy_stm32::init(Default::default());
|
2022-02-11 22:00:31 +01:00
|
|
|
let button = Input::new(p.PA0, Pull::Down);
|
|
|
|
let button = ExtiInput::new(button, p.EXTI0);
|
|
|
|
info!("Press the USER button...");
|
|
|
|
let leds = [
|
|
|
|
Output::new(p.PE9.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE10.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE11.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE12.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE13.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE14.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE15.degrade(), Level::Low, Speed::Low),
|
|
|
|
Output::new(p.PE8.degrade(), Level::Low, Speed::Low),
|
|
|
|
];
|
|
|
|
let leds = Leds::new(leds);
|
|
|
|
|
2022-04-06 00:00:29 +02:00
|
|
|
spawner.spawn(button_waiter(button)).unwrap();
|
|
|
|
spawner.spawn(led_blinker(leds)).unwrap();
|
2022-02-11 22:00:31 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2022-04-06 00:00:29 +02:00
|
|
|
async fn led_blinker(mut leds: Leds<'static>) {
|
2022-02-11 22:00:31 +01:00
|
|
|
loop {
|
2022-04-06 00:00:29 +02:00
|
|
|
leds.show().await;
|
2022-02-11 22:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::task]
|
2022-04-06 00:00:29 +02:00
|
|
|
async fn button_waiter(mut button: ExtiInput<'static, PA0>) {
|
2022-02-11 22:00:31 +01:00
|
|
|
const DOUBLE_CLICK_DELAY: u64 = 250;
|
|
|
|
const HOLD_DELAY: u64 = 1000;
|
|
|
|
|
|
|
|
button.wait_for_rising_edge().await;
|
|
|
|
loop {
|
2022-06-12 22:15:44 +02:00
|
|
|
if with_timeout(Duration::from_millis(HOLD_DELAY), button.wait_for_falling_edge())
|
|
|
|
.await
|
|
|
|
.is_err()
|
2022-02-11 22:00:31 +01:00
|
|
|
{
|
|
|
|
info!("Hold");
|
2022-04-06 00:00:29 +02:00
|
|
|
CHANNEL.send(ButtonEvent::Hold).await;
|
2022-02-11 22:00:31 +01:00
|
|
|
button.wait_for_falling_edge().await;
|
2022-06-12 22:15:44 +02:00
|
|
|
} else if with_timeout(Duration::from_millis(DOUBLE_CLICK_DELAY), button.wait_for_rising_edge())
|
|
|
|
.await
|
|
|
|
.is_err()
|
2022-02-11 22:00:31 +01:00
|
|
|
{
|
|
|
|
info!("Single click");
|
2022-04-06 00:00:29 +02:00
|
|
|
CHANNEL.send(ButtonEvent::SingleClick).await;
|
2022-02-11 22:00:31 +01:00
|
|
|
} else {
|
|
|
|
info!("Double click");
|
2022-04-06 00:00:29 +02:00
|
|
|
CHANNEL.send(ButtonEvent::DoubleClick).await;
|
2022-02-11 22:00:31 +01:00
|
|
|
button.wait_for_falling_edge().await;
|
|
|
|
}
|
|
|
|
button.wait_for_rising_edge().await;
|
|
|
|
}
|
|
|
|
}
|