mirror of
https://github.com/yuri91/ili9341-rs.git
synced 2024-06-16 12:10:33 +00:00
Compare commits
23 commits
Author | SHA1 | Date | |
---|---|---|---|
7fa69abd72 | |||
693f0bdd8f | |||
7318f971cf | |||
ac73c3ba40 | |||
aa0003d0a4 | |||
32ca78087c | |||
0cdd4e7c2e | |||
df5ea50076 | |||
00a50a13cc | |||
99f6e63918 | |||
a5e7f6498a | |||
bfb77647d5 | |||
1f47f5797e | |||
d896467aec | |||
6e8c814156 | |||
d933649c19 | |||
d4e883ce8d | |||
6e1fab6da7 | |||
b0f3b14664 | |||
8aeefc4abc | |||
11661b20b6 | |||
c3d0d08a3c | |||
8a4aee95c2 |
22
Cargo.toml
22
Cargo.toml
|
@ -1,6 +1,8 @@
|
||||||
|
# Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "ili9341"
|
name = "ili9341"
|
||||||
version = "0.5.0"
|
version = "0.6.0"
|
||||||
description = "A platform agnostic driver to interface with the ILI9341 (ald ILI9340C) TFT LCD display"
|
description = "A platform agnostic driver to interface with the ILI9341 (ald ILI9340C) TFT LCD display"
|
||||||
authors = ["Yuri Iozzelli <y.iozzelli@gmail.com>"]
|
authors = ["Yuri Iozzelli <y.iozzelli@gmail.com>"]
|
||||||
categories = ["embedded", "hardware-support", "no-std"]
|
categories = ["embedded", "hardware-support", "no-std"]
|
||||||
|
@ -11,15 +13,27 @@ edition = "2018"
|
||||||
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
display-interface = "0.4.1"
|
display-interface = "0.5"
|
||||||
embedded-hal = "0.2.5"
|
embedded-hal = "1.0.0"
|
||||||
|
|
||||||
[dependencies.embedded-graphics-core]
|
[dependencies.embedded-graphics-core]
|
||||||
optional = true
|
optional = true
|
||||||
version = "0.3"
|
version = "0.4"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
cortex-m-rtic = "1.0.0"
|
||||||
|
cortex-m = "0.7.3"
|
||||||
|
cortex-m-rt = "0.7.0"
|
||||||
|
defmt-rtt = "0.3.0"
|
||||||
|
panic-semihosting = "0.6"
|
||||||
|
|
||||||
|
[dev-dependencies.stm32f4xx-hal]
|
||||||
|
version = "0.12.0"
|
||||||
|
features = ["stm32f411"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["graphics"]
|
default = ["graphics"]
|
||||||
graphics = ["embedded-graphics-core"]
|
graphics = ["embedded-graphics-core"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "rtic"
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
# `ili9341`
|
# `ili9341`
|
||||||
|
|
||||||
|
[![Crates.io](https://img.shields.io/crates/d/ili9341.svg)](https://crates.io/crates/ili9341)
|
||||||
|
[![Crates.io](https://img.shields.io/crates/v/ili9341.svg)](https://crates.io/crates/ili9341)
|
||||||
|
[![Released API docs](https://docs.rs/ili9341/badge.svg)](https://docs.rs/ili9341)
|
||||||
|
|
||||||
> A platform agnostic driver to interface with the ILI9341 (and ILI9340C) TFT
|
> A platform agnostic driver to interface with the ILI9341 (and ILI9340C) TFT
|
||||||
> LCD display
|
> LCD display
|
||||||
|
|
||||||
|
|
109
examples/rtic.rs
Normal file
109
examples/rtic.rs
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
//! cortex-m-rtic example
|
||||||
|
//! Tested on BlackPill dev board with stm32f411ceu microcontroller
|
||||||
|
//! The LCD RESET pin was hard puled to Vcc therefore
|
||||||
|
//! DummyOutputPin was used as the reset pin
|
||||||
|
|
||||||
|
#![no_main]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
#[rtic::app(device = stm32f4xx_hal::pac)]
|
||||||
|
mod app {
|
||||||
|
use display_interface_spi::SPIInterface;
|
||||||
|
use embedded_graphics::{
|
||||||
|
mono_font::{ascii::FONT_6X10, MonoTextStyle},
|
||||||
|
pixelcolor::Rgb565,
|
||||||
|
prelude::*,
|
||||||
|
text::{Alignment, Text},
|
||||||
|
};
|
||||||
|
use embedded_hal::digital::{blocking::OutputPin, ErrorType, PinState};
|
||||||
|
use ili9341::{DisplaySize240x320, Ili9341, Orientation};
|
||||||
|
use stm32f4xx_hal::{
|
||||||
|
prelude::*,
|
||||||
|
spi::{Mode, NoMiso, Phase, Polarity},
|
||||||
|
timer::Channel,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct DummyOutputPin;
|
||||||
|
impl ErrorType for DummyOutputPin {
|
||||||
|
type Error = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputPin for DummyOutputPin {
|
||||||
|
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn set_state(&mut self, _state: PinState) -> Result<(), Self::Error> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[shared]
|
||||||
|
struct Shared {}
|
||||||
|
|
||||||
|
#[local]
|
||||||
|
struct Local {}
|
||||||
|
|
||||||
|
#[init]
|
||||||
|
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
|
||||||
|
let dp = ctx.device;
|
||||||
|
|
||||||
|
let rcc = dp.RCC.constrain();
|
||||||
|
let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(100.MHz()).freeze();
|
||||||
|
|
||||||
|
let gpioa = dp.GPIOA.split();
|
||||||
|
let gpiob = dp.GPIOB.split();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The ILI9341 driver
|
||||||
|
*/
|
||||||
|
let lcd_clk = gpiob.pb0.into_alternate();
|
||||||
|
let lcd_miso = NoMiso {};
|
||||||
|
let lcd_mosi = gpioa.pa10.into_alternate().internal_pull_up(true);
|
||||||
|
let lcd_dc = gpiob.pb1.into_push_pull_output();
|
||||||
|
let lcd_cs = gpiob.pb2.into_push_pull_output();
|
||||||
|
let mode = Mode {
|
||||||
|
polarity: Polarity::IdleLow,
|
||||||
|
phase: Phase::CaptureOnFirstTransition,
|
||||||
|
};
|
||||||
|
let lcd_spi = dp
|
||||||
|
.SPI5
|
||||||
|
.spi((lcd_clk, lcd_miso, lcd_mosi), mode, 2.MHz(), &clocks);
|
||||||
|
let spi_iface = SPIInterface::new(lcd_spi, lcd_dc, lcd_cs);
|
||||||
|
let dummy_reset = DummyOutputPin::default();
|
||||||
|
let mut delay = dp.TIM1.delay_us(&clocks);
|
||||||
|
let mut lcd = Ili9341::new(
|
||||||
|
spi_iface,
|
||||||
|
dummy_reset,
|
||||||
|
&mut delay,
|
||||||
|
Orientation::PortraitFlipped,
|
||||||
|
DisplaySize240x320,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Create a new character style
|
||||||
|
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::RED);
|
||||||
|
|
||||||
|
// Create a text at position (20, 30) and draw it using the previously defined style
|
||||||
|
Text::with_alignment(
|
||||||
|
"First line\nSecond line",
|
||||||
|
Point::new(20, 30),
|
||||||
|
style,
|
||||||
|
Alignment::Center,
|
||||||
|
)
|
||||||
|
.draw(&mut lcd)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
(Shared {}, Local {}, init::Monotonics())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[idle(local = [])]
|
||||||
|
fn idle(cx: idle::Context) -> ! {
|
||||||
|
loop {
|
||||||
|
cortex_m::asm::nop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::Ili9341;
|
use crate::Ili9341;
|
||||||
|
|
||||||
use embedded_graphics_core::{
|
use embedded_graphics_core::{
|
||||||
pixelcolor::{raw::RawU16, Rgb565},
|
pixelcolor::{raw::RawU16, Rgb565},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
@ -28,14 +27,8 @@ where
|
||||||
if self.bounding_box().contains(point) {
|
if self.bounding_box().contains(point) {
|
||||||
let x = point.x as u16;
|
let x = point.x as u16;
|
||||||
let y = point.y as u16;
|
let y = point.y as u16;
|
||||||
|
let color = RawU16::from(color).into_inner();
|
||||||
self.draw_raw_iter(
|
self.draw_raw_slice(x, y, x, y, &[color])?;
|
||||||
x,
|
|
||||||
y,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
core::iter::once(RawU16::from(color).into_inner()),
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -82,4 +75,8 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
|
||||||
|
self.clear_screen(RawU16::from(color).into_inner())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
263
src/lib.rs
263
src/lib.rs
|
@ -1,10 +1,34 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
use embedded_hal::blocking::delay::DelayMs;
|
//! ILI9341 Display Driver
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
//!
|
||||||
|
//! ### Usage
|
||||||
|
//!
|
||||||
|
//! To control the display you need to set up:
|
||||||
|
//!
|
||||||
|
//! * Interface for communicating with display ([display-interface-spi crate] for SPI)
|
||||||
|
//! * Configuration (reset pin, delay, orientation and size) for display
|
||||||
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! let iface = SPIInterface::new(spi, dc, cs);
|
||||||
|
//!
|
||||||
|
//! let mut display = Ili9341::new(
|
||||||
|
//! iface,
|
||||||
|
//! reset_gpio,
|
||||||
|
//! &mut delay,
|
||||||
|
//! Orientation::Landscape,
|
||||||
|
//! ili9341::DisplaySize240x320,
|
||||||
|
//! )
|
||||||
|
//! .unwrap();
|
||||||
|
//!
|
||||||
|
//! display.clear(Rgb565::RED).unwrap()
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! [display-interface-spi crate]: https://crates.io/crates/display-interface-spi
|
||||||
|
use embedded_hal::delay::DelayNs;
|
||||||
|
use embedded_hal::digital::OutputPin;
|
||||||
|
|
||||||
use core::iter::once;
|
use display_interface::DataFormat;
|
||||||
use display_interface::DataFormat::{U16BEIter, U8Iter};
|
|
||||||
use display_interface::WriteOnlyDataCommand;
|
use display_interface::WriteOnlyDataCommand;
|
||||||
|
|
||||||
#[cfg(feature = "graphics")]
|
#[cfg(feature = "graphics")]
|
||||||
|
@ -40,7 +64,19 @@ impl DisplaySize for DisplaySize320x480 {
|
||||||
const HEIGHT: usize = 480;
|
const HEIGHT: usize = 480;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The default orientation is Portrait
|
/// For quite a few boards (ESP32-S2-Kaluga-1, M5Stack, M5Core2 and others),
|
||||||
|
/// the ILI9341 initialization command arguments are slightly different
|
||||||
|
///
|
||||||
|
/// This trait provides the flexibility for users to define their own
|
||||||
|
/// initialization command arguments suitable for the particular board they are using
|
||||||
|
pub trait Mode {
|
||||||
|
fn mode(&self) -> u8;
|
||||||
|
|
||||||
|
fn is_landscape(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The default implementation of the Mode trait from above
|
||||||
|
/// Should work for most (but not all) boards
|
||||||
pub enum Orientation {
|
pub enum Orientation {
|
||||||
Portrait,
|
Portrait,
|
||||||
PortraitFlipped,
|
PortraitFlipped,
|
||||||
|
@ -48,6 +84,30 @@ pub enum Orientation {
|
||||||
LandscapeFlipped,
|
LandscapeFlipped,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mode for Orientation {
|
||||||
|
fn mode(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
Self::Portrait => 0x40 | 0x08,
|
||||||
|
Self::Landscape => 0x20 | 0x08,
|
||||||
|
Self::PortraitFlipped => 0x80 | 0x08,
|
||||||
|
Self::LandscapeFlipped => 0x40 | 0x80 | 0x20 | 0x08,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_landscape(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Landscape | Self::LandscapeFlipped => true,
|
||||||
|
Self::Portrait | Self::PortraitFlipped => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Specify state of specific mode of operation
|
||||||
|
pub enum ModeState {
|
||||||
|
On,
|
||||||
|
Off,
|
||||||
|
}
|
||||||
|
|
||||||
/// There are two method for drawing to the screen:
|
/// There are two method for drawing to the screen:
|
||||||
/// [Ili9341::draw_raw_iter] and [Ili9341::draw_raw_slice]
|
/// [Ili9341::draw_raw_iter] and [Ili9341::draw_raw_slice]
|
||||||
///
|
///
|
||||||
|
@ -68,7 +128,7 @@ pub struct Ili9341<IFACE, RESET> {
|
||||||
reset: RESET,
|
reset: RESET,
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
mode: Orientation,
|
landscape: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<IFACE, RESET> Ili9341<IFACE, RESET>
|
impl<IFACE, RESET> Ili9341<IFACE, RESET>
|
||||||
|
@ -76,28 +136,29 @@ where
|
||||||
IFACE: WriteOnlyDataCommand,
|
IFACE: WriteOnlyDataCommand,
|
||||||
RESET: OutputPin,
|
RESET: OutputPin,
|
||||||
{
|
{
|
||||||
pub fn new<DELAY, SIZE>(
|
pub fn new<DELAY, SIZE, MODE>(
|
||||||
interface: IFACE,
|
interface: IFACE,
|
||||||
reset: RESET,
|
reset: RESET,
|
||||||
delay: &mut DELAY,
|
delay: &mut DELAY,
|
||||||
mode: Orientation,
|
mode: MODE,
|
||||||
_display_size: SIZE,
|
_display_size: SIZE,
|
||||||
) -> Result<Self>
|
) -> Result<Self>
|
||||||
where
|
where
|
||||||
DELAY: DelayMs<u16>,
|
DELAY: DelayNs,
|
||||||
SIZE: DisplaySize,
|
SIZE: DisplaySize,
|
||||||
|
MODE: Mode,
|
||||||
{
|
{
|
||||||
let mut ili9341 = Ili9341 {
|
let mut ili9341 = Ili9341 {
|
||||||
interface,
|
interface,
|
||||||
reset,
|
reset,
|
||||||
width: SIZE::WIDTH,
|
width: SIZE::WIDTH,
|
||||||
height: SIZE::HEIGHT,
|
height: SIZE::HEIGHT,
|
||||||
mode: Orientation::Portrait,
|
landscape: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Do hardware reset by holding reset low for at least 10us
|
// Do hardware reset by holding reset low for at least 10us
|
||||||
ili9341.reset.set_low().map_err(|_| DisplayError::RSError)?;
|
ili9341.reset.set_low().map_err(|_| DisplayError::RSError)?;
|
||||||
delay.delay_ms(1);
|
let _ = delay.delay_ms(1);
|
||||||
// Set high for normal operation
|
// Set high for normal operation
|
||||||
ili9341
|
ili9341
|
||||||
.reset
|
.reset
|
||||||
|
@ -106,26 +167,26 @@ where
|
||||||
|
|
||||||
// Wait 5ms after reset before sending commands
|
// Wait 5ms after reset before sending commands
|
||||||
// and 120ms before sending Sleep Out
|
// and 120ms before sending Sleep Out
|
||||||
delay.delay_ms(5);
|
let _ = delay.delay_ms(5);
|
||||||
|
|
||||||
// Do software reset
|
// Do software reset
|
||||||
ili9341.command(Command::SoftwareReset, &[])?;
|
ili9341.command(Command::SoftwareReset, &[])?;
|
||||||
|
|
||||||
// Wait 5ms after reset before sending commands
|
// Wait 5ms after reset before sending commands
|
||||||
// and 120ms before sending Sleep Out
|
// and 120ms before sending Sleep Out
|
||||||
delay.delay_ms(120);
|
let _ = delay.delay_ms(120);
|
||||||
|
|
||||||
ili9341.set_orientation(mode)?;
|
ili9341.set_orientation(mode)?;
|
||||||
|
|
||||||
// Set pixel format to 16 bits per pixel
|
// Set pixel format to 16 bits per pixel
|
||||||
ili9341.command(Command::PixelFormatSet, &[0x55])?;
|
ili9341.command(Command::PixelFormatSet, &[0x55])?;
|
||||||
|
|
||||||
ili9341.command(Command::SleepOut, &[])?;
|
ili9341.sleep_mode(ModeState::Off)?;
|
||||||
|
|
||||||
// Wait 5ms after Sleep Out before sending commands
|
// Wait 5ms after Sleep Out before sending commands
|
||||||
delay.delay_ms(5);
|
let _ = delay.delay_ms(5);
|
||||||
|
|
||||||
ili9341.command(Command::DisplayOn, &[])?;
|
ili9341.display_mode(ModeState::On)?;
|
||||||
|
|
||||||
Ok(ili9341)
|
Ok(ili9341)
|
||||||
}
|
}
|
||||||
|
@ -136,15 +197,21 @@ where
|
||||||
IFACE: WriteOnlyDataCommand,
|
IFACE: WriteOnlyDataCommand,
|
||||||
{
|
{
|
||||||
fn command(&mut self, cmd: Command, args: &[u8]) -> Result {
|
fn command(&mut self, cmd: Command, args: &[u8]) -> Result {
|
||||||
self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?;
|
self.interface.send_commands(DataFormat::U8(&[cmd as u8]))?;
|
||||||
self.interface.send_data(U8Iter(&mut args.iter().cloned()))
|
self.interface.send_data(DataFormat::U8(args))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result {
|
fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result {
|
||||||
self.command(Command::MemoryWrite, &[])?;
|
self.command(Command::MemoryWrite, &[])?;
|
||||||
|
use DataFormat::U16BEIter;
|
||||||
self.interface.send_data(U16BEIter(&mut data.into_iter()))
|
self.interface.send_data(U16BEIter(&mut data.into_iter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_slice(&mut self, data: &[u16]) -> Result {
|
||||||
|
self.command(Command::MemoryWrite, &[])?;
|
||||||
|
self.interface.send_data(DataFormat::U16(data))
|
||||||
|
}
|
||||||
|
|
||||||
fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result {
|
fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result {
|
||||||
self.command(
|
self.command(
|
||||||
Command::ColumnAddressSet,
|
Command::ColumnAddressSet,
|
||||||
|
@ -172,9 +239,10 @@ where
|
||||||
fixed_top_lines: u16,
|
fixed_top_lines: u16,
|
||||||
fixed_bottom_lines: u16,
|
fixed_bottom_lines: u16,
|
||||||
) -> Result<Scroller> {
|
) -> Result<Scroller> {
|
||||||
let height = match self.mode {
|
let height = if self.landscape {
|
||||||
Orientation::Landscape | Orientation::LandscapeFlipped => self.width,
|
self.width
|
||||||
Orientation::Portrait | Orientation::PortraitFlipped => self.height,
|
} else {
|
||||||
|
self.height
|
||||||
} as u16;
|
} as u16;
|
||||||
let scroll_lines = height as u16 - fixed_top_lines - fixed_bottom_lines;
|
let scroll_lines = height as u16 - fixed_top_lines - fixed_bottom_lines;
|
||||||
|
|
||||||
|
@ -240,39 +308,92 @@ where
|
||||||
///
|
///
|
||||||
/// The expected format is rgb565.
|
/// The expected format is rgb565.
|
||||||
pub fn draw_raw_slice(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, data: &[u16]) -> Result {
|
pub fn draw_raw_slice(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, data: &[u16]) -> Result {
|
||||||
self.draw_raw_iter(x0, y0, x1, y1, data.iter().copied())
|
self.set_window(x0, y0, x1, y1)?;
|
||||||
|
self.write_slice(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change the orientation of the screen
|
/// Change the orientation of the screen
|
||||||
pub fn set_orientation(&mut self, mode: Orientation) -> Result {
|
pub fn set_orientation<MODE>(&mut self, mode: MODE) -> Result
|
||||||
let was_landscape = match self.mode {
|
where
|
||||||
Orientation::Landscape | Orientation::LandscapeFlipped => true,
|
MODE: Mode,
|
||||||
Orientation::Portrait | Orientation::PortraitFlipped => false,
|
{
|
||||||
};
|
self.command(Command::MemoryAccessControl, &[mode.mode()])?;
|
||||||
let is_landscape = match mode {
|
|
||||||
Orientation::Portrait => {
|
if self.landscape ^ mode.is_landscape() {
|
||||||
self.command(Command::MemoryAccessControl, &[0x40 | 0x08])?;
|
|
||||||
false
|
|
||||||
}
|
|
||||||
Orientation::Landscape => {
|
|
||||||
self.command(Command::MemoryAccessControl, &[0x20 | 0x08])?;
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Orientation::PortraitFlipped => {
|
|
||||||
self.command(Command::MemoryAccessControl, &[0x80 | 0x08])?;
|
|
||||||
false
|
|
||||||
}
|
|
||||||
Orientation::LandscapeFlipped => {
|
|
||||||
self.command(Command::MemoryAccessControl, &[0x40 | 0x80 | 0x20 | 0x08])?;
|
|
||||||
true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if was_landscape ^ is_landscape {
|
|
||||||
core::mem::swap(&mut self.height, &mut self.width);
|
core::mem::swap(&mut self.height, &mut self.width);
|
||||||
}
|
}
|
||||||
self.mode = mode;
|
self.landscape = mode.is_landscape();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fill entire screen with specfied color u16 value
|
||||||
|
pub fn clear_screen(&mut self, color: u16) -> Result {
|
||||||
|
let color = core::iter::repeat(color).take(self.width * self.height);
|
||||||
|
self.draw_raw_iter(0, 0, self.width as u16, self.height as u16, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Control the screen sleep mode:
|
||||||
|
pub fn sleep_mode(&mut self, mode: ModeState) -> Result {
|
||||||
|
match mode {
|
||||||
|
ModeState::On => self.command(Command::SleepModeOn, &[]),
|
||||||
|
ModeState::Off => self.command(Command::SleepModeOff, &[]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Control the screen display mode
|
||||||
|
pub fn display_mode(&mut self, mode: ModeState) -> Result {
|
||||||
|
match mode {
|
||||||
|
ModeState::On => self.command(Command::DisplayOn, &[]),
|
||||||
|
ModeState::Off => self.command(Command::DisplayOff, &[]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invert the pixel color on screen
|
||||||
|
pub fn invert_mode(&mut self, mode: ModeState) -> Result {
|
||||||
|
match mode {
|
||||||
|
ModeState::On => self.command(Command::InvertOn, &[]),
|
||||||
|
ModeState::Off => self.command(Command::InvertOff, &[]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Idle mode reduces the number of colors to 8
|
||||||
|
pub fn idle_mode(&mut self, mode: ModeState) -> Result {
|
||||||
|
match mode {
|
||||||
|
ModeState::On => self.command(Command::IdleModeOn, &[]),
|
||||||
|
ModeState::Off => self.command(Command::IdleModeOff, &[]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set display brightness to the value between 0 and 255
|
||||||
|
pub fn brightness(&mut self, brightness: u8) -> Result {
|
||||||
|
self.command(Command::SetBrightness, &[brightness])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set adaptive brightness value equal to [AdaptiveBrightness]
|
||||||
|
pub fn content_adaptive_brightness(&mut self, value: AdaptiveBrightness) -> Result {
|
||||||
|
self.command(Command::ContentAdaptiveBrightness, &[value as _])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configure [FrameRateClockDivision] and [FrameRate] in normal mode
|
||||||
|
pub fn normal_mode_frame_rate(
|
||||||
|
&mut self,
|
||||||
|
clk_div: FrameRateClockDivision,
|
||||||
|
frame_rate: FrameRate,
|
||||||
|
) -> Result {
|
||||||
|
self.command(
|
||||||
|
Command::NormalModeFrameRate,
|
||||||
|
&[clk_div as _, frame_rate as _],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configure [FrameRateClockDivision] and [FrameRate] in idle mode
|
||||||
|
pub fn idle_mode_frame_rate(
|
||||||
|
&mut self,
|
||||||
|
clk_div: FrameRateClockDivision,
|
||||||
|
frame_rate: FrameRate,
|
||||||
|
) -> Result {
|
||||||
|
self.command(Command::IdleModeFrameRate, &[clk_div as _, frame_rate as _])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<IFACE, RESET> Ili9341<IFACE, RESET> {
|
impl<IFACE, RESET> Ili9341<IFACE, RESET> {
|
||||||
|
@ -307,16 +428,62 @@ impl Scroller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Available Adaptive Brightness values
|
||||||
|
pub enum AdaptiveBrightness {
|
||||||
|
Off = 0x00,
|
||||||
|
UserInterfaceImage = 0x01,
|
||||||
|
StillPicture = 0x02,
|
||||||
|
MovingImage = 0x03,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Available frame rate in Hz
|
||||||
|
pub enum FrameRate {
|
||||||
|
FrameRate119 = 0x10,
|
||||||
|
FrameRate112 = 0x11,
|
||||||
|
FrameRate106 = 0x12,
|
||||||
|
FrameRate100 = 0x13,
|
||||||
|
FrameRate95 = 0x14,
|
||||||
|
FrameRate90 = 0x15,
|
||||||
|
FrameRate86 = 0x16,
|
||||||
|
FrameRate83 = 0x17,
|
||||||
|
FrameRate79 = 0x18,
|
||||||
|
FrameRate76 = 0x19,
|
||||||
|
FrameRate73 = 0x1a,
|
||||||
|
FrameRate70 = 0x1b,
|
||||||
|
FrameRate68 = 0x1c,
|
||||||
|
FrameRate65 = 0x1d,
|
||||||
|
FrameRate63 = 0x1e,
|
||||||
|
FrameRate61 = 0x1f,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Frame rate clock division
|
||||||
|
pub enum FrameRateClockDivision {
|
||||||
|
Fosc = 0x00,
|
||||||
|
FoscDiv2 = 0x01,
|
||||||
|
FoscDiv4 = 0x02,
|
||||||
|
FoscDiv8 = 0x03,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Command {
|
enum Command {
|
||||||
SoftwareReset = 0x01,
|
SoftwareReset = 0x01,
|
||||||
MemoryAccessControl = 0x36,
|
MemoryAccessControl = 0x36,
|
||||||
PixelFormatSet = 0x3a,
|
PixelFormatSet = 0x3a,
|
||||||
SleepOut = 0x11,
|
SleepModeOn = 0x10,
|
||||||
|
SleepModeOff = 0x11,
|
||||||
|
InvertOff = 0x20,
|
||||||
|
InvertOn = 0x21,
|
||||||
|
DisplayOff = 0x28,
|
||||||
DisplayOn = 0x29,
|
DisplayOn = 0x29,
|
||||||
ColumnAddressSet = 0x2a,
|
ColumnAddressSet = 0x2a,
|
||||||
PageAddressSet = 0x2b,
|
PageAddressSet = 0x2b,
|
||||||
MemoryWrite = 0x2c,
|
MemoryWrite = 0x2c,
|
||||||
VerticalScrollDefine = 0x33,
|
VerticalScrollDefine = 0x33,
|
||||||
VerticalScrollAddr = 0x37,
|
VerticalScrollAddr = 0x37,
|
||||||
|
IdleModeOff = 0x38,
|
||||||
|
IdleModeOn = 0x39,
|
||||||
|
SetBrightness = 0x51,
|
||||||
|
ContentAdaptiveBrightness = 0x55,
|
||||||
|
NormalModeFrameRate = 0xb1,
|
||||||
|
IdleModeFrameRate = 0xb2,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue