Last active
April 13, 2024 17:02
-
-
Save kalkyl/b0f524751aba1c4aad2fe7e65caeb584 to your computer and use it in GitHub Desktop.
Cancellation of running async fn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use embassy_executor::Spawner; | |
use embassy_futures::join::join; | |
use embassy_futures::select::{select, Either}; | |
use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pull}; | |
use embassy_sync::blocking_mutex::raw::NoopRawMutex; | |
use embassy_sync::signal::Signal; | |
use embassy_time::Timer; | |
use {defmt_rtt as _, panic_probe as _}; | |
enum Animation { | |
A, | |
B, | |
} | |
#[embassy_executor::main] | |
async fn main(_spawner: Spawner) { | |
let p = embassy_rp::init(Default::default()); | |
let mut led = Output::new(AnyPin::from(p.PIN_25), Level::Low); | |
let mut switch = Input::new(AnyPin::from(p.PIN_16), Pull::Up); | |
let signal: Signal<NoopRawMutex, Animation> = Signal::new(); | |
let mut animation = match switch.get_level() { | |
Level::Low => Animation::A, | |
Level::High => Animation::B, | |
}; | |
let sw_fut = async { | |
loop { | |
let _ = switch.wait_for_any_edge().await; | |
Timer::after_millis(20).await; // Debounce | |
signal.signal(match switch.get_level() { | |
Level::Low => Animation::A, | |
Level::High => Animation::B, | |
}); | |
} | |
}; | |
let anim_fut = async { | |
loop { | |
if let Either::First(new_anim) = select(signal.wait(), run_animation(&mut led, &animation)).await { | |
animation = new_anim; | |
} | |
} | |
}; | |
join(sw_fut, anim_fut).await; | |
} | |
async fn run_animation(led: &mut Output<'_>, animation: &Animation) { | |
match animation { | |
Animation::A => animation_a(led).await, | |
Animation::B => animation_b(led).await, | |
} | |
} | |
async fn animation_a(led: &mut Output<'_>) { | |
loop { | |
led.set_high(); | |
Timer::after_millis(100).await; | |
led.set_low(); | |
Timer::after_millis(100).await; | |
} | |
} | |
async fn animation_b(led: &mut Output<'_>) { | |
loop { | |
led.set_high(); | |
Timer::after_millis(500).await; | |
led.set_low(); | |
Timer::after_millis(500).await; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment