From 1c2faa2774ca9e645084a978023809b4299633b7 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Tue, 27 Feb 2024 16:22:02 -0300 Subject: [PATCH] src: lib: Add set_pwm_channels_duty_cycle and set_pwm_channel_duty_cycle_values --- src/lib.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 50ae7be0ea..06f977d73e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -542,6 +542,34 @@ impl Navigator { } } + /// Like [`set_pwm_channel_duty_cycle`](struct.Navigator.html#method.set_pwm_channel_duty_cycle). This function + /// sets the Duty Cycle for a list of multiple channels. + /// + /// # 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 + /// + /// let channels: [PwmChannel; 3] = [PwmChannel::Ch1, PwmChannel::Ch1, PwmChannel::Ch2]; + /// + /// nav.set_pwm_channels_duty_cycle(&channels, 0.5); // sets the duty cycle according to the list. + /// ``` + pub fn set_pwm_channels_duty_cycle( + &mut self, + channels: &[PwmChannel; N], + duty_cycle: f32, + ) { + for &channel in channels { + self.set_pwm_channel_duty_cycle(channel, duty_cycle) + } + } + /// 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 with multiple values. /// @@ -571,6 +599,35 @@ impl Navigator { } } + /// Like [`set_pwm_channel_duty_cycle`](struct.Navigator.html#method.set_pwm_channel_duty_cycle). This function + /// sets the Duty Cycle for a list of multiple channels with multiple values. + /// + /// # 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 + /// + /// let channels: [PwmChannel; 3] = [PwmChannel::Ch1, PwmChannel::Ch1, PwmChannel::Ch2]; + /// let values: [f32; 3] = [0.1, 0.2, 0.3]; + /// + /// nav.set_pwm_channels_duty_cycle_values(&channels, &values); // sets the duty cycle according to the lists. + /// ``` + pub fn set_pwm_channels_duty_cycle_values( + &mut self, + channels: &[PwmChannel; N], + duty_cycle: &[f32; N], + ) { + for i in 0..N { + self.set_pwm_channel_duty_cycle(channels[i], duty_cycle[i]) + } + } + /// Sets the PWM frequency of [`Navigator`]. /// /// It changes the PRE_SCALE value on PCA9685.