epd_waveshare_async/
hw.rs1use embedded_hal::{
2 digital::{ErrorType as PinErrorType, InputPin, OutputPin, PinState},
3 spi::ErrorType as SpiErrorType,
4};
5use embedded_hal_async::{delay::DelayNs, digital::Wait, spi::SpiDevice};
6
7use crate::log::trace;
8
9pub trait ErrorHw {
14 type Error;
15}
16
17pub trait SpiHw {
19 type Spi: SpiDevice;
20}
21
22pub trait DcHw {
24 type Dc: OutputPin;
25
26 fn dc(&mut self) -> &mut Self::Dc;
27}
28
29pub trait ResetHw {
31 type Reset: OutputPin;
32
33 fn reset(&mut self) -> &mut Self::Reset;
34}
35
36pub trait BusyHw {
38 type Busy: InputPin + Wait;
39
40 fn busy(&mut self) -> &mut Self::Busy;
41
42 fn busy_when(&self) -> embedded_hal::digital::PinState;
47}
48
49pub trait DelayHw {
51 type Delay: DelayNs;
52
53 fn delay(&mut self) -> &mut Self::Delay;
54}
55
56pub(crate) trait BusyWait: ErrorHw {
58 async fn wait_if_busy(&mut self) -> Result<(), Self::Error>;
62}
63
64pub(crate) trait CommandDataSend: SpiHw + ErrorHw {
66 async fn send(
68 &mut self,
69 spi: &mut Self::Spi,
70 command: u8,
71 data: &[u8],
72 ) -> Result<(), Self::Error>;
73
74 async fn send_iter<I: IntoIterator<Item = u8>>(
75 &mut self,
76 spi: &mut Self::Spi,
77 command: u8,
78 iter: Option<I>,
79 ) -> Result<(), Self::Error>;
80}
81
82impl<HW> BusyWait for HW
83where
84 HW: BusyHw + ErrorHw,
85 <HW as ErrorHw>::Error: From<<HW::Busy as PinErrorType>::Error>,
86{
87 async fn wait_if_busy(&mut self) -> Result<(), HW::Error> {
88 let busy_when = self.busy_when();
89 let busy = self.busy();
90 match busy_when {
91 PinState::High => {
92 if busy.is_high()? {
93 trace!("Waiting for busy EPD");
94 busy.wait_for_low().await?;
95 }
96 }
97 PinState::Low => {
98 if busy.is_low()? {
99 trace!("Waiting for busy EPD");
100 busy.wait_for_high().await?;
101 }
102 }
103 };
104 Ok(())
105 }
106}
107
108impl<HW> CommandDataSend for HW
109where
110 HW: DcHw + BusyHw + BusyWait + SpiHw + ErrorHw,
111 HW::Error: From<<HW::Spi as SpiErrorType>::Error>
112 + From<<HW::Dc as PinErrorType>::Error>
113 + From<<HW::Busy as PinErrorType>::Error>,
114{
115 async fn send(
116 &mut self,
117 spi: &mut Self::Spi,
118 command: u8,
119 data: &[u8],
120 ) -> Result<(), Self::Error> {
121 trace!("Sending EPD command: 0x{:02x}", command);
122 self.wait_if_busy().await?;
123
124 self.dc().set_low()?;
125 spi.write(&[command]).await?;
126
127 if data.len() > 0 {
128 self.dc().set_high()?;
129 spi.write(data).await?;
130 }
131
132 Ok(())
133 }
134
135 async fn send_iter<I: IntoIterator<Item = u8>>(
136 &mut self,
137 spi: &mut Self::Spi,
138 command: u8,
139 iter: Option<I>,
140 ) -> Result<(), Self::Error> {
141 trace!("Sending EPD command: 0x{:02x}", command);
142 self.wait_if_busy().await?;
143
144 self.dc().set_low()?;
145 spi.write(&[command]).await?;
146
147 if let Some(data) = iter {
148 self.dc().set_high()?;
149 for byte in data {
150 spi.write(&[byte]).await?;
151 }
152 }
153
154 Ok(())
155 }
156}