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 authored and patrickelectric committed Feb 29, 2024
1 parent b6fd0a4 commit 1c2faa2
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/lib.rs
Expand Up @@ -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<const N: usize>(
&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.
///
Expand Down Expand Up @@ -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<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 1c2faa2

Please sign in to comment.