From cd593c51cbfa65ee1700fb1455634a5a031e134b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Gro=C3=9F?= Date: Wed, 8 Aug 2018 15:35:08 +0200 Subject: [PATCH] Make the code more readable with a few already introduced adapter functions (e.g. command_with_data) --- src/epd1in54/mod.rs | 27 +++++++++++--------------- src/epd2in9/mod.rs | 47 ++++++++++++++++++--------------------------- src/epd4in2/mod.rs | 36 ++++++++++------------------------ 3 files changed, 40 insertions(+), 70 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index d958e10..ce743c6 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -81,21 +81,17 @@ where self.interface.data(0x9D)?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.command(Command::WRITE_VCOM_REGISTER)?; - self.interface.data(0xA8)?; + self.interface.command_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate - self.interface.command(Command::SET_DUMMY_LINE_PERIOD)?; - self.interface.data(0x1A)?; + self.interface.command_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line - self.interface.command(Command::SET_GATE_LINE_WIDTH)?; - self.interface.data(0x08)?; + self.interface.command_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?; // One Databyte with default value 0x03 // -> address: x increment, y increment, address counter is updated in x direction - self.interface.command(Command::DATA_ENTRY_MODE_SETTING)?; - self.interface.data(0x03)?; + self.interface.command_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; self.set_lut() } @@ -142,10 +138,9 @@ where fn sleep(&mut self) -> Result<(), E> { - self.interface.command(Command::DEEP_SLEEP_MODE)?; // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode - //TODO: is 0x00 needed here? - self.interface.data(0x00)?; + //TODO: is 0x00 needed here or would 0x01 be even more efficient? + self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) @@ -157,7 +152,6 @@ where fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { self.use_full_frame()?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) } @@ -173,8 +167,7 @@ where self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command(Command::WRITE_RAM)?; - self.interface.multiple_data(buffer) + self.interface.command_with_data(Command::WRITE_RAM, buffer) } fn display_frame(&mut self) -> Result<(), E> { @@ -262,8 +255,10 @@ where // 2 Databytes: A[7:0] & 0..A[8] self.interface.command_with_data( Command::SET_RAM_Y_ADDRESS_COUNTER, - &[y as u8, (y >> 8) as u8] - )?; + &[ + y as u8, + (y >> 8) as u8 + ])?; self.wait_until_idle(); Ok(()) diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index b7b08f8..bc6e0e7 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -102,11 +102,11 @@ where } -impl - WaveshareInterface +impl + WaveshareInterface for EPD2in9 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, @@ -123,10 +123,7 @@ where fn new( spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, - ) -> Result { - //let epd = EPD::new(WIDTH, HEIGHT); - //let background_color = Color::White; - + ) -> Result { let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); let mut epd = EPD2in9 { @@ -141,17 +138,16 @@ where - fn sleep(&mut self) -> Result<(), E> { - self.interface.command(Command::DEEP_SLEEP_MODE)?; + fn sleep(&mut self) -> Result<(), ERR> { // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode - //TODO: is 0x00 needed here? - self.interface.data(0x00)?; + //TODO: is 0x00 needed here? (see also epd1in54) + self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) } - fn wake_up(&mut self) -> Result<(), E> { + fn wake_up(&mut self) -> Result<(), ERR> { self.init() } @@ -159,11 +155,10 @@ where self.interface.delay_ms(delay) } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { + fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { self.use_full_frame()?; - self.interface.command(Command::WRITE_RAM)?; - self.interface.multiple_data(buffer) + self.interface.command_with_data(Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos @@ -174,19 +169,17 @@ where y: u16, width: u16, height: u16, - ) -> Result<(), E> { + ) -> Result<(), ERR> { self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command(Command::WRITE_RAM)?; - self.interface.multiple_data(buffer) + self.interface.command_with_data(Command::WRITE_RAM, buffer) } - fn display_frame(&mut self) -> Result<(), E> { + fn display_frame(&mut self) -> Result<(), ERR> { // 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.command(Command::DISPLAY_UPDATE_CONTROL_2)?; - self.interface.data(0xC4)?; + self.interface.command_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; self.interface.command(Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images @@ -194,7 +187,7 @@ where self.interface.command(Command::NOP) } - fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> { + fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { self.update_frame(buffer)?; self.display_frame() } @@ -206,12 +199,12 @@ where y: u16, width: u16, height: u16, - ) -> Result<(), E> { + ) -> Result<(), ERR> { self.update_partial_frame(buffer, x, y, width, height)?; self.display_frame() } - fn clear_frame(&mut self) -> Result<(), E> { + fn clear_frame(&mut self) -> Result<(), ERR> { self.use_full_frame()?; // clear the ram with the background color @@ -279,8 +272,7 @@ where pub(crate) fn set_ram_counter(&mut self, x: u16, y: u16) -> Result<(), E> { // 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.command(Command::SET_RAM_X_ADDRESS_COUNTER)?; - self.interface.data((x >> 3) as u8)?; + self.interface.command_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] self.interface.command(Command::SET_RAM_Y_ADDRESS_COUNTER)?; @@ -308,7 +300,6 @@ where fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { assert!(buffer.len() == 30); - self.interface.command(Command::WRITE_LUT_REGISTER)?; - self.interface.multiple_data(buffer) + self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 94dc0a0..24ee3a0 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -160,10 +160,9 @@ where /// epd4in2.sleep(); /// ``` fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); - let color = Color::White; + let mut epd = EPD4in2 { interface, color, @@ -178,9 +177,9 @@ where self.init() } + //TODO: is such a long delay really needed inbetween? fn sleep(&mut self) -> Result<(), ERR> { - self.command(Command::VCOM_AND_DATA_INTERVAL_SETTING)?; - self.send_data(0x17)?; //border floating + self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating self.command(Command::VCM_DC_SETTING)?; // VCOM to 0V self.command(Command::PANEL_SETTING)?; self.delay_ms(100); @@ -203,25 +202,17 @@ where self.interface.command_with_data(Command::VCM_DC_SETTING, &[0x12])?; - self.command(Command::VCOM_AND_DATA_INTERVAL_SETTING)?; //TODO: this was a send_command instead of a send_data. check if it's alright and doing what it should do (setting the default values) //self.send_command_u8(0x97)?; //VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7 - self.send_data(0x97)?; + self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; - self.command(Command::DATA_START_TRANSMISSION_1)?; - for _ in 0..(buffer.len()) { - self.send_data(color_value)?; - } - self.delay_ms(2); - self.command(Command::DATA_START_TRANSMISSION_2)?; - //self.send_multiple_data(buffer)?; + self.command(Command::DATA_START_TRANSMISSION_1)?; + self.interface.data_x_times(color_value, buffer.len() as u16)?; - for &elem in buffer.iter() { - self.send_data(elem)?; - } + self.delay_ms(2); - Ok(()) + self.interface.command_with_data(Command::DATA_START_TRANSMISSION_2, buffer) } fn update_partial_frame( @@ -283,19 +274,12 @@ where let color_value = self.color.get_byte_value(); self.command(Command::DATA_START_TRANSMISSION_1)?; - self.delay_ms(2); - for _ in 0..size { - self.send_data(color_value)?; - } + self.interface.data_x_times(color_value, size)?; self.delay_ms(2); self.command(Command::DATA_START_TRANSMISSION_2)?; - self.delay_ms(2); - for _ in 0..size { - self.send_data(color_value)?; - } - Ok(()) + self.interface.data_x_times(color_value, size) } /// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame())