From 488b41d0f0f3810ba65301624fa4a075c255c2c9 Mon Sep 17 00:00:00 2001 From: ZacJW Date: Sun, 14 Dec 2025 22:21:13 +0000 Subject: [PATCH] Started driver implementation --- .gitignore | 1 + Cargo.lock | 16 ++++++++ Cargo.toml | 7 ++++ src/lib.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5f6495c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "zel-gdeq031t10" +version = "0.1.0" +dependencies = [ + "embedded-hal", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f6f4648 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "zel-gdeq031t10" +version = "0.1.0" +edition = "2024" + +[dependencies] +embedded-hal = "1.0.0" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b1ef66f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,110 @@ +#![no_std] + +use embedded_hal::{ + delay::DelayNs, + digital::{InputPin, OutputPin}, spi::{Operation, SpiDevice}, +}; + +const EPD_WIDTH: u32 = 240; +const EPD_HEIGHT: u32 = 320; +const EPD_ARRAY: u32 = EPD_WIDTH * EPD_HEIGHT / 8; + +pub struct EPaperDisplay { + busy: BusyPin, + reset: ResetPin, + dc: DcPin, + spi: SpiDevice, + delay: Delay, +} + +pub enum InitError { + ResetError(ResetError), + WriteError(WriteError), +} + +pub enum WriteError { + DcError(DcError), + SpiError(SpiError), +} + +impl< + BusyPin: InputPin, + ResetPin: OutputPin, + DcPin: OutputPin, + Spi: SpiDevice, + Delay: DelayNs, +> EPaperDisplay +{ + pub fn new( + busy: BusyPin, + reset: ResetPin, + dc: DcPin, + spi: Spi, + delay: Delay, + ) -> Result> { + let mut display = Self { + busy, + reset, + dc, + spi, + delay, + }; + display.init().map(|_| display) + } + + fn write_command(&mut self, byte: u8) -> Result<(), WriteError> { + self.dc.set_low().map_err(WriteError::DcError)?; + self.spi.transaction(&mut [ + Operation::DelayNs(60), + Operation::Write(&[byte]), + Operation::DelayNs(20), + ]).map_err(WriteError::SpiError)?; + self.delay.delay_ns(40); + Ok(()) + } + + fn write_data(&mut self, byte: u8) -> Result<(), WriteError> { + self.dc.set_high().map_err(WriteError::DcError)?; + self.spi.transaction(&mut [ + Operation::DelayNs(60), + Operation::Write(&[byte]), + Operation::DelayNs(20), + ]).map_err(WriteError::SpiError)?; + 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> { + 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(); + } +}