mirror of
https://github.com/Anshorei/epd-waveshare.git
synced 2026-10-03 22:06:43 +00:00
Made SPI shareable
Disown spi for better shareabilty
This commit is contained in:
+56
-52
@@ -47,15 +47,15 @@ pub struct EPD2in9<SPI, CS, BUSY, DC, RST> {
|
||||
background_color: Color,
|
||||
}
|
||||
|
||||
impl<SPI, CS, BUSY, DC, RST, E> EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
impl<SPI, CS, BUSY, DC, RST> EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
where
|
||||
SPI: Write<u8, Error = E>,
|
||||
SPI: Write<u8>,
|
||||
CS: OutputPin,
|
||||
BUSY: InputPin,
|
||||
DC: OutputPin,
|
||||
RST: OutputPin,
|
||||
{
|
||||
fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), E> {
|
||||
fn init<DELAY: DelayMs<u8>>(&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<SPI, CS, BUSY, DC, RST, ERR>
|
||||
WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
|
||||
impl<SPI, CS, BUSY, DC, RST>
|
||||
WaveshareInterface<SPI, CS, BUSY, DC, RST>
|
||||
for EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
where
|
||||
SPI: Write<u8, Error = ERR>,
|
||||
SPI: Write<u8>,
|
||||
CS: OutputPin,
|
||||
BUSY: InputPin,
|
||||
DC: OutputPin,
|
||||
@@ -108,75 +108,77 @@ where
|
||||
}
|
||||
|
||||
fn new<DELAY: DelayMs<u8>>(
|
||||
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY,
|
||||
) -> Result<Self, ERR> {
|
||||
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst);
|
||||
spi: &mut SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY,
|
||||
) -> Result<Self, SPI::Error> {
|
||||
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<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), ERR> {
|
||||
self.init(delay)
|
||||
fn wake_up<DELAY: DelayMs<u8>>(&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<SPI, CS, BUSY, DC, RST, E> EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
impl<SPI, CS, BUSY, DC, RST> EPD2in9<SPI, CS, BUSY, DC, RST>
|
||||
where
|
||||
SPI: Write<u8, Error = E>,
|
||||
SPI: Write<u8>,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user