Skip to content

Commit

Permalink
fixup: fix tests and move things around a touch
Browse files Browse the repository at this point in the history
  • Loading branch information
posborne committed Sep 10, 2015
1 parent d92252b commit fdaee96
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 158 deletions.
3 changes: 1 addition & 2 deletions examples/nunchuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
extern crate i2cdev;
extern crate docopt;

use i2cdev::*;
use i2cdev::core::*;
use std::io::prelude::*;
use std::env::args;
use docopt::Docopt;
Expand Down Expand Up @@ -59,7 +59,6 @@ impl NunchuckReading {
}
}

#[derive(Debug)]
struct Nunchuck {
i2cdev: I2CDevice,
}
Expand Down
96 changes: 15 additions & 81 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@
// except according to those terms.

use std::os::unix::prelude::*;
use nix;
use std::io;
use std::io::prelude::*;
use std::fs::OpenOptions;
use std::fs::File;
use std::path::Path;
use std::convert;
use nix;
use ffi;

use ::{ffi, I2CSMBus, I2CError};

/// Error that occured while performing and I2C Operation
#[derive(Debug)]
pub enum I2CError {
IOError(io::Error),
NixError(nix::Error),
}

impl convert::From<nix::Error> for I2CError {
fn from(e: nix::Error) -> I2CError {
I2CError::NixError(e)
}
}

pub struct I2CDevice {
devfile: File,
slave_address: u16,
Expand Down Expand Up @@ -86,81 +98,3 @@ impl Write for I2CDevice {
}
}

impl I2CSMBus for I2CDevice {

/// This sends a single bit to the device, at the place of the Rd/Wr bit
fn smbus_write_quick(&self, bit: bool) -> Result<(), I2CError> {
ffi::i2c_smbus_write_quick(self.as_raw_fd(), bit)
}

/// Read a single byte from a device, without specifying a device register
///
/// Some devices are so simple that this interface is enough; for
/// others, it is a shorthand if you want to read the same register as in
/// the previous SMBus command.
fn smbus_read_byte(&self) -> Result<u8, I2CError> {
ffi::i2c_smbus_read_byte(self.as_raw_fd())
}

/// Write a single byte to a sdevice, without specifying a device register
///
/// This is the opposite operation as smbus_read_byte. As with read_byte,
/// no register is specified.
fn smbus_write_byte(&self, value: u8) -> Result<(), I2CError> {
ffi::i2c_smbus_write_byte(self.as_raw_fd(), value)
}

/// Read a single byte from a device, from a designated register
///
/// The register is specified through the Comm byte.
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, I2CError> {
ffi::i2c_smbus_read_byte_data(self.as_raw_fd(), register)
}

/// Write a single byte to a specific register on a device
///
/// The register is specified through the Comm byte.
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), I2CError> {
ffi::i2c_smbus_write_byte_data(self.as_raw_fd(), register, value)
}

/// Read 2 bytes form a given register on a device
fn smbus_read_word_data(&self, register: u8) -> Result<u16, I2CError> {
ffi::i2c_smbus_read_word_data(self.as_raw_fd(), register)
}

/// Write 2 bytes to a given register on a device
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), I2CError> {
ffi::i2c_smbus_write_word_data(self.as_raw_fd(), register, value)
}

/// Select a register, send 16 bits of data to it, and read 16 bits of data
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, I2CError> {
ffi::i2c_smbus_process_call(self.as_raw_fd(), register, value)
}

/// Read a block of up to 32 bytes from a device
///
/// The actual number of bytes available to read is returned in the count
/// byte. This code returns a correctly sized vector containing the
/// count bytes read from the device.
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, I2CError> {
ffi::i2c_smbus_read_block_data(self.as_raw_fd(), register)
}

/// Write a block of up to 32 bytes to a device
///
/// The opposite of the Block Read command, this writes up to 32 bytes to
/// a device, to a designated register that is specified through the
/// Comm byte. The amount of data is specified in the Count byte.
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), I2CError> {
ffi::i2c_smbus_write_block_data(self.as_raw_fd(), register, values)
}

/// Select a register, send 1 to 31 bytes of data to it, and reads
/// 1 to 31 bytes of data from it.
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), I2CError> {
ffi::i2c_smbus_write_i2c_block_data(self.as_raw_fd(), register, values)
}

}
4 changes: 2 additions & 2 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#![allow(dead_code)]
#![allow(non_camel_case_types)]

use core::*;
use nix;
use std::mem;
use std::ptr;
use std::io::Cursor;
use nix;
use std::os::unix::prelude::*;
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use ::I2CError;

bitflags! {
flags I2CMsgFlags: u16 {
Expand Down
74 changes: 1 addition & 73 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,76 +22,4 @@ extern crate byteorder;
#[macro_use] extern crate nix;

mod ffi;
mod core;

pub use core::*;
use std::io;
use std::convert;

/// Error that occured while performing and I2C Operation
#[derive(Debug)]
pub enum I2CError {
IOError(io::Error),
NixError(nix::Error),
}

impl convert::From<nix::Error> for I2CError {
fn from(e: nix::Error) -> I2CError {
I2CError::NixError(e)
}
}

pub trait I2CSMBus {
/// This sends a single bit to the device, at the place of the Rd/Wr bit
fn smbus_write_quick(&self, bit: bool) -> Result<(), I2CError>;

/// Read a single byte from a device, without specifying a device register
///
/// Some devices are so simple that this interface is enough; for
/// others, it is a shorthand if you want to read the same register as in
/// the previous SMBus command.
fn smbus_read_byte(&self) -> Result<u8, I2CError>;

/// Write a single byte to a sdevice, without specifying a device register
///
/// This is the opposite operation as smbus_read_byte. As with read_byte,
/// no register is specified.
fn smbus_write_byte(&self, value: u8) -> Result<(), I2CError>;

/// Read a single byte from a device, from a designated register
///
/// The register is specified through the Comm byte.
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, I2CError>;

/// Write a single byte to a specific register on a device
///
/// The register is specified through the Comm byte.
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), I2CError>;

/// Read 2 bytes form a given register on a device
fn smbus_read_word_data(&self, register: u8) -> Result<u16, I2CError>;

/// Write 2 bytes to a given register on a device
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), I2CError>;

/// Select a register, send 16 bits of data to it, and read 16 bits of data
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, I2CError>;

/// Read a block of up to 32 bytes from a device
///
/// The actual number of bytes available to read is returned in the count
/// byte. This code returns a correctly sized vector containing the
/// count bytes read from the device.
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, I2CError>;

/// Write a block of up to 32 bytes to a device
///
/// The opposite of the Block Read command, this writes up to 32 bytes to
/// a device, to a designated register that is specified through the
/// Comm byte. The amount of data is specified in the Count byte.
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), I2CError>;

/// Select a register, send 1 to 31 bytes of data to it, and reads
/// 1 to 31 bytes of data from it.
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), I2CError>;
}
pub mod core;
135 changes: 135 additions & 0 deletions src/smbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use ffi;
use core::*;

pub trait I2CSMBus {
/// This sends a single bit to the device, at the place of the Rd/Wr bit
fn smbus_write_quick(&self, bit: bool) -> Result<(), I2CError>;

/// Read a single byte from a device, without specifying a device register
///
/// Some devices are so simple that this interface is enough; for
/// others, it is a shorthand if you want to read the same register as in
/// the previous SMBus command.
fn smbus_read_byte(&self) -> Result<u8, I2CError>;

/// Write a single byte to a sdevice, without specifying a device register
///
/// This is the opposite operation as smbus_read_byte. As with read_byte,
/// no register is specified.
fn smbus_write_byte(&self, value: u8) -> Result<(), I2CError>;

/// Read a single byte from a device, from a designated register
///
/// The register is specified through the Comm byte.
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, I2CError>;

/// Write a single byte to a specific register on a device
///
/// The register is specified through the Comm byte.
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), I2CError>;

/// Read 2 bytes form a given register on a device
fn smbus_read_word_data(&self, register: u8) -> Result<u16, I2CError>;

/// Write 2 bytes to a given register on a device
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), I2CError>;

/// Select a register, send 16 bits of data to it, and read 16 bits of data
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, I2CError>;

/// Read a block of up to 32 bytes from a device
///
/// The actual number of bytes available to read is returned in the count
/// byte. This code returns a correctly sized vector containing the
/// count bytes read from the device.
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, I2CError>;

/// Write a block of up to 32 bytes to a device
///
/// The opposite of the Block Read command, this writes up to 32 bytes to
/// a device, to a designated register that is specified through the
/// Comm byte. The amount of data is specified in the Count byte.
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), I2CError>;

/// Select a register, send 1 to 31 bytes of data to it, and reads
/// 1 to 31 bytes of data from it.
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), I2CError>;
}


impl I2CSMBus for I2CDevice {
/// This sends a single bit to the device, at the place of the Rd/Wr bit
fn smbus_write_quick(&self, bit: bool) -> Result<(), I2CError> {
ffi::i2c_smbus_write_quick(self.as_raw_fd(), bit)
}

/// Read a single byte from a device, without specifying a device register
///
/// Some devices are so simple that this interface is enough; for
/// others, it is a shorthand if you want to read the same register as in
/// the previous SMBus command.
fn smbus_read_byte(&self) -> Result<u8, I2CError> {
ffi::i2c_smbus_read_byte(self.as_raw_fd())
}

/// Write a single byte to a sdevice, without specifying a device register
///
/// This is the opposite operation as smbus_read_byte. As with read_byte,
/// no register is specified.
fn smbus_write_byte(&self, value: u8) -> Result<(), I2CError> {
ffi::i2c_smbus_write_byte(self.as_raw_fd(), value)
}

/// Read a single byte from a device, from a designated register
///
/// The register is specified through the Comm byte.
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, I2CError> {
ffi::i2c_smbus_read_byte_data(self.as_raw_fd(), register)
}

/// Write a single byte to a specific register on a device
///
/// The register is specified through the Comm byte.
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), I2CError> {
ffi::i2c_smbus_write_byte_data(self.as_raw_fd(), register, value)
}

/// Read 2 bytes form a given register on a device
fn smbus_read_word_data(&self, register: u8) -> Result<u16, I2CError> {
ffi::i2c_smbus_read_word_data(self.as_raw_fd(), register)
}

/// Write 2 bytes to a given register on a device
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), I2CError> {
ffi::i2c_smbus_write_word_data(self.as_raw_fd(), register, value)
}

/// Select a register, send 16 bits of data to it, and read 16 bits of data
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, I2CError> {
ffi::i2c_smbus_process_call(self.as_raw_fd(), register, value)
}

/// Read a block of up to 32 bytes from a device
///
/// The actual number of bytes available to read is returned in the count
/// byte. This code returns a correctly sized vector containing the
/// count bytes read from the device.
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, I2CError> {
ffi::i2c_smbus_read_block_data(self.as_raw_fd(), register)
}

/// Write a block of up to 32 bytes to a device
///
/// The opposite of the Block Read command, this writes up to 32 bytes to
/// a device, to a designated register that is specified through the
/// Comm byte. The amount of data is specified in the Count byte.
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), I2CError> {
ffi::i2c_smbus_write_block_data(self.as_raw_fd(), register, values)
}

/// Select a register, send 1 to 31 bytes of data to it, and reads
/// 1 to 31 bytes of data from it.
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), I2CError> {
ffi::i2c_smbus_write_i2c_block_data(self.as_raw_fd(), register, values)
}
}

0 comments on commit fdaee96

Please sign in to comment.