Skip to content

Commit 350488c

Browse files
committed
added get_duty_cycle and set_duty_cycle functions
Signed-off-by: Andrea Nardi <[email protected]>
1 parent 1acd9c7 commit 350488c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ impl Pwm {
189189
Ok(())
190190
}
191191

192+
/// Get the currently configured duty_cycle as percentage of period
193+
pub fn get_duty_cycle(&self) -> Result<f32> {
194+
Ok((self.get_duty_cycle_ns()? as f32) / (self.get_period_ns()? as f32))
195+
}
196+
197+
/// The active time of the PWM signal
198+
///
199+
/// Value is as percentage of period.
200+
pub fn set_duty_cycle(&self, duty_cycle: f32) -> Result<()> {
201+
self.set_duty_cycle_ns((self.get_period_ns()? as f32 * duty_cycle).round() as u32)?;
202+
Ok(())
203+
}
204+
192205
/// Get the currently configured period in nanoseconds
193206
pub fn get_period_ns(&self) -> Result<u32> {
194207
pwm_file_parse::<u32>(&self.chip, self.number, "period")

0 commit comments

Comments
 (0)