Skip to content
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

Add: set_pwm_channel_duty_cycle #68

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "README.md"
[dependencies]
ads1x1x = "0.2.2"
ak09915_rs= "0.2.0"
approx = "0.5.1"
bmp280 = "0.4.0"
embedded-hal = "0.2.7"
icm20689 = "0.1.1"
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,37 @@ impl Navigator {
self.pwm.set_channel_off(channel.into(), value).unwrap();
}

/// Calculate and set the necessary values for the desired Duty Cycle (high value time) of selected channel.
///
/// This method also allows the relay mode using a value of 1.0.
///
/// # Examples
///
/// ```no_run
/// use navigator_rs::{Navigator, PwmChannel};
///
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.set_pwm_enable(true);
///
/// nav.set_pwm_freq_prescale(99); // sets the pwm frequency to 60 Hz
/// nav.set_pwm_channel_duty_cycle(PwmChannel::Ch1, 0.50); // sets the duty cycle to 50%
/// ```
pub fn set_pwm_channel_duty_cycle(&mut self, channel: PwmChannel, duty_cycle: f32) {
let duty_cycle = duty_cycle.max(0.0).min(1.0);
let max_value = 4095;

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

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

self.set_pwm_channel_value(channel, value);
}

/// Like [`set_pwm_channel_value`](struct.Navigator.html#method.set_pwm_channel_value). This function
/// sets the Duty Cycle for a list of multiple channels.
///
Expand Down
Loading