22//!
33//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
5+ use std:: fmt;
56use std:: ops;
67use std:: path:: { Path , PathBuf } ;
78
@@ -13,7 +14,7 @@ use embedded_hal::i2c::NoAcknowledgeSource;
1314pub struct I2cdev {
1415 inner : i2cdev:: linux:: LinuxI2CDevice ,
1516 path : PathBuf ,
16- address : Option < u8 > ,
17+ address : Option < u16 > ,
1718}
1819
1920impl I2cdev {
@@ -32,9 +33,9 @@ impl I2cdev {
3233 Ok ( dev)
3334 }
3435
35- fn set_address ( & mut self , address : u8 ) -> Result < ( ) , i2cdev:: linux:: LinuxI2CError > {
36+ fn set_address ( & mut self , address : u16 ) -> Result < ( ) , i2cdev:: linux:: LinuxI2CError > {
3637 if self . address != Some ( address) {
37- self . inner = i2cdev:: linux:: LinuxI2CDevice :: new ( & self . path , u16 :: from ( address) ) ?;
38+ self . inner = i2cdev:: linux:: LinuxI2CDevice :: new ( & self . path , address) ?;
3839 self . address = Some ( address) ;
3940 }
4041 Ok ( ( ) )
@@ -58,65 +59,17 @@ impl ops::DerefMut for I2cdev {
5859mod embedded_hal_impl {
5960 use super :: * ;
6061 use embedded_hal:: i2c:: ErrorType ;
61- use embedded_hal:: i2c:: { I2c , Operation as I2cOperation } ;
62- use i2cdev:: core:: { I2CDevice , I2CMessage , I2CTransfer } ;
62+ use embedded_hal:: i2c:: { I2c , Operation as I2cOperation , SevenBitAddress , TenBitAddress } ;
63+ use i2cdev:: core:: { I2CMessage , I2CTransfer } ;
6364 use i2cdev:: linux:: LinuxI2CMessage ;
6465 impl ErrorType for I2cdev {
6566 type Error = I2CError ;
6667 }
6768
68- impl I2c for I2cdev {
69- fn read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
70- self . set_address ( address) ?;
71- self . inner . read ( buffer) . map_err ( |err| I2CError { err } )
72- }
73-
74- fn write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
75- self . set_address ( address) ?;
76- self . inner . write ( bytes) . map_err ( |err| I2CError { err } )
77- }
78-
79- fn write_iter < B > ( & mut self , address : u8 , bytes : B ) -> Result < ( ) , Self :: Error >
80- where
81- B : IntoIterator < Item = u8 > ,
82- {
83- let bytes: Vec < _ > = bytes. into_iter ( ) . collect ( ) ;
84- self . write ( address, & bytes)
85- }
86-
87- fn write_read (
88- & mut self ,
89- address : u8 ,
90- bytes : & [ u8 ] ,
91- buffer : & mut [ u8 ] ,
92- ) -> Result < ( ) , Self :: Error > {
93- self . set_address ( address) ?;
94- let mut messages = [ LinuxI2CMessage :: write ( bytes) , LinuxI2CMessage :: read ( buffer) ] ;
95- self . inner
96- . transfer ( & mut messages)
97- . map ( drop)
98- . map_err ( |err| I2CError { err } )
99- }
100-
101- fn write_iter_read < B > (
102- & mut self ,
103- address : u8 ,
104- bytes : B ,
105- buffer : & mut [ u8 ] ,
106- ) -> Result < ( ) , Self :: Error >
107- where
108- B : IntoIterator < Item = u8 > ,
109- {
110- let bytes: Vec < _ > = bytes. into_iter ( ) . collect ( ) ;
111- self . transaction (
112- address,
113- & mut [ I2cOperation :: Write ( & bytes) , I2cOperation :: Read ( buffer) ] ,
114- )
115- }
116-
69+ impl I2c < TenBitAddress > for I2cdev {
11770 fn transaction (
11871 & mut self ,
119- address : u8 ,
72+ address : u16 ,
12073 operations : & mut [ I2cOperation ] ,
12174 ) -> Result < ( ) , Self :: Error > {
12275 // Map operations from generic to linux objects
@@ -135,13 +88,15 @@ mod embedded_hal_impl {
13588 . map ( drop)
13689 . map_err ( |err| I2CError { err } )
13790 }
91+ }
13892
139- fn transaction_iter < ' a , O > ( & mut self , address : u8 , operations : O ) -> Result < ( ) , Self :: Error >
140- where
141- O : IntoIterator < Item = I2cOperation < ' a > > ,
142- {
143- let mut ops: Vec < _ > = operations. into_iter ( ) . collect ( ) ;
144- self . transaction ( address, & mut ops)
93+ impl I2c < SevenBitAddress > for I2cdev {
94+ fn transaction (
95+ & mut self ,
96+ address : u8 ,
97+ operations : & mut [ I2cOperation ] ,
98+ ) -> Result < ( ) , Self :: Error > {
99+ I2c :: < TenBitAddress > :: transaction ( self , u16:: from ( address) , operations)
145100 }
146101 }
147102}
@@ -165,6 +120,18 @@ impl From<i2cdev::linux::LinuxI2CError> for I2CError {
165120 }
166121}
167122
123+ impl fmt:: Display for I2CError {
124+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
125+ write ! ( f, "{}" , self . err)
126+ }
127+ }
128+
129+ impl std:: error:: Error for I2CError {
130+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
131+ Some ( & self . err )
132+ }
133+ }
134+
168135impl embedded_hal:: i2c:: Error for I2CError {
169136 fn kind ( & self ) -> embedded_hal:: i2c:: ErrorKind {
170137 use embedded_hal:: i2c:: ErrorKind ;
0 commit comments