diff --git a/CHANGELOG.md b/CHANGELOG.md index 37fbb31..bb7d21c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added - -- Added QuickRefresh Trait and implemented it for Epd4in2 in #62 (thanks to @David-OConnor) +- Added QuickRefresh Trait and implemented it for EPD4in2 in #62 (thanks to @David-OConnor) - Added Epd 2in7 (B) support in #60 (thanks to @pjsier) +- Added Epd 7in5 HD support ### Changed diff --git a/README.md b/README.md index 1b72247..5ee6012 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ epd.update_and_display_frame( & mut spi, & display.buffer()) ?; | Device (with Link) | Colors | Flexible Display | Partial Refresh | Supported | Tested | | :---: | --- | :---: | :---: | :---: | :---: | +| [7.5 Inch B/W HD (A)](https://www.waveshare.com/product/displays/e-paper/epaper-1/7.5inch-hd-e-paper-hat.htm) | Black, White | ✕ | ✕ | ✔ | ✔ | | [7.5 Inch B/W V2 (A)](https://www.waveshare.com/product/7.5inch-e-paper-hat.htm) [[1](#1-75-inch-bw-v2-a)] | Black, White | ✕ | ✕ | ✔ | ✔ | | [7.5 Inch B/W (A)](https://www.waveshare.com/product/7.5inch-e-paper-hat.htm) | Black, White | ✕ | ✕ | ✔ | ✔ | | [4.2 Inch B/W (A)](https://www.waveshare.com/product/4.2inch-e-paper-module.htm) | Black, White | ✕ | Not officially [[2](#2-42-inch-e-ink-blackwhite---partial-refresh)] | ✔ | ✔ | diff --git a/src/epd7in5_hd/command.rs b/src/epd7in5_hd/command.rs new file mode 100644 index 0000000..946ace8 --- /dev/null +++ b/src/epd7in5_hd/command.rs @@ -0,0 +1,159 @@ +//! SPI Commands for the Waveshare 7.5" E-Ink Display + +use crate::traits; + +/// EPD7in5 commands +/// +/// Should rarely (never?) be needed directly. +/// +/// For more infos about the addresses and what they are doing look into the PDFs. +#[allow(dead_code)] +#[allow(non_camel_case_types)] +#[derive(Copy, Clone)] +pub(crate) enum Command { + DriverOutputControl = 0x01, + + /// Set gate driving voltage + GateDrivingVoltageControl = 0x03, + + /// Set source driving voltage + SourceDrivingVoltageControl = 0x04, + + SoftStart = 0x0C, + + /// Set the scanning start position of the gate driver. + /// The valid range is from 0 to 679. + GateScanStartPosition = 0x0F, + + /// Deep sleep mode control + DeepSleep = 0x10, + + /// Define data entry sequence + DataEntry = 0x11, + + /// resets the commands and parameters to their S/W Reset default values except R10h-Deep Sleep Mode. + /// During operation, BUSY pad will output high. + /// Note: RAM are unaffected by this command. + SwReset = 0x12, + + /// After this command initiated, HV Ready detection starts. + /// BUSY pad will output high during detection. + /// The detection result can be read from the Status Bit Read (Command 0x2F). + HvReadyDetection = 0x14, + + /// After this command initiated, VCI detection starts. + /// BUSY pad will output high during detection. + /// The detection result can be read from the Status Bit Read (Command 0x2F). + VciDetection = 0x15, + + /// Temperature Sensor Selection + TemperatureSensorControl = 0x18, + + /// Write to temperature register + TemperatureSensorWrite = 0x1A, + + /// Read from temperature register + TemperatureSensorRead = 0x1B, + + /// Write Command to External temperature sensor. + TemperatureSensorWriteExternal = 0x1C, + + /// Activate Display Update Sequence + MasterActivation = 0x20, + + /// RAM content option for Display Update + DisplayUpdateControl1 = 0x21, + + /// Display Update Sequence Option + DisplayUpdateControl2 = 0x22, + + /// After this command, data entries will be written into the BW RAM until another command is written + WriteRamBw = 0x24, + + /// After this command, data entries will be written into the RED RAM until another command is written + WriteRamRed = 0x26, + + /// Fetch data from RAM + ReadRam = 0x27, + + /// Enter VCOM sensing conditions + VcomSense = 0x28, + + /// Enter VCOM sensing conditions + VcomSenseDuration = 0x29, + + /// Program VCOM register into OTP + VcomProgramOtp = 0x2A, + + /// Reduces a glitch when ACVCOM is toggled + VcomControl = 0x2B, + + /// Write VCOM register from MCU interface + VcomWrite = 0x2C, + + /// Read Register for Display Option + OtpRead = 0x2D, + + /// CRC calculation command for OTP content validation + CrcCalculation = 0x34, + + /// CRC Status Read + CrcRead = 0x35, + + /// Program OTP Selection according to the OTP Selection Control + ProgramSelection = 0x36, + + /// Write Register for Display Option + DisplayOptionWrite = 0x37, + + /// Write register for User ID + UserIdWrite = 0x38, + + /// Select border waveform for VBD + VbdControl = 0x3C, + + /// Read RAM Option + ReadRamOption = 0x41, + + /// Specify the start/end positions of the window address in the X direction by an address unit for RAM + SetRamXStartEnd = 0x44, + + /// Specify the start/end positions of the window address in the Y direction by an address unit for RAM + SetRamYStartEnd = 0x45, + + /// Auto write RED RAM for regular pattern + AutoWriteRed = 0x46, + + /// Auto write B/W RAM for regular pattern + AutoWriteBw = 0x47, + + /// Make initial settings for the RAM X address in the address counter (AC) + SetRamXAc = 0x4E, + + /// Make initial settings for the RAM Y address in the address counter (AC) + SetRamYAc = 0x4F, + + /// This command is an empty command; it does not have any effect on the display module. + /// However, it can be used to terminate Frame Memory Write or Read Commands. + Nop = 0x7F, +} + +impl traits::Command for Command { + /// Returns the address of the command + fn address(self) -> u8 { + self as u8 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::traits::Command as CommandTrait; + + #[test] + fn command_addr() { + assert_eq!(Command::MasterActivation.address(), 0x20); + assert_eq!(Command::SwReset.address(), 0x12); + assert_eq!(Command::DisplayUpdateControl2.address(), 0x22); + } +} diff --git a/src/epd7in5_hd/graphics.rs b/src/epd7in5_hd/graphics.rs new file mode 100644 index 0000000..60c36dc --- /dev/null +++ b/src/epd7in5_hd/graphics.rs @@ -0,0 +1,149 @@ +use crate::epd7in5_hd::{DEFAULT_BACKGROUND_COLOR, HEIGHT, WIDTH}; +use crate::graphics::{Display, DisplayRotation}; +use embedded_graphics::pixelcolor::BinaryColor; +use embedded_graphics::prelude::*; + +/// Full size buffer for use with the 7in5 EPD +/// +/// Can also be manually constructed: +/// `buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); WIDTH / 8 * HEIGHT]` +pub struct Display7in5 { + buffer: [u8; WIDTH as usize * HEIGHT as usize / 8], + rotation: DisplayRotation, +} + +impl Default for Display7in5 { + fn default() -> Self { + Display7in5 { + buffer: [DEFAULT_BACKGROUND_COLOR.get_byte_value(); + WIDTH as usize * HEIGHT as usize / 8], + rotation: DisplayRotation::default(), + } + } +} + +impl DrawTarget for Display7in5 { + type Error = core::convert::Infallible; + + fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Self::Error> { + self.draw_helper(WIDTH, HEIGHT, pixel) + } + + fn size(&self) -> Size { + Size::new(WIDTH, HEIGHT) + } +} + +impl Display for Display7in5 { + fn buffer(&self) -> &[u8] { + &self.buffer + } + + fn get_mut_buffer(&mut self) -> &mut [u8] { + &mut self.buffer + } + + fn set_rotation(&mut self, rotation: DisplayRotation) { + self.rotation = rotation; + } + + fn rotation(&self) -> DisplayRotation { + self.rotation + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::color::{Black, Color}; + use crate::epd7in5_hd; + use crate::graphics::{Display, DisplayRotation}; + use embedded_graphics::{primitives::Line, style::PrimitiveStyle}; + + // test buffer length + #[test] + fn graphics_size() { + let display = Display7in5::default(); + assert_eq!(display.buffer().len(), 58080); + } + + // test default background color on all bytes + #[test] + fn graphics_default() { + let display = Display7in5::default(); + for &byte in display.buffer() { + assert_eq!(byte, epd7in5_hd::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_rotation_0() { + let mut display = Display7in5::default(); + + let _ = Line::new(Point::new(0, 0), Point::new(7, 0)) + .into_styled(PrimitiveStyle::with_stroke(Black, 1)) + .draw(&mut display); + + let buffer = display.buffer(); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, epd7in5_hd::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_rotation_90() { + let mut display = Display7in5::default(); + display.set_rotation(DisplayRotation::Rotate90); + + let _ = Line::new(Point::new(0, 872), Point::new(0, 879)) + .into_styled(PrimitiveStyle::with_stroke(Black, 1)) + .draw(&mut display); + + let buffer = display.buffer(); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, epd7in5_hd::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_rotation_180() { + let mut display = Display7in5::default(); + display.set_rotation(DisplayRotation::Rotate180); + + let _ = Line::new(Point::new(872, 527), Point::new(879, 527)) + .into_styled(PrimitiveStyle::with_stroke(Black, 1)) + .draw(&mut display); + + let buffer = display.buffer(); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, epd7in5_hd::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } + + #[test] + fn graphics_rotation_270() { + let mut display = Display7in5::default(); + display.set_rotation(DisplayRotation::Rotate270); + + let _ = Line::new(Point::new(527, 0), Point::new(527, 7)) + .into_styled(PrimitiveStyle::with_stroke(Black, 1)) + .draw(&mut display); + + let buffer = display.buffer(); + + assert_eq!(buffer[0], Color::Black.get_byte_value()); + + for &byte in buffer.iter().skip(1) { + assert_eq!(byte, epd7in5_hd::DEFAULT_BACKGROUND_COLOR.get_byte_value()); + } + } +} diff --git a/src/epd7in5_hd/mod.rs b/src/epd7in5_hd/mod.rs new file mode 100644 index 0000000..ab36a70 --- /dev/null +++ b/src/epd7in5_hd/mod.rs @@ -0,0 +1,260 @@ +//! A simple Driver for the Waveshare 7.5" E-Ink Display (HD) via SPI +//! +//! Color values for this driver are inverted compared to the [EPD 7in5 V2 driver](crate::epd7in5_v2) +//! *EPD 7in5 HD:* White = 1/0xFF, Black = 0/0x00 +//! *EPD 7in5 V2:* White = 0/0x00, Black = 1/0xFF +//! +//! # References +//! +//! - [Datasheet](https://www.waveshare.com/w/upload/2/27/7inch_HD_e-Paper_Specification.pdf) +//! - [Waveshare Python driver](https://github.com/waveshare/e-Paper/blob/master/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in5_HD.py) +//! +use embedded_hal::{ + blocking::{delay::*, spi::Write}, + digital::v2::{InputPin, OutputPin}, +}; + +use crate::color::Color; +use crate::interface::DisplayInterface; +use crate::traits::{InternalWiAdditions, RefreshLut, WaveshareDisplay}; + +pub(crate) mod command; +use self::command::Command; + +#[cfg(feature = "graphics")] +mod graphics; +#[cfg(feature = "graphics")] +pub use self::graphics::Display7in5; + +/// Width of the display +pub const WIDTH: u32 = 880; +/// Height of the display +pub const HEIGHT: u32 = 528; +/// Default Background Color +pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White; // Inverted for HD as compared to 7in5 v2 (HD: 0xFF = White) +const IS_BUSY_LOW: bool = false; + +/// EPD7in5 (HD) driver +/// +pub struct Epd7in5 { + /// Connection Interface + interface: DisplayInterface, + /// Background Color + color: Color, +} + +impl InternalWiAdditions + for Epd7in5 +where + SPI: Write, + CS: OutputPin, + BUSY: InputPin, + DC: OutputPin, + RST: OutputPin, +{ + fn init>( + &mut self, + spi: &mut SPI, + delay: &mut DELAY, + ) -> Result<(), SPI::Error> { + // Reset the device + self.interface.reset(delay, 2); + + // HD procedure as described here: + // https://github.com/waveshare/e-Paper/blob/master/RaspberryPi_JetsonNano/python/lib/waveshare_epd/epd7in5_HD.py + // and as per specs: + // https://www.waveshare.com/w/upload/2/27/7inch_HD_e-Paper_Specification.pdf + + self.wait_until_idle(); + self.command(spi, Command::SwReset)?; + self.wait_until_idle(); + + self.cmd_with_data(spi, Command::AutoWriteRed, &[0xF7])?; + self.wait_until_idle(); + self.cmd_with_data(spi, Command::AutoWriteBw, &[0xF7])?; + self.wait_until_idle(); + + self.cmd_with_data(spi, Command::SoftStart, &[0xAE, 0xC7, 0xC3, 0xC0, 0x40])?; + + self.cmd_with_data(spi, Command::DriverOutputControl, &[0xAF, 0x02, 0x01])?; + + self.cmd_with_data(spi, Command::DataEntry, &[0x01])?; + + self.cmd_with_data(spi, Command::SetRamXStartEnd, &[0x00, 0x00, 0x6F, 0x03])?; + self.cmd_with_data(spi, Command::SetRamYStartEnd, &[0xAF, 0x02, 0x00, 0x00])?; + + self.cmd_with_data(spi, Command::VbdControl, &[0x05])?; + + self.cmd_with_data(spi, Command::TemperatureSensorControl, &[0x80])?; + + self.cmd_with_data(spi, Command::DisplayUpdateControl2, &[0xB1])?; + + self.command(spi, Command::MasterActivation)?; + self.wait_until_idle(); + + self.cmd_with_data(spi, Command::SetRamXAc, &[0x00, 0x00])?; + self.cmd_with_data(spi, Command::SetRamYAc, &[0x00, 0x00])?; + + Ok(()) + } +} + +impl WaveshareDisplay + for Epd7in5 +where + SPI: Write, + CS: OutputPin, + BUSY: InputPin, + DC: OutputPin, + RST: OutputPin, +{ + type DisplayColor = Color; + 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 color = DEFAULT_BACKGROUND_COLOR; + + let mut epd = Epd7in5 { interface, color }; + + 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) -> Result<(), SPI::Error> { + self.wait_until_idle(); + self.cmd_with_data(spi, Command::DeepSleep, &[0x01])?; + Ok(()) + } + + fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { + self.wait_until_idle(); + self.cmd_with_data(spi, Command::SetRamYAc, &[0x00, 0x00])?; + self.cmd_with_data(spi, Command::WriteRamBw, buffer)?; + self.cmd_with_data(spi, Command::DisplayUpdateControl2, &[0xF7])?; + Ok(()) + } + + fn update_partial_frame( + &mut self, + _spi: &mut SPI, + _buffer: &[u8], + _x: u32, + _y: u32, + _width: u32, + _height: u32, + ) -> Result<(), SPI::Error> { + unimplemented!(); + } + + fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.command(spi, Command::MasterActivation)?; + self.wait_until_idle(); + Ok(()) + } + + fn update_and_display_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { + self.update_frame(spi, buffer)?; + self.display_frame(spi)?; + Ok(()) + } + + fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + let pixel_count = WIDTH * HEIGHT / 8; + let background_color_byte = self.color.get_byte_value(); + + self.wait_until_idle(); + self.cmd_with_data(spi, Command::SetRamYAc, &[0x00, 0x00])?; + + for cmd in &[Command::WriteRamBw, Command::WriteRamRed] { + self.command(spi, *cmd)?; + self.interface + .data_x_times(spi, background_color_byte, pixel_count)?; + } + + self.cmd_with_data(spi, Command::DisplayUpdateControl2, &[0xF7])?; + self.command(spi, Command::MasterActivation)?; + self.wait_until_idle(); + Ok(()) + } + + fn set_background_color(&mut self, color: Color) { + self.color = color; + } + + fn background_color(&self) -> &Color { + &self.color + } + + fn width(&self) -> u32 { + WIDTH + } + + fn height(&self) -> u32 { + HEIGHT + } + + fn set_lut( + &mut self, + _spi: &mut SPI, + _refresh_rate: Option, + ) -> Result<(), SPI::Error> { + unimplemented!(); + } + + fn is_busy(&self) -> bool { + self.interface.is_busy(IS_BUSY_LOW) + } +} + +impl Epd7in5 +where + SPI: Write, + CS: OutputPin, + BUSY: InputPin, + DC: OutputPin, + RST: OutputPin, +{ + fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> { + self.interface.cmd(spi, command) + } + + fn cmd_with_data( + &mut self, + spi: &mut SPI, + command: Command, + data: &[u8], + ) -> Result<(), SPI::Error> { + self.interface.cmd_with_data(spi, command, data) + } + + fn wait_until_idle(&mut self) { + self.interface.wait_until_idle(IS_BUSY_LOW) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn epd_size() { + assert_eq!(WIDTH, 880); + assert_eq!(HEIGHT, 528); + assert_eq!(DEFAULT_BACKGROUND_COLOR, Color::White); + } +} diff --git a/src/interface.rs b/src/interface.rs index 4ba1eb3..b83cc5f 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -16,7 +16,7 @@ pub(crate) struct DisplayInterface { busy: BUSY, /// Data/Command Control Pin (High for data, Low for command) dc: DC, - /// Pin for Reseting + /// Pin for Resetting rst: RST, } @@ -107,7 +107,7 @@ where spi.write(data)?; } - // deativate spi with cs high + // deactivate spi with cs high let _ = self.cs.set_high(); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index b96e6fe..f22a733 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,7 @@ pub mod epd2in9bc; pub mod epd4in2; pub mod epd5in65f; pub mod epd7in5; +pub mod epd7in5_hd; pub mod epd7in5_v2; pub(crate) mod type_a;