diff --git a/Cargo.toml b/Cargo.toml index 20345bc..30b2467 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,8 @@ xtensa-lx = { version = "0.6.0", optional = true } spin = { version = "0.9.2", optional = true } atomic-polyfill = { version = "0.1.6", optional = true } +embedded-hal-alpha = { package = "embedded-hal", version = "=1.0.0-alpha.8", optional = true } + [dev-dependencies] embedded-hal-mock = "0.8" @@ -31,3 +33,4 @@ embedded-hal-mock = "0.8" std = ["once_cell"] xtensa = ["xtensa-lx", "spin"] cortex-m = ["dep:cortex-m", "atomic-polyfill"] +eh-alpha = ["embedded-hal-alpha"] diff --git a/src/proxies.rs b/src/proxies.rs index 6862fc5..f3ffae7 100644 --- a/src/proxies.rs +++ b/src/proxies.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "eh-alpha")] +use embedded_hal_alpha::i2c as i2c_alpha; + use embedded_hal::adc; use embedded_hal::blocking::i2c; use embedded_hal::blocking::spi; @@ -61,6 +64,76 @@ where } } +// Implementations for the embedded_hal alpha + +#[cfg(feature = "eh-alpha")] +impl<'a, M: crate::BusMutex> i2c_alpha::ErrorType for I2cProxy<'a, M> +where + M::Bus: i2c_alpha::ErrorType, +{ + type Error = ::Error; +} + +#[cfg(feature = "eh-alpha")] +impl<'a, M: crate::BusMutex> i2c_alpha::blocking::I2c for I2cProxy<'a, M> +where + M::Bus: i2c_alpha::blocking::I2c, +{ + fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + self.mutex.lock(|bus| bus.read(address, buffer)) + } + + fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + self.mutex.lock(|bus| bus.write(address, bytes)) + } + + fn write_iter(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> + where + B: IntoIterator, + { + self.mutex.lock(|bus| bus.write_iter(address, bytes)) + } + + fn write_read( + &mut self, + address: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Self::Error> { + self.mutex + .lock(|bus| bus.write_read(address, bytes, buffer)) + } + + fn write_iter_read( + &mut self, + address: u8, + bytes: B, + buffer: &mut [u8], + ) -> Result<(), Self::Error> + where + B: IntoIterator, + { + self.mutex + .lock(|bus| bus.write_iter_read(address, bytes, buffer)) + } + + fn transaction<'b>( + &mut self, + address: u8, + operations: &mut [i2c_alpha::blocking::Operation<'b>], + ) -> Result<(), Self::Error> { + self.mutex.lock(|bus| bus.transaction(address, operations)) + } + + fn transaction_iter<'b, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> + where + O: IntoIterator>, + { + self.mutex + .lock(|bus| bus.transaction_iter(address, operations)) + } +} + /// Proxy type for SPI bus sharing. /// /// The `SpiProxy` implements all (blocking) SPI traits so it can be passed to drivers instead of