#![doc = document_features::document_features!()]
#![no_std]
#[cfg(feature = "lilygo_t4_s3")]
pub mod displays;
#[cfg(feature = "lilygo_t4_s3")]
pub use displays::lilygo_t4_s3::*;
extern crate alloc;
mod graphics_core;
use alloc::boxed::Box;
use embedded_graphics::prelude::PixelColor;
use embedded_graphics_core::draw_target::DrawTarget;
use embedded_hal::delay::DelayNs;
#[derive(Debug, Clone, Copy)]
pub struct DisplaySize {
pub width: u16,
pub height: u16,
}
impl DisplaySize {
pub const fn new(width: u16, height: u16) -> Self {
DisplaySize { width, height }
}
}
#[derive(Debug)]
pub enum DriverError<InterfaceError, ResetError> {
InterfaceError(InterfaceError),
ResetError(ResetError),
InvalidConfiguration(&'static str),
}
pub trait ControllerInterface {
type Error;
fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error>;
fn send_command_with_data(&mut self, cmd: u8, data: &[u8]) -> Result<(), Self::Error>;
fn send_pixels(&mut self, pixels: &[u8]) -> Result<(), Self::Error>;
fn send_pixels_start(&mut self, pixels: &[u8]) -> Result<(), Self::Error> {
self.send_pixels(pixels)
}
fn send_pixels_continue(&mut self, pixels: &[u8]) -> Result<(), Self::Error> {
self.send_pixels(pixels)
}
}
pub trait ResetInterface {
type Error;
fn reset(&mut self) -> Result<(), Self::Error>;
}
pub mod commands {
pub const NOP: u8 = 0x00;
pub const SWRESET: u8 = 0x01;
pub const RDDID: u8 = 0x04; pub const RDNUMED: u8 = 0x05; pub const RDDPM: u8 = 0x0A; pub const RDDMADCTR: u8 = 0x0B; pub const RDDCOLMOD: u8 = 0x0C; pub const RDDIM: u8 = 0x0D; pub const RDDSM: u8 = 0x0E; pub const RDDSDR: u8 = 0x0F; pub const SLPIN: u8 = 0x10;
pub const SLPOUT: u8 = 0x11;
pub const PTLON: u8 = 0x12; pub const NORON: u8 = 0x13; pub const INVOFF: u8 = 0x20; pub const INVON: u8 = 0x21; pub const ALLPOFF: u8 = 0x22; pub const ALLPON: u8 = 0x23; pub const DISPOFF: u8 = 0x28;
pub const DISPON: u8 = 0x29;
pub const CASET: u8 = 0x2A; pub const RASET: u8 = 0x2B; pub const RAMWR: u8 = 0x2C; pub const PTLAR: u8 = 0x30; pub const TEOFF: u8 = 0x34; pub const TEON: u8 = 0x35; pub const MADCTR: u8 = 0x36; pub const IDMOFF: u8 = 0x38; pub const IDMON: u8 = 0x39; pub const COLMOD: u8 = 0x3A; pub const RAMWRC: u8 = 0x3C; pub const STESL: u8 = 0x44; pub const GSL: u8 = 0x45; pub const DSTBON: u8 = 0x4F; pub const WRDISBV: u8 = 0x51; pub const RDDISBV: u8 = 0x52; pub const WRCTRLD: u8 = 0x53; pub const RDCTRLD: u8 = 0x54; pub const WRRADACL: u8 = 0x55; pub const COLORTEMP: u8 = 0x55; pub const WRHBM: u8 = 0x63; pub const RDHBM: u8 = 0x64; pub const HBM_MODE: u8 = 0x66; pub const FR_LEVEL: u8 = 0x67; pub const COLSET: u8 = 0x70; pub const COLOPT: u8 = 0x80; pub const RDDDBS: u8 = 0xA1; pub const RDDDBC: u8 = 0xA8; pub const RDFCS: u8 = 0xAA; pub const RDCCS: u8 = 0xAF; }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorMode {
Rgb565,
Rgb888,
Rgb666,
Gray8,
}
impl ColorMode {
pub const fn bytes_per_pixel(&self) -> usize {
match self {
ColorMode::Rgb565 => 2,
ColorMode::Rgb888 => 3,
ColorMode::Rgb666 => 3,
ColorMode::Gray8 => 1,
}
}
}
pub const fn framebuffer_size(display: DisplaySize, color: ColorMode) -> usize {
(display.width as usize) * (display.height as usize) * color.bytes_per_pixel()
}
pub enum Framebuffer {
Static(&'static mut [u8]),
Heap(Box<[u8]>),
}
impl Framebuffer {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
match self {
Framebuffer::Static(ref mut arr) => arr,
Framebuffer::Heap(ref mut boxed) => boxed,
}
}
pub fn as_slice(&self) -> &[u8] {
match self {
Framebuffer::Static(ref arr) => arr,
Framebuffer::Heap(ref boxed) => boxed,
}
}
pub fn len(&self) -> usize {
self.as_slice().len()
}
}
impl core::ops::Deref for Framebuffer {
type Target = [u8];
fn deref(&self) -> &Self::Target {
match self {
Framebuffer::Static(arr) => arr,
Framebuffer::Heap(boxed) => boxed,
}
}
}
impl core::ops::DerefMut for Framebuffer {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Framebuffer::Static(arr) => arr,
Framebuffer::Heap(boxed) => boxed,
}
}
}
pub struct Rm690b0Driver<IFACE, RST, C>
where
IFACE: ControllerInterface,
RST: ResetInterface,
C: PixelColor,
{
interface: IFACE,
reset: RST,
framebuffer: Framebuffer,
config: DisplaySize,
color_mode: ColorMode,
_color: core::marker::PhantomData<C>,
}
impl<IFACE, RST, C> Rm690b0Driver<IFACE, RST, C>
where
IFACE: ControllerInterface,
RST: ResetInterface,
C: PixelColor,
{
pub fn new_static<DELAY, const N: usize>(
interface: IFACE,
reset: RST,
colormode: ColorMode,
config: DisplaySize,
mut delay: DELAY,
framebuffer: &'static mut [u8; N],
) -> Result<Self, DriverError<IFACE::Error, RST::Error>>
where
DELAY: DelayNs,
{
let expected_fb_size = framebuffer_size(config, colormode);
if N != expected_fb_size {
return Err(DriverError::InvalidConfiguration(
"Framebuffer size does not match display size and color mode",
));
}
let mut driver = Self {
interface,
reset,
framebuffer: Framebuffer::Static(&mut framebuffer[..]),
config,
color_mode: colormode,
_color: core::marker::PhantomData,
};
driver.hard_reset()?;
driver.initialize_display(&mut delay, colormode)?;
Ok(driver)
}
pub fn new_heap<DELAY, const N: usize>(
interface: IFACE,
reset: RST,
color: ColorMode,
config: DisplaySize,
mut delay: DELAY,
) -> Result<Self, DriverError<IFACE::Error, RST::Error>>
where
DELAY: DelayNs,
{
let expected_fb_size = framebuffer_size(config, color);
if N != expected_fb_size {
return Err(DriverError::InvalidConfiguration(
"Framebuffer size does not match display size and color mode",
));
}
let mut driver = Self {
interface,
reset,
framebuffer: Framebuffer::Heap(Box::new([0u8; N])),
config,
color_mode: color,
_color: core::marker::PhantomData,
};
driver.hard_reset()?;
driver.initialize_display(&mut delay, color)?;
Ok(driver)
}
pub fn hard_reset(&mut self) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.reset.reset().map_err(DriverError::ResetError)?;
Ok(())
}
pub fn initialize_display<DELAY>(
&mut self,
delay: &mut DELAY,
color: ColorMode,
) -> Result<(), DriverError<IFACE::Error, RST::Error>>
where
DELAY: DelayNs,
{
self.send_command(commands::SLPOUT)?;
delay.delay_ms(120);
self.send_command_with_data(commands::MADCTR, &[0x00])?;
self.send_command_with_data(0xFE, &[0x20])?;
self.send_command_with_data(0x26, &[0x0A])?;
self.send_command_with_data(0x24, &[0x80])?;
self.send_command_with_data(0x5A, &[0x51])?;
self.send_command_with_data(0x5B, &[0x2E])?;
self.send_command_with_data(0xFE, &[0x00])?;
match color {
ColorMode::Rgb565 => {
self.send_command_with_data(commands::COLMOD, &[0x55])?;
}
ColorMode::Rgb888 => {
self.send_command_with_data(commands::COLMOD, &[0x77])?;
}
ColorMode::Rgb666 => {
self.send_command_with_data(commands::COLMOD, &[0x66])?;
}
ColorMode::Gray8 => {
self.send_command_with_data(commands::COLMOD, &[0x11])?;
}
}
self.send_command_with_data(commands::TEON, &[0x00])?;
self.send_command(commands::DISPON)?;
delay.delay_ms(20);
self.send_command_with_data(commands::WRDISBV, &[0xFF])?;
Ok(())
}
fn send_command(&mut self, cmd: u8) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.interface
.send_command(cmd)
.map_err(DriverError::InterfaceError)
}
fn send_command_with_data(
&mut self,
cmd: u8,
data: &[u8],
) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.interface
.send_command_with_data(cmd, data)
.map_err(DriverError::InterfaceError)?;
Ok(())
}
pub fn sleep_in<DELAY>(
&mut self,
delay: &mut DELAY,
) -> Result<(), DriverError<IFACE::Error, RST::Error>>
where
DELAY: DelayNs,
{
self.send_command(commands::SLPIN)?;
delay.delay_ms(5);
Ok(())
}
pub fn sleep_out<DELAY>(
&mut self,
delay: &mut DELAY,
) -> Result<(), DriverError<IFACE::Error, RST::Error>>
where
DELAY: DelayNs,
{
self.send_command(commands::SLPOUT)?;
delay.delay_ms(5);
Ok(())
}
pub fn display_off(&mut self) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.send_command(commands::DISPOFF)
}
pub fn display_on(&mut self) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.send_command(commands::DISPON)
}
pub fn set_window(
&mut self,
x_start: u16,
y_start: u16,
x_end: u16,
y_end: u16,
) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
let width = x_end.saturating_sub(x_start).saturating_add(1);
let height = y_end.saturating_sub(y_start).saturating_add(1);
if x_start % 2 != 0 || width % 2 != 0 {
return Err(DriverError::InvalidConfiguration(
"x_start and width must both be even",
));
}
if y_start % 2 != 0 || height % 2 != 0 {
return Err(DriverError::InvalidConfiguration(
"y_start and height must both be even",
));
}
if x_end < x_start
|| y_end < y_start
|| x_end >= self.config.width
|| y_end >= self.config.height
{
return Err(DriverError::InvalidConfiguration(
"Invalid window dimensions",
));
}
self.send_command_with_data(
commands::CASET,
&[
(x_start >> 8) as u8,
(x_start & 0xFF) as u8,
(x_end >> 8) as u8,
(x_end & 0xFF) as u8,
],
)?;
self.send_command_with_data(
commands::RASET,
&[
(y_start >> 8) as u8,
(y_start & 0xFF) as u8,
(y_end >> 8) as u8,
(y_end & 0xFF) as u8,
],
)?;
Ok(())
}
pub fn set_madctr(&mut self, value: u8) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.send_command_with_data(commands::MADCTR, &[value])
}
pub fn set_brightness(
&mut self,
value: u8,
) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.send_command_with_data(commands::WRDISBV, &[value])
}
pub fn flush(&mut self) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.set_window(0, 0, self.config.width - 1, self.config.height - 1)?;
self.interface
.send_pixels(&self.framebuffer)
.map_err(DriverError::InterfaceError)?;
Ok(())
}
pub fn partial_flush(
&mut self,
x_start: u16,
x_end: u16,
y_start: u16,
y_end: u16,
) -> Result<(), DriverError<IFACE::Error, RST::Error>> {
self.set_window(x_start, y_start, x_end, y_end)?;
let bytes_per_pixel = self.color_mode.bytes_per_pixel();
let fb_width = self.config.width as usize * bytes_per_pixel;
let width = (x_end - x_start + 1) as usize;
let height = (y_end - y_start + 1) as usize;
for y in 0..height {
let offset = (y_start as usize + y) * fb_width + (x_start as usize * bytes_per_pixel);
let row_end = offset + (width * bytes_per_pixel);
if offset >= self.framebuffer.len() || row_end > self.framebuffer.len() {
return Err(DriverError::InvalidConfiguration(
"Framebuffer slice out of bounds",
));
}
let row = &self.framebuffer[offset..row_end];
if y == 0 {
self.interface
.send_pixels_start(row)
.map_err(DriverError::InterfaceError)?;
} else {
self.interface
.send_pixels_continue(row)
.map_err(DriverError::InterfaceError)?;
}
}
Ok(())
}
}