From 05c36a928dffa2267d46dc787ea9894904b6f5eb Mon Sep 17 00:00:00 2001 From: "Andres O. Vela" Date: Tue, 2 Feb 2021 09:51:52 +0100 Subject: [PATCH 1/2] Simplify 8-bit GPIO interface --- parallel-gpio/src/lib.rs | 150 ++++++--------------------------------- src/lib.rs | 2 + 2 files changed, 23 insertions(+), 129 deletions(-) diff --git a/parallel-gpio/src/lib.rs b/parallel-gpio/src/lib.rs index 43b6a77..7ef7b8c 100644 --- a/parallel-gpio/src/lib.rs +++ b/parallel-gpio/src/lib.rs @@ -18,57 +18,23 @@ pub use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand}; /// All pins are supposed to be high-active, high for the D/C pin meaning "data" and the /// write-enable being pulled low before the setting of the bits and supposed to be sampled at a /// low to high edge. -pub struct PGPIO8BitInterface { - p0: P0, - p1: P1, - p2: P2, - p3: P3, - p4: P4, - p5: P5, - p6: P6, - p7: P7, +pub struct PGPIO8BitInterface { + dbus: [DBUS; 8], dc: DC, wr: WR, last: u8, } -impl - PGPIO8BitInterface +impl PGPIO8BitInterface where - P0: OutputPin, - P1: OutputPin, - P2: OutputPin, - P3: OutputPin, - P4: OutputPin, - P5: OutputPin, - P6: OutputPin, - P7: OutputPin, + DBUS: OutputPin, DC: OutputPin, WR: OutputPin, { /// Create new parallel GPIO interface for communication with a display driver - #[allow(clippy::too_many_arguments)] - pub fn new( - p0: P0, - p1: P1, - p2: P2, - p3: P3, - p4: P4, - p5: P5, - p6: P6, - p7: P7, - dc: DC, - wr: WR, - ) -> Self { + pub fn new(dbus: [DBUS; 8], dc: DC, wr: WR) -> Self { Self { - p0, - p1, - p2, - p3, - p4, - p5, - p6, - p7, + dbus, dc, wr, last: 0, @@ -77,11 +43,8 @@ where /// Consume the display interface and return /// the GPIO pins used by it - pub fn release(self) -> (P0, P1, P2, P3, P4, P5, P6, P7, DC, WR) { - ( - self.p0, self.p1, self.p2, self.p3, self.p4, self.p5, self.p6, self.p7, self.dc, - self.wr, - ) + pub fn release(self) -> ([DBUS; 8], DC, WR) { + (self.dbus, self.dc, self.wr) } fn set_value(self: &mut Self, value: u8) -> Result<(), DisplayError> { @@ -95,99 +58,30 @@ where self.last = value; - if changed & 1 != 0 { - if value & 1 != 0 { - self.p0.set_high() - } else { - self.p0.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 2 != 0 { - if value & 2 != 0 { - self.p1.set_high() - } else { - self.p1.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 4 != 0 { - if value & 4 != 0 { - self.p2.set_high() - } else { - self.p2.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 8 != 0 { - if value & 8 != 0 { - self.p3.set_high() - } else { - self.p3.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 16 != 0 { - if value & 16 != 0 { - self.p4.set_high() - } else { - self.p4.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 32 != 0 { - if value & 32 != 0 { - self.p5.set_high() - } else { - self.p5.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 64 != 0 { - if value & 64 != 0 { - self.p6.set_high() - } else { - self.p6.set_low() - } - .map_err(|_| DisplayError::BusWriteError)? - }; - - if changed & 128 != 0 { - if value & 128 != 0 { - self.p7.set_high() - } else { - self.p7.set_low() + for i in 0..8 { + if changed & (1 << i) != 0 { + if value & (1 << i) == 0 { + self.dbus[i].set_low() + } else { + self.dbus[i].set_high() + } + .map_err(|_| DisplayError::BusWriteError)?; } - .map_err(|_| DisplayError::BusWriteError)? - }; + } Ok(()) } } -impl WriteOnlyDataCommand - for PGPIO8BitInterface +impl WriteOnlyDataCommand for PGPIO8BitInterface where - P0: OutputPin, - P1: OutputPin, - P2: OutputPin, - P3: OutputPin, - P4: OutputPin, - P5: OutputPin, - P6: OutputPin, - P7: OutputPin, + DBUS: OutputPin, DC: OutputPin, WR: OutputPin, { fn send_commands(&mut self, cmds: DataFormat<'_>) -> Result<(), DisplayError> { use byte_slice_cast::*; - self.dc.set_low().map_err(|_| DisplayError::BusWriteError)?; + self.dc.set_low().map_err(|_| DisplayError::DCError)?; match cmds { DataFormat::U8(slice) => slice.iter().try_for_each(|cmd| { self.wr.set_low().map_err(|_| DisplayError::BusWriteError)?; @@ -238,9 +132,7 @@ where fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> { use byte_slice_cast::*; - self.dc - .set_high() - .map_err(|_| DisplayError::BusWriteError)?; + self.dc.set_high().map_err(|_| DisplayError::DCError)?; match buf { DataFormat::U8(slice) => slice.iter().try_for_each(|d| { self.wr.set_low().map_err(|_| DisplayError::BusWriteError)?; diff --git a/src/lib.rs b/src/lib.rs index 2f53a37..1540d40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,8 @@ pub enum DisplayError { CSError, /// The requested DataFormat is not implemented by this display interface implementation DataFormatNotImplemented, + /// Unable to assert or de-assert reset signal + RSError, } /// DI specific data format wrapper around slices of various widths From 7f8511854f9d6455c0a46404473c6454cca3b209 Mon Sep 17 00:00:00 2001 From: "Andres O. Vela" Date: Wed, 3 Feb 2021 00:00:38 +0100 Subject: [PATCH 2/2] Bump display-interface-parallel-gpio version to 0.5 and display-interface version to 0.4.1 --- Cargo.toml | 2 +- parallel-gpio/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a171193..17e9fef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "display-interface" description = "Traits for interfaces used to drive displays" -version = "0.4.0" +version = "0.4.1" authors = ["Daniel Egger "] repository = "https://github.com/therealprof/display-interface" documentation = "https://docs.rs/display-interface" diff --git a/parallel-gpio/Cargo.toml b/parallel-gpio/Cargo.toml index feeb092..60679b4 100644 --- a/parallel-gpio/Cargo.toml +++ b/parallel-gpio/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "display-interface-parallel-gpio" description = "Generic parallel GPIO interface for display interfaces" -version = "0.4.1" +version = "0.5.0" authors = ["Daniel Egger "] repository = "https://github.com/therealprof/display-interface" documentation = "https://docs.rs/display-interface-parallel-gpio"