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, SpiDevice},
11};
12
13use crate::epd2in9::RefreshMode;
14
15pub mod buffer;
16pub mod epd2in9;
17
18mod log;
19
20#[allow(async_fn_in_trait)]
21pub trait Epd<HW>
22where
23    HW: EpdHw,
24{
25    type RefreshMode;
26    type Command;
27    type Buffer;
28
29    /// Creates a buffer for use with this display.
30    fn new_buffer(&self) -> Self::Buffer;
31
32    fn width(&self) -> u32;
33
34    fn height(&self) -> u32;
35
36    /// Initialise the display. This must be called before any other operations.
37    async fn init(&mut self, spi: &mut HW::Spi, mode: RefreshMode) -> Result<(), HW::Error>;
38
39    /// Sets the refresh mode for the display.
40    async fn set_refresh_mode(
41        &mut self,
42        spi: &mut HW::Spi,
43        mode: Self::RefreshMode,
44    ) -> Result<(), HW::Error>;
45
46    /// Hardware reset the display. The display must be reinitialised after calling this.
47    async fn reset(&mut self) -> Result<(), HW::Error>;
48
49    /// Puts the display to sleep.
50    async fn sleep(&mut self, spi: &mut HW::Spi) -> Result<(), HW::Error>;
51
52    /// Wakes and re-initialises the display if it's asleep.
53    async fn wake(&mut self, spi: &mut HW::Spi) -> Result<(), HW::Error>;
54
55    /// Writes the buffers data to the display and displays it.
56    async fn display_buffer(
57        &mut self,
58        spi: &mut HW::Spi,
59        buffer: &Self::Buffer,
60    ) -> Result<(), HW::Error>;
61
62    /// Sets the window to write to during a call to [write_image]. This can enable partial writes
63    /// to a subsection of the display.
64    async fn set_window(&mut self, spi: &mut HW::Spi, shape: Rectangle) -> Result<(), HW::Error>;
65
66    /// Sets the cursor position for where the next byte of image data will be written.
67    async fn set_cursor(
68        &mut self,
69        spi: &mut HW::Spi,
70        position: Point,
71    ) -> Result<(), <HW as EpdHw>::Error>;
72
73    /// Writes raw image data, starting at the current cursor position and auto-incrementing x then y within the current window.
74    async fn write_image(&mut self, spi: &mut HW::Spi, image: &[u8]) -> Result<(), HW::Error>;
75
76    /// Updates (refreshes) the display based on the RAM. Note that this can be stateful. For
77    /// example, on the Epd2in9 display, there are two RAM buffers. Calling this function swaps
78    /// the active buffer. Consider this scenario:
79    ///
80    /// 1. [write_image] is used to turn the RAM all white.
81    /// 2. [update_display] is called, which refreshes the display to be all white.
82    /// 3. [write_image] is used to turn the RAM all black.
83    /// 4. [update_display] is called, which refreshes the display to be all black.
84    /// 5. [update_display] is called again, which refreshes the display to be all white again.
85    async fn update_display(&mut self, spi: &mut HW::Spi) -> Result<(), HW::Error>;
86
87    /// Send the following command and data to the display. Waits until the display is no longer busy before sending.
88    async fn send(
89        &mut self,
90        spi: &mut HW::Spi,
91        command: Self::Command,
92        data: &[u8],
93    ) -> Result<(), HW::Error>;
94
95    /// Waits for the current operation to complete if the display is busy.
96    /// Note that this will wait forever if the display is asleep.
97    async fn wait_if_busy(&mut self) -> Result<(), HW::Error>;
98}
99
100/// Provides access to the hardware needed to control an EPD.
101pub trait EpdHw {
102    type Spi: SpiDevice;
103    type Dc: OutputPin;
104    type Reset: OutputPin;
105    type Busy: InputPin + Wait;
106    type Delay: DelayNs;
107    type Error: CoreError
108        + From<<Self::Spi as SpiErrorType>::Error>
109        + From<<Self::Dc as PinErrorType>::Error>
110        + From<<Self::Reset as PinErrorType>::Error>
111        + From<<Self::Busy as PinErrorType>::Error>;
112
113    fn dc(&mut self) -> &mut Self::Dc;
114    fn reset(&mut self) -> &mut Self::Reset;
115    fn busy(&mut self) -> &mut Self::Busy;
116    fn delay(&mut self) -> &mut Self::Delay;
117}