diff --git a/benches/bench.rs b/benches/bench.rs index 7318c8ac0c..e1920479c9 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -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)); diff --git a/src/lib.rs b/src/lib.rs index 4e14825ddd..00b98cae81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 /// @@ -404,7 +404,7 @@ 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 { @@ -412,6 +412,33 @@ impl Navigator { } } + /// 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. @@ -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% @@ -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]; @@ -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]; @@ -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 /// @@ -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; ///