Skip to content

Commit 7f0bedc

Browse files
committed
Add get_max_duty() and convenience methods
1 parent 73623ff commit 7f0bedc

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

embedded-hal/src/pwm.rs

+50-7
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,62 @@ impl<T: ErrorType> ErrorType for &mut T {
5959

6060
/// Single PWM channel / pin
6161
pub trait SetDuty: ErrorType {
62-
/// Set the duty cycle.
62+
/// Get the maximum duty cycle value.
6363
///
64-
/// `duty` is the duty cycle. Valid values span the entire `u16` range:
64+
/// This value corresponds to a 100% duty cycle.
65+
fn get_max_duty(&self) -> u16;
66+
67+
/// Set the duty cycle to `duty / max_duty`.
68+
///
69+
/// The caller is responsible for ensuring that the duty cycle value is less than or equal to the maximum duty cycle value,
70+
/// as reported by `get_max_duty`.
71+
fn set_duty(&mut self, duty: u16) -> Result<(), Self::Error>;
72+
73+
/// Set the duty cycle to 0%, or always inactive.
74+
fn set_off(&mut self) -> Result<(), Self::Error> {
75+
self.set_duty(0)
76+
}
77+
78+
/// Set the duty cycle to 100%, or always active.
79+
fn set_on(&mut self) -> Result<(), Self::Error> {
80+
self.set_duty(self.get_max_duty())
81+
}
82+
83+
/// Set the duty cycle to `num / denom`.
6584
///
66-
/// - `duty = 0` is considered 0% duty, which makes the pin permanently low.
67-
/// - `duty = u16::MAX` is considered 100% duty, which makes the pin permanently high.
85+
/// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
86+
/// and that `denom` is not zero.
87+
fn set_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
88+
let duty = num as u32 * self.get_max_duty() as u32 / denom as u32;
89+
self.set_duty(duty as u16)
90+
}
91+
92+
/// Set the duty cycle to `percent / 100`
6893
///
69-
/// Implementations must scale the duty value linearly to the range required by the hardware.
70-
fn set_duty(&mut self, duty: u16) -> Self::Error;
94+
/// The caller is responsible for ensuring that `percent` is less than or equal to 100.
95+
fn set_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
96+
self.set_fraction(percent as u16, 100)
97+
}
7198
}
7299

73100
impl<T: SetDuty> SetDuty for &mut T {
74-
fn set_duty(&mut self, duty: u16) -> Self::Error {
101+
fn get_max_duty(&self) -> u16 {
102+
T::get_max_duty(self)
103+
}
104+
fn set_duty(&mut self, duty: u16) -> Result<(), Self::Error> {
75105
T::set_duty(self, duty)
76106
}
107+
108+
fn set_off(&mut self) -> Result<(), Self::Error> {
109+
T::set_off(self)
110+
}
111+
fn set_on(&mut self) -> Result<(), Self::Error> {
112+
T::set_on(self)
113+
}
114+
fn set_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
115+
T::set_fraction(self, num, denom)
116+
}
117+
fn set_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
118+
T::set_percent(self, percent)
119+
}
77120
}

0 commit comments

Comments
 (0)