16
16
//! Since 7-bit addressing is the mode of the majority of I2C devices,
17
17
//! `SevenBitAddress` has been set as default mode and thus can be omitted if desired.
18
18
19
- use core:: future:: Future ;
20
19
pub use embedded_hal:: i2c:: Operation ;
21
20
pub use embedded_hal:: i2c:: {
22
21
AddressMode , Error , ErrorKind , ErrorType , NoAcknowledgeSource , SevenBitAddress , TenBitAddress ,
23
22
} ;
24
23
25
24
/// Async i2c
26
25
pub 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
-
32
26
/// Reads enough bytes from slave with `address` to fill `buffer`
33
27
///
34
28
/// # I2C Events (contract)
@@ -47,12 +41,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
47
41
/// - `MAK` = master acknowledge
48
42
/// - `NMAK` = master no acknowledge
49
43
/// - `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 > ;
56
45
57
46
/// Writes bytes to slave with address `address`
58
47
///
@@ -70,12 +59,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
70
59
/// - `SAK` = slave acknowledge
71
60
/// - `Bi` = ith byte of data
72
61
/// - `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 > ;
79
63
80
64
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
81
65
/// single transaction*.
@@ -99,18 +83,12 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
99
83
/// - `MAK` = master acknowledge
100
84
/// - `NMAK` = master no acknowledge
101
85
/// - `SP` = stop condition
102
- fn write_read < ' a > (
86
+ async fn write_read < ' a > (
103
87
& ' a mut self ,
104
88
address : A ,
105
89
write : & ' a [ u8 ] ,
106
90
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 > ;
114
92
115
93
/// Execute the provided operations on the I2C bus as a single transaction.
116
94
///
@@ -125,44 +103,36 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
125
103
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
126
104
/// - `SR` = repeated start condition
127
105
/// - `SP` = stop condition
128
- fn transaction < ' a , ' b > (
106
+ async fn transaction < ' a , ' b > (
129
107
& ' a mut self ,
130
108
address : A ,
131
109
operations : & ' a mut [ Operation < ' b > ] ,
132
- ) -> Self :: TransactionFuture < ' a , ' b > ;
110
+ ) -> Result < ( ) , Self :: Error > ;
133
111
}
134
112
135
113
impl < 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 < ( ) , T :: Error > {
115
+ T :: read ( self , address, buffer) . await
140
116
}
141
117
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 < ( ) , T :: Error > {
119
+ T :: write ( self , address, bytes) . await
146
120
}
147
121
148
- type WriteReadFuture < ' a > = T :: WriteReadFuture < ' a > where Self : ' a ;
149
-
150
- fn write_read < ' a > (
122
+ async fn write_read < ' a > (
151
123
& ' a mut self ,
152
124
address : A ,
153
125
bytes : & ' a [ u8 ] ,
154
126
buffer : & ' a mut [ u8 ] ,
155
- ) -> Self :: WriteReadFuture < ' a > {
156
- T :: write_read ( self , address, bytes, buffer)
127
+ ) -> Result < ( ) , T :: Error > {
128
+ T :: write_read ( self , address, bytes, buffer) . await
157
129
}
158
130
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 > (
162
132
& ' a mut self ,
163
133
address : A ,
164
134
operations : & ' a mut [ Operation < ' b > ] ,
165
- ) -> Self :: TransactionFuture < ' a , ' b > {
166
- T :: transaction ( self , address, operations)
135
+ ) -> Result < ( ) , T :: Error > {
136
+ T :: transaction ( self , address, operations) . await
167
137
}
168
138
}
0 commit comments