Browse Source

Make delay a function parameter

This change makes delay a function parameter where necessary and stops the need of owning the delay
embedded-hal-1.0
Chris 7 years ago
parent
commit
fabc5f262e
  1. 36
      src/epd1in54/mod.rs
  2. 37
      src/epd2in9/mod.rs
  3. 53
      src/epd4in2/mod.rs
  4. 36
      src/traits/connection_interface.rs
  5. 17
      src/traits/mod.rs

36
src/epd1in54/mod.rs

@ -39,26 +39,25 @@ use traits::connection_interface::ConnectionInterface;
/// EPD1in54 driver /// EPD1in54 driver
/// ///
pub struct EPD1in54<SPI, CS, BUSY, DC, RST, Delay> { pub struct EPD1in54<SPI, CS, BUSY, DC, RST> {
/// SPI /// SPI
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay>, interface: ConnectionInterface<SPI, CS, BUSY, DC, RST>,
/// EPD (width, height) /// EPD (width, height)
//epd: EPD, //epd: EPD,
/// Color /// Color
background_color: Color, background_color: Color,
} }
impl<SPI, CS, BUSY, DC, RST, Delay, E> EPD1in54<SPI, CS, BUSY, DC, RST, Delay> impl<SPI, CS, BUSY, DC, RST, E> EPD1in54<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn init(&mut self) -> Result<(), E> { fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), E> {
self.interface.reset(); self.interface.reset(delay);
// 3 Databytes: // 3 Databytes:
// A[7:0] // A[7:0]
@ -95,8 +94,8 @@ where
} }
impl<SPI, CS, BUSY, DC, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, E> impl<SPI, CS, BUSY, DC, RST, Delay, E> WaveshareInterface<SPI, CS, BUSY, DC, RST, E>
for EPD1in54<SPI, CS, BUSY, DC, RST, Delay> for EPD1in54<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
CS: OutputPin, CS: OutputPin,
@ -113,23 +112,23 @@ where
HEIGHT HEIGHT
} }
fn new( fn new<DELAY: DelayMs<u8>>(
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY,
) -> Result<Self, E> { ) -> Result<Self, E> {
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); let interface = ConnectionInterface::new(spi, cs, busy, dc, rst);
let mut epd = EPD1in54 { let mut epd = EPD1in54 {
interface, interface,
background_color: DEFAULT_BACKGROUND_COLOR, background_color: DEFAULT_BACKGROUND_COLOR,
}; };
epd.init()?; epd.init(delay)?;
Ok(epd) Ok(epd)
} }
fn wake_up(&mut self) -> Result<(), E> { fn wake_up<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), E> {
self.init() self.init(delay)
} }
@ -143,10 +142,6 @@ where
Ok(()) Ok(())
} }
fn delay_ms(&mut self, delay: u16) {
self.interface.delay_ms(delay)
}
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.cmd_with_data(Command::WRITE_RAM, buffer) self.interface.cmd_with_data(Command::WRITE_RAM, buffer)
@ -199,14 +194,13 @@ where
} }
} }
impl<SPI, CS, BUSY, DC, RST, D, E> EPD1in54<SPI, CS, BUSY, DC, RST, D> impl<SPI, CS, BUSY, DC, RST, E> EPD1in54<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin
D: DelayUs<u16> + DelayMs<u16>,
{ {
fn wait_until_idle(&mut self) { fn wait_until_idle(&mut self) {
self.interface.wait_until_idle(false); self.interface.wait_until_idle(false);

37
src/epd2in9/mod.rs

@ -38,26 +38,25 @@ use traits::connection_interface::ConnectionInterface;
/// EPD2in9 driver /// EPD2in9 driver
/// ///
pub struct EPD2in9<SPI, CS, BUSY, DC, RST, Delay> { pub struct EPD2in9<SPI, CS, BUSY, DC, RST> {
/// SPI /// SPI
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay>, interface: ConnectionInterface<SPI, CS, BUSY, DC, RST>,
/// EPD (width, height) /// EPD (width, height)
//epd: EPD, //epd: EPD,
/// Color /// Color
background_color: Color, background_color: Color,
} }
impl<SPI, CS, BUSY, DC, RST, Delay, E> EPD2in9<SPI, CS, BUSY, DC, RST, Delay> impl<SPI, CS, BUSY, DC, RST, E> EPD2in9<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn init(&mut self) -> Result<(), E> { fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), E> {
self.interface.reset(); self.interface.reset(delay);
// 3 Databytes: // 3 Databytes:
// A[7:0] // A[7:0]
@ -90,16 +89,15 @@ where
} }
} }
impl<SPI, CS, BUSY, DC, RST, Delay, ERR> impl<SPI, CS, BUSY, DC, RST, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR> WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
for EPD2in9<SPI, CS, BUSY, DC, RST, Delay> for EPD2in9<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = ERR>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn width(&self) -> u16 { fn width(&self) -> u16 {
WIDTH WIDTH
@ -109,17 +107,17 @@ where
HEIGHT HEIGHT
} }
fn new( fn new<DELAY: DelayMs<u8>>(
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay, spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: &mut DELAY,
) -> Result<Self, ERR> { ) -> Result<Self, ERR> {
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay); let interface = ConnectionInterface::new(spi, cs, busy, dc, rst);
let mut epd = EPD2in9 { let mut epd = EPD2in9 {
interface, interface,
background_color: DEFAULT_BACKGROUND_COLOR, background_color: DEFAULT_BACKGROUND_COLOR,
}; };
epd.init()?; epd.init(delay)?;
Ok(epd) Ok(epd)
} }
@ -135,12 +133,8 @@ where
Ok(()) Ok(())
} }
fn wake_up(&mut self) -> Result<(), ERR> { fn wake_up<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), ERR> {
self.init() self.init(delay)
}
fn delay_ms(&mut self, delay: u16) {
self.interface.delay_ms(delay)
} }
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> { fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR> {
@ -195,14 +189,13 @@ where
} }
} }
impl<SPI, CS, BUSY, DC, RST, D, E> EPD2in9<SPI, CS, BUSY, DC, RST, D> impl<SPI, CS, BUSY, DC, RST, D, E> EPD2in9<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = E>, SPI: Write<u8, Error = E>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
D: DelayUs<u16> + DelayMs<u16>,
{ {
fn wait_until_idle(&mut self) { fn wait_until_idle(&mut self) {
self.interface.wait_until_idle(false); self.interface.wait_until_idle(false);

53
src/epd4in2/mod.rs

@ -64,9 +64,9 @@ use self::command::Command;
/// EPD4in2 driver /// EPD4in2 driver
/// ///
pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> { pub struct EPD4in2<SPI, CS, BUSY, DC, RST> {
/// Connection Interface /// Connection Interface
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, D>, interface: ConnectionInterface<SPI, CS, BUSY, DC, RST>,
/// Background Color /// Background Color
color: Color, color: Color,
} }
@ -74,20 +74,19 @@ pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> {
impl<SPI, CS, BUSY, DC, RST, Delay, ERR> impl<SPI, CS, BUSY, DC, RST, ERR>
InternalWiAdditions<SPI, CS, BUSY, DC, RST, Delay, ERR> InternalWiAdditions<SPI, CS, BUSY, DC, RST, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST, Delay> for EPD4in2<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = ERR>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
fn init(&mut self) -> Result<(), ERR> { fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), ERR> {
// reset the device // reset the device
self.interface.reset(); self.interface.reset(delay);
// set the power settings // set the power settings
self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?; self.interface.cmd_with_data(Command::POWER_SETTING, &[0x03, 0x00, 0x2b, 0x2b, 0xff])?;
@ -114,16 +113,15 @@ where
} }
} }
impl<SPI, CS, BUSY, DC, RST, Delay, ERR> impl<SPI, CS, BUSY, DC, RST, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR> WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST, Delay> for EPD4in2<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = ERR>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC
/// ///
@ -140,8 +138,8 @@ where
/// ///
/// epd4in2.sleep(); /// epd4in2.sleep();
/// ``` /// ```
fn new(spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: Delay) -> Result<Self, ERR> { 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, delay); let interface = ConnectionInterface::new(spi, cs, busy, dc, rst);
let color = DEFAULT_BACKGROUND_COLOR; let color = DEFAULT_BACKGROUND_COLOR;
let mut epd = EPD4in2 { let mut epd = EPD4in2 {
@ -149,13 +147,13 @@ where
color, color,
}; };
epd.init()?; epd.init(delay)?;
Ok(epd) Ok(epd)
} }
fn wake_up(&mut self) -> Result<(), ERR> { fn wake_up<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), ERR> {
self.init() self.init(delay)
} }
//TODO: is such a long delay really needed inbetween? //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.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::VCM_DC_SETTING)?; // VCOM to 0V
self.command(Command::PANEL_SETTING)?; 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 self.command(Command::POWER_SETTING)?; //VG&VS to 0V fast
for _ in 0..4 { for _ in 0..4 {
self.send_data(&[0x00])?; self.send_data(&[0x00])?;
} }
self.delay_ms(100);
//TODO: Removal of delay. TEST!
//self.delay_ms(100);
self.command(Command::POWER_OFF)?; self.command(Command::POWER_OFF)?;
self.wait_until_idle(); self.wait_until_idle();
@ -191,7 +193,8 @@ where
self.command(Command::DATA_START_TRANSMISSION_1)?; self.command(Command::DATA_START_TRANSMISSION_1)?;
self.interface.data_x_times(color_value, buffer.len() as u16)?; 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) self.interface.cmd_with_data(Command::DATA_START_TRANSMISSION_2, buffer)
} }
@ -257,7 +260,8 @@ where
self.command(Command::DATA_START_TRANSMISSION_1)?; self.command(Command::DATA_START_TRANSMISSION_1)?;
self.interface.data_x_times(color_value, size)?; 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.command(Command::DATA_START_TRANSMISSION_2)?;
self.interface.data_x_times(color_value, size) self.interface.data_x_times(color_value, size)
@ -279,14 +283,9 @@ where
fn height(&self) -> u16 { fn height(&self) -> u16 {
HEIGHT HEIGHT
} }
fn delay_ms(&mut self, delay: u16) {
self.interface.delay_ms(delay)
}
} }
impl<SPI, CS, BUSY, DC, RST, D, ERR> EPD4in2<SPI, CS, BUSY, DC, RST, D> impl<SPI, CS, BUSY, DC, RST, D, ERR> EPD4in2<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = ERR>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,

36
src/traits/connection_interface.rs

@ -7,7 +7,7 @@ use traits::Command;
/// The Connection Interface of all (?) Waveshare EPD-Devices /// The Connection Interface of all (?) Waveshare EPD-Devices
/// ///
pub(crate) struct ConnectionInterface<SPI, CS, BUSY, DC, RST, D> { pub(crate) struct ConnectionInterface<SPI, CS, BUSY, DC, RST> {
/// SPI /// SPI
spi: SPI, spi: SPI,
/// CS for SPI /// CS for SPI
@ -18,28 +18,24 @@ pub(crate) struct ConnectionInterface<SPI, CS, BUSY, DC, RST, D> {
dc: DC, dc: DC,
/// Pin for Reseting /// Pin for Reseting
rst: RST, rst: RST,
/// The concrete Delay implementation
delay: D,
} }
impl<SPI, CS, BUSY, DC, RST, Delay, ERR> impl<SPI, CS, BUSY, DC, RST, ERR>
ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay> ConnectionInterface<SPI, CS, BUSY, DC, RST>
where where
SPI: Write<u8, Error = ERR>, SPI: Write<u8, Error = ERR>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
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 { ConnectionInterface {
spi, spi,
cs, cs,
busy, busy,
dc, dc,
rst, rst,
delay,
} }
} }
@ -130,35 +126,27 @@ where
/// Most likely there was a mistake with the 2in9 busy connection /// 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 /// //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) { 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 //low: busy, high: idle
while (is_busy_low && self.busy.is_low()) || (!is_busy_low && self.busy.is_high()) { 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 //TODO: REMOVAL of DELAY: it's only waiting for the signal anyway and should continue work asap
self.delay_ms(5); //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. /// Resets the device.
/// ///
/// Often used to awake the module from deep sleep. See [EPD4in2::sleep()](EPD4in2::sleep()) /// 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? /// TODO: Takes at least 400ms of delay alone, can it be shortened?
pub(crate) fn reset(&mut self) { pub(crate) fn reset<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) {
self.rst.set_low(); self.rst.set_low();
//TODO: why 200ms? (besides being in the arduino version) //TODO: why 200ms? (besides being in the arduino version)
self.delay_ms(200); delay.delay_ms(200);
self.rst.set_high(); self.rst.set_high();
//TODO: same as 3 lines above //TODO: same as 3 lines above
self.delay_ms(200); delay.delay_ms(200);
} }
} }

17
src/traits/mod.rs

@ -26,14 +26,13 @@ trait LUTSupport<ERR> {
} }
pub(crate) trait InternalWiAdditions<SPI, CS, BUSY, DC, RST, Delay, ERR> pub(crate) trait InternalWiAdditions<SPI, CS, BUSY, DC, RST, ERR>
where where
SPI: Write<u8>, SPI: Write<u8>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
/// This initialises the EPD and powers it up /// This initialises the EPD and powers it up
/// ///
@ -45,26 +44,25 @@ where
/// This function calls [reset()](WaveshareInterface::reset()), /// This function calls [reset()](WaveshareInterface::reset()),
/// so you don't need to call reset your self when trying to wake your device up /// so you don't need to call reset your self when trying to wake your device up
/// after setting it to sleep. /// after setting it to sleep.
fn init(&mut self) -> Result<(), ERR>; fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), ERR>;
} }
pub trait WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR> pub trait WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
where where
SPI: Write<u8>, SPI: Write<u8>,
CS: OutputPin, CS: OutputPin,
BUSY: InputPin, BUSY: InputPin,
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{ {
/// Creates a new driver from a SPI peripheral, CS Pin, Busy InputPin, DC /// 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 /// This already initialises the device. That means [init()](WaveshareInterface::init()) isn't needed directly afterwards
fn new( fn new<DELAY: DelayUs<u8>>(
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, ERR> ) -> Result<Self, ERR>
where where
Self: Sized; Self: Sized;
@ -92,11 +90,6 @@ where
/// Get the height of the display /// Get the height of the display
fn height(&self) -> u16; 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 /// Transmit a full frame to the SRAM of the EPD
fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR>; fn update_frame(&mut self, buffer: &[u8]) -> Result<(), ERR>;

Loading…
Cancel
Save