#![no_std]
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;
use core::iter::once;
use display_interface::DataFormat::{U16BEIter, U8Iter};
use display_interface::WriteOnlyDataCommand;
#[cfg(feature = "graphics")]
mod graphics_core;
pub use embedded_hal::spi::MODE_0 as SPI_MODE;
pub use display_interface::DisplayError;
type Result<T = (), E = DisplayError> = core::result::Result<T, E>;
pub trait DisplaySize {
const WIDTH: usize;
const HEIGHT: usize;
}
pub struct DisplaySize240x320;
impl DisplaySize for DisplaySize240x320 {
const WIDTH: usize = 240;
const HEIGHT: usize = 320;
}
pub struct DisplaySize320x480;
impl DisplaySize for DisplaySize320x480 {
const WIDTH: usize = 320;
const HEIGHT: usize = 480;
}
pub trait Mode {
fn mode(&self) -> u8;
fn is_landscape(&self) -> bool;
}
pub enum Orientation {
Portrait,
PortraitFlipped,
Landscape,
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,
}
}
}
pub enum ModeState {
On,
Off,
}
pub struct Ili9341<IFACE, RESET> {
interface: IFACE,
reset: RESET,
width: usize,
height: usize,
landscape: bool,
}
impl<IFACE, RESET> Ili9341<IFACE, RESET>
where
IFACE: WriteOnlyDataCommand,
RESET: OutputPin,
{
pub fn new<DELAY, SIZE, MODE>(
interface: IFACE,
reset: RESET,
delay: &mut DELAY,
mode: MODE,
_display_size: SIZE,
) -> Result<Self>
where
DELAY: DelayNs,
SIZE: DisplaySize,
MODE: Mode,
{
let mut ili9341 = Ili9341 {
interface,
reset,
width: SIZE::WIDTH,
height: SIZE::HEIGHT,
landscape: false,
};
ili9341.reset.set_low().map_err(|_| DisplayError::RSError)?;
let _ = delay.delay_ms(1);
ili9341
.reset
.set_high()
.map_err(|_| DisplayError::RSError)?;
let _ = delay.delay_ms(5);
ili9341.command(Command::SoftwareReset, &[])?;
let _ = delay.delay_ms(120);
ili9341.set_orientation(mode)?;
ili9341.command(Command::PixelFormatSet, &[0x55])?;
ili9341.sleep_mode(ModeState::Off)?;
let _ = delay.delay_ms(5);
ili9341.display_mode(ModeState::On)?;
Ok(ili9341)
}
}
impl<IFACE, RESET> Ili9341<IFACE, RESET>
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()))
}
fn write_iter<I: IntoIterator<Item = u16>>(&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 {
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,
],
)
}
pub fn configure_vertical_scroll(
&mut self,
fixed_top_lines: u16,
fixed_bottom_lines: u16,
) -> Result<Scroller> {
let height = if self.landscape {
self.width
} else {
self.height
} as u16;
let scroll_lines = height as u16 - fixed_top_lines - fixed_bottom_lines;
self.command(
Command::VerticalScrollDefine,
&[
(fixed_top_lines >> 8) as u8,
(fixed_top_lines & 0xff) as u8,
(scroll_lines >> 8) as u8,
(scroll_lines & 0xff) as u8,
(fixed_bottom_lines >> 8) as u8,
(fixed_bottom_lines & 0xff) as u8,
],
)?;
Ok(Scroller::new(fixed_top_lines, fixed_bottom_lines, height))
}
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
+ (scroller.top_offset + scroller.fixed_bottom_lines - scroller.height)
}
self.command(
Command::VerticalScrollAddr,
&[
(scroller.top_offset >> 8) as u8,
(scroller.top_offset & 0xff) as u8,
],
)
}
pub fn draw_raw_iter<I: IntoIterator<Item = u16>>(
&mut self,
x0: u16,
y0: u16,
x1: u16,
y1: u16,
data: I,
) -> Result {
self.set_window(x0, y0, x1, y1)?;
self.write_iter(data)
}
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())
}
pub fn set_orientation<MODE>(&mut self, mode: MODE) -> Result
where
MODE: Mode,
{
self.command(Command::MemoryAccessControl, &[mode.mode()])?;
if self.landscape ^ mode.is_landscape() {
core::mem::swap(&mut self.height, &mut self.width);
}
self.landscape = mode.is_landscape();
Ok(())
}
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)
}
pub fn sleep_mode(&mut self, mode: ModeState) -> Result {
match mode {
ModeState::On => self.command(Command::SleepModeOn, &[]),
ModeState::Off => self.command(Command::SleepModeOff, &[]),
}
}
pub fn display_mode(&mut self, mode: ModeState) -> Result {
match mode {
ModeState::On => self.command(Command::DisplayOn, &[]),
ModeState::Off => self.command(Command::DisplayOff, &[]),
}
}
pub fn invert_mode(&mut self, mode: ModeState) -> Result {
match mode {
ModeState::On => self.command(Command::InvertOn, &[]),
ModeState::Off => self.command(Command::InvertOff, &[]),
}
}
pub fn idle_mode(&mut self, mode: ModeState) -> Result {
match mode {
ModeState::On => self.command(Command::IdleModeOn, &[]),
ModeState::Off => self.command(Command::IdleModeOff, &[]),
}
}
pub fn brightness(&mut self, brightness: u8) -> Result {
self.command(Command::SetBrightness, &[brightness])
}
pub fn content_adaptive_brightness(&mut self, value: AdaptiveBrightness) -> Result {
self.command(Command::ContentAdaptiveBrightness, &[value as _])
}
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 _],
)
}
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> {
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
self.height
}
}
pub struct Scroller {
top_offset: u16,
fixed_bottom_lines: u16,
fixed_top_lines: u16,
height: u16,
}
impl Scroller {
fn new(fixed_top_lines: u16, fixed_bottom_lines: u16, height: u16) -> Scroller {
Scroller {
top_offset: fixed_top_lines,
fixed_top_lines,
fixed_bottom_lines,
height,
}
}
}
pub enum AdaptiveBrightness {
Off = 0x00,
UserInterfaceImage = 0x01,
StillPicture = 0x02,
MovingImage = 0x03,
}
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,
}
pub enum FrameRateClockDivision {
Fosc = 0x00,
FoscDiv2 = 0x01,
FoscDiv4 = 0x02,
FoscDiv8 = 0x03,
}
#[derive(Clone, Copy)]
enum Command {
SoftwareReset = 0x01,
MemoryAccessControl = 0x36,
PixelFormatSet = 0x3a,
SleepModeOn = 0x10,
SleepModeOff = 0x11,
InvertOff = 0x20,
InvertOn = 0x21,
DisplayOff = 0x28,
DisplayOn = 0x29,
ColumnAddressSet = 0x2a,
PageAddressSet = 0x2b,
MemoryWrite = 0x2c,
VerticalScrollDefine = 0x33,
VerticalScrollAddr = 0x37,
IdleModeOff = 0x38,
IdleModeOn = 0x39,
SetBrightness = 0x51,
ContentAdaptiveBrightness = 0x55,
NormalModeFrameRate = 0xb1,
IdleModeFrameRate = 0xb2,
}