1616//! Since 7-bit addressing is the mode of the majority of I2C devices,
1717//! `SevenBitAddress` has been set as default mode and thus can be omitted if desired.
1818
19- use core:: future:: Future ;
2019pub use embedded_hal:: i2c:: Operation ;
2120pub use embedded_hal:: i2c:: {
2221 AddressMode , Error , ErrorKind , ErrorType , NoAcknowledgeSource , SevenBitAddress , TenBitAddress ,
2322} ;
2423
2524/// Async i2c
2625pub trait I2c < A : AddressMode = SevenBitAddress > : ErrorType {
27- /// Future returned by the `read` method.
28- type ReadFuture < ' a > : Future < Output = Result < ( ) , Self :: Error > >
29- where
30- Self : ' a ;
31-
3226 /// Reads enough bytes from slave with `address` to fill `buffer`
3327 ///
3428 /// # I2C Events (contract)
@@ -47,12 +41,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
4741 /// - `MAK` = master acknowledge
4842 /// - `NMAK` = master no acknowledge
4943 /// - `SP` = stop condition
50- fn read < ' a > ( & ' a mut self , address : A , read : & ' a mut [ u8 ] ) -> Self :: ReadFuture < ' a > ;
51-
52- /// Future returned by the `write` method.
53- type WriteFuture < ' a > : Future < Output = Result < ( ) , Self :: Error > >
54- where
55- Self : ' a ;
44+ async fn read < ' a > ( & ' a mut self , address : A , read : & ' a mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
5645
5746 /// Writes bytes to slave with address `address`
5847 ///
@@ -70,12 +59,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
7059 /// - `SAK` = slave acknowledge
7160 /// - `Bi` = ith byte of data
7261 /// - `SP` = stop condition
73- fn write < ' a > ( & ' a mut self , address : A , write : & ' a [ u8 ] ) -> Self :: WriteFuture < ' a > ;
74-
75- /// Future returned by the `write_read` method.
76- type WriteReadFuture < ' a > : Future < Output = Result < ( ) , Self :: Error > >
77- where
78- Self : ' a ;
62+ async fn write < ' a > ( & ' a mut self , address : A , write : & ' a [ u8 ] ) -> Result < ( ) , Self :: Error > ;
7963
8064 /// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
8165 /// single transaction*.
@@ -99,18 +83,12 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
9983 /// - `MAK` = master acknowledge
10084 /// - `NMAK` = master no acknowledge
10185 /// - `SP` = stop condition
102- fn write_read < ' a > (
86+ async fn write_read < ' a > (
10387 & ' a mut self ,
10488 address : A ,
10589 write : & ' a [ u8 ] ,
10690 read : & ' a mut [ u8 ] ,
107- ) -> Self :: WriteReadFuture < ' a > ;
108-
109- /// Future returned by the `transaction` method.
110- type TransactionFuture < ' a , ' b > : Future < Output = Result < ( ) , Self :: Error > >
111- where
112- Self : ' a ,
113- ' b : ' a ;
91+ ) -> Result < ( ) , Self :: Error > ;
11492
11593 /// Execute the provided operations on the I2C bus as a single transaction.
11694 ///
@@ -125,44 +103,36 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
125103 /// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
126104 /// - `SR` = repeated start condition
127105 /// - `SP` = stop condition
128- fn transaction < ' a , ' b > (
106+ async fn transaction < ' a , ' b > (
129107 & ' a mut self ,
130108 address : A ,
131109 operations : & ' a mut [ Operation < ' b > ] ,
132- ) -> Self :: TransactionFuture < ' a , ' b > ;
110+ ) -> Result < ( ) , Self :: Error > ;
133111}
134112
135113impl < A : AddressMode , T : I2c < A > > I2c < A > for & mut T {
136- type ReadFuture < ' a > = T :: ReadFuture < ' a > where Self : ' a ;
137-
138- fn read < ' a > ( & ' a mut self , address : A , buffer : & ' a mut [ u8 ] ) -> Self :: ReadFuture < ' a > {
139- T :: read ( self , address, buffer)
114+ async fn read < ' a > ( & ' a mut self , address : A , buffer : & ' a mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
115+ T :: read ( self , address, buffer) . await
140116 }
141117
142- type WriteFuture < ' a > = T :: WriteFuture < ' a > where Self : ' a ;
143-
144- fn write < ' a > ( & ' a mut self , address : A , bytes : & ' a [ u8 ] ) -> Self :: WriteFuture < ' a > {
145- T :: write ( self , address, bytes)
118+ async fn write < ' a > ( & ' a mut self , address : A , bytes : & ' a [ u8 ] ) -> Result < ( ) , Self :: Error > {
119+ T :: write ( self , address, bytes) . await
146120 }
147121
148- type WriteReadFuture < ' a > = T :: WriteReadFuture < ' a > where Self : ' a ;
149-
150- fn write_read < ' a > (
122+ async fn write_read < ' a > (
151123 & ' a mut self ,
152124 address : A ,
153125 bytes : & ' a [ u8 ] ,
154126 buffer : & ' a mut [ u8 ] ,
155- ) -> Self :: WriteReadFuture < ' a > {
156- T :: write_read ( self , address, bytes, buffer)
127+ ) -> Result < ( ) , Self :: Error > {
128+ T :: write_read ( self , address, bytes, buffer) . await
157129 }
158130
159- type TransactionFuture < ' a , ' b > = T :: TransactionFuture < ' a , ' b > where Self : ' a , ' b : ' a ;
160-
161- fn transaction < ' a , ' b > (
131+ async fn transaction < ' a , ' b > (
162132 & ' a mut self ,
163133 address : A ,
164134 operations : & ' a mut [ Operation < ' b > ] ,
165- ) -> Self :: TransactionFuture < ' a , ' b > {
166- T :: transaction ( self , address, operations)
135+ ) -> Result < ( ) , Self :: Error > {
136+ T :: transaction ( self , address, operations) . await
167137 }
168138}
0 commit comments