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: Get pwm enable #63

Merged
merged 5 commits into from
Dec 6, 2023
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
3 changes: 2 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ fn navigator_benchmark(c: &mut Criterion) {
bench!(read_all());

// Benchmark Outputs
bench!(pwm_enable(false));
bench!(set_pwm_enable(false));
bench!(get_pwm_enable());
bench!(set_pwm_channel_value(PwmChannel::Ch1, 100));
bench!(set_pwm_freq_hz(60.0));
bench!(set_pwm_freq_prescale(100));
Expand Down
41 changes: 34 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ impl Navigator {
.expect("Error : Error on magnetometer during self-test")
}

/// Sets the PWM IC to be enabled through firmware and OE_pin.
/// Sets the PWM IC to be enabled through OE_pin.
///
/// # Arguments
///
Expand All @@ -404,14 +404,41 @@ impl Navigator {
/// # Examples
///
/// Please check [`set_pwm_channel_value`](struct.Navigator.html#method.set_pwm_channel_value).
pub fn pwm_enable(&mut self, state: bool) {
pub fn set_pwm_enable(&mut self, state: bool) {
if state {
self.pwm.oe_pin.set_direction(Direction::Low).unwrap();
} else {
self.pwm.oe_pin.set_direction(Direction::High).unwrap();
}
}

/// Get the PWM IC enabling value through OE_pin.
///
/// # Examples
///
/// ```no_run
/// use navigator_rs::{Navigator};
/// use std::thread::sleep;
/// use std::time::Duration;
///
/// let mut nav = Navigator::new();
///
/// nav.init();
/// loop {
/// let previous = nav.get_pwm_enable();
/// println!("Enable pin logic value is {previous}.");
///
/// nav.set_pwm_enable(!previous);
///
/// println!("Enable pin logic value is {}.", nav.get_pwm_enable());
///
/// sleep(Duration::from_millis(1000));
/// }
/// ```
pub fn get_pwm_enable(&mut self) -> bool {
self.pwm.oe_pin.get_value().expect("Error: Get PWM value") == 1
}

/// Sets the Duty Cycle (high value time) of selected channel.
///
/// On PCA9685, this function sets the `OFF` counter and uses ON value as 0.
Expand All @@ -427,7 +454,7 @@ impl Navigator {
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.pwm_enable(true);
/// nav.set_pwm_enable(true);
///
/// nav.set_pwm_freq_prescale(99); // sets the pwm frequency to 60 Hz
/// nav.set_pwm_channel_value(PwmChannel::Ch1, 2048); // sets the duty cycle to 50%
Expand All @@ -453,7 +480,7 @@ impl Navigator {
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.pwm_enable(true);
/// nav.set_pwm_enable(true);
/// nav.set_pwm_freq_prescale(99); // sets the pwm frequency to 60 Hz
///
/// let channels: [PwmChannel; 3] = [PwmChannel::Ch1, PwmChannel::Ch1, PwmChannel::Ch2];
Expand Down Expand Up @@ -482,7 +509,7 @@ impl Navigator {
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.pwm_enable(true);
/// nav.set_pwm_enable(true);
/// nav.set_pwm_freq_prescale(99); // sets the pwm frequency to 60 Hz
///
/// let channels: [PwmChannel; 3] = [PwmChannel::Ch1, PwmChannel::Ch1, PwmChannel::Ch2];
Expand Down Expand Up @@ -531,7 +558,7 @@ impl Navigator {
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.pwm_enable(true);
/// nav.set_pwm_enable(true);
///
/// nav.set_pwm_freq_prescale(99); // sets the pwm frequency to 60 Hz
///
Expand Down Expand Up @@ -563,7 +590,7 @@ impl Navigator {
/// let mut nav = Navigator::new();
///
/// nav.init();
/// nav.pwm_enable(true);
/// nav.set_pwm_enable(true);
///
/// let mut i: f32 = 10.0;
///
Expand Down
Loading