1use super::Arbiter;
51use embedded_hal::i2c::{AddressMode, ErrorType, I2c as BlockingI2c, Operation};
52use embedded_hal_async::i2c::I2c as AsyncI2c;
53
54pub struct ArbiterDevice<'a, BUS> {
56 bus: &'a Arbiter<BUS>,
57}
58
59impl<'a, BUS> ArbiterDevice<'a, BUS> {
60 pub fn new(bus: &'a Arbiter<BUS>) -> Self {
62 Self { bus }
63 }
64}
65
66impl<BUS> ErrorType for ArbiterDevice<'_, BUS>
67where
68 BUS: ErrorType,
69{
70 type Error = BUS::Error;
71}
72
73impl<BUS, A> AsyncI2c<A> for ArbiterDevice<'_, BUS>
74where
75 BUS: AsyncI2c<A>,
76 A: AddressMode,
77{
78 async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
79 let mut bus = self.bus.access().await;
80 bus.read(address, read).await
81 }
82
83 async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
84 let mut bus = self.bus.access().await;
85 bus.write(address, write).await
86 }
87
88 async fn write_read(
89 &mut self,
90 address: A,
91 write: &[u8],
92 read: &mut [u8],
93 ) -> Result<(), Self::Error> {
94 let mut bus = self.bus.access().await;
95 bus.write_read(address, write, read).await
96 }
97
98 async fn transaction(
99 &mut self,
100 address: A,
101 operations: &mut [Operation<'_>],
102 ) -> Result<(), Self::Error> {
103 let mut bus = self.bus.access().await;
104 bus.transaction(address, operations).await
105 }
106}
107
108pub struct BlockingArbiterDevice<'a, BUS> {
110 bus: &'a Arbiter<BUS>,
111}
112
113impl<'a, BUS> BlockingArbiterDevice<'a, BUS> {
114 pub fn new(bus: &'a Arbiter<BUS>) -> Self {
116 Self { bus }
117 }
118
119 pub fn into_non_blocking(self) -> ArbiterDevice<'a, BUS>
121 where
122 BUS: AsyncI2c,
123 {
124 ArbiterDevice { bus: self.bus }
125 }
126}
127
128impl<'a, BUS> ErrorType for BlockingArbiterDevice<'a, BUS>
129where
130 BUS: ErrorType,
131{
132 type Error = BUS::Error;
133}
134
135impl<'a, BUS, A> AsyncI2c<A> for BlockingArbiterDevice<'a, BUS>
136where
137 BUS: BlockingI2c<A>,
138 A: AddressMode,
139{
140 async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
141 let mut bus = self.bus.access().await;
142 bus.read(address, read)
143 }
144
145 async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
146 let mut bus = self.bus.access().await;
147 bus.write(address, write)
148 }
149
150 async fn write_read(
151 &mut self,
152 address: A,
153 write: &[u8],
154 read: &mut [u8],
155 ) -> Result<(), Self::Error> {
156 let mut bus = self.bus.access().await;
157 bus.write_read(address, write, read)
158 }
159
160 async fn transaction(
161 &mut self,
162 address: A,
163 operations: &mut [Operation<'_>],
164 ) -> Result<(), Self::Error> {
165 let mut bus = self.bus.access().await;
166 bus.transaction(address, operations)
167 }
168}