use super::DcsCommand;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SetPixelFormat(PixelFormat);
impl SetPixelFormat {
pub const fn new(pixel_format: PixelFormat) -> Self {
Self(pixel_format)
}
}
impl DcsCommand for SetPixelFormat {
fn instruction(&self) -> u8 {
0x3A
}
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
buffer[0] = self.0.as_u8();
1
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum BitsPerPixel {
Three = 0b001,
Eight = 0b010,
Twelve = 0b011,
Sixteen = 0b101,
Eighteen = 0b110,
TwentyFour = 0b111,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PixelFormat {
dpi: BitsPerPixel,
dbi: BitsPerPixel,
}
impl PixelFormat {
pub const fn new(dpi: BitsPerPixel, dbi: BitsPerPixel) -> Self {
Self { dpi, dbi }
}
pub const fn with_all(bpp: BitsPerPixel) -> Self {
Self { dpi: bpp, dbi: bpp }
}
pub fn as_u8(&self) -> u8 {
(self.dpi as u8) << 4 | (self.dbi as u8)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colmod_rgb565_is_16bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Sixteen,
BitsPerPixel::Sixteen,
));
let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0101_0101u8]);
}
#[test]
fn colmod_rgb666_is_18bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Eighteen,
BitsPerPixel::Eighteen,
));
let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0110_0110u8]);
}
#[test]
fn colmod_rgb888_is_24bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Eighteen,
BitsPerPixel::TwentyFour,
));
let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0110_0111u8]);
}
#[test]
fn test_pixel_format_as_u8() {
let pf = PixelFormat::new(BitsPerPixel::Sixteen, BitsPerPixel::TwentyFour);
assert_eq!(pf.as_u8(), 0b0101_0111);
}
}