Skip to content

Commit

Permalink
pwm: Rename "duty" to "duty cycle".
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Mar 7, 2023
1 parent 3f53ffc commit d5f9747
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions embedded-hal/src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,67 +58,71 @@ impl<T: ErrorType> ErrorType for &mut T {
}

/// Single PWM channel / pin
pub trait SetDuty: ErrorType {
pub trait SetDutyCycle: ErrorType {
/// Get the maximum duty cycle value.
///
/// This value corresponds to a 100% duty cycle.
fn get_max_duty(&self) -> u16;
fn get_max_duty_cycle(&self) -> u16;

/// Set the duty cycle to `duty / max_duty`.
///
/// The caller is responsible for ensuring that the duty cycle value is less than or equal to the maximum duty cycle value,
/// as reported by `get_max_duty`.
fn set_duty(&mut self, duty: u16) -> Result<(), Self::Error>;
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error>;

/// Set the duty cycle to 0%, or always inactive.
#[inline]
fn set_off(&mut self) -> Result<(), Self::Error> {
self.set_duty(0)
fn set_duty_cycle_fully_off(&mut self) -> Result<(), Self::Error> {
self.set_duty_cycle(0)
}

/// Set the duty cycle to 100%, or always active.
#[inline]
fn set_on(&mut self) -> Result<(), Self::Error> {
self.set_duty(self.get_max_duty())
fn set_duty_cycle_fully_on(&mut self) -> Result<(), Self::Error> {
self.set_duty_cycle(self.get_max_duty_cycle())
}

/// Set the duty cycle to `num / denom`.
///
/// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
/// and that `denom` is not zero.
#[inline]
fn set_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
let duty = num as u32 * self.get_max_duty() as u32 / denom as u32;
self.set_duty(duty as u16)
fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
let duty = num as u32 * self.get_max_duty_cycle() as u32 / denom as u32;
self.set_duty_cycle(duty as u16)

This comment has been minimized.

Copy link
@nyurik

nyurik Nov 22, 2023

Contributor

Clippy is warning about u32 as u16 lossy cast. Is this the indented behavior?

This comment has been minimized.

Copy link
@Sh3Rm4n

Sh3Rm4n Nov 23, 2023

Contributor

It seems like this is done to widen the integer for multiplication, so that it does not overflow and the devision than guarantees, that this again in the range of u16.

Though

        self.set_duty_cycle(u16::try_from(duty).unwrap_or(u16::MAX))

might be safer?

This comment has been minimized.

Copy link
@nyurik

nyurik Nov 23, 2023

Contributor

thx, created a #531

}

/// Set the duty cycle to `percent / 100`
///
/// The caller is responsible for ensuring that `percent` is less than or equal to 100.
#[inline]
fn set_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
self.set_fraction(percent as u16, 100)
fn set_duty_cycle_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
self.set_duty_cycle_fraction(percent as u16, 100)
}
}

impl<T: SetDuty> SetDuty for &mut T {
fn get_max_duty(&self) -> u16 {
T::get_max_duty(self)
impl<T: SetDutyCycle> SetDutyCycle for &mut T {
fn get_max_duty_cycle(&self) -> u16 {
T::get_max_duty_cycle(self)
}
fn set_duty(&mut self, duty: u16) -> Result<(), Self::Error> {
T::set_duty(self, duty)

fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
T::set_duty_cycle(self, duty)
}

fn set_off(&mut self) -> Result<(), Self::Error> {
T::set_off(self)
fn set_duty_cycle_fully_off(&mut self) -> Result<(), Self::Error> {
T::set_duty_cycle_fully_off(self)
}
fn set_on(&mut self) -> Result<(), Self::Error> {
T::set_on(self)

fn set_duty_cycle_fully_on(&mut self) -> Result<(), Self::Error> {
T::set_duty_cycle_fully_on(self)
}
fn set_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
T::set_fraction(self, num, denom)

fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
T::set_duty_cycle_fraction(self, num, denom)
}
fn set_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
T::set_percent(self, percent)

fn set_duty_cycle_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
T::set_duty_cycle_percent(self, percent)
}
}

0 comments on commit d5f9747

Please sign in to comment.