From b876df0f5bf75ba82c874b66925773d41dfccac9 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Mon, 29 Aug 2022 11:19:03 -0700 Subject: [PATCH] Add a model for the Rgb666 encoding --- src/models/ili9342c.rs | 88 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/src/models/ili9342c.rs b/src/models/ili9342c.rs index 6c4ced8..459a029 100644 --- a/src/models/ili9342c.rs +++ b/src/models/ili9342c.rs @@ -1,5 +1,8 @@ use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand}; -use embedded_graphics_core::{pixelcolor::Rgb565, prelude::IntoStorage}; +use embedded_graphics_core::{ + pixelcolor::{Rgb565, Rgb666}, + prelude::{IntoStorage, RgbColor}, +}; use embedded_hal::{blocking::delay::DelayUs, digital::v2::OutputPin}; use crate::{instruction::Instruction, Display, DisplayOptions, Error, Orientation}; @@ -11,6 +14,11 @@ use super::{write_command, Model}; /// Backlight pin is not controlled pub struct ILI9342CRgb565; +/// ILI9342C display with Reset pin +/// in Rgb666 color mode +/// Backlight pin is not controlled +pub struct ILI9342CRgb666; + impl Model for ILI9342CRgb565 { type ColorFormat = Rgb565; @@ -37,6 +45,8 @@ impl Model for ILI9342CRgb565 { delay.delay_us(120_000); + write_command(di, Instruction::COLMOD, &[0b0101_0101])?; // 16bit 65k colors + init_common(di, delay, options).map_err(|_| Error::DisplayError) } @@ -60,6 +70,62 @@ impl Model for ILI9342CRgb565 { } } +impl Model for ILI9342CRgb666 { + type ColorFormat = Rgb666; + + fn new() -> Self { + Self + } + + fn init( + &mut self, + di: &mut DI, + rst: &mut Option, + delay: &mut DELAY, + options: DisplayOptions, + ) -> Result> + where + RST: OutputPin, + DELAY: DelayUs, + DI: WriteOnlyDataCommand, + { + match rst { + Some(ref mut rst) => self.hard_reset(rst, delay)?, + None => write_command(di, Instruction::SWRESET, &[])?, + } + + delay.delay_us(120_000); + + write_command(di, Instruction::COLMOD, &[0b0110_0110])?; // 18bit 262k colors + + init_common(di, delay, options).map_err(|_| Error::DisplayError) + } + + fn write_pixels(&mut self, di: &mut DI, colors: I) -> Result<(), DisplayError> + where + DI: WriteOnlyDataCommand, + I: IntoIterator, + { + write_command(di, Instruction::RAMWR, &[])?; + let mut iter = colors.into_iter().flat_map(|c| { + let red = c.r() << 2; + let green = c.g() << 2; + let blue = c.b() << 2; + [red, green, blue] + }); + + let buf = DataFormat::U8Iter(&mut iter); + di.send_data(buf) + } + + fn display_size(&self, orientation: Orientation) -> (u16, u16) { + match orientation { + Orientation::Portrait(_) | Orientation::PortraitInverted(_) => (320, 240), + Orientation::Landscape(_) | Orientation::LandscapeInverted(_) => (240, 320), + } + } +} + // simplified constructor for Display impl Display @@ -82,6 +148,25 @@ where } } +impl Display +where + DI: WriteOnlyDataCommand, + RST: OutputPin, +{ + /// + /// Creates a new [Display] instance with [ILI9342C] as the [Model] + /// + /// # Arguments + /// + /// * `di` - a [DisplayInterface](WriteOnlyDataCommand) for talking with the display + /// * `rst` - display hard reset [OutputPin] + /// * `model` - the display [Model] + /// + pub fn ili9342c_rgb666(di: DI, rst: RST) -> Self { + Self::with_model(di, Some(rst), ILI9342CRgb666::new()) + } +} + // common init for all color format models fn init_common( di: &mut DI, @@ -95,7 +180,6 @@ where let madctl = options.madctl() ^ 0b0000_1000; // this model has flipped RGB/BGR bit; write_command(di, Instruction::SLPOUT, &[])?; // turn off sleep - write_command(di, Instruction::COLMOD, &[0b0101_0101])?; // 16bit 65k colors write_command(di, Instruction::MADCTL, &[madctl])?; // left -> right, bottom -> top RGB write_command(di, Instruction::INVCO, &[0x0])?; //Inversion Control [00]