Skip to content

Commit d08b39c

Browse files
committed
Adapt to new I2C bus/device interface
1 parent 1ac9d6f commit d08b39c

File tree

4 files changed

+192
-145
lines changed

4 files changed

+192
-145
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ spi = ["spidev"]
2222
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
2323

2424
[dependencies]
25-
embedded-hal = "=1.0.0-alpha.8"
25+
embedded-hal = {git = "https://github.com/Dirbaio/embedded-hal", branch="i2c-bus-device"}
26+
embedded-hal-bus = {git= "https://github.com/eldruin/embedded-hal", branch="i2c-exclusive-device"}
2627
gpio-cdev = { version = "0.5.1", optional = true }
2728
sysfs_gpio = { version = "0.6.1", optional = true }
2829
i2cdev = { version = "0.5.1", optional = true }

examples/transactional-i2c.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use embedded_hal::i2c::blocking::{I2c, Operation as I2cOperation};
2-
use linux_embedded_hal::I2cdev;
1+
use embedded_hal::i2c::{
2+
blocking::{I2cBus, I2cBusBase as _, I2cDevice},
3+
Direction,
4+
};
5+
use embedded_hal_bus::i2c::blocking::ExclusiveDevice;
6+
use linux_embedded_hal::I2cBus as LinuxI2cBus;
37

48
const ADDR: u8 = 0x12;
59

@@ -9,24 +13,28 @@ struct Driver<I2C> {
913

1014
impl<I2C> Driver<I2C>
1115
where
12-
I2C: I2c,
16+
I2C: I2cDevice,
17+
I2C::Bus: I2cBus,
1318
{
1419
pub fn new(i2c: I2C) -> Self {
1520
Driver { i2c }
1621
}
1722

1823
fn read_something(&mut self) -> Result<u8, I2C::Error> {
1924
let mut read_buffer = [0];
20-
let mut ops = [
21-
I2cOperation::Write(&[0xAB]),
22-
I2cOperation::Read(&mut read_buffer),
23-
];
24-
self.i2c.transaction(ADDR, &mut ops).and(Ok(read_buffer[0]))
25+
self.i2c.transaction(|bus| {
26+
bus.start(ADDR, Direction::Write)?;
27+
bus.write(&[0xAB])?;
28+
bus.start(ADDR, Direction::Read)?;
29+
bus.read(&mut read_buffer)
30+
})?;
31+
Ok(read_buffer[0])
2532
}
2633
}
2734

2835
fn main() {
29-
let dev = I2cdev::new("/dev/i2c-1").unwrap();
36+
let bus = LinuxI2cBus::new("/dev/i2c-1").unwrap();
37+
let dev = ExclusiveDevice::new(bus);
3038
let mut driver = Driver::new(dev);
3139
let value = driver.read_something().unwrap();
3240
println!("Read value: {}", value);

0 commit comments

Comments
 (0)