1
0
Fork 0
mirror of https://codeberg.org/ral/rwedid.git synced 2024-08-16 09:59:49 +02:00

Write to bus in chunks of 8 bytes a time

This commit is contained in:
Ral 2023-06-04 03:04:16 +02:00
parent 522c63762e
commit 68c7193ffd

View file

@ -1,6 +1,9 @@
use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CMessage};
use std::thread;
use std::time::Duration;
pub fn read_from_bus(
i2c_device: &str,
slave_address: u16,
@ -28,15 +31,23 @@ pub fn write_to_bus(
data: &[u8],
) -> Result<(), std::io::Error> {
let mut dev = LinuxI2CDevice::new(i2c_device, slave_address)?;
let mut msgs = [
// Address to write to
LinuxI2CMessage::write(&[0x00]),
// Write data to chip
LinuxI2CMessage::write(data),
];
match dev.transfer(&mut msgs) {
Ok(_) => Ok(()),
Err(e) => Err(e.into()),
}
// Write 8 bytes at a time
for (i, chunk) in data.chunks_exact(8).into_iter().enumerate() {
// Memory address
let memory_address : u8 = (i as u8) * 8;
let mut msgs = [
// Address to write to
LinuxI2CMessage::write(&[memory_address]),
// Write data to chip
LinuxI2CMessage::write(&chunk),
];
dev.transfer(&mut msgs)?;
thread::sleep(Duration::from_millis(20));
};
Ok(())
}