Make delay a function parameter

This change makes delay a function parameter where necessary and stops the need of owning the delay
This commit is contained in:
Chris 2018-10-10 13:23:43 +02:00
parent 1791388a8b
commit fabc5f262e
5 changed files with 73 additions and 106 deletions

View File

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

View File

@ -38,26 +38,25 @@ use traits::connection_interface::ConnectionInterface;
/// EPD2in9 driver
///
pub struct EPD2in9<SPI, CS, BUSY, DC, RST, Delay> {
pub struct EPD2in9<SPI, CS, BUSY, DC, RST> {
/// SPI
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay>,
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST>,
/// EPD (width, height)
//epd: EPD,
/// 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
SPI: Write<u8, Error = E>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{
fn init(&mut self) -> Result<(), E> {
self.interface.reset();
fn init<DELAY: DelayMs<u8>>(&mut self, delay: &mut DELAY) -> Result<(), E> {
self.interface.reset(delay);
// 3 Databytes:
// A[7:0]
@ -90,16 +89,15 @@ where
}
}
impl<SPI, CS, BUSY, DC, RST, Delay, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR>
for EPD2in9<SPI, CS, BUSY, DC, RST, Delay>
impl<SPI, CS, BUSY, DC, RST, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
for EPD2in9<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8, Error = ERR>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{
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<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 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<DELAY: DelayMs<u8>>(&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<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
SPI: Write<u8, Error = E>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
D: DelayUs<u16> + DelayMs<u16>,
{
fn wait_until_idle(&mut self) {
self.interface.wait_until_idle(false);

View File

@ -64,9 +64,9 @@ use self::command::Command;
/// EPD4in2 driver
///
pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> {
pub struct EPD4in2<SPI, CS, BUSY, DC, RST> {
/// Connection Interface
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST, D>,
interface: ConnectionInterface<SPI, CS, BUSY, DC, RST>,
/// Background Color
color: Color,
}
@ -74,20 +74,19 @@ pub struct EPD4in2<SPI, CS, BUSY, DC, RST, D> {
impl<SPI, CS, BUSY, DC, RST, Delay, ERR>
InternalWiAdditions<SPI, CS, BUSY, DC, RST, Delay, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST, Delay>
impl<SPI, CS, BUSY, DC, RST, ERR>
InternalWiAdditions<SPI, CS, BUSY, DC, RST, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8, Error = ERR>,
CS: OutputPin,
BUSY: InputPin,
DC: 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
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<SPI, CS, BUSY, DC, RST, Delay, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, Delay, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST, Delay>
impl<SPI, CS, BUSY, DC, RST, ERR>
WaveshareInterface<SPI, CS, BUSY, DC, RST, ERR>
for EPD4in2<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8, Error = ERR>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{
/// 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<Self, ERR> {
let interface = ConnectionInterface::new(spi, cs, busy, dc, rst, delay);
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);
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<DELAY: DelayMs<u8>>(&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<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
SPI: Write<u8, Error = ERR>,
CS: OutputPin,

View File

@ -7,7 +7,7 @@ use traits::Command;
/// 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,
/// CS for SPI
@ -18,28 +18,24 @@ pub(crate) struct ConnectionInterface<SPI, CS, BUSY, DC, RST, D> {
dc: DC,
/// Pin for Reseting
rst: RST,
/// The concrete Delay implementation
delay: D,
}
impl<SPI, CS, BUSY, DC, RST, Delay, ERR>
ConnectionInterface<SPI, CS, BUSY, DC, RST, Delay>
impl<SPI, CS, BUSY, DC, RST, ERR>
ConnectionInterface<SPI, CS, BUSY, DC, RST>
where
SPI: Write<u8, Error = ERR>,
CS: OutputPin,
BUSY: InputPin,
DC: 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 {
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<DELAY: DelayMs<u8>>(&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);
}
}

View File

@ -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
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{
/// 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<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
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
Delay: DelayUs<u16> + DelayMs<u16>,
{
/// 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<DELAY: DelayUs<u8>>(
spi: SPI, cs: CS, busy: BUSY, dc: DC, rst: RST, delay: DELAY,
) -> Result<Self, ERR>
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>;