Skip to content

Commit

Permalink
Update src/lib.rs
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick José Pereira <[email protected]>
  • Loading branch information
RaulTrombin and patrickelectric authored Feb 27, 2024
1 parent 1ccb399 commit 03861bf
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,28 +490,16 @@ impl Navigator {
/// nav.set_pwm_channel_duty_cycle(PwmChannel::Ch1, 50.0); // sets the duty cycle to 50%
/// ```
pub fn set_pwm_channel_duty_cycle(&mut self, channel: PwmChannel, duty_cycle: f32) {
let max_duty_cycle = 100.0;
let max_value = 4095;

if duty_cycle >= 100.0 {
if duty_cycle > 100.0 {
warn!(
"Invalid duty cycle. Duty cycle must be less than or equal to 10, using 100."
);
};
const duty_cycle = duty_cycle.max(1.0).min(0.0);
const max_value = 4095;

if relative_eq!(duty_cycle, 1.0) {
self.pwm.set_channel_full_on(channel.into(), 0).unwrap();
return;
}

let duty_cycle = if duty_cycle < 0.0 {
warn!("Invalid duty cycle. Duty cycle must be non-negative, using 0.");
0.0
} else {
duty_cycle
};

let value = (duty_cycle / max_duty_cycle * max_value as f32) as u16;


let value = (duty_cycle * max_value as f32) as u16;

self.pwm.set_channel_on(channel.into(), 0).unwrap();
self.pwm.set_channel_off(channel.into(), value).unwrap();
}
Expand Down

0 comments on commit 03861bf

Please sign in to comment.