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

43 lines
1 KiB
Rust
Raw Normal View History

use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CMessage};
pub fn read_from_bus(
i2c_device: &str,
slave_address: u16,
data_length: usize,
) -> Result<Vec<u8>, std::io::Error> {
let mut data = vec![0; data_length];
let mut dev = LinuxI2CDevice::new(i2c_device, slave_address)?;
let mut msgs = [
// Address to read from
LinuxI2CMessage::write(&[0x00]),
// Read data into buffer
LinuxI2CMessage::read(&mut data),
];
match dev.transfer(&mut msgs) {
Ok(_) => Ok(data),
Err(e) => Err(e.into()),
}
}
pub fn write_to_bus(
i2c_device: &str,
slave_address: u16,
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()),
}
}