zel-gdeq031t10/src/lib.rs

187 lines
5.4 KiB
Rust
Raw Normal View History

2025-12-14 22:21:13 +00:00
#![no_std]
2025-12-15 16:56:34 +00:00
//! This library is an embedded-hal based driver implementation for the GDEQ031T10 e-paper display.
//!
//! **Note**: 9-bit SPI mode (where the data/command is designated with an extra bit for each byte)
//! is not supported by this driver. Only the mode with a dedicated data/command line is supported.
2025-12-14 22:21:13 +00:00
use embedded_hal::{
delay::DelayNs,
2025-12-15 11:59:07 +00:00
digital::{InputPin, OutputPin},
spi::{Operation, SpiDevice},
2025-12-14 22:21:13 +00:00
};
2025-12-15 11:59:07 +00:00
const EPD_WIDTH: usize = 240;
const EPD_HEIGHT: usize = 320;
const EPD_ARRAY: usize = EPD_WIDTH * EPD_HEIGHT / 8;
2025-12-14 22:21:13 +00:00
2025-12-15 16:56:34 +00:00
/// The display driver for the GDEQ031T10 e-paper display.
2025-12-14 22:21:13 +00:00
pub struct EPaperDisplay<BusyPin, ResetPin, DcPin, SpiDevice, Delay> {
busy: BusyPin,
reset: ResetPin,
dc: DcPin,
spi: SpiDevice,
delay: Delay,
}
2025-12-15 16:56:34 +00:00
/// An error that may occur during display initialisation.
2025-12-14 22:21:13 +00:00
pub enum InitError<ResetError, DcError, SpiError> {
2025-12-15 16:56:34 +00:00
/// This variant is for any error with setting the level of the reset pin.
///
/// This variant may be uninhabited if the `ResetPin` type parameter for [EPaperDisplay] is one that
/// doesn't produce errors.
2025-12-14 22:21:13 +00:00
ResetError(ResetError),
2025-12-15 16:56:34 +00:00
/// This variant is for errors when attempting to communicate with the display controller after reset.
2025-12-14 22:21:13 +00:00
WriteError(WriteError<DcError, SpiError>),
}
2025-12-15 16:56:34 +00:00
/// An error when trying to communicate with the display controller.
2025-12-14 22:21:13 +00:00
pub enum WriteError<DcError, SpiError> {
2025-12-15 16:56:34 +00:00
/// This variant is for any error with setting the level of the DC (Data/Command) pin.
///
/// This variant may be uninhabited if the `DcPin` type parameter for [EPaperDisplay] is one that
/// doesn't produce errors.
2025-12-14 22:21:13 +00:00
DcError(DcError),
2025-12-15 16:56:34 +00:00
/// This variant is for errors produced by the SPI driver.
2025-12-14 22:21:13 +00:00
SpiError(SpiError),
}
2025-12-15 11:59:07 +00:00
pub struct Frame([u8; EPD_ARRAY]);
impl Frame {
pub const fn new_white() -> Self {
Self([0xff; EPD_ARRAY])
}
pub const fn new_black() -> Self {
Self([0x00; EPD_ARRAY])
}
}
pub struct DoubleFrame {
old: Frame,
new: Frame,
}
pub enum PixelColour {
Black = 0,
White = 1,
}
impl DoubleFrame {
pub fn new(old: Frame, new: Frame) -> Self {
Self { old, new }
}
pub fn draw_pixel(&mut self, x: usize, y: usize, colour: PixelColour) {
}
}
impl<BusyPin: InputPin, ResetPin: OutputPin, DcPin: OutputPin, Spi: SpiDevice, Delay: DelayNs>
EPaperDisplay<BusyPin, ResetPin, DcPin, Spi, Delay>
2025-12-14 22:21:13 +00:00
{
pub fn new(
busy: BusyPin,
reset: ResetPin,
dc: DcPin,
spi: Spi,
delay: Delay,
) -> Result<Self, InitError<ResetPin::Error, DcPin::Error, Spi::Error>> {
let mut display = Self {
busy,
reset,
dc,
spi,
delay,
};
display.init().map(|_| display)
}
fn write_command(&mut self, byte: u8) -> Result<(), WriteError<DcPin::Error, Spi::Error>> {
self.dc.set_low().map_err(WriteError::DcError)?;
2025-12-15 11:59:07 +00:00
self.spi
.transaction(&mut [
Operation::DelayNs(60),
Operation::Write(&[byte]),
Operation::DelayNs(20),
])
.map_err(WriteError::SpiError)?;
2025-12-14 22:21:13 +00:00
self.delay.delay_ns(40);
Ok(())
}
fn write_data(&mut self, byte: u8) -> Result<(), WriteError<DcPin::Error, Spi::Error>> {
self.dc.set_high().map_err(WriteError::DcError)?;
2025-12-15 11:59:07 +00:00
self.spi
.transaction(&mut [
Operation::DelayNs(60),
Operation::Write(&[byte]),
Operation::DelayNs(20),
])
.map_err(WriteError::SpiError)?;
2025-12-14 22:21:13 +00:00
self.delay.delay_ns(40);
Ok(())
}
fn wait_for_display(&mut self) {
while self.busy.is_low().expect("Failed to read busy pin") {}
}
fn init(&mut self) -> Result<(), InitError<ResetPin::Error, DcPin::Error, Spi::Error>> {
self.reset.set_low().map_err(InitError::ResetError)?;
self.delay.delay_ms(10);
self.reset.set_high().map_err(InitError::ResetError)?;
self.delay.delay_ms(10);
self.write_command(0x00).map_err(InitError::WriteError)?;
self.write_data(0x1f).map_err(InitError::WriteError)?;
self.write_command(0x04).map_err(InitError::WriteError)?;
self.wait_for_display();
Ok(())
}
fn clear_display(&mut self) {
self.write_command(0x10);
for _ in 0..EPD_ARRAY {
self.write_data(0xff);
}
self.write_command(0x13);
for _ in 0..EPD_ARRAY {
self.write_data(0xff);
}
self.write_command(0x12);
self.delay.delay_ms(1);
self.wait_for_display();
}
2025-12-15 11:59:07 +00:00
pub fn draw_full_frame(&mut self, double_frame: &mut DoubleFrame) {
self.write_command(0x10);
for byte in double_frame.old.0 {
self.write_data(byte);
}
self.write_command(0x13);
for byte in double_frame.new.0 {
self.write_data(byte);
}
self.write_command(0x12);
double_frame.old.0 = double_frame.new.0;
self.delay.delay_ms(1);
self.wait_for_display();
}
2025-12-15 16:19:30 +00:00
/// Puts the display into deep sleep mode which effectively deinitialises the display controller.
///
/// Reinitialising the display with [EPaperDisplay::new] is sufficient to wake the display back up.
///
/// All display controller registers will be reset by this process.
pub fn deep_sleep(mut self) {
self.write_command(0x07);
self.write_data(0xA5);
}
2025-12-14 22:21:13 +00:00
}