Skip to content

Use f64 for increased precision #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/breathe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ const BB_PWM_CHIP: u32 = 0;
const BB_PWM_NUMBER: u32 = 0;

fn pwm_increase_to_max(pwm: &Pwm, duration_ms: u32, update_period_ms: u32) -> Result<()> {
let step: f32 = duration_ms as f32 / update_period_ms as f32;
let step: f64 = duration_ms as f64 / update_period_ms as f64;
let mut duty_cycle = 0.0;
let period_ns: u32 = pwm.get_period_ns()?;
while duty_cycle < 1.0 {
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32)?;
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f64) as u32)?;
duty_cycle += step;
}
pwm.set_duty_cycle_ns(period_ns)
}

fn pwm_decrease_to_minimum(pwm: &Pwm, duration_ms: u32, update_period_ms: u32) -> Result<()> {
let step: f32 = duration_ms as f32 / update_period_ms as f32;
let step: f64 = duration_ms as f64 / update_period_ms as f64;
let mut duty_cycle = 1.0;
let period_ns: u32 = pwm.get_period_ns()?;
while duty_cycle > 0.0 {
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f32) as u32)?;
pwm.set_duty_cycle_ns((duty_cycle * period_ns as f64) as u32)?;
duty_cycle -= step;
}
pwm.set_duty_cycle_ns(0)
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ impl Pwm {
}

/// Get the currently configured duty_cycle as percentage of period
pub fn get_duty_cycle(&self) -> Result<f32> {
Ok((self.get_duty_cycle_ns()? as f32) / (self.get_period_ns()? as f32))
pub fn get_duty_cycle<F>(&self) -> Result<f64> {
Ok((self.get_duty_cycle_ns()? as f64) / (self.get_period_ns()? as f64))
}

/// The active time of the PWM signal
///
/// Value is as percentage of period.
pub fn set_duty_cycle(&self, duty_cycle: f32) -> Result<()> {
self.set_duty_cycle_ns((self.get_period_ns()? as f32 * duty_cycle).round() as u32)?;
pub fn set_duty_cycle(&self, duty_cycle: f64) -> Result<()> {
self.set_duty_cycle_ns((self.get_period_ns()? as f64 * duty_cycle).round() as u32)?;
Ok(())
}

Expand Down