use crate::traits::Error;
use core::marker::PhantomData;
use embedded_hal::{
blocking::{delay::*, serial::Write},
digital::v2::*,
serial::Read,
};
pub(crate) struct DisplayInterface<SERIAL, WAKE, RST> {
_serial: PhantomData<SERIAL>,
wake: WAKE,
rst: RST,
}
impl<E, F, G, SERIAL, WAKE, RST> DisplayInterface<SERIAL, WAKE, RST>
where
SERIAL: Write<u8, Error = F> + Read<u8, Error = E>,
WAKE: OutputPin<Error = G>,
RST: OutputPin<Error = G>,
{
pub fn new(wake: WAKE, rst: RST) -> Self {
DisplayInterface {
_serial: PhantomData::default(),
wake,
rst,
}
}
pub(crate) fn data(&mut self, serial: &mut SERIAL, data: &[u8]) -> Result<(), Error<E, F, G>> {
self.write(serial, data)
}
pub(crate) fn read_serial(
&mut self,
serial: &mut SERIAL,
data: &mut [u8],
) -> Result<(), Error<E, F, G>> {
self.read(serial, data)
}
pub(crate) fn read(
&mut self,
serial: &mut SERIAL,
data: &mut [u8],
) -> Result<(), Error<E, F, G>> {
for item in data.iter_mut() {
match serial.read() {
Ok(byte) => *item = byte,
Err(_e) => {} };
}
Ok(())
}
fn write(&mut self, serial: &mut SERIAL, data: &[u8]) -> Result<(), Error<E, F, G>> {
serial.bwrite_all(data).map_err(Error::SerialW)
}
pub(crate) fn reset<DELAY: DelayMs<u16>>(
&mut self,
delay: &mut DELAY,
) -> Result<(), Error<E, F, G>> {
self.rst.set_low().map_err(Error::GpioE)?;
delay.delay_ms(255);
self.rst.set_high().map_err(Error::GpioE)?;
delay.delay_ms(3000);
self.rst.set_low().map_err(Error::GpioE)?;
delay.delay_ms(255);
Ok(())
}
pub(crate) fn wake<DELAY: DelayMs<u16>>(
&mut self,
delay: &mut DELAY,
) -> Result<(), Error<E, F, G>> {
self.wake.set_low().map_err(Error::GpioE)?;
delay.delay_ms(255);
self.wake.set_high().map_err(Error::GpioE)?;
delay.delay_ms(255);
self.wake.set_low().map_err(Error::GpioE)?;
delay.delay_ms(255);
Ok(())
}
}