1515
1616extern crate cast;
1717extern crate embedded_hal as hal;
18+ pub extern crate i2cdev;
1819pub extern crate spidev;
1920pub extern crate sysfs_gpio;
2021
@@ -24,6 +25,7 @@ use std::time::Duration;
2425use std:: { ops, thread} ;
2526
2627use cast:: { u32, u64} ;
28+ use i2cdev:: core:: I2CDevice ;
2729use 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