mirror of
https://github.com/Anshorei/epd-waveshare.git
synced 2026-10-03 22:06:43 +00:00
Shortened TypeDefinitions,
made init a private function, added a crate internal extended waveshareinterface, added a combined Connection_interface::data_and_command function, added a wake_up function, added a updated_and_display_partial_frame_function
This commit is contained in:
+89
-87
@@ -39,32 +39,76 @@ use interface::connection_interface::ConnectionInterface;
|
||||
|
||||
/// EPD1in54 driver
|
||||
///
|
||||
pub struct EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay> {
|
||||
pub struct EPD1in54<SPI, CS, BUSY, DC, RST, Delay> {
|
||||
/// SPI
|
||||
interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>,
|
||||
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay>,
|
||||
/// EPD (width, height)
|
||||
//epd: EPD,
|
||||
/// Color
|
||||
background_color: Color,
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DataCommand, RST, Delay, E> EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay>
|
||||
impl<SPI, CS, BUSY, DC, RST, Delay, E> EPD1in54<SPI, CS, BUSY, DC, RST, Delay>
|
||||
where
|
||||
SPI: Write<u8, Error = E>,
|
||||
CS: OutputPin,
|
||||
BUSY: InputPin,
|
||||
DataCommand: OutputPin,
|
||||
DC: OutputPin,
|
||||
RST: OutputPin,
|
||||
Delay: DelayUs<u16> + DelayMs<u16>,
|
||||
{}
|
||||
{
|
||||
fn init(&mut self) -> Result<(), E> {
|
||||
self.interface.reset();
|
||||
|
||||
impl<SPI, CS, BUSY, DataCommand, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DataCommand, RST, Delay, E>
|
||||
for EPD1in54<SPI, CS, BUSY, DataCommand, RST, Delay>
|
||||
// 3 Databytes:
|
||||
// A[7:0]
|
||||
// 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)?;
|
||||
|
||||
// 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)?;
|
||||
|
||||
// One Databyte with value 0xA8 for 7V VCOM
|
||||
self.interface.command(Command::WRITE_VCOM_REGISTER)?;
|
||||
self.interface.data(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)?;
|
||||
|
||||
// One Databyte with default value 0x08 for 2us per line
|
||||
self.interface.command(Command::SET_GATE_LINE_WIDTH)?;
|
||||
self.interface.data(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.set_lut()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, E>
|
||||
for EPD1in54<SPI, CS, BUSY, DC, RST, Delay>
|
||||
where
|
||||
SPI: Write<u8, Error = E>,
|
||||
CS: OutputPin,
|
||||
BUSY: InputPin,
|
||||
DataCommand: OutputPin,
|
||||
DC: OutputPin,
|
||||
RST: OutputPin,
|
||||
Delay: DelayUs<u16> + DelayMs<u16>,
|
||||
{
|
||||
@@ -77,8 +121,10 @@ where
|
||||
}
|
||||
|
||||
fn new(
|
||||
interface: ConnectionInterface<SPI, CS, BUSY, DataCommand, RST, Delay>,
|
||||
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay,
|
||||
) -> Result<Self, E> {
|
||||
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay);
|
||||
|
||||
let mut epd = EPD1in54 {
|
||||
interface,
|
||||
background_color: DEFAULT_BACKGROUND_COLOR,
|
||||
@@ -89,63 +135,22 @@ where
|
||||
Ok(epd)
|
||||
}
|
||||
|
||||
fn init(&mut self) -> Result<(), E> {
|
||||
self.reset();
|
||||
|
||||
// 3 Databytes:
|
||||
// A[7:0]
|
||||
// 0.. A[8]
|
||||
// 0.. B[2:0]
|
||||
// Default Values: A = Height of Screen (0x127), B = 0x00 (GD, SM and TB=0?)
|
||||
self.interface.send_command(Command::DRIVER_OUTPUT_CONTROL)?;
|
||||
self.interface.send_data(HEIGHT as u8)?;
|
||||
self.interface.send_data((HEIGHT >> 8) as u8)?;
|
||||
self.interface.send_data(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.send_command(Command::BOOSTER_SOFT_START_CONTROL)?;
|
||||
self.interface.send_data(0xD7)?;
|
||||
self.interface.send_data(0xD6)?;
|
||||
self.interface.send_data(0x9D)?;
|
||||
|
||||
// One Databyte with value 0xA8 for 7V VCOM
|
||||
self.interface.send_command(Command::WRITE_VCOM_REGISTER)?;
|
||||
self.interface.send_data(0xA8)?;
|
||||
|
||||
// One Databyte with default value 0x1A for 4 dummy lines per gate
|
||||
self.interface.send_command(Command::SET_DUMMY_LINE_PERIOD)?;
|
||||
self.interface.send_data(0x1A)?;
|
||||
|
||||
// One Databyte with default value 0x08 for 2us per line
|
||||
self.interface.send_command(Command::SET_GATE_LINE_WIDTH)?;
|
||||
self.interface.send_data(0x08)?;
|
||||
|
||||
// One Databyte with default value 0x03
|
||||
// -> address: x increment, y increment, address counter is updated in x direction
|
||||
self.interface.send_command(Command::DATA_ENTRY_MODE_SETTING)?;
|
||||
self.interface.send_data(0x03)?;
|
||||
|
||||
self.set_lut()
|
||||
fn wake_up(&mut self) -> Result<(), E> {
|
||||
self.init()
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn sleep(&mut self) -> Result<(), E> {
|
||||
self.interface.send_command(Command::DEEP_SLEEP_MODE)?;
|
||||
self.interface.command(Command::DEEP_SLEEP_MODE)?;
|
||||
// 0x00 for Normal mode (Power on Reset), 0x01 for Deep Sleep Mode
|
||||
//TODO: is 0x00 needed here?
|
||||
self.interface.send_data(0x00)?;
|
||||
self.interface.data(0x00)?;
|
||||
|
||||
self.wait_until_idle();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.interface.reset()
|
||||
}
|
||||
|
||||
fn delay_ms(&mut self, delay: u16) {
|
||||
self.interface.delay_ms(delay)
|
||||
}
|
||||
@@ -153,8 +158,7 @@ where
|
||||
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
|
||||
self.use_full_frame()?;
|
||||
|
||||
self.interface.send_command(Command::WRITE_RAM)?;
|
||||
self.interface.send_multiple_data(buffer)
|
||||
self.interface.command_with_data(Command::WRITE_RAM, buffer)
|
||||
}
|
||||
|
||||
//TODO: update description: last 3 bits will be ignored for width and x_pos
|
||||
@@ -169,25 +173,19 @@ where
|
||||
self.set_ram_area(x, y, x + width, y + height)?;
|
||||
self.set_ram_counter(x, y)?;
|
||||
|
||||
self.interface.send_command(Command::WRITE_RAM)?;
|
||||
self.interface.send_multiple_data(buffer)
|
||||
self.interface.command(Command::WRITE_RAM)?;
|
||||
self.interface.multiple_data(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.send_command(Command::DISPLAY_UPDATE_CONTROL_2)?;
|
||||
self.interface.send_data(0xC4)?;
|
||||
self.interface.command_with_data(Command::DISPLAY_UPDATE_CONTROL_2, &[0xC4])?;
|
||||
|
||||
self.interface.send_command(Command::MASTER_ACTIVATION)?;
|
||||
self.interface.command(Command::MASTER_ACTIVATION)?;
|
||||
// MASTER Activation should not be interupted to avoid currption of panel images
|
||||
// therefore a terminate command is send
|
||||
self.interface.send_command(Command::TERMINATE_COMMANDS_AND_FRAME_WRITE)
|
||||
}
|
||||
|
||||
fn update_and_display_frame(&mut self, buffer: &[u8]) -> Result<(), E> {
|
||||
self.update_frame(buffer)?;
|
||||
self.display_frame()
|
||||
self.interface.command(Command::NOP)
|
||||
}
|
||||
|
||||
fn clear_frame(&mut self) -> Result<(), E> {
|
||||
@@ -196,14 +194,19 @@ where
|
||||
// clear the ram with the background color
|
||||
let color = self.background_color.get_byte_value();
|
||||
|
||||
self.interface.send_command(Command::WRITE_RAM)?;
|
||||
self.interface.send_data_x_times(color, WIDTH / 8 * HEIGHT)
|
||||
self.interface.command(Command::WRITE_RAM)?;
|
||||
self.interface.data_x_times(color, WIDTH / 8 * HEIGHT)
|
||||
}
|
||||
|
||||
/// Sets the backgroundcolor for various commands like [WaveshareInterface::clear_frame()](clear_frame())
|
||||
|
||||
fn set_background_color(&mut self, background_color: Color) {
|
||||
self.background_color = background_color;
|
||||
}
|
||||
|
||||
|
||||
fn background_color(&self) -> &Color {
|
||||
&self.background_color
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST, D, E> EPD1in54<SPI, CS, BUSY, DC, RST, D>
|
||||
@@ -239,28 +242,28 @@ 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.send_command(Command::SET_RAM_X_ADDRESS_START_END_POSITION)?;
|
||||
self.interface.send_data((start_x >> 3) as u8)?;
|
||||
self.interface.send_data((end_x >> 3) as u8)?;
|
||||
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)?;
|
||||
|
||||
// 2 Databytes: A[7:0] & 0..A[8] for each - start and end
|
||||
self.interface.send_command(Command::SET_RAM_Y_ADDRESS_START_END_POSITION)?;
|
||||
self.interface.send_data(start_y as u8)?;
|
||||
self.interface.send_data((start_y >> 8) as u8)?;
|
||||
self.interface.send_data(end_y as u8)?;
|
||||
self.interface.send_data((end_y >> 8) as u8)
|
||||
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)
|
||||
}
|
||||
|
||||
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.send_command(Command::SET_RAM_X_ADDRESS_COUNTER)?;
|
||||
self.interface.send_data((x >> 3) as u8)?;
|
||||
self.interface.command_with_data(Command::SET_RAM_X_ADDRESS_COUNTER, &[(x >> 3) as u8])?;
|
||||
|
||||
// 2 Databytes: A[7:0] & 0..A[8]
|
||||
self.interface.send_command(Command::SET_RAM_Y_ADDRESS_COUNTER)?;
|
||||
self.interface.send_data(y as u8)?;
|
||||
self.interface.send_data((y >> 8) as u8)?;
|
||||
self.interface.command_with_data(
|
||||
Command::SET_RAM_Y_ADDRESS_COUNTER,
|
||||
&[y as u8, (y >> 8) as u8]
|
||||
)?;
|
||||
|
||||
self.wait_until_idle();
|
||||
Ok(())
|
||||
@@ -283,7 +286,6 @@ where
|
||||
|
||||
fn set_lut_helper(&mut self, buffer: &[u8]) -> Result<(), E> {
|
||||
assert!(buffer.len() == 30);
|
||||
self.interface.send_command(Command::WRITE_LUT_REGISTER)?;
|
||||
self.interface.send_multiple_data(buffer)
|
||||
self.interface.command_with_data(Command::WRITE_LUT_REGISTER, buffer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user