sh1106/interface/
spi.rs

1//! sh1106 SPI interface
2
3use hal::{self, digital::v2::OutputPin};
4
5use super::DisplayInterface;
6use crate::Error;
7
8/// SPI display interface.
9///
10/// This combines the SPI peripheral and a data/command pin
11pub struct SpiInterface<SPI, DC, CS> {
12    spi: SPI,
13    dc: DC,
14    cs: CS,
15}
16
17impl<SPI, DC, CS, CommE, PinE> SpiInterface<SPI, DC, CS>
18where
19    SPI: hal::blocking::spi::Write<u8, Error = CommE>,
20    DC: OutputPin<Error = PinE>,
21    CS: OutputPin<Error = PinE>,
22{
23    /// Create new SPI interface for communciation with sh1106
24    pub fn new(spi: SPI, dc: DC, cs: CS) -> Self {
25        Self { spi, dc, cs }
26    }
27}
28
29impl<SPI, DC, CS, CommE, PinE> DisplayInterface for SpiInterface<SPI, DC, CS>
30where
31    SPI: hal::blocking::spi::Write<u8, Error = CommE>,
32    DC: OutputPin<Error = PinE>,
33    CS: OutputPin<Error = PinE>,
34{
35    type Error = Error<CommE, PinE>;
36
37    fn init(&mut self) -> Result<(), Self::Error> {
38        self.cs.set_high().map_err(Error::Pin)
39    }
40
41    fn send_commands(&mut self, cmds: &[u8]) -> Result<(), Self::Error> {
42        self.cs.set_low().map_err(Error::Pin)?;
43        self.dc.set_low().map_err(Error::Pin)?;
44
45        self.spi.write(&cmds).map_err(Error::Comm)?;
46
47        self.dc.set_high().map_err(Error::Pin)?;
48        self.cs.set_high().map_err(Error::Pin)
49    }
50
51    fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
52        self.cs.set_low().map_err(Error::Pin)?;
53
54        // 1 = data, 0 = command
55        self.dc.set_high().map_err(Error::Pin)?;
56
57        self.spi.write(&buf).map_err(Error::Comm)?;
58
59        self.cs.set_high().map_err(Error::Pin)
60    }
61}