From 38dc5126eedc480422c2d131bc43efef8b101909 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Oct 2018 13:23:43 +0200 Subject: [PATCH] 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>;