Skip to content

Commit 272b9d2

Browse files
committed
[WIP] implements embedded-hal i2c traits using i2cdev
1 parent 17ec9f5 commit 272b9d2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ version = "0.1.0"
1010

1111
[dependencies]
1212
embedded-hal = "0.1.0"
13+
i2cdev = "0.3.1"
1314
spidev = "0.3.0"
1415
sysfs_gpio = "0.5.1"
1516

src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
extern crate cast;
1717
extern crate embedded_hal as hal;
18+
pub extern crate i2cdev;
1819
pub extern crate spidev;
1920
pub extern crate sysfs_gpio;
2021

@@ -24,6 +25,7 @@ use std::time::Duration;
2425
use std::{ops, thread};
2526

2627
use cast::{u32, u64};
28+
use i2cdev::core::I2CDevice;
2729
use spidev::SpidevTransfer;
2830

2931
/// Empty struct that provides delay functionality on top of `thread::sleep`
@@ -139,6 +141,67 @@ impl ops::DerefMut for Pin {
139141
}
140142
}
141143

144+
/// Newtype around [`i2cdev::linux::LinuxI2CDevice`] that implements the `embedded-hal` traits
145+
///
146+
/// [`i2cdev::linux::LinuxI2CDevice`]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html
147+
pub struct I2cdev(pub i2cdev::linux::LinuxI2CDevice);
148+
149+
impl I2cdev {
150+
/// See [`i2cdev::linux::LinuxI2CDevice::new`][0] for details.
151+
///
152+
/// [0]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html#method.new
153+
pub fn new<P>(path: P, address: u16) -> Result<Self, i2cdev::linux::LinuxI2CError>
154+
where
155+
P: AsRef<Path>,
156+
{
157+
i2cdev::linux::LinuxI2CDevice::new(path, address).map(I2cdev)
158+
}
159+
}
160+
161+
impl hal::blocking::i2c::Read for I2cdev {
162+
type Error = i2cdev::linux::LinuxI2CError;
163+
164+
fn read(&mut self, _address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
165+
self.0.read(buffer)
166+
}
167+
}
168+
169+
impl hal::blocking::i2c::Write for I2cdev {
170+
type Error = i2cdev::linux::LinuxI2CError;
171+
172+
fn write(&mut self, _address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
173+
self.0.write(bytes)
174+
}
175+
}
176+
177+
impl hal::blocking::i2c::WriteRead for I2cdev {
178+
type Error = i2cdev::linux::LinuxI2CError;
179+
180+
fn write_read(
181+
&mut self,
182+
_address: u8,
183+
bytes: &[u8],
184+
buffer: &mut [u8],
185+
) -> Result<(), Self::Error> {
186+
self.0.write(bytes)?;
187+
self.0.read(buffer)
188+
}
189+
}
190+
191+
impl ops::Deref for I2cdev {
192+
type Target = i2cdev::linux::LinuxI2CDevice;
193+
194+
fn deref(&self) -> &Self::Target {
195+
&self.0
196+
}
197+
}
198+
199+
impl ops::DerefMut for I2cdev {
200+
fn deref_mut(&mut self) -> &mut Self::Target {
201+
&mut self.0
202+
}
203+
}
204+
142205
/// Newtype around [`spidev::Spidev`] that implements the `embedded-hal` traits
143206
///
144207
/// [`spidev::Spidev`]: https://docs.rs/spidev/0.3.0/spidev/struct.Spidev.html

0 commit comments

Comments
 (0)