Crate i2cdev[][src]

Expand description

i2cdev

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: https://www.kernel.org/doc/Documentation/i2c/dev-interface

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)?;
    thread::sleep(Duration::from_millis(100));

    loop {
        let mut buf: [u8; 6] = [0; 6];
        dev.smbus_write_byte(0x00).unwrap();
        thread::sleep(Duration::from_millis(10));
        dev.read(&mut buf).unwrap();
        println!("Reading: {:?}", buf);
    }
}
extern crate i2cdev;

use std::thread;
use std::time::Duration;

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

const SLAVE_ADDR: u16 = 0x57;

fn write_read_transaction() -> Result<(), LinuxI2CError> {
    let mut dev = LinuxI2CDevice::new("/dev/i2c-1", SLAVE_ADDR)?;

    let mut read_data = [0; 2];
    let mut msgs = [
        LinuxI2CMessage::write(&[0x01]),
        LinuxI2CMessage::read(&mut read_data)
    ];
    dev.transfer(&mut msgs)?;

    println!("Reading: {:?}", read_data);
    Ok(())
}
extern crate i2cdev;

use std::thread;
use std::time::Duration;

use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CBus, LinuxI2CError, LinuxI2CMessage};

const SLAVE_ADDR: u16 = 0x57;

fn write_read_transaction_using_bus() -> Result<(), LinuxI2CError> {
    let mut dev = LinuxI2CBus::new("/dev/i2c-1")?;

    let mut read_data = [0; 2];
    let mut msgs = [
        LinuxI2CMessage::write(&[0x01]).with_address(SLAVE_ADDR),
        LinuxI2CMessage::read(&mut read_data).with_address(SLAVE_ADDR)
    ];
    dev.transfer(&mut msgs)?;

    println!("Reading: {:?}", read_data);
    Ok(())
}

Modules

Core I2C abstractions

Linux I2C device support

Mock I2C device