dmx_rdm/
dmx_uart_driver.rs

1#[derive(Debug)]
2#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3pub enum DmxUartDriverError<E> {
4    /// The request timed time out.
5    /// IMPORTANT: If you implement a driver make sure this error gets raised instead
6    /// of a driver specific error.
7    TimeoutError,
8    /// A driver specific error.
9    DriverError(E),
10}
11
12impl<E: core::fmt::Display> core::fmt::Display for DmxUartDriverError<E> {
13    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14        match self {
15            DmxUartDriverError::TimeoutError => write!(f, "timeout error occurred"),
16            DmxUartDriverError::DriverError(error) => error.fmt(f),
17        }
18    }
19}
20
21#[cfg(feature = "std")]
22impl<E: core::fmt::Display + core::fmt::Debug> std::error::Error for DmxUartDriverError<E> {}
23
24impl<E> From<E> for DmxUartDriverError<E> {
25    fn from(value: E) -> Self {
26        Self::DriverError(value)
27    }
28}
29
30pub trait DmxUartDriver {
31    type DriverError;
32}
33
34/// Object to implement access to the uart.
35/// It can read frames.
36/// It has to communicate at 250000 baud.
37pub trait DmxRecvUartDriver: DmxUartDriver {
38    /// Read frames (used for rdm discovery response).
39    /// Returns the number of bytes actually read.
40    fn read_frames(
41        &mut self,
42        buffer: &mut [u8],
43        timeout_us: u32,
44    ) -> Result<usize, DmxUartDriverError<Self::DriverError>>;
45
46    /// Read frames without waiting for break.
47    /// Returns the number of bytes actually read.
48    fn read_frames_no_break(
49        &mut self,
50        buffer: &mut [u8],
51        timeout_us: u32,
52    ) -> Result<usize, DmxUartDriverError<Self::DriverError>>;
53}
54
55/// Object to implement access to the uart.
56/// It can write frames.
57/// It has to communicate at 250000 baud.
58pub trait DmxRespUartDriver: DmxUartDriver {
59    /// Write dmx frames with break.
60    /// Returns the number of bytes actually written.
61    fn write_frames(
62        &mut self,
63        buffer: &[u8],
64    ) -> Result<usize, DmxUartDriverError<Self::DriverError>>;
65
66    /// Write dmx frames without break (used for rdm discovery response).
67    /// Returns the number of bytes actually written.
68    fn write_frames_no_break(
69        &mut self,
70        buffer: &[u8],
71    ) -> Result<usize, DmxUartDriverError<Self::DriverError>>;
72}