Skip to content

Commit e7d12cf

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

File tree

5 files changed

+229
-169
lines changed

5 files changed

+229
-169
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/example.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;
7+
8+
const ADDR: u8 = 0x12;
9+
10+
struct Driver<I2C> {
11+
i2c: I2C,
12+
}
13+
14+
impl<I2C> Driver<I2C>
15+
where
16+
I2C: I2cDevice,
17+
I2C::Bus: I2cBus,
18+
{
19+
pub fn new(i2c: I2C) -> Self {
20+
Driver { i2c }
21+
}
22+
23+
fn read_something(&mut self) -> Result<u8, I2C::Error> {
24+
let mut 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])
32+
}
33+
}
34+
35+
fn main() {
36+
let bus = LinuxI2cBus::new("/dev/i2c-1").unwrap();
37+
let dev = ExclusiveDevice::new(bus);
38+
let mut driver = Driver::new(dev);
39+
let value = driver.read_something().unwrap();
40+
println!("Read value: {}", value);
41+
}

examples/transactional-i2c.rs

-33
This file was deleted.

0 commit comments

Comments
 (0)