embedded_interfaces/commands/
mod.rs1use crate::{MaybeBitdumpFormattable, TransportError};
2
3pub mod i2c;
4pub mod spi;
5pub mod unsupported_executor;
6
7pub trait Executor {
9 type Command: Command;
11 type Error: core::fmt::Debug;
13}
14
15pub trait Command {
20 type In: bytemuck::Pod + MaybeBitdumpFormattable;
22 type Out: bytemuck::Pod + bytemuck::Zeroable + MaybeBitdumpFormattable;
24
25 type ExecutorError: core::fmt::Debug;
27 #[cfg(all(feature = "sync", not(feature = "async")))]
30 type SpiExecutor: spi::ExecutorSync<Command = Self, Error = Self::ExecutorError>;
31 #[cfg(all(not(feature = "sync"), feature = "async"))]
32 type SpiExecutor: spi::ExecutorAsync<Command = Self, Error = Self::ExecutorError>;
33 #[cfg(all(feature = "sync", feature = "async"))]
34 type SpiExecutor: spi::ExecutorSync<Command = Self, Error = Self::ExecutorError>
35 + spi::ExecutorAsync<Command = Self, Error = Self::ExecutorError>;
36 #[cfg(all(feature = "sync", not(feature = "async")))]
39 type I2cExecutor: i2c::ExecutorSync<Command = Self, Error = Self::ExecutorError>;
40 #[cfg(all(not(feature = "sync"), feature = "async"))]
41 type I2cExecutor: i2c::ExecutorAsync<Command = Self, Error = Self::ExecutorError>;
42 #[cfg(all(feature = "sync", feature = "async"))]
43 type I2cExecutor: i2c::ExecutorSync<Command = Self, Error = Self::ExecutorError>
44 + i2c::ExecutorAsync<Command = Self, Error = Self::ExecutorError>;
45}
46
47#[maybe_async_cfg::maybe(
50 idents(hal(sync = "embedded_hal", async = "embedded_hal_async")),
51 sync(feature = "sync"),
52 async(feature = "async")
53)]
54#[allow(async_fn_in_trait)]
55pub trait CommandInterface {
56 type BusError: core::fmt::Debug;
58
59 async fn execute<C, D>(
61 &mut self,
62 delay: &mut D,
63 input: C::In,
64 ) -> Result<C::Out, TransportError<C::ExecutorError, Self::BusError>>
65 where
66 D: hal::delay::DelayNs,
67 C: Command;
68}
69
70#[macro_export]
71macro_rules! define_executor {
72 ($executor:ident, $command_trait:ident, $error_type:ty) => {
73 #[doc = "The executor for any [`"]
74 #[doc = stringify!($command_trait)]
75 #[doc = "`]"]
76 pub struct $executor<C: $command_trait + ?Sized> {
77 _marker: PhantomData<C>,
78 }
79
80 impl<C: $command_trait> embedded_interfaces::commands::Executor for $executor<C> {
81 type Error = $error_type;
82 type Command = C;
83 }
84 };
85}