@ -1,9 +1,6 @@
use crate ::traits ::Command ;
use crate ::traits ::Command ;
use core ::marker ::PhantomData ;
use core ::marker ::PhantomData ;
use embedded_hal ::{
use crate ::eh_prelude ::* ;
blocking ::{ delay ::* , spi ::Write } ,
digital ::* ,
} ;
/// The Connection Interface of all (?) Waveshare EPD-Devices
/// The Connection Interface of all (?) Waveshare EPD-Devices
///
///
@ -29,7 +26,7 @@ where
BUSY : InputPin ,
BUSY : InputPin ,
DC : OutputPin ,
DC : OutputPin ,
RST : OutputPin ,
RST : OutputPin ,
DELAY : DelayMs < u8 > ,
DELAY : DelayUs ,
{
{
pub fn new ( cs : CS , busy : BUSY , dc : DC , rst : RST ) -> Self {
pub fn new ( cs : CS , busy : BUSY , dc : DC , rst : RST ) -> Self {
DisplayInterface {
DisplayInterface {
@ -47,7 +44,7 @@ where
/// Enables direct interaction with the device with the help of [data()](DisplayInterface::data())
/// Enables direct interaction with the device with the help of [data()](DisplayInterface::data())
pub ( crate ) fn cmd < T : Command > ( & mut self , spi : & mut SPI , command : T ) -> Result < ( ) , SPI ::Error > {
pub ( crate ) fn cmd < T : Command > ( & mut self , spi : & mut SPI , command : T ) -> Result < ( ) , SPI ::Error > {
// low for commands
// low for commands
let _ = self . dc . try_ set_low( ) ;
let _ = self . dc . set_low ( ) ;
// Transfer the command over spi
// Transfer the command over spi
self . write ( spi , & [ command . address ( ) ] )
self . write ( spi , & [ command . address ( ) ] )
@ -58,7 +55,7 @@ where
/// Enables direct interaction with the device with the help of [command()](Epd4in2::command())
/// Enables direct interaction with the device with the help of [command()](Epd4in2::command())
pub ( crate ) fn data ( & mut self , spi : & mut SPI , data : & [ u8 ] ) -> Result < ( ) , SPI ::Error > {
pub ( crate ) fn data ( & mut self , spi : & mut SPI , data : & [ u8 ] ) -> Result < ( ) , SPI ::Error > {
// high for data
// high for data
let _ = self . dc . try_ set_high( ) ;
let _ = self . dc . set_high ( ) ;
for val in data . iter ( ) . copied ( ) {
for val in data . iter ( ) . copied ( ) {
// Transfer data one u8 at a time over spi
// Transfer data one u8 at a time over spi
@ -91,7 +88,7 @@ where
repetitions : u32 ,
repetitions : u32 ,
) -> Result < ( ) , SPI ::Error > {
) -> Result < ( ) , SPI ::Error > {
// high for data
// high for data
let _ = self . dc . try_ set_high( ) ;
let _ = self . dc . set_high ( ) ;
// Transfer data (u8) over spi
// Transfer data (u8) over spi
for _ in 0 .. repetitions {
for _ in 0 .. repetitions {
self . write ( spi , & [ val ] ) ? ;
self . write ( spi , & [ val ] ) ? ;
@ -102,21 +99,21 @@ where
// spi write helper/abstraction function
// spi write helper/abstraction function
fn write ( & mut self , spi : & mut SPI , data : & [ u8 ] ) -> Result < ( ) , SPI ::Error > {
fn write ( & mut self , spi : & mut SPI , data : & [ u8 ] ) -> Result < ( ) , SPI ::Error > {
// activate spi with cs low
// activate spi with cs low
let _ = self . cs . try_ set_low( ) ;
let _ = self . cs . set_low ( ) ;
// transfer spi data
// transfer spi data
// Be careful!! Linux has a default limit of 4096 bytes per spi transfer
// Be careful!! Linux has a default limit of 4096 bytes per spi transfer
// see https://raspberrypi.stackexchange.com/questions/65595/spi-transfer-fails-with-buffer-size-greater-than-4096
// see https://raspberrypi.stackexchange.com/questions/65595/spi-transfer-fails-with-buffer-size-greater-than-4096
if cfg! ( target_os = "linux" ) {
if cfg! ( target_os = "linux" ) {
for data_chunk in data . chunks ( 4096 ) {
for data_chunk in data . chunks ( 4096 ) {
spi . try_ write( data_chunk ) ? ;
spi . write ( data_chunk ) ? ;
}
}
} else {
} else {
spi . try_ write( data ) ? ;
spi . write ( data ) ? ;
}
}
// deactivate spi with cs high
// deactivate spi with cs high
let _ = self . cs . try_ set_high( ) ;
let _ = self . cs . set_high ( ) ;
Ok ( ( ) )
Ok ( ( ) )
}
}
@ -158,8 +155,8 @@ 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 is_busy ( & self , is_busy_low : bool ) -> bool {
pub ( crate ) fn is_busy ( & self , is_busy_low : bool ) -> bool {
( is_busy_low & & self . busy . try_ is_low( ) . unwrap_or ( false ) )
( is_busy_low & & self . busy . is_low ( ) . unwrap_or ( false ) )
| | ( ! is_busy_low & & self . busy . try_ is_high( ) . unwrap_or ( false ) )
| | ( ! is_busy_low & & self . busy . is_high ( ) . unwrap_or ( false ) )
}
}
/// Resets the device.
/// Resets the device.
@ -169,15 +166,15 @@ where
/// The timing of keeping the reset pin low seems to be important and different per device.
/// The timing of keeping the reset pin low seems to be important and different per device.
/// Most displays seem to require keeping it low for 10ms, but the 7in5_v2 only seems to reset
/// Most displays seem to require keeping it low for 10ms, but the 7in5_v2 only seems to reset
/// properly with 2ms
/// properly with 2ms
pub ( crate ) fn reset ( & mut self , delay : & mut DELAY , duration : u8 ) {
pub ( crate ) fn reset ( & mut self , delay : & mut DELAY , duration : u32 ) {
let _ = self . rst . try_ set_high( ) ;
let _ = self . rst . set_high ( ) ;
delay . try_ delay_ms( 10 ) ;
delay . delay_ms ( 10 ) ;
let _ = self . rst . try_ set_low( ) ;
let _ = self . rst . set_low ( ) ;
delay . try_ delay_ms( duration ) ;
delay . delay_ms ( duration ) ;
let _ = self . rst . try_ set_high( ) ;
let _ = self . rst . set_high ( ) ;
//TODO: the upstream libraries always sleep for 200ms here
//TODO: the upstream libraries always sleep for 200ms here
// 10ms works fine with just for the 7in5_v2 but this needs to be validated for other devices
// 10ms works fine with just for the 7in5_v2 but this needs to be validated for other devices
delay . try_ delay_ms( 200 ) ;
delay . delay_ms ( 200 ) ;
}
}
}
}