#![doc = include_str!("../../docs/buzzer.md")]
use embassy_stm32::{
gpio::OutputType,
peripherals::{PA0, TIM2},
time::khz,
timer::simple_pwm::{PwmPin, SimplePwm, SimplePwmChannel},
};
use embassy_sync::{
blocking_mutex::raw::{RawMutex, ThreadModeRawMutex},
mutex::{Mutex, TryLockError},
};
use embedded_hal::pwm::SetDutyCycle;
pub type BuddyBuzzer<'a> = Buzzer<ThreadModeRawMutex, SimplePwmChannel<'a, TIM2>>;
pub(crate) fn build_buzzer<'a>(ch: PA0, tim: TIM2) -> BuddyBuzzer<'a> {
let buzzer = PwmPin::new_ch1(ch, OutputType::PushPull);
let pwm = SimplePwm::new(
tim,
Some(buzzer),
None,
None,
None,
khz(21),
Default::default(),
);
let mut channels = pwm.split();
channels.ch1.enable();
Buzzer::new(channels.ch1)
}
pub struct Buzzer<M: RawMutex, T: SetDutyCycle> {
ch: Mutex<M, T>,
}
impl<M: RawMutex, T: SetDutyCycle> Buzzer<M, T> {
pub fn new(ch: T) -> Self {
Self { ch: Mutex::new(ch) }
}
pub async fn set_duty_cycle_fully_off(&self) {
let mut ch = self.ch.lock().await;
ch.set_duty_cycle_fully_off().unwrap();
}
pub fn try_set_duty_cycle_fully_off(&self) -> Result<(), TryLockError> {
let mut ch = self.ch.try_lock()?;
ch.set_duty_cycle_fully_off().unwrap();
Ok(())
}
pub async fn set_duty_cycle_fully_on(&self) {
let mut ch = self.ch.lock().await;
ch.set_duty_cycle_fully_on().unwrap();
}
pub fn try_set_duty_cycle_fully_on(&self) -> Result<(), TryLockError> {
let mut ch = self.ch.try_lock()?;
ch.set_duty_cycle_fully_on().unwrap();
Ok(())
}
pub async fn set_duty_cycle_fraction(&self, num: u16, denom: u16) {
let mut ch = self.ch.lock().await;
ch.set_duty_cycle_fraction(num, denom).unwrap();
}
pub fn try_set_duty_cycle_fraction(&self, num: u16, denom: u16) -> Result<(), TryLockError> {
let mut ch = self.ch.try_lock()?;
ch.set_duty_cycle_fraction(num, denom).unwrap();
Ok(())
}
pub async fn set_duty_cycle_percent(&self, percent: u8) {
let mut ch = self.ch.lock().await;
ch.set_duty_cycle_percent(percent).unwrap();
}
pub fn try_set_duty_cycle_percent(&self, percent: u8) -> Result<(), TryLockError> {
let mut ch = self.ch.try_lock()?;
ch.set_duty_cycle_percent(percent).unwrap();
Ok(())
}
}