Skip to content

Commit 2fc4bad

Browse files
bors[bot]Andful
andauthored
Merge #13
13: Added get_duty_cycle and set_duty_cycle functions r=posborne a=Andful `README.md` documents functions `get_duty_cycle` and `set_duty_cycle` but does not contain its implementations. This pull request should fix that. Co-authored-by: Andrea Nardi <[email protected]>
2 parents 89b078e + 350488c commit 2fc4bad

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
@@ -196,6 +196,19 @@ impl Pwm {
196196
Ok(())
197197
}
198198

199+
/// Get the currently configured duty_cycle as percentage of period
200+
pub fn get_duty_cycle(&self) -> Result<f32> {
201+
Ok((self.get_duty_cycle_ns()? as f32) / (self.get_period_ns()? as f32))
202+
}
203+
204+
/// The active time of the PWM signal
205+
///
206+
/// Value is as percentage of period.
207+
pub fn set_duty_cycle(&self, duty_cycle: f32) -> Result<()> {
208+
self.set_duty_cycle_ns((self.get_period_ns()? as f32 * duty_cycle).round() as u32)?;
209+
Ok(())
210+
}
211+
199212
/// Get the currently configured period in nanoseconds
200213
pub fn get_period_ns(&self) -> Result<u32> {
201214
pwm_file_parse::<u32>(&self.chip, self.number, "period")

0 commit comments

Comments
 (0)