Browse Source

Make the code more readable with a few already introduced adapter functions (e.g. command_with_data)

embedded-hal-1.0
Christoph Groß 7 years ago
parent
commit
cd593c51cb
  1. 27
      src/epd1in54/mod.rs
  2. 47
      src/epd2in9/mod.rs
  3. 36
      src/epd4in2/mod.rs

27
src/epd1in54/mod.rs

@ -81,21 +81,17 @@ where
self.interface.data(0x9D)?; self.interface.data(0x9D)?;
// One Databyte with value 0xA8 for 7V VCOM // One Databyte with value 0xA8 for 7V VCOM
self.interface.command(Command::WRITE_VCOM_REGISTER)?; self.interface.command_with_data(Command::WRITE_VCOM_REGISTER, &[0xA8])?;
self.interface.data(0xA8)?;
// One Databyte with default value 0x1A for 4 dummy lines per gate // One Databyte with default value 0x1A for 4 dummy lines per gate
self.interface.command(Command::SET_DUMMY_LINE_PERIOD)?; self.interface.command_with_data(Command::SET_DUMMY_LINE_PERIOD, &[0x1A])?;
self.interface.data(0x1A)?;
// One Databyte with default value 0x08 for 2us per line // One Databyte with default value 0x08 for 2us per line
self.interface.command(Command::SET_GATE_LINE_WIDTH)?; self.interface.command_with_data(Command::SET_GATE_LINE_WIDTH, &[0x08])?;
self.interface.data(0x08)?;
// One Databyte with default value 0x03 // One Databyte with default value 0x03
// -> address: x increment, y increment, address counter is updated in x direction // -> address: x increment, y increment, address counter is updated in x direction
self.interface.command(Command::DATA_ENTRY_MODE_SETTING)?; self.interface.command_with_data(Command::DATA_ENTRY_MODE_SETTING, &[0x03])?;
self.interface.data(0x03)?;
self.set_lut() self.set_lut()
} }
@ -142,10 +138,9 @@ where
fn sleep(&mut self) -> Result<(), E> { fn sleep(&mut self) -> Result<(), E> {
self.interface.command(Command::DEEP_SLEEP_MODE)?;
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
//TODO: is 0x00 needed here? //TODO: is 0x00 needed here or would 0x01 be even more efficient?
self.interface.data(0x00)?; self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?;
self.wait_until_idle(); self.wait_until_idle();
Ok(()) Ok(())
@ -157,7 +152,6 @@ where
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
self.use_full_frame()?; self.use_full_frame()?;
self.interface.command_with_data(Command::WRITE_RAM, buffer) self.interface.command_with_data(Command::WRITE_RAM, buffer)
} }
@ -173,8 +167,7 @@ where
self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_area(x, y, x + width, y + height)?;
self.set_ram_counter(x, y)?; self.set_ram_counter(x, y)?;
self.interface.command(Command::WRITE_RAM)?; self.interface.command_with_data(Command::WRITE_RAM, buffer)
self.interface.multiple_data(buffer)
} }
fn display_frame(&mut self) -> Result<(), E> { fn display_frame(&mut self) -> Result<(), E> {
@ -262,8 +255,10 @@ where
// 2 Databytes: A[7:0] & 0..A[8] // 2 Databytes: A[7:0] & 0..A[8]
self.interface.command_with_data( self.interface.command_with_data(
Command::SET_RAM_Y_ADDRESS_COUNTER, Command::SET_RAM_Y_ADDRESS_COUNTER,
&[y as u8, (y >> 8) as u8] &[
)?; y as u8,
(y >> 8) as u8
])?;
self.wait_until_idle(); self.wait_until_idle();
Ok(()) Ok(())

47
src/epd2in9/mod.rs

@ -102,11 +102,11 @@ where
} }
impl<SPI, CS, BUSY, DC, RST, Delay, E> impl<SPI, CS, BUSY, DC, RST, Delay, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR>
for EPD2in9<SPI, CS, BUSY, DC, RST, Delay> for EPD2in9<SPI, CS, BUSY, DC, RST, Delay>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
@ -123,10 +123,7 @@ where
fn new( fn new(
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay,
) -> Result<Self, E> { ) -> Result<Self, ERR> {
//let epd = EPD::new(WIDTH, HEIGHT);
//let background_color = Color::White;
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay);
let mut epd = EPD2in9 { let mut epd = EPD2in9 {
@ -141,17 +138,16 @@ where
fn sleep(&mut self) -> Result<(), E> { fn sleep(&mut self) -> Result<(), ERR> {
self.interface.command(Command::DEEP_SLEEP_MODE)?;
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode // 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
//TODO: is 0x00 needed here? //TODO: is 0x00 needed here? (see also epd1in54)
self.interface.data(0x00)?; self.interface.command_with_data(Command::DEEP_SLEEP_MODE, &[0x00])?;
self.wait_until_idle(); self.wait_until_idle();
Ok(()) Ok(())
} }
fn wake_up(&mut self) -> Result<(), E> { fn wake_up(&mut self) -> Result<(), ERR> {
self.init() self.init()
} }
@ -159,11 +155,10 @@ where
self.interface.delay_ms(delay) self.interface.delay_ms(delay)
} }
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> {
self.use_full_frame()?; self.use_full_frame()?;
self.interface.command(Command::WRITE_RAM)?; self.interface.command_with_data(Command::WRITE_RAM, buffer)
self.interface.multiple_data(buffer)
} }
//TODO: update description: last 3 bits will be ignored for width and x_pos //TODO: update description: last 3 bits will be ignored for width and x_pos
@ -174,19 +169,17 @@ where
y: u16, y: u16,
width: u16, width: u16,
height: u16, height: u16,
) -> Result<(), E> { ) -> Result<(), ERR> {
self.set_ram_area(x, y, x + width, y + height)?; self.set_ram_area(x, y, x + width, y + height)?;
self.set_ram_counter(x, y)?; self.set_ram_counter(x, y)?;
self.interface.command(Command::WRITE_RAM)?; self.interface.command_with_data(Command::WRITE_RAM, buffer)
self.interface.multiple_data(buffer)
} }
fn display_frame(&mut self) -> Result<(), E> { fn display_frame(&mut self) -> Result<(), ERR> {
// enable clock signal, enable cp, display pattern -> 0xC4 (tested with the arduino version) // 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) //TODO: test control_1 or control_2 with default value 0xFF (from the datasheet)
self.interface.command(Command::DISPLAY_UPDATE_CONTROL_2)?; self.interface.command_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?;
self.interface.data(0xC4)?;
self.interface.command(Command::MASTER_ACTIVATION)?; self.interface.command(Command::MASTER_ACTIVATION)?;
// MASTER Activation should not be interupted to avoid currption of panel images // MASTER Activation should not be interupted to avoid currption of panel images
@ -194,7 +187,7 @@ where
self.interface.command(Command::NOP) self.interface.command(Command::NOP)
} }
fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> { fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> {
self.update_frame(buffer)?; self.update_frame(buffer)?;
self.display_frame() self.display_frame()
} }
@ -206,12 +199,12 @@ where
y: u16, y: u16,
width: u16, width: u16,
height: u16, height: u16,
) -> Result<(), E> { ) -> Result<(), ERR> {
self.update_partial_frame(buffer, x, y, width, height)?; self.update_partial_frame(buffer, x, y, width, height)?;
self.display_frame() self.display_frame()
} }
fn clear_frame(&mut self) -> Result<(), E> { fn clear_frame(&mut self) -> Result<(), ERR> {
self.use_full_frame()?; self.use_full_frame()?;
// clear the ram with the background color // clear the ram with the background color
@ -279,8 +272,7 @@ where
pub(crate) fn set_ram_counter(&mut self, x: u16, y: u16) -> Result<(), E> { 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 // x is positioned in bytes, so the last 3 bits which show the position inside a byte in the ram
// aren't relevant // aren't relevant
self.interface.command(Command::SET_RAM_X_ADDRESS_COUNTER)?; self.interface.command_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?;
self.interface.data((x >> 3) as u8)?;
// 2 Databytes: A[7:0] & 0..A[8] // 2 Databytes: A[7:0] & 0..A[8]
self.interface.command(Command::SET_RAM_Y_ADDRESS_COUNTER)?; self.interface.command(Command::SET_RAM_Y_ADDRESS_COUNTER)?;
@ -308,7 +300,6 @@ where
fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> { fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> {
assert!(buffer.len() == 30); assert!(buffer.len() == 30);
self.interface.command(Command::WRITE_LUT_REGISTER)?; self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer)
self.interface.multiple_data(buffer)
} }
} }

36
src/epd4in2/mod.rs

@ -160,10 +160,9 @@ where
/// epd4in2.sleep(); /// epd4in2.sleep();
/// ``` /// ```
fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Result<Self, ERR> { fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Result<Self, ERR> {
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay);
let color = Color::White; let color = Color::White;
let mut epd = EPD4in2 { let mut epd = EPD4in2 {
interface, interface,
color, color,
@ -178,9 +177,9 @@ where
self.init() self.init()
} }
//TODO: is such a long delay really needed inbetween?
fn sleep(&mut self) -> Result<(), ERR> { fn sleep(&mut self) -> Result<(), ERR> {
self.command(Command::VCOM_AND_DATA_INTERVAL_SETTING)?; self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x17])?; //border floating
self.send_data(0x17)?; //border floating
self.command(Command::VCM_DC_SETTING)?; // VCOM to 0V self.command(Command::VCM_DC_SETTING)?; // VCOM to 0V
self.command(Command::PANEL_SETTING)?; self.command(Command::PANEL_SETTING)?;
self.delay_ms(100); self.delay_ms(100);
@ -203,25 +202,17 @@ where
self.interface.command_with_data(Command::VCM_DC_SETTING, &[0x12])?; self.interface.command_with_data(Command::VCM_DC_SETTING, &[0x12])?;
self.command(Command::VCOM_AND_DATA_INTERVAL_SETTING)?;
//TODO: this was a send_command instead of a send_data. check if it's alright and doing what it should do (setting the default values) //TODO: this was a send_command instead of a send_data. check if it's alright and doing what it should do (setting the default values)
//self.send_command_u8(0x97)?; //VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7 //self.send_command_u8(0x97)?; //VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
self.send_data(0x97)?; self.interface.command_with_data(Command::VCOM_AND_DATA_INTERVAL_SETTING, &[0x97])?;
self.command(Command::DATA_START_TRANSMISSION_1)?;
for _ in 0..(buffer.len()) {
self.send_data(color_value)?;
}
self.delay_ms(2);
self.command(Command::DATA_START_TRANSMISSION_2)?; self.command(Command::DATA_START_TRANSMISSION_1)?;
//self.send_multiple_data(buffer)?; self.interface.data_x_times(color_value, buffer.len() as u16)?;
for &elem in buffer.iter() { self.delay_ms(2);
self.send_data(elem)?;
}
Ok(()) self.interface.command_with_data(Command::DATA_START_TRANSMISSION_2, buffer)
} }
fn update_partial_frame( fn update_partial_frame(
@ -283,19 +274,12 @@ where
let color_value = self.color.get_byte_value(); let color_value = self.color.get_byte_value();
self.command(Command::DATA_START_TRANSMISSION_1)?; self.command(Command::DATA_START_TRANSMISSION_1)?;
self.delay_ms(2); self.interface.data_x_times(color_value, size)?;
for _ in 0..size {
self.send_data(color_value)?;
}
self.delay_ms(2); self.delay_ms(2);
self.command(Command::DATA_START_TRANSMISSION_2)?; self.command(Command::DATA_START_TRANSMISSION_2)?;
self.delay_ms(2); self.interface.data_x_times(color_value, size)
for _ in 0..size {
self.send_data(color_value)?;
}
Ok(())
} }
/// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame()) /// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame())

Loading…
Cancel
Save