use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use std::thread; use std::time::{Duration, Instant}; use embedded_hal::digital::v2::OutputPin; use esp_idf_hal::gpio::{Gpio17, Output}; use esp_idf_hal::peripherals::Peripherals; enum NoteDuration { DottedQuarter, Quarter, Eighth, TripletEighth, Sixteenth, } fn main() -> anyhow::Result<()> { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. esp_idf_sys::link_patches(); println!("Hello, world!"); let peripherals = Peripherals::take().unwrap(); let mut speaker = peripherals.pins.gpio17.into_output()?; loop { // kinda sus thread::sleep(Duration::from_millis(600)); play_note(&mut speaker, 523, NoteDuration::Eighth)?; play_note(&mut speaker, 622, NoteDuration::Eighth)?; play_note(&mut speaker, 698, NoteDuration::Eighth)?; play_note(&mut speaker, 739, NoteDuration::Eighth)?; play_note(&mut speaker, 698, NoteDuration::Eighth)?; play_note(&mut speaker, 622, NoteDuration::Eighth)?; play_note(&mut speaker, 523, NoteDuration::DottedQuarter)?; play_note(&mut speaker, 466, NoteDuration::Sixteenth)?; play_note(&mut speaker, 587, NoteDuration::Sixteenth)?; play_note(&mut speaker, 523, NoteDuration::Quarter)?; thread::sleep(Duration::from_millis(1200)); play_note(&mut speaker, 523, NoteDuration::Eighth)?; play_note(&mut speaker, 622, NoteDuration::Eighth)?; play_note(&mut speaker, 698, NoteDuration::Eighth)?; play_note(&mut speaker, 739, NoteDuration::Eighth)?; play_note(&mut speaker, 698, NoteDuration::Eighth)?; play_note(&mut speaker, 622, NoteDuration::Eighth)?; play_note(&mut speaker, 739, NoteDuration::DottedQuarter)?; thread::sleep(Duration::from_millis(300)); play_note(&mut speaker, 739, NoteDuration::TripletEighth)?; play_note(&mut speaker, 698, NoteDuration::TripletEighth)?; play_note(&mut speaker, 622, NoteDuration::TripletEighth)?; play_note(&mut speaker, 739, NoteDuration::TripletEighth)?; play_note(&mut speaker, 698, NoteDuration::TripletEighth)?; play_note(&mut speaker, 622, NoteDuration::TripletEighth)?; play_note(&mut speaker, 523, NoteDuration::Quarter)?; } loop { thread::sleep(Duration::from_millis(1000)) } } fn play_note(pin: &mut Gpio17, hz: u64, duration: NoteDuration) -> anyhow::Result<()> { let duration = match duration { NoteDuration::DottedQuarter => 900, NoteDuration::Quarter => 600, NoteDuration::Eighth => 300, NoteDuration::TripletEighth => 200, NoteDuration::Sixteenth => 150, }; let now = Instant::now(); loop { if now.elapsed().as_millis() >= duration { break; } pin.set_high()?; thread::sleep(Duration::from_millis(1000 / hz)); pin.set_low()?; } Ok(()) }