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
21
pub use embedded_hal:: i2c:: Operation ;
20
22
pub use embedded_hal:: i2c:: {
21
23
AddressMode , Error , ErrorKind , ErrorType , NoAcknowledgeSource , SevenBitAddress , TenBitAddress ,
@@ -42,9 +44,15 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
42
44
/// - `NMAK` = master no acknowledge
43
45
/// - `SP` = stop condition
44
46
#[ inline]
45
- async fn read ( & mut self , address : A , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
46
- self . transaction ( address, & mut [ Operation :: Read ( read) ] )
47
- . await
47
+ fn read (
48
+ & mut self ,
49
+ address : A ,
50
+ read : & mut [ u8 ] ,
51
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
52
+ async move {
53
+ self . transaction ( address, & mut [ Operation :: Read ( read) ] )
54
+ . await
55
+ }
48
56
}
49
57
50
58
/// Writes bytes to slave with address `address`.
@@ -64,9 +72,11 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
64
72
/// - `Bi` = ith byte of data
65
73
/// - `SP` = stop condition
66
74
#[ inline]
67
- async fn write ( & mut self , address : A , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
68
- self . transaction ( address, & mut [ Operation :: Write ( write) ] )
69
- . await
75
+ fn write ( & mut self , address : A , write : & [ u8 ] ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
76
+ async move {
77
+ self . transaction ( address, & mut [ Operation :: Write ( write) ] )
78
+ . await
79
+ }
70
80
}
71
81
72
82
/// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a
@@ -92,17 +102,19 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
92
102
/// - `NMAK` = master no acknowledge
93
103
/// - `SP` = stop condition
94
104
#[ inline]
95
- async fn write_read (
105
+ fn write_read (
96
106
& mut self ,
97
107
address : A ,
98
108
write : & [ u8 ] ,
99
109
read : & mut [ u8 ] ,
100
- ) -> Result < ( ) , Self :: Error > {
101
- self . transaction (
102
- address,
103
- & mut [ Operation :: Write ( write) , Operation :: Read ( read) ] ,
104
- )
105
- . await
110
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
111
+ async move {
112
+ self . transaction (
113
+ address,
114
+ & mut [ Operation :: Write ( write) , Operation :: Read ( read) ] ,
115
+ )
116
+ . await
117
+ }
106
118
}
107
119
108
120
/// Execute the provided operations on the I2C bus as a single transaction.
@@ -118,40 +130,44 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
118
130
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
119
131
/// - `SR` = repeated start condition
120
132
/// - `SP` = stop condition
121
- async fn transaction (
133
+ fn transaction (
122
134
& mut self ,
123
135
address : A ,
124
136
operations : & mut [ Operation < ' _ > ] ,
125
- ) -> Result < ( ) , Self :: Error > ;
137
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > ;
126
138
}
127
139
128
140
impl < A : AddressMode , T : I2c < A > + ?Sized > I2c < A > for & mut T {
129
141
#[ inline]
130
- async fn read ( & mut self , address : A , read : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
131
- T :: read ( self , address, read) . await
142
+ fn read (
143
+ & mut self ,
144
+ address : A ,
145
+ read : & mut [ u8 ] ,
146
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
147
+ async move { T :: read ( self , address, read) . await }
132
148
}
133
149
134
150
#[ inline]
135
- async fn write ( & mut self , address : A , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
136
- T :: write ( self , address, write) . await
151
+ fn write ( & mut self , address : A , write : & [ u8 ] ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
152
+ async move { T :: write ( self , address, write) . await }
137
153
}
138
154
139
155
#[ inline]
140
- async fn write_read (
156
+ fn write_read (
141
157
& mut self ,
142
158
address : A ,
143
159
write : & [ u8 ] ,
144
160
read : & mut [ u8 ] ,
145
- ) -> Result < ( ) , Self :: Error > {
146
- T :: write_read ( self , address, write, read) . await
161
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
162
+ async move { T :: write_read ( self , address, write, read) . await }
147
163
}
148
164
149
165
#[ inline]
150
- async fn transaction (
166
+ fn transaction (
151
167
& mut self ,
152
168
address : A ,
153
169
operations : & mut [ Operation < ' _ > ] ,
154
- ) -> Result < ( ) , Self :: Error > {
155
- T :: transaction ( self , address, operations) . await
170
+ ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
171
+ async move { T :: transaction ( self , address, operations) . await }
156
172
}
157
173
}
0 commit comments