1use crate::error::Error;
4
5pub trait Interface {
9 type Error;
11
12 fn send_command(&mut self, command: u8, args: &[u8]) -> Result<(), Error<Self::Error>>;
14
15 fn send_data(&mut self, data: &[u8]) -> Result<(), Error<Self::Error>>;
19}
20
21impl<T: Interface> Interface for &mut T {
23 type Error = T::Error;
24
25 #[inline]
26 fn send_command(&mut self, command: u8, args: &[u8]) -> Result<(), Error<Self::Error>> {
27 T::send_command(self, command, args)
28 }
29
30 #[inline]
31 fn send_data(&mut self, data: &[u8]) -> Result<(), Error<Self::Error>> {
32 T::send_data(self, data)
33 }
34}
35
36pub struct SpiInterface<SPI, DC> {
41 spi: SPI,
42 dc: DC,
43}
44
45impl<SPI, DC> SpiInterface<SPI, DC> {
46 pub fn new(spi: SPI, dc: DC) -> Self {
48 Self { spi, dc }
49 }
50
51 pub fn release(self) -> (SPI, DC) {
53 (self.spi, self.dc)
54 }
55}
56
57impl<SPI, DC> Interface for SpiInterface<SPI, DC>
58where
59 SPI: embedded_hal::spi::SpiDevice,
60 DC: embedded_hal::digital::OutputPin,
61{
62 type Error = SPI::Error;
63
64 fn send_command(&mut self, command: u8, args: &[u8]) -> Result<(), Error<Self::Error>> {
65 self.dc.set_low().map_err(|_| Error::DcPin)?;
67 self.spi.write(&[command]).map_err(Error::Bus)?;
68
69 if !args.is_empty() {
70 self.dc.set_high().map_err(|_| Error::DcPin)?;
72 self.spi.write(args).map_err(Error::Bus)?;
73 }
74
75 Ok(())
76 }
77
78 fn send_data(&mut self, data: &[u8]) -> Result<(), Error<Self::Error>> {
79 self.dc.set_high().map_err(|_| Error::DcPin)?;
81 self.spi.write(data).map_err(Error::Bus)?;
82 Ok(())
83 }
84}