From 363d7a0650fc9c34bcfeb9949fef8f5171740827 Mon Sep 17 00:00:00 2001 From: Theo Hussey Date: Thu, 19 Dec 2019 00:23:12 +0000 Subject: [PATCH] changed to use digital v2 output pin, corrected embedded-graphics endianness --- src/lib.rs | 97 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c1aac72..87041a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ extern crate embedded_graphics; use hal::blocking::delay::DelayMs; use hal::blocking::spi; -use hal::digital::OutputPin; +use hal::digital::v2::OutputPin; use hal::spi::{Mode, Phase, Polarity}; use core::fmt::Debug; @@ -23,8 +23,9 @@ const WIDTH: usize = 240; const HEIGHT: usize = 320; #[derive(Debug)] -pub enum Error { - Spi(E), +pub enum Error { + Spi(SpiE), + OutputPin(PinE) } /// The default orientation is Portrait @@ -60,20 +61,21 @@ pub struct Ili9341 { height: usize, } -impl Ili9341 +impl Ili9341 where - SPI: spi::Transfer + spi::Write, - CS: OutputPin, - DC: OutputPin, - RESET: OutputPin, + SPI: spi::Transfer + spi::Write, + CS: OutputPin, + DC: OutputPin, + RESET: OutputPin, { + pub fn new>( spi: SPI, cs: CS, dc: DC, reset: RESET, delay: &mut DELAY, - ) -> Result> { + ) -> Result> { let mut ili9341 = Ili9341 { spi, cs, @@ -83,7 +85,7 @@ where height: HEIGHT, }; - ili9341.hard_reset(delay); + ili9341.hard_reset(delay)?; ili9341.command(Command::SoftwareReset, &[])?; delay.delay_ms(200); @@ -124,62 +126,63 @@ where Ok(ili9341) } - fn hard_reset>(&mut self, delay: &mut DELAY) { + fn hard_reset>(&mut self, delay: &mut DELAY) -> Result<(), Error> { // set high if previously low - self.reset.set_high(); + self.reset.set_high().map_err(Error::OutputPin)?; delay.delay_ms(200); // set low for reset - self.reset.set_low(); + self.reset.set_low().map_err(Error::OutputPin)?; delay.delay_ms(200); // set high for normal operation - self.reset.set_high(); + self.reset.set_high().map_err(Error::OutputPin)?; delay.delay_ms(200); - } - fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), Error> { - self.cs.set_low(); - - self.dc.set_low(); - self.spi.write(&[cmd as u8]).map_err(Error::Spi)?; - - self.dc.set_high(); - self.spi.write(args).map_err(Error::Spi)?; - - self.cs.set_high(); Ok(()) } - fn write_iter>(&mut self, data: I) -> Result<(), Error> { - self.cs.set_low(); + fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), Error> { + self.cs.set_low().map_err(Error::OutputPin)?; - self.dc.set_low(); + self.dc.set_low().map_err(Error::OutputPin)?; + self.spi.write(&[cmd as u8]).map_err(Error::Spi)?; + + self.dc.set_high().map_err(Error::OutputPin)?; + self.spi.write(args).map_err(Error::Spi)?; + + self.cs.set_high().map_err(Error::OutputPin)?; + Ok(()) + } + fn write_iter>(&mut self, data: I) -> Result<(), Error> { + self.cs.set_low().map_err(Error::OutputPin)?; + + self.dc.set_low().map_err(Error::OutputPin)?; self.spi .write(&[Command::MemoryWrite as u8]) .map_err(Error::Spi)?; - self.dc.set_high(); + self.dc.set_high().map_err(Error::OutputPin)?; for d in data.into_iter() { self.spi .write(&[(d >> 8) as u8, (d & 0xff) as u8]) .map_err(Error::Spi)?; } - self.cs.set_high(); + self.cs.set_high().map_err(Error::OutputPin)?; Ok(()) } - fn write_raw(&mut self, data: &[u8]) -> Result<(), Error> { - self.cs.set_low(); + fn write_raw(&mut self, data: &[u8]) -> Result<(), Error> { + self.cs.set_low().map_err(Error::OutputPin)?; - self.dc.set_low(); + self.dc.set_low().map_err(Error::OutputPin)?; self.spi .write(&[Command::MemoryWrite as u8]) .map_err(Error::Spi)?; - self.dc.set_high(); + self.dc.set_high().map_err(Error::OutputPin)?; self.spi.write(data).map_err(Error::Spi)?; - self.cs.set_high(); + self.cs.set_high().map_err(Error::OutputPin)?; Ok(()) } - 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<(), Error> { self.command( Command::ColumnAddressSet, &[ @@ -216,7 +219,7 @@ where x1: u16, y1: u16, data: I, - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.set_window(x0, y0, x1, y1)?; self.write_iter(data) } @@ -237,12 +240,12 @@ where x1: u16, y1: u16, data: &[u8], - ) -> Result<(), Error> { + ) -> Result<(), Error> { self.set_window(x0, y0, x1, y1)?; self.write_raw(data) } /// 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<(), Error> { match mode { Orientation::Portrait => { self.width = WIDTH; @@ -282,14 +285,16 @@ use embedded_graphics::drawable; use embedded_graphics::{drawable::Pixel, pixelcolor::Rgb565, Drawing}; #[cfg(feature = "graphics")] -impl Drawing for Ili9341 +impl Drawing for Ili9341 where - SPI: spi::Transfer + spi::Write, - CS: OutputPin, - DC: OutputPin, - RESET: OutputPin, - E: Debug, + SPI: spi::Transfer + spi::Write, + CS: OutputPin, + DC: OutputPin, + RESET: OutputPin, + SpiE: Debug, + PinE: Debug, { + fn draw(&mut self, item_pixels: T) where T: IntoIterator>, @@ -304,7 +309,7 @@ where pos.y as u16, &embedded_graphics::pixelcolor::raw::RawU16::from(color) .into_inner() - .to_le_bytes(), + .to_be_bytes(), ) .expect("Failed to communicate with device"); }