//! A simple Driver for the Waveshare 1.54" E-Ink Display via SPI //! //! # Example for the 1.54 in E-Ink Display //! //!```rust, no_run //!# use embedded_hal_mock::*; //!# fn main() -> Result<(), MockError> { //!use embedded_graphics::{ //! pixelcolor::BinaryColor::On as Black, prelude::*, primitives::{Line, PrimitiveStyleBuilder}, //!}; //!use epd_waveshare::{epd1in54::*, prelude::*}; //!# //!# let expectations = []; //!# let mut spi = spi::Mock::new(&expectations); //!# let expectations = []; //!# let cs_pin = pin::Mock::new(&expectations); //!# let busy_in = pin::Mock::new(&expectations); //!# let dc = pin::Mock::new(&expectations); //!# let rst = pin::Mock::new(&expectations); //!# let mut delay = delay::MockNoop::new(); //! //!// Setup EPD //!let mut epd = Epd1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?; //! //!// Use display graphics from embedded-graphics //!let mut display = Display1in54::default(); //! //!// Use embedded graphics for drawing a line //!let style = PrimitiveStyleBuilder::new() //! .stroke_color(Black) //! .stroke_width(1) //! .build(); //!let _ = Line::new(Point::new(0, 120), Point::new(0, 295)) //! .into_styled(style) //! .draw(&mut display); //! //!// Display updated frame //!epd.update_frame(&mut spi, &display.buffer(), &mut delay)?; //!epd.display_frame(&mut spi, &mut delay)?; //! //!// Set the EPD to sleep //!epd.sleep(&mut spi, &mut delay)?; //!# Ok(()) //!# } //!``` /// Width of the display pub const WIDTH: u32 = 200; /// Height of the display pub const HEIGHT: u32 = 200; /// Default Background Color pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White; //const DPI: u16 = 184; const IS_BUSY_LOW: bool = false; use embedded_hal::{ blocking::{delay::*, spi::Write}, digital::v2::*, }; use crate::type_a::{ command::Command, constants::{LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}, }; use crate::color::Color; use crate::traits::{RefreshLut, WaveshareDisplay}; use crate::interface::DisplayInterface; #[cfg(feature = "graphics")] mod graphics; #[cfg(feature = "graphics")] pub use crate::epd1in54::graphics::Display1in54; /// Epd1in54 driver pub struct Epd1in54 { /// SPI interface: DisplayInterface, /// Color background_color: Color, /// Refresh LUT refresh: RefreshLut, } impl Epd1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, DELAY: DelayMs, { fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { self.interface.reset(delay, 10); // 3 Databytes: // A[7:0] // 0.. A[8] // 0.. B[2:0] // Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?) self.interface.cmd_with_data( spi, Command::DriverOutputControl, &[HEIGHT as u8, (HEIGHT >> 8) as u8, 0x00], )?; // 3 Databytes: (and default values from datasheet and arduino) // 1 .. A[6:0] = 0xCF | 0xD7 // 1 .. B[6:0] = 0xCE | 0xD6 // 1 .. C[6:0] = 0x8D | 0x9D //TODO: test self.interface .cmd_with_data(spi, Command::BoosterSoftStartControl, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM self.interface .cmd_with_data(spi, Command::WriteVcomRegister, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate self.interface .cmd_with_data(spi, Command::SetDummyLinePeriod, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line self.interface .cmd_with_data(spi, Command::SetGateLineWidth, &[0x08])?; // One Databyte with default value 0x03 // -> address: x increment, y increment, address counter is updated in x direction self.interface .cmd_with_data(spi, Command::DataEntryModeSetting, &[0x03])?; self.set_lut(spi, None)?; self.wait_until_idle(); Ok(()) } } impl WaveshareDisplay for Epd1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, DELAY: DelayMs, { type DisplayColor = Color; fn width(&self) -> u32 { WIDTH } fn height(&self) -> u32 { HEIGHT } fn new( spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { let interface = DisplayInterface::new(cs, busy, dc, rst); let mut epd = Epd1in54 { interface, background_color: DEFAULT_BACKGROUND_COLOR, refresh: RefreshLut::Full, }; epd.init(spi, delay)?; Ok(epd) } fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { self.init(spi, delay) } fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> { self.wait_until_idle(); // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode //TODO: is 0x00 needed here or would 0x01 be even more efficient? self.interface .cmd_with_data(spi, Command::DeepSleepMode, &[0x00])?; Ok(()) } fn update_frame( &mut self, spi: &mut SPI, buffer: &[u8], _delay: &mut DELAY, ) -> Result<(), SPI::Error> { self.wait_until_idle(); self.use_full_frame(spi)?; self.interface .cmd_with_data(spi, Command::WriteRam, buffer)?; Ok(()) } //TODO: update description: last 3 bits will be ignored for width and x_pos fn update_partial_frame( &mut self, spi: &mut SPI, buffer: &[u8], x: u32, y: u32, width: u32, height: u32, ) -> Result<(), SPI::Error> { self.wait_until_idle(); self.set_ram_area(spi, x, y, x + width, y + height)?; self.set_ram_counter(spi, x, y)?; self.interface .cmd_with_data(spi, Command::WriteRam, buffer)?; Ok(()) } fn display_frame(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> { self.wait_until_idle(); // enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version) //TODO: test control_1 or control_2 with default value 0xFF (from the datasheet) self.interface .cmd_with_data(spi, Command::DisplayUpdateControl2, &[0xC4])?; self.interface.cmd(spi, Command::MasterActivation)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send self.interface.cmd(spi, Command::Nop)?; Ok(()) } fn update_and_display_frame( &mut self, spi: &mut SPI, buffer: &[u8], delay: &mut DELAY, ) -> Result<(), SPI::Error> { self.update_frame(spi, buffer, delay)?; self.display_frame(spi, delay)?; Ok(()) } fn clear_frame(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> { self.wait_until_idle(); self.use_full_frame(spi)?; // clear the ram with the background color let color = self.background_color.get_byte_value(); self.interface.cmd(spi, Command::WriteRam)?; self.interface .data_x_times(spi, color, WIDTH / 8 * HEIGHT)?; Ok(()) } fn set_background_color(&mut self, background_color: Color) { self.background_color = background_color; } fn background_color(&self) -> &Color { &self.background_color } fn set_lut( &mut self, spi: &mut SPI, refresh_rate: Option, ) -> Result<(), SPI::Error> { if let Some(refresh_lut) = refresh_rate { self.refresh = refresh_lut; } match self.refresh { RefreshLut::Full => self.set_lut_helper(spi, &LUT_FULL_UPDATE), RefreshLut::Quick => self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE), } } fn is_busy(&self) -> bool { self.interface.is_busy(IS_BUSY_LOW) } } impl Epd1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, DELAY: DelayMs, { fn wait_until_idle(&mut self) { let _ = self.interface.wait_until_idle(IS_BUSY_LOW); } pub(crate) fn use_full_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // choose full frame/ram self.set_ram_area(spi, 0, 0, WIDTH - 1, HEIGHT - 1)?; // start from the beginning self.set_ram_counter(spi, 0, 0) } pub(crate) fn set_ram_area( &mut self, spi: &mut SPI, start_x: u32, start_y: u32, end_x: u32, end_y: u32, ) -> Result<(), SPI::Error> { self.wait_until_idle(); assert!(start_x < end_x); assert!(start_y < end_y); // x is positioned in bytes, so the last 3 bits which show the position inside a byte in the ram // aren't relevant self.interface.cmd_with_data( spi, Command::SetRamXAddressStartEndPosition, &[(start_x >> 3) as u8, (end_x >> 3) as u8], )?; // 2 Databytes: A[7:0] & 0..A[8] for each - start and end self.interface.cmd_with_data( spi, Command::SetRamYAddressStartEndPosition, &[ start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8, ], )?; Ok(()) } pub(crate) fn set_ram_counter( &mut self, spi: &mut SPI, x: u32, y: u32, ) -> Result<(), SPI::Error> { self.wait_until_idle(); // x is positioned in bytes, so the last 3 bits which show the position inside a byte in the ram // aren't relevant self.interface .cmd_with_data(spi, Command::SetRamXAddressCounter, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] self.interface.cmd_with_data( spi, Command::SetRamYAddressCounter, &[y as u8, (y >> 8) as u8], )?; Ok(()) } fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { self.wait_until_idle(); assert!(buffer.len() == 30); self.interface .cmd_with_data(spi, Command::WriteLutRegister, buffer)?; Ok(()) } } #[cfg(test)] mod tests { use super::*; #[test] fn epd_size() { assert_eq!(WIDTH, 200); assert_eq!(HEIGHT, 200); assert_eq!(DEFAULT_BACKGROUND_COLOR, Color::White); } }