aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/adapter.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/adapter.rs')
-rw-r--r--embassy-embedded-hal/src/adapter.rs79
1 files changed, 31 insertions, 48 deletions
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs
index 1c43f015f..3680984f1 100644
--- a/embassy-embedded-hal/src/adapter.rs
+++ b/embassy-embedded-hal/src/adapter.rs
@@ -38,32 +38,31 @@ where
38 E: embedded_hal_1::i2c::Error + 'static, 38 E: embedded_hal_1::i2c::Error + 'static,
39 T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>, 39 T: blocking::i2c::WriteRead<Error = E> + blocking::i2c::Read<Error = E> + blocking::i2c::Write<Error = E>,
40{ 40{
41 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 41 async fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Result<(), Self::Error> {
42 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 42 self.wrapped.read(address, buffer)
43 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
44
45 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
46 async move { self.wrapped.read(address, buffer) }
47 } 43 }
48 44
49 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { 45 async fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Result<(), Self::Error> {
50 async move { self.wrapped.write(address, bytes) } 46 self.wrapped.write(address, bytes)
51 } 47 }
52 48
53 fn write_read<'a>(&'a mut self, address: u8, bytes: &'a [u8], buffer: &'a mut [u8]) -> Self::WriteReadFuture<'a> { 49 async fn write_read<'a>(
54 async move { self.wrapped.write_read(address, bytes, buffer) } 50 &'a mut self,
51 address: u8,
52 bytes: &'a [u8],
53 buffer: &'a mut [u8],
54 ) -> Result<(), Self::Error> {
55 self.wrapped.write_read(address, bytes, buffer)
55 } 56 }
56 57
57 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; 58 async fn transaction<'a, 'b>(
58
59 fn transaction<'a, 'b>(
60 &'a mut self, 59 &'a mut self,
61 address: u8, 60 address: u8,
62 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], 61 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
63 ) -> Self::TransactionFuture<'a, 'b> { 62 ) -> Result<(), Self::Error> {
64 let _ = address; 63 let _ = address;
65 let _ = operations; 64 let _ = operations;
66 async move { todo!() } 65 todo!()
67 } 66 }
68} 67}
69 68
@@ -84,23 +83,17 @@ where
84 E: embedded_hal_1::spi::Error + 'static, 83 E: embedded_hal_1::spi::Error + 'static,
85 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 84 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
86{ 85{
87 type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 86 async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> {
88 87 // Ensure we write the expected bytes
89 fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> { 88 for i in 0..core::cmp::min(read.len(), write.len()) {
90 async move { 89 read[i] = write[i].clone();
91 // Ensure we write the expected bytes
92 for i in 0..core::cmp::min(read.len(), write.len()) {
93 read[i] = write[i].clone();
94 }
95 self.wrapped.transfer(read)?;
96 Ok(())
97 } 90 }
91 self.wrapped.transfer(read)?;
92 Ok(())
98 } 93 }
99 94
100 type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 95 async fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Result<(), Self::Error> {
101 96 todo!()
102 fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
103 async move { todo!() }
104 } 97 }
105} 98}
106 99
@@ -109,10 +102,8 @@ where
109 E: embedded_hal_1::spi::Error + 'static, 102 E: embedded_hal_1::spi::Error + 'static,
110 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 103 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
111{ 104{
112 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 105 async fn flush(&mut self) -> Result<(), Self::Error> {
113 106 Ok(())
114 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
115 async move { Ok(()) }
116 } 107 }
117} 108}
118 109
@@ -121,13 +112,9 @@ where
121 E: embedded_hal_1::spi::Error + 'static, 112 E: embedded_hal_1::spi::Error + 'static,
122 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 113 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
123{ 114{
124 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 115 async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
125 116 self.wrapped.write(data)?;
126 fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { 117 Ok(())
127 async move {
128 self.wrapped.write(data)?;
129 Ok(())
130 }
131 } 118 }
132} 119}
133 120
@@ -136,13 +123,9 @@ where
136 E: embedded_hal_1::spi::Error + 'static, 123 E: embedded_hal_1::spi::Error + 'static,
137 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, 124 T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
138{ 125{
139 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; 126 async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
140 127 self.wrapped.transfer(data)?;
141 fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { 128 Ok(())
142 async move {
143 self.wrapped.transfer(data)?;
144 Ok(())
145 }
146 } 129 }
147} 130}
148 131
@@ -192,7 +175,7 @@ where
192 } 175 }
193 176
194 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a; 177 type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a;
195 fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { 178 fn flush(&mut self) -> Result<(), Self::Error> {
196 async move { self.wrapped.bflush() } 179 async move { self.wrapped.bflush() }
197 } 180 }
198} 181}