1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! This module provides shims for the `embedded-hal` hardware corresponding to the SSD1322's
//! supported electrical/bus interfaces. It is a shim between `embedded-hal` implementations and
//! the display driver's command layer.

use nb;

/// An interface for the SSD1322 implements this trait, which provides the basic operations for
/// sending pre-encoded commands and data to the chip via the interface.
pub trait DisplayInterface {
    type Error;

    fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error>;
    fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
    fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error>;
}

pub mod spi {
    //! The SPI interface supports the "4-wire" interface of the driver, such that each word on the
    //! SPI bus is 8 bits. The "3-wire" mode is not supported, as it replaces the D/C GPIO with a
    //! 9th bit on each SPI word, and `embedded-hal` SPI traits do not currently support
    //! non-byte-aligned SPI word lengths.

    use embedded_hal as hal;

    use super::DisplayInterface;
    use nb;

    /// The union of all errors that may occur on the SPI interface. This consists of variants for
    /// the error types of the D/C GPIO and the SPI bus.
    #[derive(Debug)]
    pub enum SpiInterfaceError<DCE, SPIE> {
        DCError(DCE),
        SPIError(SPIE),
    }

    impl<DCE, SPIE> SpiInterfaceError<DCE, SPIE> {
        fn from_dc(e: DCE) -> Self {
            Self::DCError(e)
        }
        fn from_spi(e: SPIE) -> Self {
            Self::SPIError(e)
        }
    }

    /// A configured `DisplayInterface` for controlling an SSD1322 via 4-wire SPI.
    pub struct SpiInterface<SPI, DC> {
        /// The SPI master device connected to the SSD1322.
        spi: SPI,
        /// A GPIO output pin connected to the D/C (data/command) pin of the SSD1322 (the fourth
        /// "wire" of "4-wire" mode).
        dc: DC,
    }

    impl<SPI, DC> SpiInterface<SPI, DC>
    where
        SPI: hal::spi::FullDuplex<u8>,
        DC: hal::digital::v2::OutputPin,
    {
        /// Create a new SPI interface to communicate with the display driver. `spi` is the SPI
        /// master device, and `dc` is the GPIO output pin connected to the D/C pin of the SSD1322.
        pub fn new(spi: SPI, dc: DC) -> Self {
            Self { spi: spi, dc: dc }
        }
    }

    impl<SPI, DC> DisplayInterface for SpiInterface<SPI, DC>
    where
        SPI: hal::spi::FullDuplex<u8>,
        DC: hal::digital::v2::OutputPin,
    {
        type Error = SpiInterfaceError<
            <DC as hal::digital::v2::OutputPin>::Error,
            <SPI as hal::spi::FullDuplex<u8>>::Error,
        >;

        /// Send a command word to the display's command register. Synchronous.
        fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error> {
            // The SPI device has FIFOs that we must ensure are drained before the bus will
            // quiesce. This must happen before asserting DC for a command.
            while let Ok(_) = self.spi.read() {
                self.dc.set_high().map_err(Self::Error::from_dc)?;
            }
            self.dc.set_low().map_err(Self::Error::from_dc)?;
            let bus_op = nb::block!(self.spi.send(cmd))
                .and_then(|_| nb::block!(self.spi.read()))
                .map_err(Self::Error::from_spi)
                .map(core::mem::drop);
            self.dc.set_high().map_err(Self::Error::from_dc)?;
            bus_op
        }

        /// Send a sequence of data words to the display from a buffer. Synchronous.
        fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
            for word in buf {
                nb::block!(self.spi.send(word.clone())).map_err(Self::Error::from_spi)?;
                nb::block!(self.spi.read()).map_err(Self::Error::from_spi)?;
            }
            Ok(())
        }

        /// Send a data word to the display asynchronously, using `nb` style non-blocking send. If
        /// the hardware FIFO is full, returns `WouldBlock` which means the word was not accepted
        /// and should be retried later.
        fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error> {
            match self.spi.send(word) {
                Ok(()) => {
                    let _ = self.spi.read();
                    Ok(())
                }
                Err(nb::Error::Other(e)) => Err(nb::Error::Other(Self::Error::from_spi(e))),
                Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
            }
        }
    }
}

#[cfg(test)]
pub mod test_spy {
    //! An interface for use in unit tests to spy on whatever was sent to it.

    use super::DisplayInterface;
    use nb;
    use std::cell::RefCell;
    use std::rc::Rc;

    #[derive(Clone, Debug, PartialEq)]
    pub enum Sent {
        Cmd(u8),
        Data(Vec<u8>),
    }

    pub struct TestSpyInterface {
        sent: Rc<RefCell<Vec<Sent>>>,
    }

    impl TestSpyInterface {
        pub fn new() -> Self {
            TestSpyInterface {
                sent: Rc::new(RefCell::new(Vec::new())),
            }
        }
        pub fn split(&self) -> Self {
            Self {
                sent: self.sent.clone(),
            }
        }
        pub fn check(&self, cmd: u8, data: &[u8]) {
            let sent = self.sent.borrow();
            if data.len() == 0 {
                assert_eq!(sent.len(), 1);
            } else {
                assert_eq!(sent.len(), 2);
                assert_eq!(sent[1], Sent::Data(data.to_vec()));
            }
            assert_eq!(sent[0], Sent::Cmd(cmd));
        }
        pub fn check_multi(&self, expect: &[Sent]) {
            assert_eq!(*self.sent.borrow(), expect);
        }
        pub fn clear(&mut self) {
            self.sent.borrow_mut().clear()
        }
    }

    impl DisplayInterface for TestSpyInterface {
        type Error = core::convert::Infallible;

        fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error> {
            self.sent.borrow_mut().push(Sent::Cmd(cmd));
            Ok(())
        }
        fn send_data(&mut self, data: &[u8]) -> Result<(), Self::Error> {
            self.sent.borrow_mut().push(Sent::Data(data.to_vec()));
            Ok(())
        }
        fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error> {
            let mut sent = self.sent.borrow_mut();
            {
                let last_idx = sent.len() - 1;
                match &mut sent[last_idx] {
                    Sent::Cmd(_) => {}
                    Sent::Data(ref mut d) => {
                        d.push(word);
                        return Ok(());
                    }
                };
            }
            sent.push(Sent::Data(vec![word]));
            Ok(())
        }
    }
}