66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| #![no_std]
 | |
| #![no_main]
 | |
| /// This example demonstrates how to access a given pin from more than one embassy task
 | |
| /// The on-board LED is toggled by two tasks with slightly different periods, leading to the
 | |
| /// apparent duty cycle of the LED increasing, then decreasing, linearly. The phenomenon is similar
 | |
| /// to interference and the 'beats' you can hear if you play two frequencies close to one another
 | |
| /// [Link explaining it](https://www.physicsclassroom.com/class/sound/Lesson-3/Interference-and-Beats)
 | |
| use defmt::*;
 | |
| use embassy_executor::Spawner;
 | |
| use embassy_rp::block::ImageDef;
 | |
| use embassy_rp::gpio;
 | |
| use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
 | |
| use embassy_sync::channel::{Channel, Sender};
 | |
| use embassy_time::{Duration, Ticker};
 | |
| use gpio::{AnyPin, Level, Output};
 | |
| use {defmt_rtt as _, panic_probe as _};
 | |
| 
 | |
| #[link_section = ".start_block"]
 | |
| #[used]
 | |
| pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
 | |
| 
 | |
| // Program metadata for `picotool info`
 | |
| #[link_section = ".bi_entries"]
 | |
| #[used]
 | |
| pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
 | |
|     embassy_rp::binary_info::rp_program_name!(c"example"),
 | |
|     embassy_rp::binary_info::rp_cargo_version!(),
 | |
|     embassy_rp::binary_info::rp_program_description!(c"Blinky"),
 | |
|     embassy_rp::binary_info::rp_program_build_attribute!(),
 | |
| ];
 | |
| 
 | |
| enum LedState {
 | |
|     Toggle,
 | |
| }
 | |
| static CHANNEL: Channel<ThreadModeRawMutex, LedState, 64> = Channel::new();
 | |
| 
 | |
| #[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::High);
 | |
| 
 | |
|     let dt = 100 * 1_000_000;
 | |
|     let k = 1.003;
 | |
| 
 | |
|     unwrap!(spawner.spawn(toggle_led(CHANNEL.sender(), Duration::from_nanos(dt))));
 | |
|     unwrap!(spawner.spawn(toggle_led(
 | |
|         CHANNEL.sender(),
 | |
|         Duration::from_nanos((dt as f64 * k) as u64)
 | |
|     )));
 | |
| 
 | |
|     loop {
 | |
|         match CHANNEL.receive().await {
 | |
|             LedState::Toggle => led.toggle(),
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| #[embassy_executor::task(pool_size = 2)]
 | |
| async fn toggle_led(control: Sender<'static, ThreadModeRawMutex, LedState, 64>, delay: Duration) {
 | |
|     let mut ticker = Ticker::every(delay);
 | |
|     loop {
 | |
|         control.send(LedState::Toggle).await;
 | |
|         ticker.next().await;
 | |
|     }
 | |
| }
 |