use anyhow::{Context, Result};
use i2cdriver::I2CDriver;
fn main() -> Result<()> {
let flags = xflags::parse_or_exit! {
optional -d, --device device: String
};
let mut i2c = I2CDriver::open(flags.device.unwrap_or("/dev/ttyUSB0".to_string()).as_str())
.context("open I2CDriver")?;
println!("open");
let got = i2c.echo(b'a')?;
if got != b'a' {
panic!("echo test expected 'a', got {}", got);
}
i2c.reboot()?;
std::thread::sleep(std::time::Duration::new(1, 0));
i2c.reset()?;
let status = i2c.status()?;
println!("status:\n\t{:?}\n", status);
let found = i2c.scan()?;
println!(
"found devices:\n\t{}\n",
found
.into_iter()
.map(|x| format!("{} (0x{:02x})", x, x))
.collect::<Vec<String>>()
.join(", ")
);
println!("monitoring for 1 second...");
let i2c = i2c.start_monitor()?;
std::thread::sleep(std::time::Duration::new(1, 0));
i2c.stop_monitor()?;
println!("done");
Ok(())
}