pub mod codecs;
use crate::TransportError;
use crate::registers::{ReadableRegister, Register, RegisterCodec, WritableRegister};
#[maybe_async_cfg::maybe(
idents(hal(sync = "embedded_hal", async = "embedded_hal_async")),
sync(feature = "sync"),
async(feature = "async")
)]
#[allow(async_fn_in_trait)]
pub trait Codec: RegisterCodec {
async fn read_register<R, I>(interface: &mut I) -> Result<R, TransportError<Self::Error, I::Error>>
where
R: Register<CodecError = Self::Error> + ReadableRegister,
I: hal::spi::r#SpiDevice;
async fn write_register<R, I>(
interface: &mut I,
register: impl AsRef<R>,
) -> Result<(), TransportError<Self::Error, I::Error>>
where
R: Register<CodecError = Self::Error> + WritableRegister,
I: hal::spi::r#SpiDevice;
}
#[maybe_async_cfg::maybe(
idents(hal(sync = "embedded_hal", async = "embedded_hal_async"), Codec, RegisterInterface),
sync(feature = "sync"),
async(feature = "async")
)]
impl<I> crate::registers::RegisterInterface for crate::spi::SpiDevice<I>
where
I: hal::spi::r#SpiDevice,
{
type BusError = I::Error;
#[inline]
async fn read_register<R>(&mut self) -> Result<R, TransportError<<R as Register>::CodecError, Self::BusError>>
where
R: ReadableRegister,
{
<R::SpiCodec as Codec>::read_register::<R, _>(&mut self.interface).await
}
#[inline]
async fn write_register<R>(
&mut self,
register: impl AsRef<R>,
) -> Result<(), TransportError<<R as Register>::CodecError, Self::BusError>>
where
R: WritableRegister,
{
<R::SpiCodec as Codec>::write_register(&mut self.interface, register).await
}
}