@@ -20,7 +20,7 @@ pub extern crate spidev;
20
20
pub extern crate sysfs_gpio;
21
21
22
22
use std:: io:: { self , Write } ;
23
- use std:: path:: Path ;
23
+ use std:: path:: { Path , PathBuf } ;
24
24
use std:: time:: Duration ;
25
25
use std:: { ops, thread} ;
26
26
@@ -144,33 +144,52 @@ impl ops::DerefMut for Pin {
144
144
/// Newtype around [`i2cdev::linux::LinuxI2CDevice`] that implements the `embedded-hal` traits
145
145
///
146
146
/// [`i2cdev::linux::LinuxI2CDevice`]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html
147
- pub struct I2cdev ( pub i2cdev:: linux:: LinuxI2CDevice ) ;
147
+ pub struct I2cdev {
148
+ inner : i2cdev:: linux:: LinuxI2CDevice ,
149
+ path : PathBuf ,
150
+ address : Option < u8 > ,
151
+ }
148
152
149
153
impl I2cdev {
150
154
/// See [`i2cdev::linux::LinuxI2CDevice::new`][0] for details.
151
155
///
152
156
/// [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 >
157
+ pub fn new < P > ( path : P ) -> Result < Self , i2cdev:: linux:: LinuxI2CError >
154
158
where
155
159
P : AsRef < Path > ,
156
160
{
157
- i2cdev:: linux:: LinuxI2CDevice :: new ( path, address) . map ( I2cdev )
161
+ let dev = I2cdev {
162
+ path : path. as_ref ( ) . to_path_buf ( ) ,
163
+ inner : i2cdev:: linux:: LinuxI2CDevice :: new ( path, 0 ) ?,
164
+ address : None ,
165
+ } ;
166
+ Ok ( dev)
167
+ }
168
+
169
+ fn set_address ( & mut self , address : u8 ) -> Result < ( ) , i2cdev:: linux:: LinuxI2CError > {
170
+ if self . address != Some ( address) {
171
+ self . inner = i2cdev:: linux:: LinuxI2CDevice :: new ( & self . path , address as u16 ) ?;
172
+ self . address = Some ( address) ;
173
+ }
174
+ Ok ( ( ) )
158
175
}
159
176
}
160
177
161
178
impl hal:: blocking:: i2c:: Read for I2cdev {
162
179
type Error = i2cdev:: linux:: LinuxI2CError ;
163
180
164
- fn read ( & mut self , _address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
165
- self . 0 . read ( buffer)
181
+ fn read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
182
+ self . set_address ( address) ?;
183
+ self . inner . read ( buffer)
166
184
}
167
185
}
168
186
169
187
impl hal:: blocking:: i2c:: Write for I2cdev {
170
188
type Error = i2cdev:: linux:: LinuxI2CError ;
171
189
172
- fn write ( & mut self , _address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
173
- self . 0 . write ( bytes)
190
+ fn write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
191
+ self . set_address ( address) ?;
192
+ self . inner . write ( bytes)
174
193
}
175
194
}
176
195
@@ -179,26 +198,27 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
179
198
180
199
fn write_read (
181
200
& mut self ,
182
- _address : u8 ,
201
+ address : u8 ,
183
202
bytes : & [ u8 ] ,
184
203
buffer : & mut [ u8 ] ,
185
204
) -> Result < ( ) , Self :: Error > {
186
- self . 0 . write ( bytes) ?;
187
- self . 0 . read ( buffer)
205
+ self . set_address ( address) ?;
206
+ self . inner . write ( bytes) ?;
207
+ self . inner . read ( buffer)
188
208
}
189
209
}
190
210
191
211
impl ops:: Deref for I2cdev {
192
212
type Target = i2cdev:: linux:: LinuxI2CDevice ;
193
213
194
214
fn deref ( & self ) -> & Self :: Target {
195
- & self . 0
215
+ & self . inner
196
216
}
197
217
}
198
218
199
219
impl ops:: DerefMut for I2cdev {
200
220
fn deref_mut ( & mut self ) -> & mut Self :: Target {
201
- & mut self . 0
221
+ & mut self . inner
202
222
}
203
223
}
204
224
0 commit comments