Crate i2cdev

source ·
Expand description


The i2cdev crate provides a safe interface for interface with i2c devices under Linux. The API wraps the Linux kernel interface for interacting with i2c in userspace:

extern crate i2cdev;
use std::thread;
use std::time::Duration;

use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};

const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
// real code should probably not use unwrap()
fn i2cfun() -> Result<(), LinuxI2CError> {
    let mut dev = LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR)?;

    // init sequence
    dev.smbus_write_byte_data(0xF0, 0x55)?;
    dev.smbus_write_byte_data(0xFB, 0x00)?;

    loop {
        let mut buf: [u8; 6] = [0; 6];
        thread::sleep(Duration::from_millis(10)); buf).unwrap();
        println!("Reading: {:?}", buf);