Created
April 2, 2024 14:31
-
-
Save tarfu/fa689e68982f347260b9f9b071314a50 to your computer and use it in GitHub Desktop.
Crude embassy-rp pwm HAL abstraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use core::convert::Infallible; | |
use embassy_rp::pwm::{Channel, Config, Pwm}; | |
use embedded_hal::pwm::{ErrorType, SetDutyCycle}; | |
pub struct PwmChannelB<'d, T: Channel> { | |
pwm: Pwm<'d, T>, | |
config: Config, | |
max_is_logical_one: bool | |
} | |
impl<'d, T: Channel> PwmChannelB<'d, T> { | |
pub fn new(pwm: Pwm<'d, T>, config: Config, max_is_logical_one: bool) -> Self { | |
Self { pwm, config, max_is_logical_one } | |
} | |
} | |
impl<'d, T: Channel> ErrorType for PwmChannelB<'d, T> { | |
type Error = Infallible; | |
} | |
impl<'d, T: Channel> SetDutyCycle for PwmChannelB<'d, T> { | |
fn max_duty_cycle(&self) -> u16 { | |
self.config.top | |
} | |
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { | |
self.config.compare_b = if self.max_is_logical_one && duty >= self.config.top { | |
self.config.top + 1 | |
} else { | |
duty | |
}; | |
self.pwm.set_config(&self.config); | |
Ok(()) | |
} | |
} | |
pub struct PwmChannelA<'d, T: Channel> { | |
pwm: Pwm<'d, T>, | |
config: Config, | |
max_is_logical_one: bool | |
} | |
impl<'d, T: Channel> crate::pico_pwm_hal::PwmChannelA<'d, T> { | |
pub fn new(pwm: Pwm<'d, T>, config: Config, max_is_logical_one: bool) -> Self { | |
Self { pwm, config, max_is_logical_one } | |
} | |
} | |
impl<'d, T: Channel> ErrorType for crate::pico_pwm_hal::PwmChannelA<'d, T> { | |
type Error = Infallible; | |
} | |
impl<'d, T: Channel> SetDutyCycle for crate::pico_pwm_hal::PwmChannelA<'d, T> { | |
fn max_duty_cycle(&self) -> u16 { | |
self.config.top | |
} | |
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { | |
self.config.compare_a = if self.max_is_logical_one && duty >= self.config.top { | |
self.config.top + 1 | |
} else { | |
duty | |
}; | |
self.pwm.set_config(&self.config); | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment