display_interface/
lib.rs

1//! A generic display interface
2//!
3//! This crate contains an error type and traits to implement for bus interface drivers drivers to
4//! be consumed by display drivers. It abstracts over the different communication methods available
5//! to drive a display and allows a driver writer to focus on driving the display itself and only
6//! have to implement a single interface.
7
8#![no_std]
9#![allow(async_fn_in_trait)]
10
11pub mod prelude;
12
13/// A ubiquitous error type for all kinds of problems which could happen when communicating with a
14/// display
15#[derive(Clone, Debug)]
16#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
17#[non_exhaustive]
18pub enum DisplayError {
19    /// Invalid data format selected for interface selected
20    InvalidFormatError,
21    /// Unable to write to bus
22    BusWriteError,
23    /// Unable to assert or de-assert data/command switching signal
24    DCError,
25    /// Unable to assert chip select signal
26    CSError,
27    /// The requested DataFormat is not implemented by this display interface implementation
28    DataFormatNotImplemented,
29    /// Unable to assert or de-assert reset signal
30    RSError,
31    /// Attempted to write to a non-existing pixel outside the display's bounds
32    OutOfBoundsError,
33}
34
35/// DI specific data format wrapper around slices of various widths
36/// Display drivers need to implement non-trivial conversions (e.g. with padding)
37/// as the hardware requires.
38#[non_exhaustive]
39pub enum DataFormat<'a> {
40    /// Slice of unsigned bytes
41    U8(&'a [u8]),
42    /// Slice of unsigned 16bit values with the same endianness as the system, not recommended
43    U16(&'a [u16]),
44    /// Slice of unsigned 16bit values to be sent in big endian byte order
45    U16BE(&'a mut [u16]),
46    /// Slice of unsigned 16bit values to be sent in little endian byte order
47    U16LE(&'a mut [u16]),
48    /// Iterator over unsigned bytes
49    U8Iter(&'a mut dyn Iterator<Item = u8>),
50    /// Iterator over unsigned 16bit values to be sent in big endian byte order
51    U16BEIter(&'a mut dyn Iterator<Item = u16>),
52    /// Iterator over unsigned 16bit values to be sent in little endian byte order
53    U16LEIter(&'a mut dyn Iterator<Item = u16>),
54}
55
56/// This trait implements a write-only interface for a display which has separate data and command
57/// modes. It is the responsibility of implementations to activate the correct mode in their
58/// implementation when corresponding method is called.
59pub trait WriteOnlyDataCommand {
60    /// Send a batch of commands to display
61    fn send_commands(&mut self, cmd: DataFormat<'_>) -> Result<(), DisplayError>;
62
63    /// Send pixel data to display
64    fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError>;
65}
66
67/// This trait implements a write-only interface for a display which has separate data and command
68/// modes. It is the responsibility of implementations to activate the correct mode in their
69/// implementation when corresponding method is called.
70pub trait AsyncWriteOnlyDataCommand {
71    /// Send a batch of commands to display
72    async fn send_commands(&mut self, cmd: DataFormat<'_>) -> Result<(), DisplayError>;
73
74    /// Send pixel data to display
75    async fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError>;
76}