Skip to content

Commit

Permalink
20240718
Browse files Browse the repository at this point in the history
  • Loading branch information
long568 committed Jul 18, 2024
1 parent c4307c3 commit 3b47052
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,8 @@ macro_rules! set_nchs_on_off {
on: &[u16; $num],
off: &[u16; $num],
) -> Result<(), Error<E>> {
let mut data = [0; 1 + $num * 4];
data[0] = get_register_on(start);
for (i, (on, off)) in on.iter().zip(off).enumerate() {
if *on > 4095 || *off > 4095 {
return Err(Error::InvalidInputData);
}
data[i * 4 + 1] = *on as u8;
data[i * 4 + 2] = (*on >> 8) as u8;
data[i * 4 + 3] = *off as u8;
data[i * 4 + 4] = (*off >> 8) as u8;
}
self.enable_auto_increment().await?;
self.i2c
.write(self.address, &data)
.await
.map_err(Error::I2C)
const M: usize = 4 * $num + 1;
self.set_chs_on_off::<$num, M>(start, on, off)
}
}
};
Expand Down Expand Up @@ -229,8 +215,32 @@ where
}

set_nchs_on_off!(2);

set_nchs_on_off!(4);
set_nchs_on_off!(8);

async fn set_chs_on_off<const N: usize, const M: usize>(
&mut self,
start: Channel,
on: &[u16; N],
off: &[u16; N],
) -> Result<(), Error<E>> {
let mut data = [0; M];
data[0] = get_register_on(start);
for (i, (on, off)) in on.iter().zip(off).enumerate() {
if *on > 4095 || *off > 4095 {
return Err(Error::InvalidInputData);
}
data[i * 4 + 1] = *on as u8;
data[i * 4 + 2] = (*on >> 8) as u8;
data[i * 4 + 3] = *off as u8;
data[i * 4 + 4] = (*off >> 8) as u8;
}
self.enable_auto_increment().await?;
self.i2c
.write(self.address, &data)
.await
.map_err(Error::I2C)
}
}

macro_rules! get_register {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! - Set the _on_ and _off_ counters for each channel at once. See: [`set_all_on_off()`](Pca9685::set_all_on_off).
//! - Set the _on_ and _off_ counters for a subset of channels at once. See for example: [`set_first_4_channels_on_off()`](Pca9685::set_first_4_channels_on_off).
//! - Set the _on_ and _off_ counters **and** the always-on/always-off flags for each channel at once. See: [`set_all_channels()`](Pca9685::set_all_channels).
//! - Set the _on_ and _off_ counters for a subset of channels at once from an arbitrary starting position.[`set_4chs_on_off()`](Pca9685::set_4chs_on_off).
//! - Set the prescale value. See: [`set_prescale()`](Pca9685::set_prescale).
//! - Select the output logic state direct or inverted. See: [`set_output_logic_state()`](Pca9685::set_output_logic_state).
//! - Set when the outputs change. See: [`set_output_change_behavior()`](Pca9685::set_output_change_behavior).
Expand Down

0 comments on commit 3b47052

Please sign in to comment.