i2cdev/lib.rs
1// Copyright 2015, Paul Osborne <osbpau@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! # i2cdev
10//!
11//! The `i2cdev` crate provides a safe interface for interface
12//! with i2c devices under Linux. The API wraps the Linux
13//! kernel interface for interacting with i2c in userspace:
14//! https://www.kernel.org/doc/Documentation/i2c/dev-interface
15//! ```rust,no_run
16//! extern crate i2cdev;
17//!
18//! use std::thread;
19//! use std::time::Duration;
20//!
21//! use i2cdev::core::*;
22//! use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
23//!
24//! const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
25//!
26//! // real code should probably not use unwrap()
27//! fn i2cfun() -> Result<(), LinuxI2CError> {
28//! let mut dev = LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR)?;
29//!
30//! // init sequence
31//! dev.smbus_write_byte_data(0xF0, 0x55)?;
32//! dev.smbus_write_byte_data(0xFB, 0x00)?;
33//! thread::sleep(Duration::from_millis(100));
34//!
35//! loop {
36//! let mut buf: [u8; 6] = [0; 6];
37//! dev.smbus_write_byte(0x00).unwrap();
38//! thread::sleep(Duration::from_millis(10));
39//! dev.read(&mut buf).unwrap();
40//! println!("Reading: {:?}", buf);
41//! }
42//! }
43//! ```
44//!
45//! ```rust,no_run
46//! extern crate i2cdev;
47//!
48//! use std::thread;
49//! use std::time::Duration;
50//!
51//! use i2cdev::core::*;
52//! use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError, LinuxI2CMessage};
53//!
54//! const SLAVE_ADDR: u16 = 0x57;
55//!
56//! fn write_read_transaction() -> Result<(), LinuxI2CError> {
57//! let mut dev = LinuxI2CDevice::new("/dev/i2c-1", SLAVE_ADDR)?;
58//!
59//! let mut read_data = [0; 2];
60//! let mut msgs = [
61//! LinuxI2CMessage::write(&[0x01]),
62//! LinuxI2CMessage::read(&mut read_data)
63//! ];
64//! dev.transfer(&mut msgs)?;
65//!
66//! println!("Reading: {:?}", read_data);
67//! Ok(())
68//! }
69//! ```
70//!
71//! ```rust,no_run
72//! extern crate i2cdev;
73//!
74//! use std::thread;
75//! use std::time::Duration;
76//!
77//! use i2cdev::core::*;
78//! use i2cdev::linux::{LinuxI2CBus, LinuxI2CError, LinuxI2CMessage};
79//!
80//! const SLAVE_ADDR: u16 = 0x57;
81//!
82//! fn write_read_transaction_using_bus() -> Result<(), LinuxI2CError> {
83//! let mut dev = LinuxI2CBus::new("/dev/i2c-1")?;
84//!
85//! let mut read_data = [0; 2];
86//! let mut msgs = [
87//! LinuxI2CMessage::write(&[0x01]).with_address(SLAVE_ADDR),
88//! LinuxI2CMessage::read(&mut read_data).with_address(SLAVE_ADDR)
89//! ];
90//! dev.transfer(&mut msgs)?;
91//!
92//! println!("Reading: {:?}", read_data);
93//! Ok(())
94//! }
95//! ```
96
97#![crate_name = "i2cdev"]
98#![crate_type = "lib"]
99#![deny(missing_docs)]
100
101#[macro_use]
102extern crate bitflags;
103extern crate byteorder;
104extern crate libc;
105#[macro_use]
106extern crate nix;
107
108#[cfg(any(target_os = "linux", target_os = "android"))]
109mod ffi;
110
111/// Core I2C abstractions
112pub mod core;
113
114/// Linux I2C device support
115#[cfg(any(target_os = "linux", target_os = "android"))]
116pub mod linux;
117
118/// Mock I2C device
119pub mod mock;