Skip to main content

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