epd_waveshare_async/
lib.rs

1#![no_std]
2
3use core::error::Error as CoreError;
4
5use embedded_graphics::{prelude::Point, primitives::Rectangle};
6use embedded_hal::digital::{ErrorType as PinErrorType, InputPin, OutputPin};
7use embedded_hal_async::{
8    delay::DelayNs,
9    digital::Wait,
10    spi::{ErrorType as SpiErrorType, SpiBus},
11};
12
13pub mod buffer;
14pub mod epd2in9;
15
16#[derive(Debug, Copy, Clone, PartialEq)]
17pub enum Error {
18    InvalidArgument,
19}
20
21#[allow(async_fn_in_trait)]
22pub trait Epd<HW>
23where
24    HW: EpdHw,
25{
26    type Command;
27    type Buffer;
28
29    /// Initialise the display. This must be called before any other operations.
30    async fn init(&mut self, lut: &[u8]) -> Result<(), HW::Error>;
31
32    /// Clear the display. This may or may not clear internal RAM.
33    async fn clear(&mut self) -> Result<(), HW::Error>;
34
35    /// Hardware reset the display. The display must be reinitialised after calling this.
36    async fn reset(&mut self) -> Result<(), HW::Error>;
37
38    /// Puts the display to sleep.
39    async fn sleep(&mut self) -> Result<(), HW::Error>;
40
41    /// Wakes and re-initialises the display if it's asleep.
42    async fn wake(&mut self) -> Result<(), HW::Error>;
43
44    /// Writes the buffers data to the display and displays it.
45    async fn display_buffer(&mut self, buffer: &Self::Buffer) -> Result<(), HW::Error>;
46
47    /// Sets the window to write to during a call to [write_image]. This can enable partial writes
48    /// to a subsection of the display.
49    async fn set_window(&mut self, shape: Rectangle) -> Result<(), HW::Error>;
50
51    /// Sets the cursor position for where the next byte of image data will be written.
52    async fn set_cursor(&mut self, position: Point) -> Result<(), <HW as EpdHw>::Error>;
53
54    /// Writes raw image data, starting at the current cursor position and auto-incrementing x then y within the current window.
55    async fn write_image(&mut self, image: &[u8]) -> Result<(), HW::Error>;
56
57    /// Updates (refreshes) the display to match the latest data that has been written to RAM.
58    async fn update_display(&mut self) -> Result<(), HW::Error>;
59
60    /// Send the following command and data to the display. Waits until the display is no longer busy before sending.
61    async fn send(&mut self, command: Self::Command, data: &[u8]) -> Result<(), HW::Error>;
62
63    /// Waits for the current operation to complete if the display is busy.
64    /// Note that this will wait forever if the display is asleep.
65    async fn wait_if_busy(&mut self) -> Result<(), HW::Error>;
66}
67
68/// Provides access to the hardware needed to control an EPD.
69pub trait EpdHw {
70    type Spi: SpiBus;
71    type Cs: OutputPin;
72    type Dc: OutputPin;
73    type Reset: OutputPin;
74    type Busy: InputPin + Wait;
75    type Delay: DelayNs;
76    type Error: CoreError
77        + From<<Self::Spi as SpiErrorType>::Error>
78        + From<<Self::Cs as PinErrorType>::Error>
79        + From<<Self::Dc as PinErrorType>::Error>
80        + From<<Self::Reset as PinErrorType>::Error>
81        + From<<Self::Busy as PinErrorType>::Error>
82        + From<Error>;
83
84    fn spi(&mut self) -> &mut Self::Spi;
85    fn cs(&mut self) -> &mut Self::Cs;
86    fn dc(&mut self) -> &mut Self::Dc;
87    fn reset(&mut self) -> &mut Self::Reset;
88    fn busy(&mut self) -> &mut Self::Busy;
89    fn delay(&mut self) -> &mut Self::Delay;
90}