Skip to content

Improve docs #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::error::Error;
/// in use and the address of the slave device. The trait is based on the
/// Linux i2cdev interface.
pub trait I2CDevice {
/// Error type
type Error: Error;

/// Read data from the device to fill the provided slice
Expand Down Expand Up @@ -128,12 +129,14 @@ pub trait I2CDevice {
/// Typical implementations will store state with references to the bus
/// in use. The trait is based on the Linux i2cdev interface.
pub trait I2CTransfer<'a> {
/// I2C transfer error type
type Error: Error;
/// I2C transfer message type
type Message: I2CMessage<'a>;

// Performs multiple serially chained I2C read/write transactions. On
// success the return code is the number of successfully executed
// transactions
/// Performs multiple serially chained I2C read/write transactions. On
/// success the return code is the number of successfully executed
/// transactions
fn transfer(&mut self, msgs: &'a mut [Self::Message]) -> Result<u32, Self::Error>;
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

#![crate_name = "i2cdev"]
#![crate_type = "lib"]
#![deny(missing_docs)]

#[macro_use]
extern crate bitflags;
Expand All @@ -107,8 +108,12 @@ extern crate nix;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod ffi;

/// Core I2C abstractions
pub mod core;

/// Linux I2C device support
#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod linux;

/// Mock I2C device
pub mod mock;
7 changes: 7 additions & 0 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ use std::path::Path;
// Expose these core structs from this module
pub use core::I2CMessage;

/// Concrete linux I2C device
pub struct LinuxI2CDevice {
devfile: File,
slave_address: u16,
}

/// Linux I2C bus
pub struct LinuxI2CBus {
devfile: File,
}

/// Linux I2C errors
#[derive(Debug)]
pub enum LinuxI2CError {
/// OS error
Nix(nix::Error),
/// Input/output error
Io(io::Error),
}

Expand Down Expand Up @@ -322,6 +327,7 @@ impl<'a> I2CMessage<'a> for LinuxI2CMessage<'a> {
}

impl<'a> LinuxI2CMessage<'a> {
/// Set the target device address for the message
pub fn with_address(self, slave_address: u16) -> Self {
Self {
addr: slave_address,
Expand All @@ -331,6 +337,7 @@ impl<'a> LinuxI2CMessage<'a> {
}
}

/// Set optional message flags
pub fn with_flags(self, flags: I2CMessageFlags) -> Self {
Self {
addr: self.addr,
Expand Down
8 changes: 8 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use core::{I2CDevice, I2CMessage, I2CTransfer};
use std::io;

/// I2C mock result type
pub type I2CResult<T> = io::Result<T>;

/// Mock I2C device register map
pub struct I2CRegisterMap {
registers: [u8; 0xFF],
offset: usize,
Expand All @@ -22,13 +24,15 @@ impl Default for I2CRegisterMap {
}

impl I2CRegisterMap {
/// Create new mock I2C register map
pub fn new() -> I2CRegisterMap {
I2CRegisterMap {
registers: [0x00; 0xFF],
offset: 0,
}
}

/// Set several registers starting at the given offset
pub fn write_regs(&mut self, offset: usize, data: &[u8]) {
println!("WRITE | 0x{:X} : {:?}", offset, data);
self.registers[offset..(data.len() + offset)].clone_from_slice(&data);
Expand Down Expand Up @@ -56,12 +60,15 @@ impl I2CRegisterMap {
}
}

/// Mock I2C device exposing a register map
#[derive(Default)]
pub struct MockI2CDevice {
/// I2C register map
pub regmap: I2CRegisterMap,
}

impl MockI2CDevice {
/// Create a new mock I2C device
pub fn new() -> MockI2CDevice {
MockI2CDevice {
regmap: I2CRegisterMap::new(),
Expand Down Expand Up @@ -111,6 +118,7 @@ enum MessageType<'a> {
Read(&'a mut [u8]),
}

/// Mock I2C message
pub struct MockI2CMessage<'a> {
msg_type: MessageType<'a>,
}
Expand Down