From 7612aad99a7f490173c81784a8b6d1c792f2452a Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 09:20:34 +0200 Subject: [PATCH 01/14] Deletion of single u8 data Transfers and renaming of various functions command_with_data -> cmd_with_data command -> cmd multiple_data -> data --- src/epd1in54/mod.rs | 57 ++++++++--------- src/epd2in9/mod.rs | 61 +++++++----------- src/epd4in2/command.rs | 11 ++++ src/epd4in2/mod.rs | 89 +++++++++++---------------- src/interface/connection_interface.rs | 29 +++------ 5 files changed, 105 insertions(+), 142 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 0f282ca..93de08e 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -65,33 +65,30 @@ where // 0.. A[8] // 0.. B[2:0] // Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?) - self.interface.command(Command::DRIVER_OUTPUT_CONTROL)?; - self.interface.data(HEIGHT as u8)?; - self.interface.data((HEIGHT >> 8) as u8)?; - self.interface.data(0x00)?; + self.interface.cmd_with_data( + Command::DRIVER_OUTPUT_CONTROL, + &[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.command(Command::BOOSTER_SOFT_START_CONTROL)?; - self.interface.data(0xD7)?; - self.interface.data(0xD6)?; - self.interface.data(0x9D)?; + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.command_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; + self.interface.cmd_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate - self.interface.command_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; + self.interface.cmd_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line - self.interface.command_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?; + self.interface.cmd_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_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; + self.interface.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; self.set_lut() } @@ -140,7 +137,7 @@ where fn sleep(&mut self) -> Result<(), E> { // 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.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) @@ -152,7 +149,7 @@ where fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { self.use_full_frame()?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos @@ -167,18 +164,18 @@ where self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } fn display_frame(&mut self) -> Result<(), E> { // 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_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.command(Command::MASTER_ACTIVATION)?; + self.interface.cmd(Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.command(Command::NOP) + self.interface.cmd(Command::NOP) } fn clear_frame(&mut self) -> Result<(), E> { @@ -187,7 +184,7 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.command(Command::WRITE_RAM)?; + self.interface.cmd(Command::WRITE_RAM)?; self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) } @@ -235,25 +232,25 @@ where // 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_START_END_POSITION)?; - self.interface.data((start_x >> 3) as u8)?; - self.interface.data((end_x >> 3) as u8)?; + self.interface.cmd_with_data( + Command::SET_RAM_X_ADDRESS_START_END_POSITION, + &[(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.command(Command::SET_RAM_Y_ADDRESS_START_END_POSITION)?; - self.interface.data(start_y as u8)?; - self.interface.data((start_y >> 8) as u8)?; - self.interface.data(end_y as u8)?; - self.interface.data((end_y >> 8) as u8) + self.interface.cmd_with_data( + Command::SET_RAM_Y_ADDRESS_START_END_POSITION, + &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] + ) } 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_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] - self.interface.command_with_data( + self.interface.cmd_with_data( Command::SET_RAM_Y_ADDRESS_COUNTER, &[ y as u8, @@ -281,7 +278,7 @@ where fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { assert!(buffer.len() == 30); - self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 90b5e02..5a86d83 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -64,42 +64,30 @@ where // 0.. A[8] // 0.. B[2:0] // Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?) - self.interface.command(Command::DRIVER_OUTPUT_CONTROL)?; - self.interface.data(HEIGHT as u8)?; - self.interface.data((HEIGHT >> 8) as u8)?; - self.interface.data(0x00)?; + self.interface.cmd_with_data(Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 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.command(Command::BOOSTER_SOFT_START_CONTROL)?; - self.interface.data(0xD7)?; - self.interface.data(0xD6)?; - self.interface.data(0x9D)?; + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.command(Command::WRITE_VCOM_REGISTER)?; - self.interface.data(0xA8)?; + self.interface.cmd_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.cmd_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.cmd_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.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; self.set_lut() } - - } impl @@ -141,7 +129,7 @@ where fn sleep(&mut self) -> Result<(), ERR> { // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode //TODO: is 0x00 needed here? (see also epd1in54) - self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) @@ -158,7 +146,7 @@ where fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { self.use_full_frame()?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos @@ -173,18 +161,18 @@ where self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } 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_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.command(Command::MASTER_ACTIVATION)?; + self.interface.cmd(Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.command(Command::NOP) + self.interface.cmd(Command::NOP) } fn clear_frame(&mut self) -> Result<(), ERR> { @@ -193,7 +181,7 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.command(Command::WRITE_RAM)?; + self.interface.cmd(Command::WRITE_RAM)?; self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) } @@ -240,27 +228,24 @@ where // 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_START_END_POSITION)?; - self.interface.data((start_x >> 3) as u8)?; - self.interface.data((end_x >> 3) as u8)?; + self.interface.cmd_with_data( + Command::SET_RAM_X_ADDRESS_START_END_POSITION, + &[(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.command(Command::SET_RAM_Y_ADDRESS_START_END_POSITION)?; - self.interface.data(start_y as u8)?; - self.interface.data((start_y >> 8) as u8)?; - self.interface.data(end_y as u8)?; - self.interface.data((end_y >> 8) as u8) + self.interface.cmd_with_data(Command::SET_RAM_Y_ADDRESS_START_END_POSITION, + &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] + ) } 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_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_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)?; - self.interface.data(y as u8)?; - self.interface.data((y >> 8) as u8)?; + self.interface.cmd_with_data(Command::SET_RAM_Y_ADDRESS_COUNTER, &[y as u8, (y >> 8) as u8])?; self.wait_until_idle(); Ok(()) @@ -283,7 +268,7 @@ where fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { assert!(buffer.len() == 30); - self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd4in2/command.rs b/src/epd4in2/command.rs index 75800cf..80c016b 100644 --- a/src/epd4in2/command.rs +++ b/src/epd4in2/command.rs @@ -12,8 +12,18 @@ use interface; #[derive(Copy, Clone)] pub(crate) enum Command { /// Set Resolution, LUT selection, BWR pixels, gate scan direction, source shift direction, booster switch, soft reset + /// One Byte of Data: + /// 0x0F Red Mode, LUT from OTP + /// 0x1F B/W Mode, LUT from OTP + /// 0x2F Red Mode, LUT set by registers + /// 0x3F B/W Mode, LUT set by registers PANEL_SETTING = 0x00, /// selecting internal and external power + /// self.send_data(0x03)?; //VDS_EN, VDG_EN + /// self.send_data(0x00)?; //VCOM_HV, VGHL_LV[1], VGHL_LV[0] + /// self.send_data(0x2b)?; //VDH + /// self.send_data(0x2b)?; //VDL + /// self.send_data(0xff)?; //VDHR POWER_SETTING = 0x01, /// After the Power Off command, the driver will power off following the Power Off Sequence. This command will turn off charge /// pump, T-con, source driver, gate driver, VCOM, and temperature sensor, but register data will be kept until VDD becomes OFF. @@ -26,6 +36,7 @@ pub(crate) enum Command { /// This command enables the internal bandgap, which will be cleared by the next POF. POWER_ON_MEASURE = 0x05, /// Starting data transmission + /// 3-times: self.send_data(0x17)?; //07 0f 17 1f 27 2F 37 2f BOOSTER_SOFT_START = 0x06, /// After this command is transmitted, the chip would enter the deep-sleep mode to save power. /// diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 81ca731..8d84d14 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -90,43 +90,24 @@ where self.interface.reset(); // set the power settings - self.command(Command::POWER_SETTING)?; - self.send_data(0x03)?; //VDS_EN, VDG_EN - self.send_data(0x00)?; //VCOM_HV, VGHL_LV[1], VGHL_LV[0] - self.send_data(0x2b)?; //VDH - self.send_data(0x2b)?; //VDL - self.send_data(0xff)?; //VDHR + self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; // start the booster - self.command(Command::BOOSTER_SOFT_START)?; - for _ in 0..3 { - self.send_data(0x17)?; //07 0f 17 1f 27 2F 37 2f - } + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START, &[0x17, 0x17, 0x17])?; // power on self.command(Command::POWER_ON)?; self.wait_until_idle(); // set the panel settings - self.command(Command::PANEL_SETTING)?; - // 0x0F Red Mode, LUT from OTP - // 0x1F B/W Mode, LUT from OTP - // 0x2F Red Mode, LUT set by registers - // 0x3F B/W Mode, LUT set by registers - self.send_data(0x3F)?; - - // the values used by waveshare before for the panel settings - // instead of our one liner: - // SendData(0xbf); // KW-BF KWR-AF BWROTP 0f - // SendData(0x0b); + self.cmd_with_data(Command::PANEL_SETTING, &[0x3F])?; // Set Frequency, 200 Hz didn't work on my board // 150Hz and 171Hz wasn't tested yet // TODO: Test these other frequencies // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ DEFAULT: 3c 50Hz - self.command(Command::PLL_CONTROL)?; - self.send_data(0x3A)?; - + self.cmd_with_data(Command::PLL_CONTROL, &[0x3A])?; + self.set_lut()?; Ok(()) @@ -179,20 +160,20 @@ where //TODO: is such a long delay really needed inbetween? fn sleep(&mut self) -> Result<(), ERR> { - self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating + self.interface.cmd_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); self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast for _ in 0..4 { - self.send_data(0x00)?; + self.send_data(&[0x00])?; } self.delay_ms(100); self.command(Command::POWER_OFF)?; self.wait_until_idle(); - self.interface.command_with_data(Command::DEEP_SLEEP, &[0xA5]) + self.interface.cmd_with_data(Command::DEEP_SLEEP, &[0xA5]) } fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { @@ -200,11 +181,11 @@ where self.send_resolution()?; - self.interface.command_with_data(Command::VCM_DC_SETTING, &[0x12])?; + self.interface.cmd_with_data(Command::VCM_DC_SETTING, &[0x12])?; //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.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; + self.interface.cmd_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; self.command(Command::DATA_START_TRANSMISSION_1)?; @@ -212,7 +193,7 @@ where self.delay_ms(2); - self.interface.command_with_data(Command::DATA_START_TRANSMISSION_2, buffer) + self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer) } fn update_partial_frame( @@ -230,20 +211,20 @@ where self.command(Command::PARTIAL_IN)?; self.command(Command::PARTIAL_WINDOW)?; - self.send_data((x >> 8) as u8)?; + self.send_data(&[(x >> 8) as u8])?; let tmp = x & 0xf8; - self.send_data(tmp as u8)?; // x should be the multiple of 8, the last 3 bit will always be ignored + self.send_data(&[tmp as u8])?; // x should be the multiple of 8, the last 3 bit will always be ignored let tmp = tmp + width - 1; - self.send_data((tmp >> 8) as u8)?; - self.send_data((tmp | 0x07) as u8)?; + self.send_data(&[(tmp >> 8) as u8])?; + self.send_data(&[(tmp | 0x07) as u8])?; - self.send_data((y >> 8) as u8)?; - self.send_data(y as u8)?; + self.send_data(&[(y >> 8) as u8])?; + self.send_data(&[y as u8])?; - self.send_data(((y + height - 1) >> 8) as u8)?; - self.send_data((y + height - 1) as u8)?; + self.send_data(&[((y + height - 1) >> 8) as u8])?; + self.send_data(&[(y + height - 1) as u8])?; - self.send_data(0x01)?; // Gates scan both inside and outside of the partial window. (default) + self.send_data(&[0x01])?; // Gates scan both inside and outside of the partial window. (default) //TODO: handle dtm somehow let is_dtm1 = false; @@ -253,7 +234,7 @@ where self.command(Command::DATA_START_TRANSMISSION_2)? } - self.send_multiple_data(buffer)?; + self.send_data(buffer)?; self.command(Command::PARTIAL_OUT) } @@ -315,15 +296,15 @@ where D: DelayUs + DelayMs, { fn command(&mut self, command: Command) -> Result<(), ERR> { - self.interface.command(command) + self.interface.cmd(command) } - fn send_data(&mut self, val: u8) -> Result<(), ERR> { - self.interface.data(val) + fn send_data(&mut self, data: &[u8]) -> Result<(), ERR> { + self.interface.data(data) } - fn send_multiple_data(&mut self, data: &[u8]) -> Result<(), ERR> { - self.interface.multiple_data(data) + fn cmd_with_data(&mut self, command: Command, data: &[u8]) -> Result<(), ERR> { + self.interface.cmd_with_data(command, data) } fn wait_until_idle(&mut self) { @@ -335,10 +316,10 @@ where let h = self.height(); self.command(Command::RESOLUTION_SETTING)?; - self.send_data((w >> 8) as u8)?; - self.send_data(w as u8)?; - self.send_data((h >> 8) as u8)?; - self.send_data(h as u8) + self.send_data(&[(w >> 8) as u8])?; + self.send_data(&[w as u8])?; + self.send_data(&[(h >> 8) as u8])?; + self.send_data(&[h as u8]) } /// Fill the look-up table for the EPD @@ -372,23 +353,23 @@ where ) -> Result<(), ERR> { // LUT VCOM self.command(Command::LUT_FOR_VCOM)?; - self.send_multiple_data(lut_vcom)?; + self.send_data(lut_vcom)?; // LUT WHITE to WHITE self.command(Command::LUT_WHITE_TO_WHITE)?; - self.send_multiple_data(lut_ww)?; + self.send_data(lut_ww)?; // LUT BLACK to WHITE self.command(Command::LUT_BLACK_TO_WHITE)?; - self.send_multiple_data(lut_bw)?; + self.send_data(lut_bw)?; // LUT WHITE to BLACK self.command(Command::LUT_WHITE_TO_BLACK)?; - self.send_multiple_data(lut_wb)?; + self.send_data(lut_wb)?; // LUT BLACK to BLACK self.command(Command::LUT_BLACK_TO_BLACK)?; - self.send_multiple_data(lut_bb)?; + self.send_data(lut_bb)?; Ok(()) } diff --git a/src/interface/connection_interface.rs b/src/interface/connection_interface.rs index f825807..d55750e 100644 --- a/src/interface/connection_interface.rs +++ b/src/interface/connection_interface.rs @@ -48,7 +48,7 @@ where /// Enables direct interaction with the device with the help of [data()](ConnectionInterface::data()) /// /// //TODO: make public? - pub(crate) fn command(&mut self, command: T) -> Result<(), ERR> { + pub(crate) fn cmd(&mut self, command: T) -> Result<(), ERR> { // low for commands self.dc.set_low(); @@ -56,25 +56,25 @@ where self.with_cs(|epd| epd.spi.write(&[command.address()])) } - /// Basic function for sending a single u8 of data over spi + /// Basic function for sending an array of u8-values of data over spi /// - /// Enables direct interaction with the device with the help of [Ecommand()](ConnectionInterface::command()) + /// Enables direct interaction with the device with the help of [command()](EPD4in2::command()) /// /// //TODO: make public? - pub(crate) fn data(&mut self, val: u8) -> Result<(), ERR> { + pub(crate) fn data(&mut self, data: &[u8]) -> Result<(), ERR> { // high for data self.dc.set_high(); - // Transfer data (u8) over spi - self.with_cs(|epd| epd.spi.write(&[val])) + // Transfer data (u8-array) over spi + self.with_cs(|epd| epd.spi.write(data)) } /// Basic function for sending [Commands](Command) and the data belonging to it. /// /// //TODO: make public? - pub(crate) fn command_with_data(&mut self, command: T, data: &[u8]) -> Result<(), ERR> { - self.command(command)?; - self.multiple_data(data) + pub(crate) fn cmd_with_data(&mut self, command: T, data: &[u8]) -> Result<(), ERR> { + self.cmd(command)?; + self.data(data) } /// Basic function for sending the same byte of data (one u8) multiple times over spi @@ -99,18 +99,7 @@ where }) } - /// Basic function for sending an array of u8-values of data over spi - /// - /// Enables direct interaction with the device with the help of [command()](EPD4in2::command()) - /// - /// //TODO: make public? - pub(crate) fn multiple_data(&mut self, data: &[u8]) -> Result<(), ERR> { - // high for data - self.dc.set_high(); - // Transfer data (u8-array) over spi - self.with_cs(|epd| epd.spi.write(data)) - } // spi write helper/abstraction function fn with_cs(&mut self, f: F) -> Result<(), ERR> From 6efa8640146625466dec184efebb3f197f955b01 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 11:37:36 +0200 Subject: [PATCH 02/14] Renamed folder `interface` to `traits` --- src/epd1in54/mod.rs | 4 ++-- src/epd2in9/mod.rs | 4 ++-- src/epd4in2/command.rs | 6 +++--- src/epd4in2/mod.rs | 2 +- src/lib.rs | 4 ++-- src/{interface => traits}/connection_interface.rs | 2 +- src/{interface => traits}/mod.rs | 0 src/type_a/command.rs | 6 +++--- 8 files changed, 14 insertions(+), 14 deletions(-) rename src/{interface => traits}/connection_interface.rs (99%) rename src/{interface => traits}/mod.rs (100%) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 93de08e..f90a814 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -33,9 +33,9 @@ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use color::Color; -use interface::{WaveshareInterface}; +use traits::{WaveshareInterface}; -use interface::connection_interface::ConnectionInterface; +use traits::connection_interface::ConnectionInterface; /// EPD1in54 driver /// diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 5a86d83..5b8145e 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -32,9 +32,9 @@ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use color::Color; -use interface::*; +use traits::*; -use interface::connection_interface::ConnectionInterface; +use traits::connection_interface::ConnectionInterface; /// EPD2in9 driver /// diff --git a/src/epd4in2/command.rs b/src/epd4in2/command.rs index 80c016b..60173e8 100644 --- a/src/epd4in2/command.rs +++ b/src/epd4in2/command.rs @@ -1,5 +1,5 @@ //! SPI Commands for the Waveshare 4.2" E-Ink Display -use interface; +use traits; /// EPD4IN2 commands /// /// Should rarely (never?) be needed directly. @@ -155,7 +155,7 @@ pub(crate) enum Command { POWER_SAVING = 0xE3, } -impl interface::Command for Command { +impl traits::Command for Command { /// Returns the address of the command fn address(self) -> u8 { self as u8 @@ -165,7 +165,7 @@ impl interface::Command for Command { #[cfg(test)] mod tests { use super::*; - use interface::Command as CommandTrait; + use traits::Command as CommandTrait; #[test] fn command_addr() { diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 8d84d14..9ab5ab5 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -51,7 +51,7 @@ use hal::{ digital::*, }; -use interface::{connection_interface::ConnectionInterface, WaveshareInterface, InternalWiAdditions}; +use traits::{connection_interface::ConnectionInterface, WaveshareInterface, InternalWiAdditions}; //The Lookup Tables for the Display mod constants; diff --git a/src/lib.rs b/src/lib.rs index 42ceaf3..5e34505 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,8 +50,8 @@ use hal::spi::{Mode, Phase, Polarity}; #[cfg(feature = "graphics")] pub mod drawing; -mod interface; -pub use interface::{WaveshareInterface}; +mod traits; +pub use traits::{WaveshareInterface}; pub mod color; diff --git a/src/interface/connection_interface.rs b/src/traits/connection_interface.rs similarity index 99% rename from src/interface/connection_interface.rs rename to src/traits/connection_interface.rs index d55750e..842adb0 100644 --- a/src/interface/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -3,7 +3,7 @@ use hal::{ digital::*, }; -use interface::Command; +use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// diff --git a/src/interface/mod.rs b/src/traits/mod.rs similarity index 100% rename from src/interface/mod.rs rename to src/traits/mod.rs diff --git a/src/type_a/command.rs b/src/type_a/command.rs index 5272c07..36d6027 100644 --- a/src/type_a/command.rs +++ b/src/type_a/command.rs @@ -1,6 +1,6 @@ //! SPI Commands for the Waveshare 2.9" and 1.54" E-Ink Display -use interface; +use traits; /// EPD1in54 and EPD2IN9 commands /// @@ -71,7 +71,7 @@ pub(crate) enum Command { NOP = 0xFF, } -impl interface::Command for Command { +impl traits::Command for Command { /// Returns the address of the command fn address(self) -> u8 { self as u8 @@ -81,7 +81,7 @@ impl interface::Command for Command { #[cfg(test)] mod tests { use super::Command; - use interface::Command as CommandTrait; + use traits::Command as CommandTrait; #[test] fn command_addr() { From 38dc5126eedc480422c2d131bc43efef8b101909 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 13:23:43 +0200 Subject: [PATCH 03/14] Make delay a function parameter This change makes delay a function parameter where necessary and stops the need of owning the delay --- src/epd1in54/mod.rs | 36 +++++++++----------- src/epd2in9/mod.rs | 37 +++++++++------------ src/epd4in2/mod.rs | 53 +++++++++++++++--------------- src/traits/connection_interface.rs | 36 +++++++------------- src/traits/mod.rs | 17 +++------- 5 files changed, 73 insertions(+), 106 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index f90a814..5d0c7f1 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -39,26 +39,25 @@ use traits::connection_interface::ConnectionInterface; /// EPD1in54 driver /// -pub struct EPD1in54 { +pub struct EPD1in54 { /// SPI - interface: ConnectionInterface, + interface: ConnectionInterface, /// EPD (width, height) //epd: EPD, /// Color background_color: Color, } -impl EPD1in54 +impl EPD1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), E> { - self.interface.reset(); + fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.interface.reset(delay); // 3 Databytes: // A[7:0] @@ -95,8 +94,8 @@ where } -impl WaveshareInterface - for EPD1in54 +impl WaveshareInterface + for EPD1in54 where SPI: Write, CS: OutputPin, @@ -113,23 +112,23 @@ where HEIGHT } - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let mut epd = EPD1in54 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } - fn wake_up(&mut self) -> Result<(), E> { - self.init() + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.init(delay) } @@ -143,10 +142,6 @@ where Ok(()) } - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) - } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { self.use_full_frame()?; self.interface.cmd_with_data(Command::WRITE_RAM, buffer) @@ -199,14 +194,13 @@ where } } -impl EPD1in54 +impl EPD1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, - RST: OutputPin, - D: DelayUs + DelayMs, + RST: OutputPin { fn wait_until_idle(&mut self) { self.interface.wait_until_idle(false); diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 5b8145e..db6fa03 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -38,26 +38,25 @@ use traits::connection_interface::ConnectionInterface; /// EPD2in9 driver /// -pub struct EPD2in9 { +pub struct EPD2in9 { /// SPI - interface: ConnectionInterface, + interface: ConnectionInterface, /// EPD (width, height) //epd: EPD, /// Color background_color: Color, } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), E> { - self.interface.reset(); + fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.interface.reset(delay); // 3 Databytes: // A[7:0] @@ -90,16 +89,15 @@ where } } -impl - WaveshareInterface - for EPD2in9 +impl + WaveshareInterface + for EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { fn width(&self) -> u16 { WIDTH @@ -109,17 +107,17 @@ where HEIGHT } - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let mut epd = EPD2in9 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } @@ -135,12 +133,8 @@ where Ok(()) } - fn wake_up(&mut self) -> Result<(), ERR> { - self.init() - } - - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { + self.init(delay) } fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { @@ -195,14 +189,13 @@ where } } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - D: DelayUs + DelayMs, { fn wait_until_idle(&mut self) { self.interface.wait_until_idle(false); diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 9ab5ab5..3905f99 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -64,9 +64,9 @@ use self::command::Command; /// EPD4in2 driver /// -pub struct EPD4in2 { +pub struct EPD4in2 { /// Connection Interface - interface: ConnectionInterface, + interface: ConnectionInterface, /// Background Color color: Color, } @@ -74,20 +74,19 @@ pub struct EPD4in2 { -impl - InternalWiAdditions - for EPD4in2 +impl + InternalWiAdditions + for EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), ERR> { + fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { // reset the device - self.interface.reset(); + self.interface.reset(delay); // set the power settings self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; @@ -114,16 +113,15 @@ where } } -impl - WaveshareInterface - for EPD4in2 +impl + WaveshareInterface + for EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// @@ -140,8 +138,8 @@ 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); + fn new>(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY) -> Result { + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let color = DEFAULT_BACKGROUND_COLOR; let mut epd = EPD4in2 { @@ -149,13 +147,13 @@ where color, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } - fn wake_up(&mut self) -> Result<(), ERR> { - self.init() + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { + self.init(delay) } //TODO: is such a long delay really needed inbetween? @@ -163,13 +161,17 @@ where self.interface.cmd_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); + + //TODO: Removal of delay. TEST! + //self.delay_ms(100); self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast for _ in 0..4 { self.send_data(&[0x00])?; } - self.delay_ms(100); + + //TODO: Removal of delay. TEST! + //self.delay_ms(100); self.command(Command::POWER_OFF)?; self.wait_until_idle(); @@ -191,7 +193,8 @@ where self.command(Command::DATA_START_TRANSMISSION_1)?; self.interface.data_x_times(color_value, buffer.len() as u16)?; - self.delay_ms(2); + //TODO: Removal of delay. TEST! + //self.delay_ms(2); self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer) } @@ -257,7 +260,8 @@ where self.command(Command::DATA_START_TRANSMISSION_1)?; self.interface.data_x_times(color_value, size)?; - self.delay_ms(2); + //TODO: Removal of delay. TEST! + //self.delay_ms(2); self.command(Command::DATA_START_TRANSMISSION_2)?; self.interface.data_x_times(color_value, size) @@ -279,14 +283,9 @@ where fn height(&self) -> u16 { HEIGHT } - - - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) - } } -impl EPD4in2 +impl EPD4in2 where SPI: Write, CS: OutputPin, diff --git a/src/traits/connection_interface.rs b/src/traits/connection_interface.rs index 842adb0..978ba2e 100644 --- a/src/traits/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -7,7 +7,7 @@ use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// -pub(crate) struct ConnectionInterface { +pub(crate) struct ConnectionInterface { /// SPI spi: SPI, /// CS for SPI @@ -18,28 +18,24 @@ pub(crate) struct ConnectionInterface { dc: DC, /// Pin for Reseting rst: RST, - /// The concrete Delay implementation - delay: D, } -impl - ConnectionInterface +impl + ConnectionInterface where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Self { + pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST) -> Self { ConnectionInterface { spi, cs, busy, dc, rst, - delay, } } @@ -130,35 +126,27 @@ where /// Most likely there was a mistake with the 2in9 busy connection /// //TODO: use the #cfg feature to make this compile the right way for the certain types pub(crate) fn wait_until_idle(&mut self, is_busy_low: bool) { - self.delay_ms(1); + // TODO: removal of delay. TEST! + //self.delay_ms(1); //low: busy, high: idle while (is_busy_low && self.busy.is_low()) || (!is_busy_low && self.busy.is_high()) { - //TODO: shorten the time? it was 100 in the beginning - self.delay_ms(5); + //TODO: REMOVAL of DELAY: it's only waiting for the signal anyway and should continue work asap + //old: shorten the time? it was 100 in the beginning + //self.delay_ms(5); } } - /// Abstraction of setting the delay for simpler calls - /// - /// maximum delay ~65 seconds (u16:max in ms) - pub(crate) fn delay_ms(&mut self, delay: u16) { - self.delay.delay_ms(delay); - } - /// Resets the device. /// /// Often used to awake the module from deep sleep. See [EPD4in2::sleep()](EPD4in2::sleep()) /// /// TODO: Takes at least 400ms of delay alone, can it be shortened? - pub(crate) fn reset(&mut self) { + pub(crate) fn reset>(&mut self, delay: &mut DELAY) { self.rst.set_low(); - //TODO: why 200ms? (besides being in the arduino version) - self.delay_ms(200); - + delay.delay_ms(200); self.rst.set_high(); - //TODO: same as 3 lines above - self.delay_ms(200); + delay.delay_ms(200); } } diff --git a/src/traits/mod.rs b/src/traits/mod.rs index dc8bbb6..c6f58ea 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -26,14 +26,13 @@ trait LUTSupport { } -pub(crate) trait InternalWiAdditions +pub(crate) trait InternalWiAdditions where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// This initialises the EPD and powers it up /// @@ -45,26 +44,25 @@ where /// This function calls [reset()](WaveshareInterface::reset()), /// so you don't need to call reset your self when trying to wake your device up /// after setting it to sleep. - fn init(&mut self) -> Result<(), ERR>; + fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; } -pub trait WaveshareInterface +pub trait WaveshareInterface where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: DELAY, ) -> Result where Self: Sized; @@ -92,11 +90,6 @@ where /// Get the height of the display fn height(&self) -> u16; - /// Abstraction of setting the delay for simpler calls - /// - /// maximum delay ~65 seconds (u16:max in ms) - fn delay_ms(&mut self, delay: u16); - /// Transmit a full frame to the SRAM of the EPD fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR>; From 829a9e71eb77ba33e94d138e30f183e914a582d9 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 13:30:52 +0200 Subject: [PATCH 04/14] Fix previous Delay Commit Some stuff was forgotten in the previous commit --- src/epd1in54/mod.rs | 3 +-- src/epd2in9/mod.rs | 2 +- src/epd4in2/mod.rs | 3 +-- src/traits/mod.rs | 6 +++--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 5d0c7f1..7b19b7d 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -94,7 +94,7 @@ where } -impl WaveshareInterface +impl WaveshareInterface for EPD1in54 where SPI: Write, @@ -102,7 +102,6 @@ where BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { fn width(&self) -> u16 { WIDTH diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index db6fa03..7eaeccd 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -189,7 +189,7 @@ where } } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 3905f99..f4bac81 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -285,14 +285,13 @@ where } } -impl EPD4in2 +impl EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - D: DelayUs + DelayMs, { fn command(&mut self, command: Command) -> Result<(), ERR> { self.interface.cmd(command) diff --git a/src/traits/mod.rs b/src/traits/mod.rs index c6f58ea..0ec5c8f 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -61,8 +61,8 @@ where /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards - fn new>( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: DELAY, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result where Self: Sized; @@ -75,7 +75,7 @@ where /// and initialising which already contains the reset fn sleep(&mut self) -> Result<(), ERR>; - fn wake_up(&mut self) -> Result<(), ERR>; + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; /// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame()) From 0ddaf9679a358c3078e8bcad06bba23d7242a3f7 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 09:20:34 +0200 Subject: [PATCH 05/14] Deletion of single u8 data Transfers and renaming of various functions command_with_data -> cmd_with_data command -> cmd multiple_data -> data --- src/epd1in54/mod.rs | 57 ++++++++--------- src/epd2in9/mod.rs | 61 +++++++----------- src/epd4in2/command.rs | 11 ++++ src/epd4in2/mod.rs | 89 +++++++++++---------------- src/interface/connection_interface.rs | 29 +++------ 5 files changed, 105 insertions(+), 142 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 0f282ca..93de08e 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -65,33 +65,30 @@ where // 0.. A[8] // 0.. B[2:0] // Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?) - self.interface.command(Command::DRIVER_OUTPUT_CONTROL)?; - self.interface.data(HEIGHT as u8)?; - self.interface.data((HEIGHT >> 8) as u8)?; - self.interface.data(0x00)?; + self.interface.cmd_with_data( + Command::DRIVER_OUTPUT_CONTROL, + &[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.command(Command::BOOSTER_SOFT_START_CONTROL)?; - self.interface.data(0xD7)?; - self.interface.data(0xD6)?; - self.interface.data(0x9D)?; + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.command_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; + self.interface.cmd_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate - self.interface.command_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; + self.interface.cmd_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line - self.interface.command_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?; + self.interface.cmd_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_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; + self.interface.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; self.set_lut() } @@ -140,7 +137,7 @@ where fn sleep(&mut self) -> Result<(), E> { // 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.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) @@ -152,7 +149,7 @@ where fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { self.use_full_frame()?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos @@ -167,18 +164,18 @@ where self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } fn display_frame(&mut self) -> Result<(), E> { // 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_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.command(Command::MASTER_ACTIVATION)?; + self.interface.cmd(Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.command(Command::NOP) + self.interface.cmd(Command::NOP) } fn clear_frame(&mut self) -> Result<(), E> { @@ -187,7 +184,7 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.command(Command::WRITE_RAM)?; + self.interface.cmd(Command::WRITE_RAM)?; self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) } @@ -235,25 +232,25 @@ where // 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_START_END_POSITION)?; - self.interface.data((start_x >> 3) as u8)?; - self.interface.data((end_x >> 3) as u8)?; + self.interface.cmd_with_data( + Command::SET_RAM_X_ADDRESS_START_END_POSITION, + &[(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.command(Command::SET_RAM_Y_ADDRESS_START_END_POSITION)?; - self.interface.data(start_y as u8)?; - self.interface.data((start_y >> 8) as u8)?; - self.interface.data(end_y as u8)?; - self.interface.data((end_y >> 8) as u8) + self.interface.cmd_with_data( + Command::SET_RAM_Y_ADDRESS_START_END_POSITION, + &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] + ) } 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_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] - self.interface.command_with_data( + self.interface.cmd_with_data( Command::SET_RAM_Y_ADDRESS_COUNTER, &[ y as u8, @@ -281,7 +278,7 @@ where fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { assert!(buffer.len() == 30); - self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 90b5e02..5a86d83 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -64,42 +64,30 @@ where // 0.. A[8] // 0.. B[2:0] // Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?) - self.interface.command(Command::DRIVER_OUTPUT_CONTROL)?; - self.interface.data(HEIGHT as u8)?; - self.interface.data((HEIGHT >> 8) as u8)?; - self.interface.data(0x00)?; + self.interface.cmd_with_data(Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 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.command(Command::BOOSTER_SOFT_START_CONTROL)?; - self.interface.data(0xD7)?; - self.interface.data(0xD6)?; - self.interface.data(0x9D)?; + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.command(Command::WRITE_VCOM_REGISTER)?; - self.interface.data(0xA8)?; + self.interface.cmd_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.cmd_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.cmd_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.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; self.set_lut() } - - } impl @@ -141,7 +129,7 @@ where fn sleep(&mut self) -> Result<(), ERR> { // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode //TODO: is 0x00 needed here? (see also epd1in54) - self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) @@ -158,7 +146,7 @@ where fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { self.use_full_frame()?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos @@ -173,18 +161,18 @@ where self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_counter(x, y)?; - self.interface.command_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(Command::WRITE_RAM, buffer) } 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_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.command(Command::MASTER_ACTIVATION)?; + self.interface.cmd(Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.command(Command::NOP) + self.interface.cmd(Command::NOP) } fn clear_frame(&mut self) -> Result<(), ERR> { @@ -193,7 +181,7 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.command(Command::WRITE_RAM)?; + self.interface.cmd(Command::WRITE_RAM)?; self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) } @@ -240,27 +228,24 @@ where // 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_START_END_POSITION)?; - self.interface.data((start_x >> 3) as u8)?; - self.interface.data((end_x >> 3) as u8)?; + self.interface.cmd_with_data( + Command::SET_RAM_X_ADDRESS_START_END_POSITION, + &[(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.command(Command::SET_RAM_Y_ADDRESS_START_END_POSITION)?; - self.interface.data(start_y as u8)?; - self.interface.data((start_y >> 8) as u8)?; - self.interface.data(end_y as u8)?; - self.interface.data((end_y >> 8) as u8) + self.interface.cmd_with_data(Command::SET_RAM_Y_ADDRESS_START_END_POSITION, + &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] + ) } 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_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_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)?; - self.interface.data(y as u8)?; - self.interface.data((y >> 8) as u8)?; + self.interface.cmd_with_data(Command::SET_RAM_Y_ADDRESS_COUNTER, &[y as u8, (y >> 8) as u8])?; self.wait_until_idle(); Ok(()) @@ -283,7 +268,7 @@ where fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { assert!(buffer.len() == 30); - self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd4in2/command.rs b/src/epd4in2/command.rs index 75800cf..80c016b 100644 --- a/src/epd4in2/command.rs +++ b/src/epd4in2/command.rs @@ -12,8 +12,18 @@ use interface; #[derive(Copy, Clone)] pub(crate) enum Command { /// Set Resolution, LUT selection, BWR pixels, gate scan direction, source shift direction, booster switch, soft reset + /// One Byte of Data: + /// 0x0F Red Mode, LUT from OTP + /// 0x1F B/W Mode, LUT from OTP + /// 0x2F Red Mode, LUT set by registers + /// 0x3F B/W Mode, LUT set by registers PANEL_SETTING = 0x00, /// selecting internal and external power + /// self.send_data(0x03)?; //VDS_EN, VDG_EN + /// self.send_data(0x00)?; //VCOM_HV, VGHL_LV[1], VGHL_LV[0] + /// self.send_data(0x2b)?; //VDH + /// self.send_data(0x2b)?; //VDL + /// self.send_data(0xff)?; //VDHR POWER_SETTING = 0x01, /// After the Power Off command, the driver will power off following the Power Off Sequence. This command will turn off charge /// pump, T-con, source driver, gate driver, VCOM, and temperature sensor, but register data will be kept until VDD becomes OFF. @@ -26,6 +36,7 @@ pub(crate) enum Command { /// This command enables the internal bandgap, which will be cleared by the next POF. POWER_ON_MEASURE = 0x05, /// Starting data transmission + /// 3-times: self.send_data(0x17)?; //07 0f 17 1f 27 2F 37 2f BOOSTER_SOFT_START = 0x06, /// After this command is transmitted, the chip would enter the deep-sleep mode to save power. /// diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 81ca731..8d84d14 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -90,43 +90,24 @@ where self.interface.reset(); // set the power settings - self.command(Command::POWER_SETTING)?; - self.send_data(0x03)?; //VDS_EN, VDG_EN - self.send_data(0x00)?; //VCOM_HV, VGHL_LV[1], VGHL_LV[0] - self.send_data(0x2b)?; //VDH - self.send_data(0x2b)?; //VDL - self.send_data(0xff)?; //VDHR + self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; // start the booster - self.command(Command::BOOSTER_SOFT_START)?; - for _ in 0..3 { - self.send_data(0x17)?; //07 0f 17 1f 27 2F 37 2f - } + self.interface.cmd_with_data(Command::BOOSTER_SOFT_START, &[0x17, 0x17, 0x17])?; // power on self.command(Command::POWER_ON)?; self.wait_until_idle(); // set the panel settings - self.command(Command::PANEL_SETTING)?; - // 0x0F Red Mode, LUT from OTP - // 0x1F B/W Mode, LUT from OTP - // 0x2F Red Mode, LUT set by registers - // 0x3F B/W Mode, LUT set by registers - self.send_data(0x3F)?; - - // the values used by waveshare before for the panel settings - // instead of our one liner: - // SendData(0xbf); // KW-BF KWR-AF BWROTP 0f - // SendData(0x0b); + self.cmd_with_data(Command::PANEL_SETTING, &[0x3F])?; // Set Frequency, 200 Hz didn't work on my board // 150Hz and 171Hz wasn't tested yet // TODO: Test these other frequencies // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ DEFAULT: 3c 50Hz - self.command(Command::PLL_CONTROL)?; - self.send_data(0x3A)?; - + self.cmd_with_data(Command::PLL_CONTROL, &[0x3A])?; + self.set_lut()?; Ok(()) @@ -179,20 +160,20 @@ where //TODO: is such a long delay really needed inbetween? fn sleep(&mut self) -> Result<(), ERR> { - self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating + self.interface.cmd_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); self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast for _ in 0..4 { - self.send_data(0x00)?; + self.send_data(&[0x00])?; } self.delay_ms(100); self.command(Command::POWER_OFF)?; self.wait_until_idle(); - self.interface.command_with_data(Command::DEEP_SLEEP, &[0xA5]) + self.interface.cmd_with_data(Command::DEEP_SLEEP, &[0xA5]) } fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { @@ -200,11 +181,11 @@ where self.send_resolution()?; - self.interface.command_with_data(Command::VCM_DC_SETTING, &[0x12])?; + self.interface.cmd_with_data(Command::VCM_DC_SETTING, &[0x12])?; //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.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; + self.interface.cmd_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; self.command(Command::DATA_START_TRANSMISSION_1)?; @@ -212,7 +193,7 @@ where self.delay_ms(2); - self.interface.command_with_data(Command::DATA_START_TRANSMISSION_2, buffer) + self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer) } fn update_partial_frame( @@ -230,20 +211,20 @@ where self.command(Command::PARTIAL_IN)?; self.command(Command::PARTIAL_WINDOW)?; - self.send_data((x >> 8) as u8)?; + self.send_data(&[(x >> 8) as u8])?; let tmp = x & 0xf8; - self.send_data(tmp as u8)?; // x should be the multiple of 8, the last 3 bit will always be ignored + self.send_data(&[tmp as u8])?; // x should be the multiple of 8, the last 3 bit will always be ignored let tmp = tmp + width - 1; - self.send_data((tmp >> 8) as u8)?; - self.send_data((tmp | 0x07) as u8)?; + self.send_data(&[(tmp >> 8) as u8])?; + self.send_data(&[(tmp | 0x07) as u8])?; - self.send_data((y >> 8) as u8)?; - self.send_data(y as u8)?; + self.send_data(&[(y >> 8) as u8])?; + self.send_data(&[y as u8])?; - self.send_data(((y + height - 1) >> 8) as u8)?; - self.send_data((y + height - 1) as u8)?; + self.send_data(&[((y + height - 1) >> 8) as u8])?; + self.send_data(&[(y + height - 1) as u8])?; - self.send_data(0x01)?; // Gates scan both inside and outside of the partial window. (default) + self.send_data(&[0x01])?; // Gates scan both inside and outside of the partial window. (default) //TODO: handle dtm somehow let is_dtm1 = false; @@ -253,7 +234,7 @@ where self.command(Command::DATA_START_TRANSMISSION_2)? } - self.send_multiple_data(buffer)?; + self.send_data(buffer)?; self.command(Command::PARTIAL_OUT) } @@ -315,15 +296,15 @@ where D: DelayUs + DelayMs, { fn command(&mut self, command: Command) -> Result<(), ERR> { - self.interface.command(command) + self.interface.cmd(command) } - fn send_data(&mut self, val: u8) -> Result<(), ERR> { - self.interface.data(val) + fn send_data(&mut self, data: &[u8]) -> Result<(), ERR> { + self.interface.data(data) } - fn send_multiple_data(&mut self, data: &[u8]) -> Result<(), ERR> { - self.interface.multiple_data(data) + fn cmd_with_data(&mut self, command: Command, data: &[u8]) -> Result<(), ERR> { + self.interface.cmd_with_data(command, data) } fn wait_until_idle(&mut self) { @@ -335,10 +316,10 @@ where let h = self.height(); self.command(Command::RESOLUTION_SETTING)?; - self.send_data((w >> 8) as u8)?; - self.send_data(w as u8)?; - self.send_data((h >> 8) as u8)?; - self.send_data(h as u8) + self.send_data(&[(w >> 8) as u8])?; + self.send_data(&[w as u8])?; + self.send_data(&[(h >> 8) as u8])?; + self.send_data(&[h as u8]) } /// Fill the look-up table for the EPD @@ -372,23 +353,23 @@ where ) -> Result<(), ERR> { // LUT VCOM self.command(Command::LUT_FOR_VCOM)?; - self.send_multiple_data(lut_vcom)?; + self.send_data(lut_vcom)?; // LUT WHITE to WHITE self.command(Command::LUT_WHITE_TO_WHITE)?; - self.send_multiple_data(lut_ww)?; + self.send_data(lut_ww)?; // LUT BLACK to WHITE self.command(Command::LUT_BLACK_TO_WHITE)?; - self.send_multiple_data(lut_bw)?; + self.send_data(lut_bw)?; // LUT WHITE to BLACK self.command(Command::LUT_WHITE_TO_BLACK)?; - self.send_multiple_data(lut_wb)?; + self.send_data(lut_wb)?; // LUT BLACK to BLACK self.command(Command::LUT_BLACK_TO_BLACK)?; - self.send_multiple_data(lut_bb)?; + self.send_data(lut_bb)?; Ok(()) } diff --git a/src/interface/connection_interface.rs b/src/interface/connection_interface.rs index f825807..d55750e 100644 --- a/src/interface/connection_interface.rs +++ b/src/interface/connection_interface.rs @@ -48,7 +48,7 @@ where /// Enables direct interaction with the device with the help of [data()](ConnectionInterface::data()) /// /// //TODO: make public? - pub(crate) fn command(&mut self, command: T) -> Result<(), ERR> { + pub(crate) fn cmd(&mut self, command: T) -> Result<(), ERR> { // low for commands self.dc.set_low(); @@ -56,25 +56,25 @@ where self.with_cs(|epd| epd.spi.write(&[command.address()])) } - /// Basic function for sending a single u8 of data over spi + /// Basic function for sending an array of u8-values of data over spi /// - /// Enables direct interaction with the device with the help of [Ecommand()](ConnectionInterface::command()) + /// Enables direct interaction with the device with the help of [command()](EPD4in2::command()) /// /// //TODO: make public? - pub(crate) fn data(&mut self, val: u8) -> Result<(), ERR> { + pub(crate) fn data(&mut self, data: &[u8]) -> Result<(), ERR> { // high for data self.dc.set_high(); - // Transfer data (u8) over spi - self.with_cs(|epd| epd.spi.write(&[val])) + // Transfer data (u8-array) over spi + self.with_cs(|epd| epd.spi.write(data)) } /// Basic function for sending [Commands](Command) and the data belonging to it. /// /// //TODO: make public? - pub(crate) fn command_with_data(&mut self, command: T, data: &[u8]) -> Result<(), ERR> { - self.command(command)?; - self.multiple_data(data) + pub(crate) fn cmd_with_data(&mut self, command: T, data: &[u8]) -> Result<(), ERR> { + self.cmd(command)?; + self.data(data) } /// Basic function for sending the same byte of data (one u8) multiple times over spi @@ -99,18 +99,7 @@ where }) } - /// Basic function for sending an array of u8-values of data over spi - /// - /// Enables direct interaction with the device with the help of [command()](EPD4in2::command()) - /// - /// //TODO: make public? - pub(crate) fn multiple_data(&mut self, data: &[u8]) -> Result<(), ERR> { - // high for data - self.dc.set_high(); - // Transfer data (u8-array) over spi - self.with_cs(|epd| epd.spi.write(data)) - } // spi write helper/abstraction function fn with_cs(&mut self, f: F) -> Result<(), ERR> From 1791388a8bd81df832174c40672e40376d666160 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 11:37:36 +0200 Subject: [PATCH 06/14] Renamed folder `interface` to `traits` --- src/epd1in54/mod.rs | 4 ++-- src/epd2in9/mod.rs | 4 ++-- src/epd4in2/command.rs | 6 +++--- src/epd4in2/mod.rs | 2 +- src/lib.rs | 4 ++-- src/{interface => traits}/connection_interface.rs | 2 +- src/{interface => traits}/mod.rs | 0 src/type_a/command.rs | 6 +++--- 8 files changed, 14 insertions(+), 14 deletions(-) rename src/{interface => traits}/connection_interface.rs (99%) rename src/{interface => traits}/mod.rs (100%) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 93de08e..f90a814 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -33,9 +33,9 @@ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use color::Color; -use interface::{WaveshareInterface}; +use traits::{WaveshareInterface}; -use interface::connection_interface::ConnectionInterface; +use traits::connection_interface::ConnectionInterface; /// EPD1in54 driver /// diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 5a86d83..5b8145e 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -32,9 +32,9 @@ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use color::Color; -use interface::*; +use traits::*; -use interface::connection_interface::ConnectionInterface; +use traits::connection_interface::ConnectionInterface; /// EPD2in9 driver /// diff --git a/src/epd4in2/command.rs b/src/epd4in2/command.rs index 80c016b..60173e8 100644 --- a/src/epd4in2/command.rs +++ b/src/epd4in2/command.rs @@ -1,5 +1,5 @@ //! SPI Commands for the Waveshare 4.2" E-Ink Display -use interface; +use traits; /// EPD4IN2 commands /// /// Should rarely (never?) be needed directly. @@ -155,7 +155,7 @@ pub(crate) enum Command { POWER_SAVING = 0xE3, } -impl interface::Command for Command { +impl traits::Command for Command { /// Returns the address of the command fn address(self) -> u8 { self as u8 @@ -165,7 +165,7 @@ impl interface::Command for Command { #[cfg(test)] mod tests { use super::*; - use interface::Command as CommandTrait; + use traits::Command as CommandTrait; #[test] fn command_addr() { diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 8d84d14..9ab5ab5 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -51,7 +51,7 @@ use hal::{ digital::*, }; -use interface::{connection_interface::ConnectionInterface, WaveshareInterface, InternalWiAdditions}; +use traits::{connection_interface::ConnectionInterface, WaveshareInterface, InternalWiAdditions}; //The Lookup Tables for the Display mod constants; diff --git a/src/lib.rs b/src/lib.rs index 42ceaf3..5e34505 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,8 +50,8 @@ use hal::spi::{Mode, Phase, Polarity}; #[cfg(feature = "graphics")] pub mod drawing; -mod interface; -pub use interface::{WaveshareInterface}; +mod traits; +pub use traits::{WaveshareInterface}; pub mod color; diff --git a/src/interface/connection_interface.rs b/src/traits/connection_interface.rs similarity index 99% rename from src/interface/connection_interface.rs rename to src/traits/connection_interface.rs index d55750e..842adb0 100644 --- a/src/interface/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -3,7 +3,7 @@ use hal::{ digital::*, }; -use interface::Command; +use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// diff --git a/src/interface/mod.rs b/src/traits/mod.rs similarity index 100% rename from src/interface/mod.rs rename to src/traits/mod.rs diff --git a/src/type_a/command.rs b/src/type_a/command.rs index 5272c07..36d6027 100644 --- a/src/type_a/command.rs +++ b/src/type_a/command.rs @@ -1,6 +1,6 @@ //! SPI Commands for the Waveshare 2.9" and 1.54" E-Ink Display -use interface; +use traits; /// EPD1in54 and EPD2IN9 commands /// @@ -71,7 +71,7 @@ pub(crate) enum Command { NOP = 0xFF, } -impl interface::Command for Command { +impl traits::Command for Command { /// Returns the address of the command fn address(self) -> u8 { self as u8 @@ -81,7 +81,7 @@ impl interface::Command for Command { #[cfg(test)] mod tests { use super::Command; - use interface::Command as CommandTrait; + use traits::Command as CommandTrait; #[test] fn command_addr() { From fabc5f262efc25111c94d3b531a27f07872756f2 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 13:23:43 +0200 Subject: [PATCH 07/14] Make delay a function parameter This change makes delay a function parameter where necessary and stops the need of owning the delay --- src/epd1in54/mod.rs | 36 +++++++++----------- src/epd2in9/mod.rs | 37 +++++++++------------ src/epd4in2/mod.rs | 53 +++++++++++++++--------------- src/traits/connection_interface.rs | 36 +++++++------------- src/traits/mod.rs | 17 +++------- 5 files changed, 73 insertions(+), 106 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index f90a814..5d0c7f1 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -39,26 +39,25 @@ use traits::connection_interface::ConnectionInterface; /// EPD1in54 driver /// -pub struct EPD1in54 { +pub struct EPD1in54 { /// SPI - interface: ConnectionInterface, + interface: ConnectionInterface, /// EPD (width, height) //epd: EPD, /// Color background_color: Color, } -impl EPD1in54 +impl EPD1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), E> { - self.interface.reset(); + fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.interface.reset(delay); // 3 Databytes: // A[7:0] @@ -95,8 +94,8 @@ where } -impl WaveshareInterface - for EPD1in54 +impl WaveshareInterface + for EPD1in54 where SPI: Write, CS: OutputPin, @@ -113,23 +112,23 @@ where HEIGHT } - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let mut epd = EPD1in54 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } - fn wake_up(&mut self) -> Result<(), E> { - self.init() + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.init(delay) } @@ -143,10 +142,6 @@ where Ok(()) } - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) - } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { self.use_full_frame()?; self.interface.cmd_with_data(Command::WRITE_RAM, buffer) @@ -199,14 +194,13 @@ where } } -impl EPD1in54 +impl EPD1in54 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, - RST: OutputPin, - D: DelayUs + DelayMs, + RST: OutputPin { fn wait_until_idle(&mut self) { self.interface.wait_until_idle(false); diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 5b8145e..db6fa03 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -38,26 +38,25 @@ use traits::connection_interface::ConnectionInterface; /// EPD2in9 driver /// -pub struct EPD2in9 { +pub struct EPD2in9 { /// SPI - interface: ConnectionInterface, + interface: ConnectionInterface, /// EPD (width, height) //epd: EPD, /// Color background_color: Color, } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), E> { - self.interface.reset(); + fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + self.interface.reset(delay); // 3 Databytes: // A[7:0] @@ -90,16 +89,15 @@ where } } -impl - WaveshareInterface - for EPD2in9 +impl + WaveshareInterface + for EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { fn width(&self) -> u16 { WIDTH @@ -109,17 +107,17 @@ where HEIGHT } - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let mut epd = EPD2in9 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } @@ -135,12 +133,8 @@ where Ok(()) } - fn wake_up(&mut self) -> Result<(), ERR> { - self.init() - } - - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { + self.init(delay) } fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { @@ -195,14 +189,13 @@ where } } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - D: DelayUs + DelayMs, { fn wait_until_idle(&mut self) { self.interface.wait_until_idle(false); diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 9ab5ab5..3905f99 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -64,9 +64,9 @@ use self::command::Command; /// EPD4in2 driver /// -pub struct EPD4in2 { +pub struct EPD4in2 { /// Connection Interface - interface: ConnectionInterface, + interface: ConnectionInterface, /// Background Color color: Color, } @@ -74,20 +74,19 @@ pub struct EPD4in2 { -impl - InternalWiAdditions - for EPD4in2 +impl + InternalWiAdditions + for EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - fn init(&mut self) -> Result<(), ERR> { + fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { // reset the device - self.interface.reset(); + self.interface.reset(delay); // set the power settings self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; @@ -114,16 +113,15 @@ where } } -impl - WaveshareInterface - for EPD4in2 +impl + WaveshareInterface + for EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// @@ -140,8 +138,8 @@ 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); + fn new>(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY) -> Result { + let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); let color = DEFAULT_BACKGROUND_COLOR; let mut epd = EPD4in2 { @@ -149,13 +147,13 @@ where color, }; - epd.init()?; + epd.init(delay)?; Ok(epd) } - fn wake_up(&mut self) -> Result<(), ERR> { - self.init() + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { + self.init(delay) } //TODO: is such a long delay really needed inbetween? @@ -163,13 +161,17 @@ where self.interface.cmd_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); + + //TODO: Removal of delay. TEST! + //self.delay_ms(100); self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast for _ in 0..4 { self.send_data(&[0x00])?; } - self.delay_ms(100); + + //TODO: Removal of delay. TEST! + //self.delay_ms(100); self.command(Command::POWER_OFF)?; self.wait_until_idle(); @@ -191,7 +193,8 @@ where self.command(Command::DATA_START_TRANSMISSION_1)?; self.interface.data_x_times(color_value, buffer.len() as u16)?; - self.delay_ms(2); + //TODO: Removal of delay. TEST! + //self.delay_ms(2); self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer) } @@ -257,7 +260,8 @@ where self.command(Command::DATA_START_TRANSMISSION_1)?; self.interface.data_x_times(color_value, size)?; - self.delay_ms(2); + //TODO: Removal of delay. TEST! + //self.delay_ms(2); self.command(Command::DATA_START_TRANSMISSION_2)?; self.interface.data_x_times(color_value, size) @@ -279,14 +283,9 @@ where fn height(&self) -> u16 { HEIGHT } - - - fn delay_ms(&mut self, delay: u16) { - self.interface.delay_ms(delay) - } } -impl EPD4in2 +impl EPD4in2 where SPI: Write, CS: OutputPin, diff --git a/src/traits/connection_interface.rs b/src/traits/connection_interface.rs index 842adb0..978ba2e 100644 --- a/src/traits/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -7,7 +7,7 @@ use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// -pub(crate) struct ConnectionInterface { +pub(crate) struct ConnectionInterface { /// SPI spi: SPI, /// CS for SPI @@ -18,28 +18,24 @@ pub(crate) struct ConnectionInterface { dc: DC, /// Pin for Reseting rst: RST, - /// The concrete Delay implementation - delay: D, } -impl - ConnectionInterface +impl + ConnectionInterface where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { - pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Self { + pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST) -> Self { ConnectionInterface { spi, cs, busy, dc, rst, - delay, } } @@ -130,35 +126,27 @@ where /// Most likely there was a mistake with the 2in9 busy connection /// //TODO: use the #cfg feature to make this compile the right way for the certain types pub(crate) fn wait_until_idle(&mut self, is_busy_low: bool) { - self.delay_ms(1); + // TODO: removal of delay. TEST! + //self.delay_ms(1); //low: busy, high: idle while (is_busy_low && self.busy.is_low()) || (!is_busy_low && self.busy.is_high()) { - //TODO: shorten the time? it was 100 in the beginning - self.delay_ms(5); + //TODO: REMOVAL of DELAY: it's only waiting for the signal anyway and should continue work asap + //old: shorten the time? it was 100 in the beginning + //self.delay_ms(5); } } - /// Abstraction of setting the delay for simpler calls - /// - /// maximum delay ~65 seconds (u16:max in ms) - pub(crate) fn delay_ms(&mut self, delay: u16) { - self.delay.delay_ms(delay); - } - /// Resets the device. /// /// Often used to awake the module from deep sleep. See [EPD4in2::sleep()](EPD4in2::sleep()) /// /// TODO: Takes at least 400ms of delay alone, can it be shortened? - pub(crate) fn reset(&mut self) { + pub(crate) fn reset>(&mut self, delay: &mut DELAY) { self.rst.set_low(); - //TODO: why 200ms? (besides being in the arduino version) - self.delay_ms(200); - + delay.delay_ms(200); self.rst.set_high(); - //TODO: same as 3 lines above - self.delay_ms(200); + delay.delay_ms(200); } } diff --git a/src/traits/mod.rs b/src/traits/mod.rs index dc8bbb6..c6f58ea 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -26,14 +26,13 @@ trait LUTSupport { } -pub(crate) trait InternalWiAdditions +pub(crate) trait InternalWiAdditions where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// This initialises the EPD and powers it up /// @@ -45,26 +44,25 @@ where /// This function calls [reset()](WaveshareInterface::reset()), /// so you don't need to call reset your self when trying to wake your device up /// after setting it to sleep. - fn init(&mut self) -> Result<(), ERR>; + fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; } -pub trait WaveshareInterface +pub trait WaveshareInterface where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards - fn new( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: DELAY, ) -> Result where Self: Sized; @@ -92,11 +90,6 @@ where /// Get the height of the display fn height(&self) -> u16; - /// Abstraction of setting the delay for simpler calls - /// - /// maximum delay ~65 seconds (u16:max in ms) - fn delay_ms(&mut self, delay: u16); - /// Transmit a full frame to the SRAM of the EPD fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR>; From 2f4ebfecabd6fd3bea6d5f2d1b61ede8ae1c8149 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 13:30:52 +0200 Subject: [PATCH 08/14] Fix previous Delay Commit Some stuff was forgotten in the previous commit --- src/epd1in54/mod.rs | 3 +-- src/epd2in9/mod.rs | 2 +- src/epd4in2/mod.rs | 3 +-- src/traits/mod.rs | 6 +++--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 5d0c7f1..7b19b7d 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -94,7 +94,7 @@ where } -impl WaveshareInterface +impl WaveshareInterface for EPD1in54 where SPI: Write, @@ -102,7 +102,6 @@ where BUSY: InputPin, DC: OutputPin, RST: OutputPin, - Delay: DelayUs + DelayMs, { fn width(&self) -> u16 { WIDTH diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index db6fa03..7eaeccd 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -189,7 +189,7 @@ where } } -impl EPD2in9 +impl EPD2in9 where SPI: Write, CS: OutputPin, diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index 3905f99..f4bac81 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -285,14 +285,13 @@ where } } -impl EPD4in2 +impl EPD4in2 where SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, - D: DelayUs + DelayMs, { fn command(&mut self, command: Command) -> Result<(), ERR> { self.interface.cmd(command) diff --git a/src/traits/mod.rs b/src/traits/mod.rs index c6f58ea..0ec5c8f 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -61,8 +61,8 @@ where /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards - fn new>( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: DELAY, + fn new>( + spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result where Self: Sized; @@ -75,7 +75,7 @@ where /// and initialising which already contains the reset fn sleep(&mut self) -> Result<(), ERR>; - fn wake_up(&mut self) -> Result<(), ERR>; + fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; /// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame()) From 99d328575fff687b8c95ebba709cabfa36ff3680 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 14:25:15 +0200 Subject: [PATCH 09/14] fix examples --- examples/embedded_linux_epd1in54/src/main.rs | 12 ++++++------ examples/embedded_linux_epd4in2/src/main.rs | 19 +++++++++++-------- examples/stm32f3discovery/src/main.rs | 4 ++-- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/embedded_linux_epd1in54/src/main.rs b/examples/embedded_linux_epd1in54/src/main.rs index 0ea9a5e..8754d94 100644 --- a/examples/embedded_linux_epd1in54/src/main.rs +++ b/examples/embedded_linux_epd1in54/src/main.rs @@ -108,16 +108,16 @@ fn run() -> Result<(), std::io::Error> { rst.set_value(1).expect("rst Value set to 1"); // Configure Delay - let delay = Delay {}; + let mut delay = Delay {}; // Setup of the needed pins is finished here // Now the "real" usage of the eink-waveshare-rs crate begins - let mut epd = EPD1in54::new(spi, cs_pin, busy_in, dc, rst, delay)?; + let mut epd = EPD1in54::new(spi, cs_pin, busy_in, dc, rst, &mut delay)?; // Clear the full screen - epd.clear_frame(); - epd.display_frame(); + epd.clear_frame()?; + epd.display_frame()?; // Speeddemo let small_buffer = [Color::Black.get_byte_value(), 16 as u8 / 8 * 16 as u8]; @@ -129,8 +129,8 @@ fn run() -> Result<(), std::io::Error> { } // Clear the full screen - epd.clear_frame(); - epd.display_frame(); + epd.clear_frame()?; + epd.display_frame()?; // Draw some squares let mut small_buffer = [Color::Black.get_byte_value(), 160 as u8 / 8 * 160 as u8]; diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index a5e1f9f..cf74b42 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -26,7 +26,10 @@ use lin_hal::Delay; // from https://github.com/rudihorn/max31865/blob/extra_examples/examples/rpi.rs // (slightly changed now as OutputPin doesn't provide is_high and is_low anymore) extern crate embedded_hal; -use embedded_hal::digital::{InputPin}; +use embedded_hal::{ + digital::{InputPin}, +}; +use embedded_hal::prelude::*; struct HackInputPin<'a> { pin: &'a Pin @@ -59,7 +62,7 @@ impl<'a> InputPin for HackInputPin<'a> { * */ -fn main() { +fn main() -> Result<(), std::io::Error> { // Configure SPI // Settings are taken from @@ -97,7 +100,7 @@ fn main() { rst.set_direction(Direction::Out).expect("rst Direction"); rst.set_value(1).expect("rst Value set to 1"); - let delay = Delay {}; + let mut delay = Delay {}; @@ -105,7 +108,7 @@ fn main() { //TODO: wait for Digital::InputPin //fixed currently with the HackInputPin, see further above - let mut epd4in2 = EPD4in2::new(spi, cs, busy_in, dc, rst, delay).expect("eink inialize error"); + let mut epd4in2 = EPD4in2::new(spi, cs, busy_in, dc, rst, &mut delay).expect("eink inialize error"); //let mut buffer = [0u8, epd4in2.get_width() / 8 * epd4in2.get_height()]; let mut buffer = [0u8; 15000]; @@ -129,7 +132,7 @@ fn main() { epd4in2.update_frame(graphics.get_buffer()).expect("display and transfer error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.delay_ms(3000); + delay.delay_ms(3000u16); epd4in2.clear_frame().expect("clear frame error"); @@ -150,7 +153,7 @@ fn main() { epd4in2.update_partial_frame(circle_graphics.get_buffer(), 160,240, 32, 32).expect("Partial Window Error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.delay_ms(3000); + delay.delay_ms(3000u16); @@ -162,7 +165,7 @@ fn main() { epd4in2.update_frame(graphics.get_buffer()).expect("display and transfer error"); epd4in2.display_frame().expect("Display Frame Error"); - epd4in2.delay_ms(3000); + delay.delay_ms(3000u16); - epd4in2.sleep().expect("sleeping error"); + epd4in2.sleep() } diff --git a/examples/stm32f3discovery/src/main.rs b/examples/stm32f3discovery/src/main.rs index a8a4a72..1de6e4a 100644 --- a/examples/stm32f3discovery/src/main.rs +++ b/examples/stm32f3discovery/src/main.rs @@ -109,7 +109,7 @@ fn main() -> ! { // rst.set_value(1).expect("rst Value set to 1"); // // Configure Delay - let delay = Delay::new(cp.SYST, clocks); + let mut delay = Delay::new(cp.SYST, clocks); // copied from the l3gd20 example // The `L3gd20` abstraction exposed by the `f3` crate requires a specific pin configuration to @@ -130,7 +130,7 @@ fn main() -> ! { // // Setup of the needed pins is finished here // // Now the "real" usage of the eink-waveshare-rs crate begins - let mut epd = EPD1in54::new(spi, cs, busy, dc, rst, delay).unwrap(); + let mut epd = EPD1in54::new(spi, cs, busy, dc, rst, &mut delay).unwrap(); From 6a24acf62bdd1ff49913ad9a2e68b0d4e70a5ed6 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 14:43:18 +0200 Subject: [PATCH 10/14] Removed data_x_times function --- src/epd1in54/mod.rs | 7 +++++-- src/epd2in9/mod.rs | 7 +++++-- src/epd4in2/mod.rs | 20 ++++++++++++++------ src/traits/connection_interface.rs | 24 ------------------------ 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 7b19b7d..6c17f3e 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -178,8 +178,11 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.cmd(Command::WRITE_RAM)?; - self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) + //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? + self.interface.cmd_with_data( + Command::WRITE_RAM, + &[color; WIDTH as usize / 8 * HEIGHT as usize] + ) } diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 7eaeccd..35199f0 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -175,8 +175,11 @@ where // clear the ram with the background color let color = self.background_color.get_byte_value(); - self.interface.cmd(Command::WRITE_RAM)?; - self.interface.data_x_times(color, WIDTH / 8 * HEIGHT) + //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? + self.interface.cmd_with_data( + Command::WRITE_RAM, + &[color; WIDTH as usize / 8 * HEIGHT as usize] + ) } /// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame()) diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index f4bac81..ca8e494 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -191,7 +191,9 @@ where self.command(Command::DATA_START_TRANSMISSION_1)?; - self.interface.data_x_times(color_value, buffer.len() as u16)?; + for _ in 0..buffer.len() { + self.send_data(&[color_value])?; + } //TODO: Removal of delay. TEST! //self.delay_ms(2); @@ -254,17 +256,23 @@ where fn clear_frame(&mut self) -> Result<(), ERR> { self.send_resolution()?; - let size = WIDTH / 8 * HEIGHT; + //let size = WIDTH as usize / 8 * HEIGHT as usize; let color_value = self.color.get_byte_value(); - self.command(Command::DATA_START_TRANSMISSION_1)?; - self.interface.data_x_times(color_value, size)?; + //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? + self.interface.cmd_with_data( + Command::DATA_START_TRANSMISSION_1, + &[color_value; WIDTH as usize / 8 * HEIGHT as usize] + )?; //TODO: Removal of delay. TEST! //self.delay_ms(2); - self.command(Command::DATA_START_TRANSMISSION_2)?; - self.interface.data_x_times(color_value, size) + //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? + self.interface.cmd_with_data( + Command::DATA_START_TRANSMISSION_2, + &[color_value; WIDTH as usize / 8 * HEIGHT as usize] + ) } /// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame()) diff --git a/src/traits/connection_interface.rs b/src/traits/connection_interface.rs index 978ba2e..a3da8c6 100644 --- a/src/traits/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -73,30 +73,6 @@ where self.data(data) } - /// Basic function for sending the same byte of data (one u8) multiple times over spi - /// - /// Enables direct interaction with the device with the help of [command()](ConnectionInterface::command()) - /// - /// //TODO: make public? - pub(crate) fn data_x_times( - &mut self, - val: u8, - repetitions: u16, - ) -> Result<(), ERR> { - // high for data - self.dc.set_high(); - - // Transfer data (u8) over spi - self.with_cs(|epd| { - for _ in 0..repetitions { - epd.spi.write(&[val])?; - } - Ok(()) - }) - } - - - // spi write helper/abstraction function fn with_cs(&mut self, f: F) -> Result<(), ERR> where From 87832f604365f0f9be1a6e9ad9e05a7bb58d3a74 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 16:06:27 +0200 Subject: [PATCH 11/14] Made SPI shareable Disown spi for better shareabilty --- src/epd1in54/mod.rs | 99 ++++++++------- src/epd2in9/mod.rs | 108 +++++++++-------- src/epd4in2/mod.rs | 186 ++++++++++++++++------------- src/traits/connection_interface.rs | 36 +++--- src/traits/mod.rs | 23 ++-- 5 files changed, 241 insertions(+), 211 deletions(-) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index 6c17f3e..d220532 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -48,15 +48,15 @@ pub struct EPD1in54 { background_color: Color, } -impl EPD1in54 +impl EPD1in54 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, { - fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + fn init>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { self.interface.reset(delay); // 3 Databytes: @@ -65,6 +65,7 @@ where // 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::DRIVER_OUTPUT_CONTROL, &[HEIGHT as u8, (HEIGHT >> 8) as u8, 0x00] )?; @@ -74,27 +75,27 @@ where // 1 .. B[6:0] = 0xCE | 0xD6 // 1 .. C[6:0] = 0x8D | 0x9D //TODO: test - self.interface.cmd_with_data(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; + self.interface.cmd_with_data(spi, Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.cmd_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; + self.interface.cmd_with_data(spi, Command::WRITE_VCOM_REGISTER, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate - self.interface.cmd_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; + self.interface.cmd_with_data(spi, Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line - self.interface.cmd_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?; + self.interface.cmd_with_data(spi, 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.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; + self.interface.cmd_with_data(spi, Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; - self.set_lut() + self.set_lut(spi) } } -impl WaveshareInterface +impl WaveshareInterface for EPD1in54 where SPI: Write, @@ -112,74 +113,76 @@ where } fn new>( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, - ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); + spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, + ) -> Result { + let interface = ConnectionInterface::new(cs, busy, dc, rst); let mut epd = EPD1in54 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init(delay)?; + epd.init(spi, delay)?; Ok(epd) } - fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), E> { - self.init(delay) + fn wake_up>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { + self.init(spi, delay) } - fn sleep(&mut self) -> Result<(), E> { + fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // 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(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(spi, Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { - self.use_full_frame()?; - self.interface.cmd_with_data(Command::WRITE_RAM, buffer) + fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { + self.use_full_frame(spi)?; + self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer) } //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: u16, y: u16, width: u16, height: u16, - ) -> Result<(), E> { - self.set_ram_area(x, y, x + width, y + height)?; - self.set_ram_counter(x, y)?; + ) -> Result<(), SPI::Error> { + self.set_ram_area(spi, x, y, x + width, y + height)?; + self.set_ram_counter(spi, x, y)?; - self.interface.cmd_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer) } - fn display_frame(&mut self) -> Result<(), E> { + fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // 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(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(spi, Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.cmd(Command::MASTER_ACTIVATION)?; + self.interface.cmd(spi, Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.cmd(Command::NOP) + self.interface.cmd(spi, Command::NOP) } - fn clear_frame(&mut self) -> Result<(), E> { - self.use_full_frame()?; + fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.use_full_frame(spi)?; // clear the ram with the background color let color = self.background_color.get_byte_value(); //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? self.interface.cmd_with_data( + spi, Command::WRITE_RAM, &[color; WIDTH as usize / 8 * HEIGHT as usize] ) @@ -196,9 +199,9 @@ where } } -impl EPD1in54 +impl EPD1in54 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, @@ -208,45 +211,49 @@ where self.interface.wait_until_idle(false); } - pub(crate) fn use_full_frame(&mut self) -> Result<(), E> { + pub(crate) fn use_full_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // choose full frame/ram - self.set_ram_area(0, 0, WIDTH - 1, HEIGHT - 1)?; + self.set_ram_area(spi, 0, 0, WIDTH - 1, HEIGHT - 1)?; // start from the beginning - self.set_ram_counter(0, 0) + self.set_ram_counter(spi, 0, 0) } pub(crate) fn set_ram_area( - &mut self, + &mut self, + spi: &mut SPI, start_x: u16, start_y: u16, end_x: u16, end_y: u16, - ) -> Result<(), E> { + ) -> Result<(), SPI::Error> { 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::SET_RAM_X_ADDRESS_START_END_POSITION, &[(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::SET_RAM_Y_ADDRESS_START_END_POSITION, &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] ) } - pub(crate) fn set_ram_counter(&mut self, x: u16, y: u16) -> Result<(), E> { + pub(crate) fn set_ram_counter(&mut self, spi: &mut SPI, x: u16, y: u16) -> Result<(), SPI::Error> { // 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(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_with_data(spi, Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] self.interface.cmd_with_data( + spi, Command::SET_RAM_Y_ADDRESS_COUNTER, &[ y as u8, @@ -258,13 +265,13 @@ where } /// Uses the slower full update - pub fn set_lut(&mut self) -> Result<(), E> { - self.set_lut_helper(&LUT_FULL_UPDATE) + pub fn set_lut(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.set_lut_helper(spi, &LUT_FULL_UPDATE) } /// Uses the quick partial refresh - pub fn set_lut_quick(&mut self) -> Result<(), E> { - self.set_lut_helper(&LUT_PARTIAL_UPDATE) + pub fn set_lut_quick(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE) } //TODO: assert length for LUT is exactly 30 @@ -272,9 +279,9 @@ where // self.set_lut_helper(buffer) //} - fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { + fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { assert!(buffer.len() == 30); - self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index 35199f0..c154d19 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -47,15 +47,15 @@ pub struct EPD2in9 { background_color: Color, } -impl EPD2in9 +impl EPD2in9 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, { - fn init>(&mut self, delay: &mut DELAY) -> Result<(), E> { + fn init>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { self.interface.reset(delay); // 3 Databytes: @@ -63,37 +63,37 @@ where // 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(Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 0x00])?; + self.interface.cmd_with_data(spi, Command::DRIVER_OUTPUT_CONTROL, &[0x27, 0x01, 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(Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; + self.interface.cmd_with_data(spi, Command::BOOSTER_SOFT_START_CONTROL, &[0xD7, 0xD6, 0x9D])?; // One Databyte with value 0xA8 for 7V VCOM - self.interface.cmd_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?; + self.interface.cmd_with_data(spi, Command::WRITE_VCOM_REGISTER, &[0xA8])?; // One Databyte with default value 0x1A for 4 dummy lines per gate - self.interface.cmd_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; + self.interface.cmd_with_data(spi, Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?; // One Databyte with default value 0x08 for 2us per line - self.interface.cmd_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?; + self.interface.cmd_with_data(spi, 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.cmd_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; + self.interface.cmd_with_data(spi, Command::DATA_ENTRY_MODE_SETTING, &[0x03])?; - self.set_lut() + self.set_lut(spi) } } -impl - WaveshareInterface +impl + WaveshareInterface for EPD2in9 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, @@ -108,75 +108,77 @@ where } fn new>( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, - ) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); + spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, + ) -> Result { + let interface = ConnectionInterface::new(cs, busy, dc, rst); let mut epd = EPD2in9 { interface, background_color: DEFAULT_BACKGROUND_COLOR, }; - epd.init(delay)?; + epd.init(spi, delay)?; Ok(epd) } - fn sleep(&mut self) -> Result<(), ERR> { + fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode //TODO: is 0x00 needed here? (see also epd1in54) - self.interface.cmd_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?; + self.interface.cmd_with_data(spi, Command::DEEP_SLEEP_MODE, &[0x00])?; self.wait_until_idle(); Ok(()) } - fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { - self.init(delay) + fn wake_up>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { + self.init(spi, delay) } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { - self.use_full_frame()?; + fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { + self.use_full_frame(spi)?; - self.interface.cmd_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer) } //TODO: update description: last 3 bits will be ignored for width and x_pos fn update_partial_frame( - &mut self, + &mut self, + spi: &mut SPI, buffer: &[u8], x: u16, y: u16, width: u16, height: u16, - ) -> Result<(), ERR> { - self.set_ram_area(x, y, x + width, y + height)?; - self.set_ram_counter(x, y)?; + ) -> Result<(), SPI::Error> { + self.set_ram_area(spi, x, y, x + width, y + height)?; + self.set_ram_counter(spi, x, y)?; - self.interface.cmd_with_data(Command::WRITE_RAM, buffer) + self.interface.cmd_with_data(spi, Command::WRITE_RAM, buffer) } - fn display_frame(&mut self) -> Result<(), ERR> { + fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // 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(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; + self.interface.cmd_with_data(spi, Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?; - self.interface.cmd(Command::MASTER_ACTIVATION)?; + self.interface.cmd(spi, Command::MASTER_ACTIVATION)?; // MASTER Activation should not be interupted to avoid currption of panel images // therefore a terminate command is send - self.interface.cmd(Command::NOP) + self.interface.cmd(spi, Command::NOP) } - fn clear_frame(&mut self) -> Result<(), ERR> { - self.use_full_frame()?; + fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.use_full_frame(spi)?; // clear the ram with the background color let color = self.background_color.get_byte_value(); //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? self.interface.cmd_with_data( + spi, Command::WRITE_RAM, &[color; WIDTH as usize / 8 * HEIGHT as usize] ) @@ -192,9 +194,9 @@ where } } -impl EPD2in9 +impl EPD2in9 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, @@ -204,57 +206,59 @@ where self.interface.wait_until_idle(false); } - pub(crate) fn use_full_frame(&mut self) -> Result<(), E> { + pub(crate) fn use_full_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { // choose full frame/ram - self.set_ram_area(0, 0, WIDTH - 1, HEIGHT - 1)?; + self.set_ram_area(spi, 0, 0, WIDTH - 1, HEIGHT - 1)?; // start from the beginning - self.set_ram_counter(0, 0) + self.set_ram_counter(spi, 0, 0) } pub(crate) fn set_ram_area( - &mut self, + &mut self, + spi: &mut SPI, start_x: u16, start_y: u16, end_x: u16, end_y: u16, - ) -> Result<(), E> { + ) -> Result<(), SPI::Error> { 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::SET_RAM_X_ADDRESS_START_END_POSITION, &[(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(Command::SET_RAM_Y_ADDRESS_START_END_POSITION, + self.interface.cmd_with_data(spi, Command::SET_RAM_Y_ADDRESS_START_END_POSITION, &[start_y as u8, (start_y >> 8) as u8, end_y as u8, (end_y >> 8) as u8] ) } - pub(crate) fn set_ram_counter(&mut self, x: u16, y: u16) -> Result<(), E> { + pub(crate) fn set_ram_counter(&mut self, spi: &mut SPI, x: u16, y: u16) -> Result<(), SPI::Error> { // 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(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; + self.interface.cmd_with_data(spi, Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?; // 2 Databytes: A[7:0] & 0..A[8] - self.interface.cmd_with_data(Command::SET_RAM_Y_ADDRESS_COUNTER, &[y as u8, (y >> 8) as u8])?; + self.interface.cmd_with_data(spi, Command::SET_RAM_Y_ADDRESS_COUNTER, &[y as u8, (y >> 8) as u8])?; self.wait_until_idle(); Ok(()) } /// Uses the slower full update - pub fn set_lut(&mut self) -> Result<(), E> { - self.set_lut_helper(&LUT_FULL_UPDATE) + pub fn set_lut(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.set_lut_helper(spi, &LUT_FULL_UPDATE) } /// Uses the quick partial refresh - pub fn set_lut_quick(&mut self) -> Result<(), E> { - self.set_lut_helper(&LUT_PARTIAL_UPDATE) + pub fn set_lut_quick(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.set_lut_helper(spi, &LUT_PARTIAL_UPDATE) } //TODO: assert length for LUT is exactly 30 @@ -262,9 +266,9 @@ where // self.set_lut_helper(buffer) //} - fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { + fn set_lut_helper(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { assert!(buffer.len() == 30); - self.interface.cmd_with_data(Command::WRITE_LUT_REGISTER, buffer) + self.interface.cmd_with_data(spi, Command::WRITE_LUT_REGISTER, buffer) } } diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index ca8e494..a51371d 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -74,50 +74,50 @@ pub struct EPD4in2 { -impl - InternalWiAdditions +impl + InternalWiAdditions for EPD4in2 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, { - fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { + fn init>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { // reset the device self.interface.reset(delay); // set the power settings - self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; + self.interface.cmd_with_data(spi, Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; // start the booster - self.interface.cmd_with_data(Command::BOOSTER_SOFT_START, &[0x17, 0x17, 0x17])?; + self.interface.cmd_with_data(spi, Command::BOOSTER_SOFT_START, &[0x17, 0x17, 0x17])?; // power on - self.command(Command::POWER_ON)?; + self.command(spi, Command::POWER_ON)?; self.wait_until_idle(); // set the panel settings - self.cmd_with_data(Command::PANEL_SETTING, &[0x3F])?; + self.cmd_with_data(spi, Command::PANEL_SETTING, &[0x3F])?; // Set Frequency, 200 Hz didn't work on my board // 150Hz and 171Hz wasn't tested yet // TODO: Test these other frequencies // 3A 100HZ 29 150Hz 39 200HZ 31 171HZ DEFAULT: 3c 50Hz - self.cmd_with_data(Command::PLL_CONTROL, &[0x3A])?; + self.cmd_with_data(spi, Command::PLL_CONTROL, &[0x3A])?; - self.set_lut()?; + self.set_lut(spi)?; Ok(()) } } -impl - WaveshareInterface +impl + WaveshareInterface for EPD4in2 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, @@ -138,8 +138,8 @@ where /// /// epd4in2.sleep(); /// ``` - fn new>(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY) -> Result { - let interface = ConnectionInterface::new(spi, cs, busy, dc, rst); + fn new>(spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY) -> Result { + let interface = ConnectionInterface::new(cs, busy, dc, rst); let color = DEFAULT_BACKGROUND_COLOR; let mut epd = EPD4in2 { @@ -147,120 +147,122 @@ where color, }; - epd.init(delay)?; + epd.init(spi, delay)?; Ok(epd) } - fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR> { - self.init(delay) + fn wake_up>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { + self.init(spi, delay) } //TODO: is such a long delay really needed inbetween? - fn sleep(&mut self) -> Result<(), ERR> { - self.interface.cmd_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)?; + fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.interface.cmd_with_data(spi, Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating + self.command(spi, Command::VCM_DC_SETTING)?; // VCOM to 0V + self.command(spi, Command::PANEL_SETTING)?; //TODO: Removal of delay. TEST! //self.delay_ms(100); - self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast + self.command(spi, Command::POWER_SETTING)?; //VG&VS to 0V fast for _ in 0..4 { - self.send_data(&[0x00])?; + self.send_data(spi, &[0x00])?; } //TODO: Removal of delay. TEST! //self.delay_ms(100); - self.command(Command::POWER_OFF)?; + self.command(spi, Command::POWER_OFF)?; self.wait_until_idle(); - self.interface.cmd_with_data(Command::DEEP_SLEEP, &[0xA5]) + self.interface.cmd_with_data(spi, Command::DEEP_SLEEP, &[0xA5]) } - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { + fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error> { let color_value = self.color.get_byte_value(); - self.send_resolution()?; + self.send_resolution(spi)?; - self.interface.cmd_with_data(Command::VCM_DC_SETTING, &[0x12])?; + self.interface.cmd_with_data(spi, Command::VCM_DC_SETTING, &[0x12])?; //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.interface.cmd_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; + self.interface.cmd_with_data(spi, Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?; - self.command(Command::DATA_START_TRANSMISSION_1)?; + self.command(spi, Command::DATA_START_TRANSMISSION_1)?; for _ in 0..buffer.len() { - self.send_data(&[color_value])?; + self.send_data(spi, &[color_value])?; } //TODO: Removal of delay. TEST! //self.delay_ms(2); - self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer) + self.interface.cmd_with_data(spi, Command::DATA_START_TRANSMISSION_2, buffer) } fn update_partial_frame( - &mut self, + &mut self, + spi: &mut SPI, buffer: &[u8], x: u16, y: u16, width: u16, height: u16, - ) -> Result<(), ERR> { + ) -> Result<(), SPI::Error> { if buffer.len() as u16 != width / 8 * height { //TODO: panic!! or sth like that //return Err("Wrong buffersize"); } - self.command(Command::PARTIAL_IN)?; - self.command(Command::PARTIAL_WINDOW)?; - self.send_data(&[(x >> 8) as u8])?; + self.command(spi, Command::PARTIAL_IN)?; + self.command(spi, Command::PARTIAL_WINDOW)?; + self.send_data(spi, &[(x >> 8) as u8])?; let tmp = x & 0xf8; - self.send_data(&[tmp as u8])?; // x should be the multiple of 8, the last 3 bit will always be ignored + self.send_data(spi, &[tmp as u8])?; // x should be the multiple of 8, the last 3 bit will always be ignored let tmp = tmp + width - 1; - self.send_data(&[(tmp >> 8) as u8])?; - self.send_data(&[(tmp | 0x07) as u8])?; + self.send_data(spi, &[(tmp >> 8) as u8])?; + self.send_data(spi, &[(tmp | 0x07) as u8])?; - self.send_data(&[(y >> 8) as u8])?; - self.send_data(&[y as u8])?; + self.send_data(spi, &[(y >> 8) as u8])?; + self.send_data(spi, &[y as u8])?; - self.send_data(&[((y + height - 1) >> 8) as u8])?; - self.send_data(&[(y + height - 1) as u8])?; + self.send_data(spi, &[((y + height - 1) >> 8) as u8])?; + self.send_data(spi, &[(y + height - 1) as u8])?; - self.send_data(&[0x01])?; // Gates scan both inside and outside of the partial window. (default) + self.send_data(spi, &[0x01])?; // Gates scan both inside and outside of the partial window. (default) //TODO: handle dtm somehow let is_dtm1 = false; if is_dtm1 { - self.command(Command::DATA_START_TRANSMISSION_1)? + self.command(spi, Command::DATA_START_TRANSMISSION_1)? } else { - self.command(Command::DATA_START_TRANSMISSION_2)? + self.command(spi, Command::DATA_START_TRANSMISSION_2)? } - self.send_data(buffer)?; + self.send_data(spi, buffer)?; - self.command(Command::PARTIAL_OUT) + self.command(spi, Command::PARTIAL_OUT) } - fn display_frame(&mut self) -> Result<(), ERR> { - self.command(Command::DISPLAY_REFRESH)?; + fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.command(spi, Command::DISPLAY_REFRESH)?; self.wait_until_idle(); Ok(()) } - fn clear_frame(&mut self) -> Result<(), ERR> { - self.send_resolution()?; + fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.send_resolution(spi)?; //let size = WIDTH as usize / 8 * HEIGHT as usize; let color_value = self.color.get_byte_value(); //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? self.interface.cmd_with_data( + spi, Command::DATA_START_TRANSMISSION_1, &[color_value; WIDTH as usize / 8 * HEIGHT as usize] )?; @@ -270,6 +272,7 @@ where //TODO: this is using a big buffer atm, is it better to just loop over sending a single byte? self.interface.cmd_with_data( + spi, Command::DATA_START_TRANSMISSION_2, &[color_value; WIDTH as usize / 8 * HEIGHT as usize] ) @@ -293,45 +296,45 @@ where } } -impl EPD4in2 +impl EPD4in2 where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, { - fn command(&mut self, command: Command) -> Result<(), ERR> { - self.interface.cmd(command) + fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> { + self.interface.cmd(spi, command) } - fn send_data(&mut self, data: &[u8]) -> Result<(), ERR> { - self.interface.data(data) + fn send_data(&mut self, spi: &mut SPI, data: &[u8]) -> Result<(), SPI::Error> { + self.interface.data(spi, data) } - fn cmd_with_data(&mut self, command: Command, data: &[u8]) -> Result<(), ERR> { - self.interface.cmd_with_data(command, data) + 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(true) } - fn send_resolution(&mut self) -> Result<(), ERR> { + fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { let w = self.width(); let h = self.height(); - self.command(Command::RESOLUTION_SETTING)?; - self.send_data(&[(w >> 8) as u8])?; - self.send_data(&[w as u8])?; - self.send_data(&[(h >> 8) as u8])?; - self.send_data(&[h as u8]) + self.command(spi, Command::RESOLUTION_SETTING)?; + self.send_data(spi, &[(w >> 8) as u8])?; + self.send_data(spi, &[w as u8])?; + self.send_data(spi, &[(h >> 8) as u8])?; + self.send_data(spi, &[h as u8]) } /// Fill the look-up table for the EPD //TODO: make public? - fn set_lut(&mut self) -> Result<(), ERR> { - self.set_lut_helper(&LUT_VCOM0, &LUT_WW, &LUT_BW, &LUT_WB, &LUT_BB) + fn set_lut(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { + self.set_lut_helper(spi, &LUT_VCOM0, &LUT_WW, &LUT_BW, &LUT_WB, &LUT_BB) } /// Fill the look-up table for a quick display (partial refresh) @@ -339,8 +342,9 @@ where /// Is automatically done by [EPD4in2::display_frame_quick()](EPD4in2::display_frame_quick()) /// //TODO: make public? #[cfg(feature = "epd4in2_fast_update")] - fn set_lut_quick(&mut self) -> Result<(), ERR> { + fn set_lut_quick(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> { self.set_lut_helper( + spi, &LUT_VCOM0_QUICK, &LUT_WW_QUICK, &LUT_BW_QUICK, @@ -351,33 +355,47 @@ where fn set_lut_helper( &mut self, + spi: &mut SPI, lut_vcom: &[u8], lut_ww: &[u8], lut_bw: &[u8], lut_wb: &[u8], lut_bb: &[u8], - ) -> Result<(), ERR> { + ) -> Result<(), SPI::Error> { // LUT VCOM - self.command(Command::LUT_FOR_VCOM)?; - self.send_data(lut_vcom)?; + self.cmd_with_data( + spi, + Command::LUT_FOR_VCOM, + lut_vcom + )?; // LUT WHITE to WHITE - self.command(Command::LUT_WHITE_TO_WHITE)?; - self.send_data(lut_ww)?; + self.cmd_with_data( + spi, + Command::LUT_WHITE_TO_WHITE, + lut_ww + )?; // LUT BLACK to WHITE - self.command(Command::LUT_BLACK_TO_WHITE)?; - self.send_data(lut_bw)?; + self.cmd_with_data( + spi, + Command::LUT_BLACK_TO_WHITE, + lut_bw + )?; // LUT WHITE to BLACK - self.command(Command::LUT_WHITE_TO_BLACK)?; - self.send_data(lut_wb)?; + self.cmd_with_data( + spi, + Command::LUT_WHITE_TO_BLACK, + lut_wb, + )?; // LUT BLACK to BLACK - self.command(Command::LUT_BLACK_TO_BLACK)?; - self.send_data(lut_bb)?; - - Ok(()) + self.cmd_with_data( + spi, + Command::LUT_BLACK_TO_BLACK, + lut_bb, + ) } } diff --git a/src/traits/connection_interface.rs b/src/traits/connection_interface.rs index a3da8c6..0a18c86 100644 --- a/src/traits/connection_interface.rs +++ b/src/traits/connection_interface.rs @@ -3,13 +3,15 @@ use hal::{ digital::*, }; +use core::marker::PhantomData; + use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// pub(crate) struct ConnectionInterface { /// SPI - spi: SPI, + _spi: PhantomData, /// CS for SPI cs: CS, /// Low for busy, Wait until display is ready! @@ -20,18 +22,18 @@ pub(crate) struct ConnectionInterface { rst: RST, } -impl +impl ConnectionInterface where - SPI: Write, + SPI: Write, CS: OutputPin, BUSY: InputPin, DC: OutputPin, RST: OutputPin, { - pub fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST) -> Self { + pub fn new(cs: CS, busy: BUSY, dc: DC, rst: RST) -> Self { ConnectionInterface { - spi, + _spi: PhantomData::default(), cs, busy, dc, @@ -44,12 +46,12 @@ where /// Enables direct interaction with the device with the help of [data()](ConnectionInterface::data()) /// /// //TODO: make public? - pub(crate) fn cmd(&mut self, command: T) -> Result<(), ERR> { + pub(crate) fn cmd(&mut self, spi: &mut SPI, command: T) -> Result<(), SPI::Error> { // low for commands self.dc.set_low(); // Transfer the command over spi - self.with_cs(|epd| epd.spi.write(&[command.address()])) + self.write(spi, &[command.address()]) } /// Basic function for sending an array of u8-values of data over spi @@ -57,35 +59,33 @@ where /// Enables direct interaction with the device with the help of [command()](EPD4in2::command()) /// /// //TODO: make public? - pub(crate) fn data(&mut self, data: &[u8]) -> Result<(), ERR> { + pub(crate) fn data(&mut self, spi: &mut SPI, data: &[u8]) -> Result<(), SPI::Error> { // high for data self.dc.set_high(); // Transfer data (u8-array) over spi - self.with_cs(|epd| epd.spi.write(data)) + self.write(spi, data) } /// Basic function for sending [Commands](Command) and the data belonging to it. /// /// //TODO: make public? - pub(crate) fn cmd_with_data(&mut self, command: T, data: &[u8]) -> Result<(), ERR> { - self.cmd(command)?; - self.data(data) + pub(crate) fn cmd_with_data(&mut self, spi: &mut SPI, command: T, data: &[u8]) -> Result<(), SPI::Error> { + self.cmd(spi, command)?; + self.data(spi, data) } // spi write helper/abstraction function - fn with_cs(&mut self, f: F) -> Result<(), ERR> - where - F: FnOnce(&mut Self) -> Result<(), ERR>, + fn write(&mut self, spi: &mut SPI, data: &[u8]) -> Result<(), SPI::Error> { // activate spi with cs low self.cs.set_low(); // transfer spi data - let result = f(self); + spi.write(data)?; // deativate spi with cs high self.cs.set_high(); - // return result - result + + Ok(()) } /// Waits until device isn't busy anymore (busy == HIGH) diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 0ec5c8f..87860bc 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -26,7 +26,7 @@ trait LUTSupport { } -pub(crate) trait InternalWiAdditions +pub(crate) trait InternalWiAdditions where SPI: Write, CS: OutputPin, @@ -44,11 +44,11 @@ where /// This function calls [reset()](WaveshareInterface::reset()), /// so you don't need to call reset your self when trying to wake your device up /// after setting it to sleep. - fn init>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; + fn init>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error>; } -pub trait WaveshareInterface +pub trait WaveshareInterface where SPI: Write, CS: OutputPin, @@ -62,8 +62,8 @@ where /// /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards fn new>( - spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, - ) -> Result + spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, + ) -> Result where Self: Sized; @@ -73,9 +73,9 @@ where /// But you can also use [wake_up()](WaveshareInterface::wake_up()) to awaken. /// But as you need to power it up once more anyway you can also just directly use [new()](WaveshareInterface::new()) for resetting /// and initialising which already contains the reset - fn sleep(&mut self) -> Result<(), ERR>; + fn sleep(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>; - fn wake_up>(&mut self, delay: &mut DELAY) -> Result<(), ERR>; + fn wake_up>(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error>; /// Sets the backgroundcolor for various commands like [clear_frame()](WaveshareInterface::clear_frame()) @@ -91,24 +91,25 @@ where fn height(&self) -> u16; /// Transmit a full frame to the SRAM of the EPD - fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR>; + fn update_frame(&mut self, spi: &mut SPI, buffer: &[u8]) -> Result<(), SPI::Error>; /// Transmits partial data to the SRAM of the EPD /// /// BUFFER needs to be of size: w / 8 * h ! fn update_partial_frame( &mut self, + spi: &mut SPI, buffer: &[u8], x: u16, y: u16, width: u16, height: u16, - ) -> Result<(), ERR>; + ) -> Result<(), SPI::Error>; /// Displays the frame data from SRAM - fn display_frame(&mut self) -> Result<(), ERR>; + fn display_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>; /// Clears the frame buffer on the EPD with the declared background color /// The background color can be changed with [`set_background_color`] - fn clear_frame(&mut self) -> Result<(), ERR>; + fn clear_frame(&mut self, spi: &mut SPI) -> Result<(), SPI::Error>; } From 0363e8527f9c18949a52cda9e16772f426cff531 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 16:22:41 +0200 Subject: [PATCH 12/14] Adapted examples to previous commit --- examples/embedded_linux_epd1in54/src/main.rs | 26 ++++++++--------- examples/embedded_linux_epd4in2/src/main.rs | 30 ++++++++++---------- examples/stm32f3discovery/src/main.rs | 26 ++++++++--------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/examples/embedded_linux_epd1in54/src/main.rs b/examples/embedded_linux_epd1in54/src/main.rs index 8754d94..e1969e4 100644 --- a/examples/embedded_linux_epd1in54/src/main.rs +++ b/examples/embedded_linux_epd1in54/src/main.rs @@ -113,40 +113,40 @@ fn run() -> Result<(), std::io::Error> { // Setup of the needed pins is finished here // Now the "real" usage of the eink-waveshare-rs crate begins - let mut epd = EPD1in54::new(spi, cs_pin, busy_in, dc, rst, &mut delay)?; + let mut epd = EPD1in54::new(&mut spi, cs_pin, busy_in, dc, rst, &mut delay)?; // Clear the full screen - epd.clear_frame()?; - epd.display_frame()?; + epd.clear_frame(&mut spi)?; + epd.display_frame(&mut spi)?; // Speeddemo let small_buffer = [Color::Black.get_byte_value(), 16 as u8 / 8 * 16 as u8]; let number_of_runs = 100; for i in 0..number_of_runs { let offset = i * 8 % 150; - epd.update_partial_frame(&small_buffer, 25 + offset, 25 + offset, 16, 16)?; - epd.display_frame()?; + epd.update_partial_frame(&mut spi, &small_buffer, 25 + offset, 25 + offset, 16, 16)?; + epd.display_frame(&mut spi)?; } // Clear the full screen - epd.clear_frame()?; - epd.display_frame()?; + epd.clear_frame(&mut spi)?; + epd.display_frame(&mut spi)?; // Draw some squares - let mut small_buffer = [Color::Black.get_byte_value(), 160 as u8 / 8 * 160 as u8]; - epd.update_partial_frame(&small_buffer, 20, 20, 160, 160)?; + let mut small_buffer = [Color::Black.get_byte_value(), 160 as u8 / 8 * 160 as u8]; + epd.update_partial_frame(&mut spi, &small_buffer, 20, 20, 160, 160)?; small_buffer = [Color::White.get_byte_value(), 80 as u8 / 8 * 80 as u8]; - epd.update_partial_frame(&small_buffer, 60, 60, 80, 80)?; + epd.update_partial_frame(&mut spi, &small_buffer, 60, 60, 80, 80)?; small_buffer = [Color::Black.get_byte_value(), 8]; - epd.update_partial_frame(&small_buffer, 96, 96, 8, 8)?; + epd.update_partial_frame(&mut spi, &small_buffer, 96, 96, 8, 8)?; // Display updated frame - epd.display_frame()?; + epd.display_frame(&mut spi)?; // Set the EPD to sleep - epd.sleep()?; + epd.sleep(&mut spi)?; Ok(()) } diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index cf74b42..e45ed0d 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -108,7 +108,7 @@ fn main() -> Result<(), std::io::Error> { //TODO: wait for Digital::InputPin //fixed currently with the HackInputPin, see further above - let mut epd4in2 = EPD4in2::new(spi, cs, busy_in, dc, rst, &mut delay).expect("eink inialize error"); + let mut epd4in2 = EPD4in2::new(&mut spi, cs, busy_in, dc, rst, &mut delay).expect("eink inialize error"); //let mut buffer = [0u8, epd4in2.get_width() / 8 * epd4in2.get_height()]; let mut buffer = [0u8; 15000]; @@ -129,29 +129,29 @@ fn main() -> Result<(), std::io::Error> { graphics.draw_vertical_line(200, 50, 200, &Color::Black); - epd4in2.update_frame(graphics.get_buffer()).expect("display and transfer error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_frame(&mut spi, graphics.get_buffer())?; + epd4in2.display_frame(&mut spi)?; delay.delay_ms(3000u16); - epd4in2.clear_frame().expect("clear frame error"); + epd4in2.clear_frame(&mut spi)?; //Test fast updating a bit more let mut small_buffer = [0x00; 128]; let mut circle_graphics = Graphics::new(32,32, &mut small_buffer); circle_graphics.draw_circle(16,16, 10, &Color::Black); - epd4in2.update_partial_frame(circle_graphics.get_buffer(), 16,16, 32, 32).expect("Partial Window Error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 16,16, 32, 32)?; + epd4in2.display_frame(&mut spi)?; - epd4in2.update_partial_frame(circle_graphics.get_buffer(), 128,64, 32, 32).expect("Partial Window Error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 128,64, 32, 32)?; + epd4in2.display_frame(&mut spi)?; - epd4in2.update_partial_frame(circle_graphics.get_buffer(), 320,24, 32, 32).expect("Partial Window Error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 320,24, 32, 32)?; + epd4in2.display_frame(&mut spi)?; - epd4in2.update_partial_frame(circle_graphics.get_buffer(), 160,240, 32, 32).expect("Partial Window Error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_partial_frame(&mut spi, circle_graphics.get_buffer(), 160,240, 32, 32)?; + epd4in2.display_frame(&mut spi)?; delay.delay_ms(3000u16); @@ -162,10 +162,10 @@ fn main() -> Result<(), std::io::Error> { graphics.draw_string_8x8(16, 16, "hello", &Color::Black); graphics.draw_char_8x8(250, 250, '#', &Color::Black); graphics.draw_char_8x8(300, 16, '7', &Color::Black); - epd4in2.update_frame(graphics.get_buffer()).expect("display and transfer error"); - epd4in2.display_frame().expect("Display Frame Error"); + epd4in2.update_frame(&mut spi, graphics.get_buffer())?; + epd4in2.display_frame(&mut spi)?; delay.delay_ms(3000u16); - epd4in2.sleep() + epd4in2.sleep(&mut spi) } diff --git a/examples/stm32f3discovery/src/main.rs b/examples/stm32f3discovery/src/main.rs index 1de6e4a..3dd32ac 100644 --- a/examples/stm32f3discovery/src/main.rs +++ b/examples/stm32f3discovery/src/main.rs @@ -119,7 +119,7 @@ fn main() -> ! { let miso = gpioa.pa6.into_af5(&mut gpioa.moder, &mut gpioa.afrl); let mosi = gpioa.pa7.into_af5(&mut gpioa.moder, &mut gpioa.afrl); - let spi = Spi::spi1( + let mut spi = Spi::spi1( p.SPI1, (sck, miso, mosi), SPI_MODE, @@ -130,42 +130,42 @@ fn main() -> ! { // // Setup of the needed pins is finished here // // Now the "real" usage of the eink-waveshare-rs crate begins - let mut epd = EPD1in54::new(spi, cs, busy, dc, rst, &mut delay).unwrap(); + let mut epd = EPD1in54::new(&mut spi, cs, busy, dc, rst, &mut delay).unwrap(); // Clear the full screen - epd.clear_frame().unwrap(); - epd.display_frame().unwrap(); + epd.clear_frame(&mut spi).unwrap(); + epd.display_frame(&mut spi).unwrap(); // Speeddemo let small_buffer = [Color::Black.get_byte_value(), 16 as u8 / 8 * 16 as u8]; let number_of_runs = 100; for i in 0..number_of_runs { let offset = i * 8 % 150; - epd.update_partial_frame(&small_buffer, 25 + offset, 25 + offset, 16, 16).unwrap(); - epd.display_frame().unwrap(); + epd.update_partial_frame(&mut spi, &small_buffer, 25 + offset, 25 + offset, 16, 16).unwrap(); + epd.display_frame(&mut spi).unwrap(); } // Clear the full screen - epd.clear_frame().unwrap(); - epd.display_frame().unwrap(); + epd.clear_frame(&mut spi).unwrap(); + epd.display_frame(&mut spi).unwrap(); // Draw some squares let mut small_buffer = [Color::Black.get_byte_value(), 160 as u8 / 8 * 160 as u8]; - epd.update_partial_frame(&small_buffer, 20, 20, 160, 160).unwrap(); + epd.update_partial_frame(&mut spi, &small_buffer, 20, 20, 160, 160).unwrap(); small_buffer = [Color::White.get_byte_value(), 80 as u8 / 8 * 80 as u8]; - epd.update_partial_frame(&small_buffer, 60, 60, 80, 80).unwrap(); + epd.update_partial_frame(&mut spi, &small_buffer, 60, 60, 80, 80).unwrap(); small_buffer = [Color::Black.get_byte_value(), 8]; - epd.update_partial_frame(&small_buffer, 96, 96, 8, 8).unwrap(); + epd.update_partial_frame(&mut spi, &small_buffer, 96, 96, 8, 8).unwrap(); // Display updated frame - epd.display_frame().unwrap(); + epd.display_frame(&mut spi).unwrap(); // Set the EPD to sleep - epd.sleep().unwrap(); + epd.sleep(&mut spi).unwrap(); loop {} } From 89489da657951e10bfd9dfa93660606961067a68 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 17:01:03 +0200 Subject: [PATCH 13/14] Renaming of WaveshareInterface and Connectioninterface WaveshareInterface -> WaveshareDisplay traits::interface::ConnectionInterface -> interface::DisplayInterface --- src/epd1in54/mod.rs | 10 +++++----- src/epd2in9/mod.rs | 8 ++++---- src/epd4in2/mod.rs | 9 +++++---- src/{traits/connection_interface.rs => interface.rs} | 11 +++++------ src/lib.rs | 5 ++++- src/{traits/mod.rs => traits.rs} | 0 6 files changed, 23 insertions(+), 20 deletions(-) rename src/{traits/connection_interface.rs => interface.rs} (93%) rename src/{traits/mod.rs => traits.rs} (100%) diff --git a/src/epd1in54/mod.rs b/src/epd1in54/mod.rs index d220532..35e47d0 100644 --- a/src/epd1in54/mod.rs +++ b/src/epd1in54/mod.rs @@ -33,15 +33,15 @@ use type_a::{command::Command, LUT_FULL_UPDATE, LUT_PARTIAL_UPDATE}; use color::Color; -use traits::{WaveshareInterface}; +use traits::{WaveshareDisplay}; -use traits::connection_interface::ConnectionInterface; +use interface::DisplayInterface; /// EPD1in54 driver /// pub struct EPD1in54 { /// SPI - interface: ConnectionInterface, + interface: DisplayInterface, /// EPD (width, height) //epd: EPD, /// Color @@ -95,7 +95,7 @@ where } -impl WaveshareInterface +impl WaveshareDisplay for EPD1in54 where SPI: Write, @@ -115,7 +115,7 @@ where fn new>( spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(cs, busy, dc, rst); + let interface = DisplayInterface::new(cs, busy, dc, rst); let mut epd = EPD1in54 { interface, diff --git a/src/epd2in9/mod.rs b/src/epd2in9/mod.rs index c154d19..1110d11 100644 --- a/src/epd2in9/mod.rs +++ b/src/epd2in9/mod.rs @@ -34,13 +34,13 @@ use color::Color; use traits::*; -use traits::connection_interface::ConnectionInterface; +use interface::DisplayInterface; /// EPD2in9 driver /// pub struct EPD2in9 { /// SPI - interface: ConnectionInterface, + interface: DisplayInterface, /// EPD (width, height) //epd: EPD, /// Color @@ -90,7 +90,7 @@ where } impl - WaveshareInterface + WaveshareDisplay for EPD2in9 where SPI: Write, @@ -110,7 +110,7 @@ where fn new>( spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY, ) -> Result { - let interface = ConnectionInterface::new(cs, busy, dc, rst); + let interface = DisplayInterface::new(cs, busy, dc, rst); let mut epd = EPD2in9 { interface, diff --git a/src/epd4in2/mod.rs b/src/epd4in2/mod.rs index a51371d..87fa40f 100644 --- a/src/epd4in2/mod.rs +++ b/src/epd4in2/mod.rs @@ -51,7 +51,8 @@ use hal::{ digital::*, }; -use traits::{connection_interface::ConnectionInterface, WaveshareInterface, InternalWiAdditions}; +use traits::{WaveshareDisplay, InternalWiAdditions}; +use interface::DisplayInterface; //The Lookup Tables for the Display mod constants; @@ -66,7 +67,7 @@ use self::command::Command; /// pub struct EPD4in2 { /// Connection Interface - interface: ConnectionInterface, + interface: DisplayInterface, /// Background Color color: Color, } @@ -114,7 +115,7 @@ where } impl - WaveshareInterface + WaveshareDisplay for EPD4in2 where SPI: Write, @@ -139,7 +140,7 @@ where /// epd4in2.sleep(); /// ``` fn new>(spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY) -> Result { - let interface = ConnectionInterface::new(cs, busy, dc, rst); + let interface = DisplayInterface::new(cs, busy, dc, rst); let color = DEFAULT_BACKGROUND_COLOR; let mut epd = EPD4in2 { diff --git a/src/traits/connection_interface.rs b/src/interface.rs similarity index 93% rename from src/traits/connection_interface.rs rename to src/interface.rs index 0a18c86..623a2fc 100644 --- a/src/traits/connection_interface.rs +++ b/src/interface.rs @@ -2,14 +2,12 @@ use hal::{ blocking::{delay::*, spi::Write}, digital::*, }; - use core::marker::PhantomData; - use traits::Command; /// The Connection Interface of all (?) Waveshare EPD-Devices /// -pub(crate) struct ConnectionInterface { +pub(crate) struct DisplayInterface { /// SPI _spi: PhantomData, /// CS for SPI @@ -23,7 +21,7 @@ pub(crate) struct ConnectionInterface { } impl - ConnectionInterface + DisplayInterface where SPI: Write, CS: OutputPin, @@ -32,7 +30,7 @@ where RST: OutputPin, { pub fn new(cs: CS, busy: BUSY, dc: DC, rst: RST) -> Self { - ConnectionInterface { + DisplayInterface { _spi: PhantomData::default(), cs, busy, @@ -43,7 +41,7 @@ where /// Basic function for sending [Commands](Command). /// - /// Enables direct interaction with the device with the help of [data()](ConnectionInterface::data()) + /// Enables direct interaction with the device with the help of [data()](DisplayInterface::data()) /// /// //TODO: make public? pub(crate) fn cmd(&mut self, spi: &mut SPI, command: T) -> Result<(), SPI::Error> { @@ -70,6 +68,7 @@ where /// Basic function for sending [Commands](Command) and the data belonging to it. /// /// //TODO: make public? + /// TODO: directly use ::write? cs wouldn't needed to be changed twice than pub(crate) fn cmd_with_data(&mut self, spi: &mut SPI, command: T, data: &[u8]) -> Result<(), SPI::Error> { self.cmd(spi, command)?; self.data(spi, data) diff --git a/src/lib.rs b/src/lib.rs index 5e34505..6492411 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,10 +51,13 @@ use hal::spi::{Mode, Phase, Polarity}; pub mod drawing; mod traits; -pub use traits::{WaveshareInterface}; +pub use traits::{WaveshareDisplay}; pub mod color; +/// Interface for the physical connection between display and the controlling device +mod interface; + #[cfg(feature = "epd4in2")] mod epd4in2; #[cfg(feature = "epd4in2")] diff --git a/src/traits/mod.rs b/src/traits.rs similarity index 100% rename from src/traits/mod.rs rename to src/traits.rs From f131636b016607e9d7ac34bdd099afe580b1723c Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 17:03:18 +0200 Subject: [PATCH 14/14] Addition to previous commit (examples and forgotten save) --- examples/embedded_linux_epd1in54/src/main.rs | 2 +- examples/embedded_linux_epd4in2/src/main.rs | 2 +- examples/stm32f3discovery/src/main.rs | 2 +- src/traits.rs | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/embedded_linux_epd1in54/src/main.rs b/examples/embedded_linux_epd1in54/src/main.rs index e1969e4..fe2db14 100644 --- a/examples/embedded_linux_epd1in54/src/main.rs +++ b/examples/embedded_linux_epd1in54/src/main.rs @@ -9,7 +9,7 @@ use eink_waveshare_rs::{ EPD1in54, //drawing::{Graphics}, color::Color, - WaveshareInterface, + WaveshareDisplay, }; use lin_hal::spidev::{self, SpidevOptions}; diff --git a/examples/embedded_linux_epd4in2/src/main.rs b/examples/embedded_linux_epd4in2/src/main.rs index e45ed0d..eb7b102 100644 --- a/examples/embedded_linux_epd4in2/src/main.rs +++ b/examples/embedded_linux_epd4in2/src/main.rs @@ -9,7 +9,7 @@ use eink_waveshare_rs::{ EPD4in2, drawing::{Graphics}, color::Color, - WaveshareInterface, + WaveshareDisplay, }; use lin_hal::spidev::{self, SpidevOptions}; diff --git a/examples/stm32f3discovery/src/main.rs b/examples/stm32f3discovery/src/main.rs index 3dd32ac..ded3f40 100644 --- a/examples/stm32f3discovery/src/main.rs +++ b/examples/stm32f3discovery/src/main.rs @@ -30,7 +30,7 @@ use eink_waveshare_rs::{ SPI_MODE, //drawing::{Graphics}, color::Color, - WaveshareInterface, + WaveshareDisplay, }; diff --git a/src/traits.rs b/src/traits.rs index 87860bc..8e95745 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -6,8 +6,8 @@ use hal::{ use color::Color; -/// Interface for the physical connection between display and the controlling device -pub(crate) mod connection_interface; + + /// All commands need to have this trait which gives the address of the command @@ -48,7 +48,7 @@ where } -pub trait WaveshareInterface +pub trait WaveshareDisplay where SPI: Write, CS: OutputPin,