diff --git a/examples/nunchuck.rs b/examples/nunchuck.rs index c779131e..121f0faf 100644 --- a/examples/nunchuck.rs +++ b/examples/nunchuck.rs @@ -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; @@ -59,7 +59,6 @@ impl NunchuckReading { } } -#[derive(Debug)] struct Nunchuck { i2cdev: I2CDevice, } diff --git a/src/core.rs b/src/core.rs index 179d54cb..2b2b521c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -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 for I2CError { + fn from(e: nix::Error) -> I2CError { + I2CError::NixError(e) + } +} + pub struct I2CDevice { devfile: File, slave_address: u16, @@ -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 { - 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 { - 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 { - 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 { - 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, 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) - } - -} diff --git a/src/ffi.rs b/src/ffi.rs index 8bed2f15..a430e00c 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -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 { diff --git a/src/lib.rs b/src/lib.rs index 1a0ceb06..6d32406a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 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; - - /// 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; - - /// 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; - - /// 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; - - /// 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, 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; diff --git a/src/smbus.rs b/src/smbus.rs new file mode 100644 index 00000000..b7ec9712 --- /dev/null +++ b/src/smbus.rs @@ -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; + + /// 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; + + /// 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; + + /// 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; + + /// 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, 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 { + 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 { + 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 { + 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 { + 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, 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) + } +}