lcd_async/dcs/
set_pixel_format.rs

1//! Module for the COLMOD instruction constructors
2
3use super::DcsCommand;
4
5/// Set Pixel Format
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct SetPixelFormat(PixelFormat);
8
9impl SetPixelFormat {
10    /// Creates a new Set Pixel Format command.
11    pub const fn new(pixel_format: PixelFormat) -> Self {
12        Self(pixel_format)
13    }
14}
15
16impl DcsCommand for SetPixelFormat {
17    fn instruction(&self) -> u8 {
18        0x3A
19    }
20
21    fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
22        buffer[0] = self.0.as_u8();
23        1
24    }
25}
26
27///
28/// Bits per pixel for DBI and DPI fields of [PixelFormat]
29///
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[repr(u8)]
32pub enum BitsPerPixel {
33    /// 3 bits per pixel.
34    Three = 0b001,
35    /// 8 bits per pixel.
36    Eight = 0b010,
37    /// 12 bits per pixel.
38    Twelve = 0b011,
39    /// 16 bits per pixel.
40    Sixteen = 0b101,
41    /// 18 bits per pixel.
42    Eighteen = 0b110,
43    /// 24 bits per pixel.
44    TwentyFour = 0b111,
45}
46
47///
48/// Defines pixel format as combination of DPI and DBI
49///
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct PixelFormat {
52    dpi: BitsPerPixel,
53    dbi: BitsPerPixel,
54}
55
56impl PixelFormat {
57    ///
58    /// Construct a new [PixelFormat] with given [BitsPerPixel] values
59    /// for DPI and DBI fields
60    ///
61    pub const fn new(dpi: BitsPerPixel, dbi: BitsPerPixel) -> Self {
62        Self { dpi, dbi }
63    }
64
65    ///
66    /// Construct a new [PixelFormat] with same [BitsPerPixel] value
67    /// for both DPI and DBI fields
68    ///
69    pub const fn with_all(bpp: BitsPerPixel) -> Self {
70        Self { dpi: bpp, dbi: bpp }
71    }
72
73    ///
74    /// Returns the corresponding u8 containing both DPI and DBI bits
75    ///
76    pub fn as_u8(&self) -> u8 {
77        (self.dpi as u8) << 4 | (self.dbi as u8)
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn colmod_rgb565_is_16bit() {
87        let colmod = SetPixelFormat::new(PixelFormat::new(
88            BitsPerPixel::Sixteen,
89            BitsPerPixel::Sixteen,
90        ));
91
92        let mut bytes = [0u8];
93        assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
94        assert_eq!(bytes, [0b0101_0101u8]);
95    }
96
97    #[test]
98    fn colmod_rgb666_is_18bit() {
99        let colmod = SetPixelFormat::new(PixelFormat::new(
100            BitsPerPixel::Eighteen,
101            BitsPerPixel::Eighteen,
102        ));
103
104        let mut bytes = [0u8];
105        assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
106        assert_eq!(bytes, [0b0110_0110u8]);
107    }
108
109    #[test]
110    fn colmod_rgb888_is_24bit() {
111        let colmod = SetPixelFormat::new(PixelFormat::new(
112            BitsPerPixel::Eighteen,
113            BitsPerPixel::TwentyFour,
114        ));
115
116        let mut bytes = [0u8];
117        assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
118        assert_eq!(bytes, [0b0110_0111u8]);
119    }
120
121    #[test]
122    fn test_pixel_format_as_u8() {
123        let pf = PixelFormat::new(BitsPerPixel::Sixteen, BitsPerPixel::TwentyFour);
124        assert_eq!(pf.as_u8(), 0b0101_0111);
125    }
126}