use crate::qspi::{Frame, PixelMode};
use embedded_hal::digital::OutputPin;
pub trait Interface {
type Error;
const SPI_RAM_WRITE: bool = false;
fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error>;
fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error>;
}
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[allow(async_fn_in_trait)]
pub trait AsyncInterface {
type Error;
const SPI_RAM_WRITE: bool = false;
async fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error>;
async fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error>;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SpiError<S, P> {
Spi(S),
Dc(P),
}
#[derive(Debug)]
pub struct SpiInterface<SPI, DC> {
spi: SPI,
dc: DC,
}
impl<SPI, DC> SpiInterface<SPI, DC> {
pub fn new(spi: SPI, dc: DC) -> Self {
Self { spi, dc }
}
pub fn release(self) -> (SPI, DC) {
(self.spi, self.dc)
}
}
impl<SPI, DC> Interface for SpiInterface<SPI, DC>
where
SPI: embedded_hal::spi::SpiDevice,
DC: OutputPin,
{
type Error = SpiError<SPI::Error, DC::Error>;
const SPI_RAM_WRITE: bool = true;
fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error> {
self.dc.set_low().map_err(SpiError::Dc)?;
self.spi.write(&[command]).map_err(SpiError::Spi)?;
if !params.is_empty() {
self.dc.set_high().map_err(SpiError::Dc)?;
self.spi.write(params).map_err(SpiError::Spi)?;
}
Ok(())
}
fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error> {
self.write_command(command, data)
}
}
#[cfg(feature = "async")]
impl<SPI, DC> AsyncInterface for SpiInterface<SPI, DC>
where
SPI: embedded_hal_async::spi::SpiDevice,
DC: OutputPin,
{
type Error = SpiError<SPI::Error, DC::Error>;
const SPI_RAM_WRITE: bool = true;
async fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error> {
self.dc.set_low().map_err(SpiError::Dc)?;
self.spi.write(&[command]).await.map_err(SpiError::Spi)?;
if !params.is_empty() {
self.dc.set_high().map_err(SpiError::Dc)?;
self.spi.write(params).await.map_err(SpiError::Spi)?;
}
Ok(())
}
async fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error> {
AsyncInterface::write_command(self, command, data).await
}
}
#[derive(Debug)]
pub struct FramedSpiInterface<SPI> {
spi: SPI,
}
impl<SPI> FramedSpiInterface<SPI> {
pub fn new(spi: SPI) -> Self {
Self { spi }
}
pub fn release(self) -> SPI {
self.spi
}
}
impl<SPI> Interface for FramedSpiInterface<SPI>
where
SPI: embedded_hal::spi::SpiDevice,
{
type Error = SPI::Error;
const SPI_RAM_WRITE: bool = true;
fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error> {
use embedded_hal::spi::Operation;
let header = Frame::command(command).header();
self.spi
.transaction(&mut [Operation::Write(&header), Operation::Write(params)])
}
fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error> {
use embedded_hal::spi::Operation;
let header = Frame::pixels(command, PixelMode::Single).header();
self.spi
.transaction(&mut [Operation::Write(&header), Operation::Write(data)])
}
}
#[cfg(feature = "async")]
impl<SPI> AsyncInterface for FramedSpiInterface<SPI>
where
SPI: embedded_hal_async::spi::SpiDevice,
{
type Error = SPI::Error;
const SPI_RAM_WRITE: bool = true;
async fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), Self::Error> {
use embedded_hal_async::spi::Operation;
let header = Frame::command(command).header();
self.spi
.transaction(&mut [Operation::Write(&header), Operation::Write(params)])
.await
}
async fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error> {
use embedded_hal_async::spi::Operation;
let header = Frame::pixels(command, PixelMode::Single).header();
self.spi
.transaction(&mut [Operation::Write(&header), Operation::Write(data)])
.await
}
}