From 954d111959d88605cffac1a3eacdf7bede47959a Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 10 May 2021 17:08:17 -0500 Subject: [PATCH 1/3] Use `display_interface::DisplayError` --- Cargo.toml | 2 +- src/graphics.rs | 9 ++++----- src/lib.rs | 52 +++++++++++++++++++++---------------------------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e106fd1..f03bffd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ edition = "2018" [dependencies] -display-interface = "0.4.0" +display-interface = "0.4.1" embedded-hal = "0.2.4" [dependencies.embedded-graphics] diff --git a/src/graphics.rs b/src/graphics.rs index 7a73acd..9af929a 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,6 +1,6 @@ use crate::{Ili9341, OutputPin}; -use core::{fmt::Debug, iter}; +use core::iter; use embedded_graphics::{ drawable::Pixel, @@ -14,13 +14,12 @@ use embedded_graphics::{ DrawTarget, }; -impl DrawTarget for Ili9341 +impl DrawTarget for Ili9341 where IFACE: display_interface::WriteOnlyDataCommand, - RESET: OutputPin, - PinE: Debug, + RESET: OutputPin, { - type Error = crate::Error; + type Error = display_interface::DisplayError; fn size(&self) -> Size { Size::new(self.width as u32, self.height as u32) diff --git a/src/lib.rs b/src/lib.rs index beca9ed..95fefa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,8 @@ use display_interface::WriteOnlyDataCommand; pub use embedded_hal::spi::MODE_0 as SPI_MODE; +pub use display_interface::DisplayError; + /// Trait that defines display size information pub trait DisplaySize { /// Width in pixels @@ -36,12 +38,6 @@ impl DisplaySize for DisplaySize320x480 { const HEIGHT: usize = 480; } -#[derive(Debug)] -pub enum Error { - Interface, - OutputPin(PinE), -} - /// The default orientation is Portrait pub enum Orientation { Portrait, @@ -74,10 +70,10 @@ pub struct Ili9341 { mode: Orientation, } -impl Ili9341 +impl Ili9341 where IFACE: WriteOnlyDataCommand, - RESET: OutputPin, + RESET: OutputPin, { pub fn new( interface: IFACE, @@ -85,7 +81,7 @@ where delay: &mut DELAY, mode: Orientation, _display_size: SIZE, - ) -> Result> + ) -> Result where DELAY: DelayMs, SIZE: DisplaySize, @@ -99,10 +95,13 @@ where }; // Do hardware reset by holding reset low for at least 10us - ili9341.reset.set_low().map_err(Error::OutputPin)?; + ili9341.reset.set_low().map_err(|_| DisplayError::RSError)?; delay.delay_ms(1); // Set high for normal operation - ili9341.reset.set_high().map_err(Error::OutputPin)?; + ili9341 + .reset + .set_high() + .map_err(|_| DisplayError::RSError)?; // Wait 5ms after reset before sending commands // and 120ms before sending Sleep Out @@ -130,23 +129,17 @@ where Ok(ili9341) } - fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), Error> { - self.interface - .send_commands(U8Iter(&mut once(cmd as u8))) - .map_err(|_| Error::Interface)?; - self.interface - .send_data(U8Iter(&mut args.iter().cloned())) - .map_err(|_| Error::Interface) + fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), DisplayError> { + self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?; + self.interface.send_data(U8Iter(&mut args.iter().cloned())) } - fn write_iter>(&mut self, data: I) -> Result<(), Error> { + fn write_iter>(&mut self, data: I) -> Result<(), DisplayError> { self.command(Command::MemoryWrite, &[])?; - self.interface - .send_data(U16BEIter(&mut data.into_iter())) - .map_err(|_| Error::Interface) + self.interface.send_data(U16BEIter(&mut data.into_iter())) } - fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), Error> { + fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), DisplayError> { self.command( Command::ColumnAddressSet, &[ @@ -164,8 +157,7 @@ where (y1 >> 8) as u8, (y1 & 0xff) as u8, ], - )?; - Ok(()) + ) } /// Configures the screen for hardware-accelerated vertical scrolling. @@ -173,7 +165,7 @@ where &mut self, fixed_top_lines: u16, fixed_bottom_lines: u16, - ) -> Result> { + ) -> Result { let height = match self.mode { Orientation::Landscape | Orientation::LandscapeFlipped => self.width, Orientation::Portrait | Orientation::PortraitFlipped => self.height, @@ -199,7 +191,7 @@ where &mut self, scroller: &mut Scroller, num_lines: u16, - ) -> Result<(), Error> { + ) -> Result<(), DisplayError> { scroller.top_offset += num_lines; if scroller.top_offset > (scroller.height - scroller.fixed_bottom_lines) { scroller.top_offset = scroller.fixed_top_lines @@ -231,7 +223,7 @@ where x1: u16, y1: u16, data: I, - ) -> Result<(), Error> { + ) -> Result<(), DisplayError> { self.set_window(x0, y0, x1, y1)?; self.write_iter(data) } @@ -252,13 +244,13 @@ where x1: u16, y1: u16, data: &[u16], - ) -> Result<(), Error> { + ) -> Result<(), DisplayError> { self.set_window(x0, y0, x1, y1)?; self.write_iter(data.iter().cloned()) } /// Change the orientation of the screen - pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), Error> { + pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), DisplayError> { let was_landscape = match self.mode { Orientation::Landscape | Orientation::LandscapeFlipped => true, Orientation::Portrait | Orientation::PortraitFlipped => false, From be44c24eeab1bdb89c596ffda5f4cf126c3f4794 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 10 May 2021 17:15:59 -0500 Subject: [PATCH 2/3] Use Result alias --- src/lib.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 95fefa6..16a0373 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,8 @@ pub use embedded_hal::spi::MODE_0 as SPI_MODE; pub use display_interface::DisplayError; +type Result = core::result::Result; + /// Trait that defines display size information pub trait DisplaySize { /// Width in pixels @@ -81,7 +83,7 @@ where delay: &mut DELAY, mode: Orientation, _display_size: SIZE, - ) -> Result + ) -> Result where DELAY: DelayMs, SIZE: DisplaySize, @@ -129,17 +131,17 @@ where Ok(ili9341) } - fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), DisplayError> { + fn command(&mut self, cmd: Command, args: &[u8]) -> Result { self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?; self.interface.send_data(U8Iter(&mut args.iter().cloned())) } - fn write_iter>(&mut self, data: I) -> Result<(), DisplayError> { + fn write_iter>(&mut self, data: I) -> Result { self.command(Command::MemoryWrite, &[])?; self.interface.send_data(U16BEIter(&mut data.into_iter())) } - fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), DisplayError> { + fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result { self.command( Command::ColumnAddressSet, &[ @@ -165,7 +167,7 @@ where &mut self, fixed_top_lines: u16, fixed_bottom_lines: u16, - ) -> Result { + ) -> Result { let height = match self.mode { Orientation::Landscape | Orientation::LandscapeFlipped => self.width, Orientation::Portrait | Orientation::PortraitFlipped => self.height, @@ -187,11 +189,7 @@ where Ok(Scroller::new(fixed_top_lines, fixed_bottom_lines, height)) } - pub fn scroll_vertically( - &mut self, - scroller: &mut Scroller, - num_lines: u16, - ) -> Result<(), DisplayError> { + pub fn scroll_vertically(&mut self, scroller: &mut Scroller, num_lines: u16) -> Result { scroller.top_offset += num_lines; if scroller.top_offset > (scroller.height - scroller.fixed_bottom_lines) { scroller.top_offset = scroller.fixed_top_lines @@ -223,7 +221,7 @@ where x1: u16, y1: u16, data: I, - ) -> Result<(), DisplayError> { + ) -> Result { self.set_window(x0, y0, x1, y1)?; self.write_iter(data) } @@ -237,20 +235,13 @@ where /// video memory. /// /// The expected format is rgb565. - pub fn draw_raw( - &mut self, - x0: u16, - y0: u16, - x1: u16, - y1: u16, - data: &[u16], - ) -> Result<(), DisplayError> { + pub fn draw_raw(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, data: &[u16]) -> Result { self.set_window(x0, y0, x1, y1)?; self.write_iter(data.iter().cloned()) } /// Change the orientation of the screen - pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), DisplayError> { + pub fn set_orientation(&mut self, mode: Orientation) -> Result { let was_landscape = match self.mode { Orientation::Landscape | Orientation::LandscapeFlipped => true, Orientation::Portrait | Orientation::PortraitFlipped => false, From 535feb236cd8cc9b26d6692b4a05c8ccdb4b63eb Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 10 May 2021 17:25:05 -0500 Subject: [PATCH 3/3] Loosen trait bounds --- src/graphics.rs | 3 +-- src/lib.rs | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index 9af929a..5aa5b85 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -1,4 +1,4 @@ -use crate::{Ili9341, OutputPin}; +use crate::Ili9341; use core::iter; @@ -17,7 +17,6 @@ use embedded_graphics::{ impl DrawTarget for Ili9341 where IFACE: display_interface::WriteOnlyDataCommand, - RESET: OutputPin, { type Error = display_interface::DisplayError; diff --git a/src/lib.rs b/src/lib.rs index 16a0373..b54f2a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,7 +130,12 @@ where Ok(ili9341) } +} +impl Ili9341 +where + IFACE: WriteOnlyDataCommand, +{ fn command(&mut self, cmd: Command, args: &[u8]) -> Result { self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?; self.interface.send_data(U8Iter(&mut args.iter().cloned())) @@ -270,7 +275,9 @@ where self.mode = mode; Ok(()) } +} +impl Ili9341 { /// Get the current screen width. It can change based on the current orientation pub fn width(&self) -> usize { self.width