|
| 1 | +//! A simple Driver for the Waveshare 3.7" E-Ink Display via SPI |
| 2 | +//! |
| 3 | +//! |
| 4 | +//! Build with the help of documentation/code from [Waveshare](https://www.waveshare.com/wiki/3.7inch_e-Paper_HAT), |
| 5 | +use embedded_hal::{ |
| 6 | + blocking::{delay::DelayUs, spi::Write}, |
| 7 | + digital::v2::{InputPin, OutputPin}, |
| 8 | +}; |
| 9 | + |
| 10 | +pub(crate) mod command; |
| 11 | +mod constants; |
| 12 | + |
| 13 | +use self::command::Command; |
| 14 | +use self::constants::*; |
| 15 | + |
| 16 | +use crate::buffer_len; |
| 17 | +use crate::color::Color; |
| 18 | +use crate::interface::DisplayInterface; |
| 19 | +use crate::traits::{InternalWiAdditions, RefreshLut, WaveshareDisplay}; |
| 20 | + |
| 21 | +/// Width of the display. |
| 22 | +pub const WIDTH: u32 = 280; |
| 23 | + |
| 24 | +/// Height of the display |
| 25 | +pub const HEIGHT: u32 = 480; |
| 26 | + |
| 27 | +/// Default Background Color |
| 28 | +pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White; |
| 29 | + |
| 30 | +const IS_BUSY_LOW: bool = false; |
| 31 | + |
| 32 | +/// Display with Fullsize buffer for use with the 3in7 EPD |
| 33 | +#[cfg(feature = "graphics")] |
| 34 | +pub type Display3in7 = crate::graphics::Display< |
| 35 | + WIDTH, |
| 36 | + HEIGHT, |
| 37 | + false, |
| 38 | + { buffer_len(WIDTH as usize, HEIGHT as usize) }, |
| 39 | + Color, |
| 40 | +>; |
| 41 | + |
| 42 | +/// EPD3in7 driver |
| 43 | +pub struct EPD3in7<SPI, CS, BUSY, DC, RST, DELAY> { |
| 44 | + /// Connection Interface |
| 45 | + interface: DisplayInterface<SPI, CS, BUSY, DC, RST, DELAY>, |
| 46 | + /// Background Color |
| 47 | + background_color: Color, |
| 48 | +} |
| 49 | + |
| 50 | +impl<SPI, CS, BUSY, DC, RST, DELAY> InternalWiAdditions<SPI, CS, BUSY, DC, RST, DELAY> |
| 51 | + for EPD3in7<SPI, CS, BUSY, DC, RST, DELAY> |
| 52 | +where |
| 53 | + SPI: Write<u8>, |
| 54 | + CS: OutputPin, |
| 55 | + BUSY: InputPin, |
| 56 | + DC: OutputPin, |
| 57 | + RST: OutputPin, |
| 58 | + DELAY: DelayUs<u32>, |
| 59 | +{ |
| 60 | + fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 61 | + // reset the device |
| 62 | + self.interface.reset(delay, 30, 10); |
| 63 | + |
| 64 | + self.interface.cmd(spi, Command::SwReset)?; |
| 65 | + delay.delay_us(300000u32); |
| 66 | + |
| 67 | + self.interface |
| 68 | + .cmd_with_data(spi, Command::AutoWriteRedRamRegularPattern, &[0xF7])?; |
| 69 | + self.interface.wait_until_idle(delay, IS_BUSY_LOW); |
| 70 | + self.interface |
| 71 | + .cmd_with_data(spi, Command::AutoWriteBwRamRegularPattern, &[0xF7])?; |
| 72 | + self.interface.wait_until_idle(delay, IS_BUSY_LOW); |
| 73 | + |
| 74 | + self.interface |
| 75 | + .cmd_with_data(spi, Command::GateSetting, &[0xDF, 0x01, 0x00])?; |
| 76 | + self.interface |
| 77 | + .cmd_with_data(spi, Command::GateVoltage, &[0x00])?; |
| 78 | + self.interface |
| 79 | + .cmd_with_data(spi, Command::GateVoltageSource, &[0x41, 0xA8, 0x32])?; |
| 80 | + |
| 81 | + self.interface |
| 82 | + .cmd_with_data(spi, Command::DataEntrySequence, &[0x03])?; |
| 83 | + |
| 84 | + self.interface |
| 85 | + .cmd_with_data(spi, Command::BorderWaveformControl, &[0x03])?; |
| 86 | + |
| 87 | + self.interface.cmd_with_data( |
| 88 | + spi, |
| 89 | + Command::BoosterSoftStartControl, |
| 90 | + &[0xAE, 0xC7, 0xC3, 0xC0, 0xC0], |
| 91 | + )?; |
| 92 | + |
| 93 | + self.interface |
| 94 | + .cmd_with_data(spi, Command::TemperatureSensorSelection, &[0x80])?; |
| 95 | + |
| 96 | + self.interface |
| 97 | + .cmd_with_data(spi, Command::WriteVcomRegister, &[0x44])?; |
| 98 | + |
| 99 | + self.interface.cmd_with_data( |
| 100 | + spi, |
| 101 | + Command::DisplayOption, |
| 102 | + &[0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x4F, 0xFF, 0xFF, 0xFF, 0xFF], |
| 103 | + )?; |
| 104 | + |
| 105 | + self.interface.cmd_with_data( |
| 106 | + spi, |
| 107 | + Command::SetRamXAddressStartEndPosition, |
| 108 | + &[0x00, 0x00, 0x17, 0x01], |
| 109 | + )?; |
| 110 | + self.interface.cmd_with_data( |
| 111 | + spi, |
| 112 | + Command::SetRamYAddressStartEndPosition, |
| 113 | + &[0x00, 0x00, 0xDF, 0x01], |
| 114 | + )?; |
| 115 | + |
| 116 | + self.interface |
| 117 | + .cmd_with_data(spi, Command::DisplayUpdateSequenceSetting, &[0xCF])?; |
| 118 | + |
| 119 | + self.set_lut(spi, delay, Some(RefreshLut::Full))?; |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl<SPI, CS, BUSY, DC, RST, DELAY> WaveshareDisplay<SPI, CS, BUSY, DC, RST, DELAY> |
| 125 | + for EPD3in7<SPI, CS, BUSY, DC, RST, DELAY> |
| 126 | +where |
| 127 | + SPI: Write<u8>, |
| 128 | + CS: OutputPin, |
| 129 | + BUSY: InputPin, |
| 130 | + DC: OutputPin, |
| 131 | + RST: OutputPin, |
| 132 | + DELAY: DelayUs<u32>, |
| 133 | +{ |
| 134 | + type DisplayColor = Color; |
| 135 | + |
| 136 | + fn new( |
| 137 | + spi: &mut SPI, |
| 138 | + cs: CS, |
| 139 | + busy: BUSY, |
| 140 | + dc: DC, |
| 141 | + rst: RST, |
| 142 | + delay: &mut DELAY, |
| 143 | + delay_us: Option<u32>, |
| 144 | + ) -> Result<Self, SPI::Error> { |
| 145 | + let mut epd = EPD3in7 { |
| 146 | + interface: DisplayInterface::new(cs, busy, dc, rst, delay_us), |
| 147 | + background_color: DEFAULT_BACKGROUND_COLOR, |
| 148 | + }; |
| 149 | + |
| 150 | + epd.init(spi, delay)?; |
| 151 | + Ok(epd) |
| 152 | + } |
| 153 | + |
| 154 | + fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 155 | + self.init(spi, delay) |
| 156 | + } |
| 157 | + |
| 158 | + fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 159 | + self.interface.cmd_with_data(spi, Command::Sleep, &[0xF7])?; |
| 160 | + self.interface.cmd(spi, Command::PowerOff)?; |
| 161 | + self.interface |
| 162 | + .cmd_with_data(spi, Command::Sleep2, &[0xA5])?; |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | + |
| 166 | + fn set_background_color(&mut self, color: Self::DisplayColor) { |
| 167 | + self.background_color = color; |
| 168 | + } |
| 169 | + |
| 170 | + fn background_color(&self) -> &Self::DisplayColor { |
| 171 | + &self.background_color |
| 172 | + } |
| 173 | + |
| 174 | + fn width(&self) -> u32 { |
| 175 | + WIDTH |
| 176 | + } |
| 177 | + |
| 178 | + fn height(&self) -> u32 { |
| 179 | + HEIGHT |
| 180 | + } |
| 181 | + |
| 182 | + fn update_frame( |
| 183 | + &mut self, |
| 184 | + spi: &mut SPI, |
| 185 | + buffer: &[u8], |
| 186 | + _delay: &mut DELAY, |
| 187 | + ) -> Result<(), SPI::Error> { |
| 188 | + assert!(buffer.len() == buffer_len(WIDTH as usize, HEIGHT as usize)); |
| 189 | + self.interface |
| 190 | + .cmd_with_data(spi, Command::SetRamXAddressCounter, &[0x00, 0x00])?; |
| 191 | + self.interface |
| 192 | + .cmd_with_data(spi, Command::SetRamYAddressCounter, &[0x00, 0x00])?; |
| 193 | + |
| 194 | + self.interface |
| 195 | + .cmd_with_data(spi, Command::WriteRam, buffer)?; |
| 196 | + |
| 197 | + Ok(()) |
| 198 | + } |
| 199 | + |
| 200 | + #[allow(unused)] |
| 201 | + fn update_partial_frame( |
| 202 | + &mut self, |
| 203 | + spi: &mut SPI, |
| 204 | + delay: &mut DELAY, |
| 205 | + buffer: &[u8], |
| 206 | + x: u32, |
| 207 | + y: u32, |
| 208 | + width: u32, |
| 209 | + height: u32, |
| 210 | + ) -> Result<(), SPI::Error> { |
| 211 | + todo!() |
| 212 | + } |
| 213 | + |
| 214 | + fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 215 | + //self.interface |
| 216 | + // .cmd_with_data(spi, Command::WRITE_LUT_REGISTER, &LUT_1GRAY_GC)?; |
| 217 | + self.interface.cmd(spi, Command::DisplayUpdateSequence)?; |
| 218 | + self.interface.wait_until_idle(delay, IS_BUSY_LOW); |
| 219 | + Ok(()) |
| 220 | + } |
| 221 | + |
| 222 | + fn update_and_display_frame( |
| 223 | + &mut self, |
| 224 | + spi: &mut SPI, |
| 225 | + buffer: &[u8], |
| 226 | + delay: &mut DELAY, |
| 227 | + ) -> Result<(), SPI::Error> { |
| 228 | + self.update_frame(spi, buffer, delay)?; |
| 229 | + self.display_frame(spi, delay)?; |
| 230 | + Ok(()) |
| 231 | + } |
| 232 | + |
| 233 | + fn clear_frame(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 234 | + self.interface |
| 235 | + .cmd_with_data(spi, Command::SetRamXAddressCounter, &[0x00, 0x00])?; |
| 236 | + self.interface |
| 237 | + .cmd_with_data(spi, Command::SetRamYAddressCounter, &[0x00, 0x00])?; |
| 238 | + |
| 239 | + let color = self.background_color.get_byte_value(); |
| 240 | + self.interface.cmd(spi, Command::WriteRam)?; |
| 241 | + self.interface.data_x_times(spi, color, WIDTH * HEIGHT)?; |
| 242 | + |
| 243 | + Ok(()) |
| 244 | + } |
| 245 | + |
| 246 | + fn set_lut( |
| 247 | + &mut self, |
| 248 | + spi: &mut SPI, |
| 249 | + _delay: &mut DELAY, |
| 250 | + refresh_rate: Option<RefreshLut>, |
| 251 | + ) -> Result<(), SPI::Error> { |
| 252 | + let buffer = match refresh_rate { |
| 253 | + Some(RefreshLut::Full) | None => &LUT_1GRAY_GC, |
| 254 | + Some(RefreshLut::Quick) => &LUT_1GRAY_DU, |
| 255 | + }; |
| 256 | + |
| 257 | + self.interface |
| 258 | + .cmd_with_data(spi, Command::WriteLutRegister, buffer)?; |
| 259 | + Ok(()) |
| 260 | + } |
| 261 | + |
| 262 | + fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { |
| 263 | + self.interface.wait_until_idle(delay, IS_BUSY_LOW); |
| 264 | + Ok(()) |
| 265 | + } |
| 266 | +} |
0 commit comments