epd_datafuri/interface/
mod.rs1use display_interface::DisplayError;
3use embedded_hal::{
4 delay::DelayNs,
5 digital::{InputPin, OutputPin},
6 spi::SpiDevice,
7};
8
9const RESET_DELAY_MS: u8 = 10;
10
11pub struct SpiDisplayInterface<SPI, BSY, DC, RST> {
17 spi: SPI,
19 busy: BSY,
21 dc: DC,
23 rst: RST,
25}
26
27impl<SPI, BSY, DC, RST> SpiDisplayInterface<SPI, BSY, DC, RST> {
28 pub fn new(spi: SPI, busy: BSY, dc: DC, rst: RST) -> Self {
30 SpiDisplayInterface { spi, busy, dc, rst }
31 }
32}
33
34impl<SPI, BSY, DC, RST> SpiDisplayInterface<SPI, BSY, DC, RST>
35where
36 SPI: SpiDevice,
37 RST: OutputPin,
38 DC: OutputPin,
39 BSY: InputPin,
40{
41 pub(crate) fn cmd(&mut self, command: u8) -> Result<(), DisplayError> {
43 log::trace!("cmd 0x{:02X}", command);
44
45 self.dc.set_low().map_err(|_| DisplayError::DCError)?;
47
48 self.spi
50 .write(&[command])
51 .map_err(|_| DisplayError::BusWriteError)
52 }
53
54 pub(crate) fn data(&mut self, data: &[u8]) -> Result<(), DisplayError> {
56 self.dc.set_high().map_err(|_| DisplayError::DCError)?;
58
59 self.spi
61 .write(data)
62 .map_err(|_| DisplayError::BusWriteError)
63 }
64
65 pub(crate) fn cmd_with_data(&mut self, command: u8, data: &[u8]) -> Result<(), DisplayError> {
67 log::trace!("cmd 0x{:02X}, data {:02X?}", command, data);
68 self.cmd(command)?;
69 self.data(data)
70 }
71
72 pub(crate) fn data_x_times(&mut self, val: u8, repetitions: u32) -> Result<(), DisplayError> {
75 log::trace!("writing data 0x{:02X} x {}", val, repetitions);
76 let _ = self.dc.set_high();
78 for _ in 0..repetitions {
80 self.spi
81 .write(&[val])
82 .map_err(|_| DisplayError::BusWriteError)?;
83 }
84 Ok(())
85 }
86
87 pub(crate) fn wait_until_idle(&mut self, delay: &mut impl DelayNs) {
91 log::trace!("Waiting until display is idle");
92 while self.busy.is_high().unwrap_or(true) {
93 delay.delay_ms(1)
94 }
95 }
96
97 pub(crate) fn wait_until_idle_active_low(&mut self, delay: &mut impl DelayNs) {
101 log::trace!("Waiting until display is idle (active-low busy)");
102 while self.busy.is_low().unwrap_or(true) {
103 delay.delay_ms(1)
104 }
105 }
106
107 pub(crate) fn hard_reset(&mut self, delay: &mut impl DelayNs) -> Result<(), DisplayError> {
109 log::trace!("Resetting display");
110 self.rst.set_low().map_err(|_| DisplayError::RSError)?;
111 delay.delay_ms(RESET_DELAY_MS.into());
112 self.rst.set_high().map_err(|_| DisplayError::RSError)?;
113 delay.delay_ms(RESET_DELAY_MS.into());
114 Ok(())
115 }
116}