Skip to content

Commit

Permalink
Add a model for the Rgb666 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Aug 29, 2022
1 parent 3058745 commit b876df0
Showing 1 changed file with 86 additions and 2 deletions.
88 changes: 86 additions & 2 deletions src/models/ili9342c.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;

Expand All @@ -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)
}

Expand All @@ -60,6 +70,62 @@ impl Model for ILI9342CRgb565 {
}
}

impl Model for ILI9342CRgb666 {
type ColorFormat = Rgb666;

fn new() -> Self {
Self
}

fn init<RST, DELAY, DI>(
&mut self,
di: &mut DI,
rst: &mut Option<RST>,
delay: &mut DELAY,
options: DisplayOptions,
) -> Result<u8, Error<RST::Error>>
where
RST: OutputPin,
DELAY: DelayUs<u32>,
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<DI, I>(&mut self, di: &mut DI, colors: I) -> Result<(), DisplayError>
where
DI: WriteOnlyDataCommand,
I: IntoIterator<Item = Self::ColorFormat>,
{
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<DI, RST> Display<DI, RST, ILI9342CRgb565>
Expand All @@ -82,6 +148,25 @@ where
}
}

impl<DI, RST> Display<DI, RST, ILI9342CRgb666>
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<DELAY, DI>(
di: &mut DI,
Expand All @@ -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]

Expand Down

0 comments on commit b876df0

Please sign in to comment.