diff --git a/CHANGELOG.md b/CHANGELOG.md index 10d342e..0a35c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Set default features to build both sysfs and cdev pin types - Removed `Pin` export, use `CdevPin` or `SysfsPin` +- Adapted to `embedded-hal` `1.0.0-alpha.3` release. +- Updated `nb` to version `1`. ## [v0.3.0] - 2019-11-25 @@ -41,7 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added -- implementation of the unproven `embedded_hal::::digital::InputPin` trait. +- implementation of the unproven `embedded_hal::::digital::InputPin` trait. ## [v0.2.0] - 2018-05-14 diff --git a/Cargo.toml b/Cargo.toml index a79cfec..9849fb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,12 +19,12 @@ gpio_cdev = ["gpio-cdev"] default = [ "gpio_cdev", "gpio_sysfs" ] [dependencies] -embedded-hal = "=1.0.0-alpha.3" +embedded-hal = "=1.0.0-alpha.4" gpio-cdev = { version = "0.3", optional = true } sysfs_gpio = { version = "0.5", optional = true } i2cdev = "0.4.3" -nb = "0.1.1" +nb = "1" serial-core = "0.4.0" serial-unix = "0.4.0" spidev = "0.4" diff --git a/examples/transactional-i2c.rs b/examples/transactional-i2c.rs new file mode 100644 index 0000000..451ae43 --- /dev/null +++ b/examples/transactional-i2c.rs @@ -0,0 +1,35 @@ +extern crate embedded_hal; +extern crate linux_embedded_hal; +use embedded_hal::blocking::i2c::{Operation as I2cOperation, Transactional}; +use linux_embedded_hal::I2cdev; + +const ADDR: u8 = 0x12; + +struct Driver { + i2c: I2C, +} + +impl Driver +where + I2C: Transactional, +{ + pub fn new(i2c: I2C) -> Self { + Driver { i2c } + } + + fn read_something(&mut self) -> Result { + let mut read_buffer = [0]; + let mut ops = [ + I2cOperation::Write(&[0xAB]), + I2cOperation::Read(&mut read_buffer), + ]; + self.i2c.try_exec(ADDR, &mut ops).and(Ok(read_buffer[0])) + } +} + +fn main() { + let dev = I2cdev::new("/dev/i2c-1").unwrap(); + let mut driver = Driver::new(dev); + let value = driver.read_something().unwrap(); + println!("Read value: {}", value); +} diff --git a/src/lib.rs b/src/lib.rs index bc24a93..be8131f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ use std::time::Duration; use std::{ops, thread}; use cast::{u32, u64}; +use hal::blocking::i2c::Operation as I2cOperation; use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer}; use i2cdev::linux::LinuxI2CMessage; use spidev::SpidevTransfer; @@ -209,6 +210,26 @@ impl embedded_hal::blocking::i2c::WriteRead for I2cdev { } } +impl hal::blocking::i2c::Transactional for I2cdev { + type Error = i2cdev::linux::LinuxI2CError; + + fn try_exec(&mut self, address: u8, operations: &mut [I2cOperation]) -> Result<(), Self::Error> + { + // Map operations from generic to linux objects + let mut messages: Vec<_> = operations + .as_mut() + .iter_mut() + .map(|a| match a { + I2cOperation::Write(w) => LinuxI2CMessage::write(w), + I2cOperation::Read(r) => LinuxI2CMessage::read(r), + }) + .collect(); + + self.set_address(address)?; + self.inner.transfer(&mut messages).map(drop) + } +} + impl ops::Deref for I2cdev { type Target = i2cdev::linux::LinuxI2CDevice;