use embedded_hal::PwmPin;
use rp2040_hal::{
gpio::{bank0::Gpio28, Pin, PinId},
pwm::{FreeRunning, Pwm6, Slice},
};
pub mod music {
pub const C: f32 = 261.63;
pub const D: f32 = 293.66;
pub const E: f32 = 329.63;
pub const F: f32 = 349.23;
pub const G: f32 = 392.00;
pub const A: f32 = 440.00;
pub const B: f32 = 493.88;
pub const REST: f32 = 0.0;
}
const XTAL_FREQ: f32 = 12_000_000.0;
fn calc_note(freq: f32) -> u16 {
(XTAL_FREQ / 40.0 / freq) as u16
}
pub struct Audio {
pwm: Slice<Pwm6, FreeRunning>,
}
impl Audio {
pub fn new(
mut pwm: Slice<Pwm6, FreeRunning>,
audio_pin: Pin<Gpio28, <Gpio28 as PinId>::Reset>,
) -> Self {
pwm.enable();
pwm.set_div_frac(40);
pwm.set_div_int(40);
let channel = &mut pwm.channel_a;
channel.output_to(audio_pin);
Self { pwm }
}
pub fn play(&mut self, freq: f32) {
let top = calc_note(freq);
self.pwm.channel_a.set_duty(top / 2);
self.pwm.set_top(top);
}
pub fn stop(&mut self) {
self.pwm.channel_a.set_duty(0);
}
}