2018-02-24 13:37:17 +00:00
|
|
|
#![no_std]
|
|
|
|
|
2018-06-19 21:29:24 +00:00
|
|
|
#[cfg(feature = "graphics")]
|
|
|
|
extern crate embedded_graphics;
|
|
|
|
|
2020-01-28 17:53:24 +00:00
|
|
|
use embedded_hal::blocking::delay::DelayMs;
|
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
2018-02-24 13:37:17 +00:00
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
use core::iter::once;
|
|
|
|
use display_interface::DataFormat::{U16BEIter, U8Iter};
|
|
|
|
use display_interface::WriteOnlyDataCommand;
|
2020-01-28 16:56:12 +00:00
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
pub mod spi;
|
2020-02-06 23:10:53 +00:00
|
|
|
|
2020-01-28 16:45:23 +00:00
|
|
|
/// Trait representing the interface to the hardware.
|
|
|
|
///
|
|
|
|
/// Intended to abstract the various buses (SPI, MPU 8/9/16-bit) from the Controller code.
|
|
|
|
pub trait Interface {
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// Sends a command with a sequence of 8-bit arguments
|
|
|
|
///
|
|
|
|
/// Mostly used for sending configuration commands
|
|
|
|
fn write(&mut self, command: u8, data: &[u8]) -> Result<(), Self::Error>;
|
|
|
|
|
|
|
|
/// Sends a command with a sequence of 16-bit data words
|
|
|
|
///
|
|
|
|
/// Mostly used for sending MemoryWrite command and other commands
|
|
|
|
/// with 16-bit arguments
|
2020-03-01 14:29:25 +00:00
|
|
|
fn write_iter(
|
|
|
|
&mut self,
|
|
|
|
command: u8,
|
|
|
|
data: impl IntoIterator<Item = u16>,
|
|
|
|
) -> Result<(), Self::Error>;
|
2020-01-28 16:45:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 16:49:06 +00:00
|
|
|
const WIDTH: usize = 240;
|
|
|
|
const HEIGHT: usize = 320;
|
2018-02-24 13:37:17 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-06-01 12:30:03 +00:00
|
|
|
pub enum Error<PinE> {
|
|
|
|
Interface,
|
2019-12-19 14:49:16 +00:00
|
|
|
OutputPin(PinE),
|
2018-02-24 13:37:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// The default orientation is Portrait
|
2018-03-04 14:43:28 +00:00
|
|
|
pub enum Orientation {
|
|
|
|
Portrait,
|
|
|
|
PortraitFlipped,
|
|
|
|
Landscape,
|
|
|
|
LandscapeFlipped,
|
|
|
|
}
|
|
|
|
|
2018-03-04 17:44:19 +00:00
|
|
|
/// There are two method for drawing to the screen:
|
|
|
|
/// [draw_raw](struct.Ili9341.html#method.draw_raw) and
|
|
|
|
/// [draw_iter](struct.Ili9341.html#method.draw_iter).
|
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// In both cases the expected pixel format is rgb565.
|
2018-03-04 17:44:19 +00:00
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// The hardware makes it efficient to draw rectangles on the screen.
|
2018-03-04 17:44:19 +00:00
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// What happens is the following:
|
2018-03-04 17:44:19 +00:00
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// - A drawing window is prepared (with the 2 opposite corner coordinates)
|
|
|
|
/// - The starting point for drawint is the top left corner of this window
|
|
|
|
/// - Every pair of bytes received is intepreted as a pixel value in rgb565
|
|
|
|
/// - As soon as a pixel is received, an internal counter is incremented,
|
|
|
|
/// and the next word will fill the next pixel (the adjacent on the right, or
|
|
|
|
/// the first of the next row if the row ended)
|
2020-01-28 17:02:35 +00:00
|
|
|
pub struct Ili9341<IFACE, RESET> {
|
|
|
|
interface: IFACE,
|
2018-02-24 13:37:17 +00:00
|
|
|
reset: RESET,
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
impl<PinE, IFACE, RESET> Ili9341<IFACE, RESET>
|
2020-03-01 14:29:25 +00:00
|
|
|
where
|
2020-06-01 12:30:03 +00:00
|
|
|
IFACE: WriteOnlyDataCommand,
|
2020-03-01 14:29:25 +00:00
|
|
|
RESET: OutputPin<Error = PinE>,
|
2020-01-28 17:02:35 +00:00
|
|
|
{
|
|
|
|
pub fn new<DELAY: DelayMs<u16>>(
|
|
|
|
interface: IFACE,
|
|
|
|
reset: RESET,
|
|
|
|
delay: &mut DELAY,
|
2020-06-01 12:30:03 +00:00
|
|
|
) -> Result<Self, Error<PinE>> {
|
2018-02-24 13:37:17 +00:00
|
|
|
let mut ili9341 = Ili9341 {
|
2020-01-28 16:56:12 +00:00
|
|
|
interface,
|
2018-02-24 13:37:17 +00:00
|
|
|
reset,
|
2018-03-04 16:49:06 +00:00
|
|
|
width: WIDTH,
|
|
|
|
height: HEIGHT,
|
2018-02-24 13:37:17 +00:00
|
|
|
};
|
|
|
|
|
2020-08-28 19:32:16 +00:00
|
|
|
// Do hardware reset by holding reset low for at least 10us
|
|
|
|
ili9341.reset.set_low().map_err(Error::OutputPin)?;
|
|
|
|
delay.delay_ms(1);
|
|
|
|
// Set high for normal operation
|
|
|
|
ili9341.reset.set_high().map_err(Error::OutputPin)?;
|
|
|
|
|
|
|
|
// Wait 5ms after reset before sending commands
|
|
|
|
// and 120ms before sending Sleep Out
|
|
|
|
delay.delay_ms(5);
|
|
|
|
|
|
|
|
// Do software reset
|
2018-02-24 13:37:17 +00:00
|
|
|
ili9341.command(Command::SoftwareReset, &[])?;
|
2020-08-28 19:32:16 +00:00
|
|
|
|
|
|
|
// Wait 5ms after reset before sending commands
|
|
|
|
// and 120ms before sending Sleep Out
|
|
|
|
delay.delay_ms(120);
|
2018-02-24 13:37:17 +00:00
|
|
|
|
|
|
|
ili9341.command(Command::MemoryAccessControl, &[0x48])?;
|
|
|
|
ili9341.command(Command::PixelFormatSet, &[0x55])?;
|
2020-08-28 19:32:16 +00:00
|
|
|
|
2018-02-24 13:37:17 +00:00
|
|
|
ili9341.command(Command::SleepOut, &[])?;
|
2020-08-28 19:32:16 +00:00
|
|
|
|
|
|
|
// Wait 5ms after Sleep Out before sending commands
|
|
|
|
delay.delay_ms(5);
|
|
|
|
|
2018-02-24 13:37:17 +00:00
|
|
|
ili9341.command(Command::DisplayOn, &[])?;
|
|
|
|
|
|
|
|
Ok(ili9341)
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), Error<PinE>> {
|
|
|
|
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)
|
2018-02-24 13:37:17 +00:00
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result<(), Error<PinE>> {
|
|
|
|
self.command(Command::MemoryWrite, &[])?;
|
|
|
|
self.interface
|
|
|
|
.send_data(U16BEIter(&mut data.into_iter()))
|
|
|
|
.map_err(|_| Error::Interface)
|
2018-02-24 13:37:17 +00:00
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2020-06-01 12:30:03 +00:00
|
|
|
fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), Error<PinE>> {
|
2018-02-24 13:37:17 +00:00
|
|
|
self.command(
|
|
|
|
Command::ColumnAddressSet,
|
|
|
|
&[
|
|
|
|
(x0 >> 8) as u8,
|
|
|
|
(x0 & 0xff) as u8,
|
|
|
|
(x1 >> 8) as u8,
|
|
|
|
(x1 & 0xff) as u8,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
self.command(
|
|
|
|
Command::PageAddressSet,
|
|
|
|
&[
|
|
|
|
(y0 >> 8) as u8,
|
|
|
|
(y0 & 0xff) as u8,
|
|
|
|
(y1 >> 8) as u8,
|
|
|
|
(y1 & 0xff) as u8,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// Draw a rectangle on the screen, represented by top-left corner (x0, y0)
|
2018-03-04 17:44:19 +00:00
|
|
|
/// and bottom-right corner (x1, y1).
|
|
|
|
///
|
|
|
|
/// The border is included.
|
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// This method accepts an iterator of rgb565 pixel values.
|
2018-03-04 17:44:19 +00:00
|
|
|
///
|
2018-03-04 17:29:04 +00:00
|
|
|
/// The iterator is useful to avoid wasting memory by holding a buffer for
|
|
|
|
/// the whole screen when it is not necessary.
|
2018-06-19 21:30:04 +00:00
|
|
|
pub fn draw_iter<I: IntoIterator<Item = u16>>(
|
|
|
|
&mut self,
|
|
|
|
x0: u16,
|
|
|
|
y0: u16,
|
|
|
|
x1: u16,
|
|
|
|
y1: u16,
|
|
|
|
data: I,
|
2020-06-01 12:30:03 +00:00
|
|
|
) -> Result<(), Error<PinE>> {
|
2018-02-24 13:37:17 +00:00
|
|
|
self.set_window(x0, y0, x1, y1)?;
|
|
|
|
self.write_iter(data)
|
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// Draw a rectangle on the screen, represented by top-left corner (x0, y0)
|
2018-03-04 17:44:19 +00:00
|
|
|
/// and bottom-right corner (x1, y1).
|
|
|
|
///
|
|
|
|
/// The border is included.
|
|
|
|
///
|
2020-01-28 17:35:30 +00:00
|
|
|
/// This method accepts a raw buffer of words that will be copied to the screen
|
2018-03-04 17:44:19 +00:00
|
|
|
/// video memory.
|
|
|
|
///
|
2020-01-28 17:35:30 +00:00
|
|
|
/// The expected format is rgb565.
|
2018-06-19 21:30:04 +00:00
|
|
|
pub fn draw_raw(
|
|
|
|
&mut self,
|
|
|
|
x0: u16,
|
|
|
|
y0: u16,
|
|
|
|
x1: u16,
|
|
|
|
y1: u16,
|
2020-01-28 17:35:30 +00:00
|
|
|
data: &[u16],
|
2020-06-01 12:30:03 +00:00
|
|
|
) -> Result<(), Error<PinE>> {
|
2018-02-24 13:37:17 +00:00
|
|
|
self.set_window(x0, y0, x1, y1)?;
|
2020-01-28 17:35:30 +00:00
|
|
|
self.write_iter(data.iter().cloned())
|
2018-02-24 13:37:17 +00:00
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// Change the orientation of the screen
|
2020-06-01 12:30:03 +00:00
|
|
|
pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), Error<PinE>> {
|
2018-03-04 14:43:28 +00:00
|
|
|
match mode {
|
|
|
|
Orientation::Portrait => {
|
2018-03-04 16:49:06 +00:00
|
|
|
self.width = WIDTH;
|
|
|
|
self.height = HEIGHT;
|
2018-06-19 21:30:04 +00:00
|
|
|
self.command(Command::MemoryAccessControl, &[0x40 | 0x08])
|
|
|
|
}
|
2018-03-04 14:43:28 +00:00
|
|
|
Orientation::Landscape => {
|
2018-03-04 16:49:06 +00:00
|
|
|
self.width = HEIGHT;
|
|
|
|
self.height = WIDTH;
|
2018-06-19 21:30:04 +00:00
|
|
|
self.command(Command::MemoryAccessControl, &[0x20 | 0x08])
|
|
|
|
}
|
2018-03-04 14:43:28 +00:00
|
|
|
Orientation::PortraitFlipped => {
|
2018-03-04 16:49:06 +00:00
|
|
|
self.width = WIDTH;
|
|
|
|
self.height = HEIGHT;
|
2018-06-19 21:30:04 +00:00
|
|
|
self.command(Command::MemoryAccessControl, &[0x80 | 0x08])
|
|
|
|
}
|
2018-03-04 14:43:28 +00:00
|
|
|
Orientation::LandscapeFlipped => {
|
2018-03-04 16:49:06 +00:00
|
|
|
self.width = HEIGHT;
|
|
|
|
self.height = WIDTH;
|
2018-06-19 21:30:04 +00:00
|
|
|
self.command(Command::MemoryAccessControl, &[0x40 | 0x80 | 0x20 | 0x08])
|
|
|
|
}
|
2018-02-24 13:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// Get the current screen width. It can change based on the current orientation
|
2018-02-24 13:37:17 +00:00
|
|
|
pub fn width(&self) -> usize {
|
|
|
|
self.width
|
|
|
|
}
|
2020-01-28 17:03:35 +00:00
|
|
|
|
2018-03-04 17:29:04 +00:00
|
|
|
/// Get the current screen heighth. It can change based on the current orientation
|
2018-02-24 13:37:17 +00:00
|
|
|
pub fn height(&self) -> usize {
|
|
|
|
self.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 18:38:22 +00:00
|
|
|
#[cfg(feature = "graphics")]
|
2020-03-01 15:44:41 +00:00
|
|
|
mod graphics;
|
2018-06-19 21:29:24 +00:00
|
|
|
|
2018-02-24 13:37:17 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum Command {
|
|
|
|
SoftwareReset = 0x01,
|
|
|
|
MemoryAccessControl = 0x36,
|
|
|
|
PixelFormatSet = 0x3a,
|
|
|
|
SleepOut = 0x11,
|
|
|
|
DisplayOn = 0x29,
|
|
|
|
ColumnAddressSet = 0x2a,
|
|
|
|
PageAddressSet = 0x2b,
|
|
|
|
MemoryWrite = 0x2c,
|
|
|
|
}
|