diff --git a/embassy-strike-driver/src/drivers.rs b/embassy-strike-driver/src/drivers.rs index e522011..de7c71f 100644 --- a/embassy-strike-driver/src/drivers.rs +++ b/embassy-strike-driver/src/drivers.rs @@ -97,6 +97,10 @@ pub mod rp { } impl PwmSource for PwmDriver<'_> { + fn max_duty(&self) -> u16 { + self.pwm.max_duty_cycle() + } + fn set_duty(&mut self, duty: u16) { let duty = duty.clamp(0, self.pwm.max_duty_cycle()); diff --git a/embassy-strike-driver/src/lib.rs b/embassy-strike-driver/src/lib.rs index de0c804..1f6b82e 100644 --- a/embassy-strike-driver/src/lib.rs +++ b/embassy-strike-driver/src/lib.rs @@ -141,6 +141,7 @@ where pub async fn tune(&mut self, samples: &mut [u16]) { let mut duty = 0; + let max_duty = self.pwm.max_duty(); self.pwm.set_duty(duty); Timer::after_secs(2).await; let mut act_value = self.adc.sample_average(samples).await; @@ -149,7 +150,7 @@ where while act_value < 1500 { duty += 2; - if duty >= 256 { + if duty >= max_duty { #[cfg(feature = "debug")] info!("RESET: {} @ Duty {}", act_value, duty); duty = 0; @@ -183,8 +184,8 @@ where ) { loop { let (time, samples) = dma.send().await; - *time = self.get_timestamp(); self.adc.sample(samples).await; + *time = self.get_timestamp(); dma.send_done(); } } @@ -309,6 +310,10 @@ mod tests { struct MockPwm<'a>(&'a MockMachine); impl PwmSource for MockPwm<'_> { + fn max_duty(&self) -> u16 { + 255 + } + fn set_duty(&mut self, duty: u16) { self.0.pwm.set(duty); } diff --git a/embassy-strike-driver/src/traits.rs b/embassy-strike-driver/src/traits.rs index de18acc..c3c9d77 100644 --- a/embassy-strike-driver/src/traits.rs +++ b/embassy-strike-driver/src/traits.rs @@ -11,6 +11,7 @@ pub trait AdcSource { } pub trait PwmSource { + fn max_duty(&self) -> u16; fn set_duty(&mut self, duty: u16); fn enable(&mut self); fn disable(&mut self);