extern crate i2cdev;
extern crate docopt;
#[cfg(any(target_os = "linux", target_os = "android"))]
use i2cdev::linux::{LinuxI2CBus, I2CMsg};
#[cfg(any(target_os = "linux", target_os = "android"))]
use i2cdev::core::I2CBus;
use std::env::args;
use docopt::Docopt;
const USAGE: &str = "
Reads registers from a PCA9956B IC via Linux i2cdev.
Assumes the PCA9956B is using address 0x20.
Usage:
pca9956b <device>
pca9956b (-h | --help)
pca9956b --version
Options:
-h --help Show this help text.
--version Show version.
";
const ADDR: u16 = 0x20;
#[cfg(any(target_os = "linux", target_os = "android"))]
fn main() {
let args = Docopt::new(USAGE)
.and_then(|d| d.argv(args()).parse())
.unwrap_or_else(|e| e.exit());
let path = args.get_str("<device>");
let mut bus = match LinuxI2CBus::new(path) {
Ok(bus) => bus,
Err(_e) => {
println!("Error opening I2C Bus {} {}", path, _e);
return
}
};
println!("Opened I2C Bus OK: {}", path);
let mut dataw: Vec<u8> = vec![0b1000_0000];
let mut data: Vec<u8> = vec![0; 10];
let mut msgs: Vec<I2CMsg> = Vec::new();
msgs.push(I2CMsg::new(ADDR, &mut dataw));
msgs.push(I2CMsg::new(ADDR, &mut data));
msgs[1].set_read();
match bus.rdwr(&mut msgs) {
Ok(rc) => {
println!("Successful RDWR call: {} messages processed", rc)
},
Err(_e) => {
println!("Error reading/writing {}", _e);
return
},
}
let mut output = "Result: 0x".to_string();
let data = msgs[1].data();
for byte in data {
output = format!("{}{:02x}", output, byte);
}
println!("{}", output);
}