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][kernel-doc].
14//!
15//! [kernel-doc]: https://www.kernel.org/doc/Documentation/i2c/dev-interface
16//!
17//! ## Examples
18//!
19//! ### Using the SMBus methods to read from a Wii Nunchuk
20//! ```rust,no_run
21//! extern crate i2cdev;
22//!
23//! use std::thread;
24//! use std::time::Duration;
25//!
26//! use i2cdev::core::*;
27//! use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
28//!
29//! const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
30//!
31//! // real code should probably not use unwrap()
32//! fn i2cfun() -> Result<(), LinuxI2CError> {
33//!     let mut dev = LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR)?;
34//!
35//!     // init sequence
36//!     dev.smbus_write_byte_data(0xF0, 0x55)?;
37//!     dev.smbus_write_byte_data(0xFB, 0x00)?;
38//!     thread::sleep(Duration::from_millis(100));
39//!
40//!     loop {
41//!         let mut buf: [u8; 6] = [0; 6];
42//!         dev.smbus_write_byte(0x00).unwrap();
43//!         thread::sleep(Duration::from_millis(10));
44//!         dev.read(&mut buf).unwrap();
45//!         println!("Reading: {:?}", buf);
46//!     }
47//! }
48//! ```
49//!
50//! ### Using the `transfer` API with an individual device
51//! ```rust,no_run
52//! extern crate i2cdev;
53//!
54//! use i2cdev::core::*;
55//! use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError, LinuxI2CMessage};
56//!
57//! const SLAVE_ADDR: u16 = 0x57;
58//!
59//! fn write_read_transaction() -> Result<(), LinuxI2CError> {
60//!     let mut dev = LinuxI2CDevice::new("/dev/i2c-1", SLAVE_ADDR)?;
61//!
62//!     let mut read_data = [0; 2];
63//!     let mut msgs = [
64//!         LinuxI2CMessage::write(&[0x01]),
65//!         LinuxI2CMessage::read(&mut read_data)
66//!     ];
67//!     dev.transfer(&mut msgs)?;
68//!
69//!     println!("Reading: {:?}", read_data);
70//!     Ok(())
71//! }
72//! ```
73//!
74//! ### Using `transfer` with `LinuxI2CBus`
75//! ```rust,no_run
76//! extern crate i2cdev;
77//!
78//! use i2cdev::core::*;
79//! use i2cdev::linux::{LinuxI2CBus, LinuxI2CError, LinuxI2CMessage};
80//!
81//! const SLAVE_ADDR: u16 = 0x57;
82//!
83//! fn write_read_transaction_using_bus() -> Result<(), LinuxI2CError> {
84//!     let mut dev = LinuxI2CBus::new("/dev/i2c-1")?;
85//!
86//!     let mut read_data = [0; 2];
87//!     let mut msgs = [
88//!         LinuxI2CMessage::write(&[0x01]).with_address(SLAVE_ADDR),
89//!         LinuxI2CMessage::read(&mut read_data).with_address(SLAVE_ADDR)
90//!     ];
91//!     dev.transfer(&mut msgs)?;
92//!
93//!     println!("Reading: {:?}", read_data);
94//!     Ok(())
95//! }
96//! ```
97
98#![crate_name = "i2cdev"]
99#![crate_type = "lib"]
100#![deny(missing_docs)]
101
102#[macro_use]
103extern crate bitflags;
104extern crate byteorder;
105extern crate libc;
106#[macro_use]
107extern crate nix;
108
109#[cfg(any(target_os = "linux", target_os = "android"))]
110mod ffi;
111
112/// Core I2C abstractions
113pub mod core;
114
115/// Linux I2C device support
116#[cfg(any(target_os = "linux", target_os = "android"))]
117pub mod linux;
118
119/// Mock I2C device
120pub mod mock;