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

Updade: Integrations navigator 0.3.1 - lib: PWM: Add set_pwm_channels_duty_cycle_values for Python and C #65

Merged
merged 1 commit into from Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/lib.rs
Expand Up @@ -453,7 +453,7 @@ fn set_pwm_channels_value_py(channels: Vec<PwmChannel>, value: u16) {
}

#[cpy_fn_c]
#[comment = "Sets the duty cycle for a list of multiple channels with multiple values."]
#[comment = "Sets the duty cycle (from 0 to 4096) for a list of multiple channels with multiple values."]
fn set_pwm_channels_values_c(channels: *const PwmChannel, values: *const u16, length: usize) {
let array_channels = unsafe {
assert!(!channels.is_null());
Expand All @@ -468,6 +468,27 @@ fn set_pwm_channels_values_c(channels: *const PwmChannel, values: *const u16, le
}
}

#[cpy_fn_c]
#[comment = "Sets the duty cycle (from 0.0 to 1.0) for a list of multiple channels with multiple values."]
fn set_pwm_channels_duty_cycle_values_c(
channels: *const PwmChannel,
duty_cycle: *const f32,
length: usize,
) {
let array_channels = unsafe {
assert!(!channels.is_null());
std::slice::from_raw_parts(channels, length)
};
let array_values = unsafe {
assert!(!duty_cycle.is_null());
std::slice::from_raw_parts(duty_cycle, length)
};
for i in 0..length {
with_navigator!()
.set_pwm_channel_duty_cycle(array_channels[i].clone().into(), array_values[i]);
}
}

#[cpy_fn_py]
#[comment = "Like :py:func:`set_pwm_channel_value`. This function sets the duty cycle for a list of
multiple channels with multiple values.\n
Expand All @@ -488,6 +509,26 @@ fn set_pwm_channels_values_py(channels: Vec<PwmChannel>, values: Vec<u16>) {
}
}

#[cpy_fn_py]
#[comment = "Like :py:func:`set_pwm_channel_duty_cycle`. This function sets the duty cycle for a list of
multiple channels with multiple values.\n
Args:\n
channels ([:py:class:`PwmChannel`]): A list of PWM channels to configure.\n
duty_cycle_values (f32) : Duty cycle count value (0.0 : 1.0).\n
Examples:\n
You can use this method like :py:func:`set_pwm_channel_duty_cycle`.\n
>>> navigator.set_pwm_channels_duty_cycle_values([PwmChannel.Ch1, PwmChannel.Ch5], [0.25, 0.75])"]
fn set_pwm_channels_duty_cycle_values_py(channels: Vec<PwmChannel>, duty_cycle_values: Vec<f32>) {
if channels.len() != duty_cycle_values.len() {
println!("The number of values is different from the number of PWM channels.");
return;
}

for i in 0..channels.len() {
with_navigator!()
.set_pwm_channel_duty_cycle(channels[i].clone().into(), duty_cycle_values[i]);
}
}
cpy_module!(
name = bluerobotics_navigator,
types = [AdcChannel, UserLed, PwmChannel, AxisData, ADCData],
Expand All @@ -511,6 +552,7 @@ cpy_module!(
set_pwm_freq_hz,
set_pwm_channel_value,
set_pwm_channels_value,
set_pwm_channels_values
set_pwm_channels_values,
set_pwm_channels_duty_cycle_values
]
);