Skip to content

Commit

Permalink
src: lib: Add set_pwm_channels_duty_cycle and set_pwm_channel_duty_cy…
Browse files Browse the repository at this point in the history
…cle_values
  • Loading branch information
RaulTrombin committed Feb 27, 2024
1 parent f67b2cc commit fea6e56
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,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<const N: usize>(
&mut self,
channels: &[PwmChannel; N],
duty_cycle: f32,
) {
for &channel in channels.iter().take(N) {
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.
///
Expand Down Expand Up @@ -570,6 +598,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<const N: usize>(
&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.
Expand Down

0 comments on commit fea6e56

Please sign in to comment.