lcd_async/dcs/
set_pixel_format.rs1use super::DcsCommand;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8pub struct SetPixelFormat(PixelFormat);
9
10impl SetPixelFormat {
11 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32#[cfg_attr(feature = "defmt", derive(defmt::Format))]
33#[repr(u8)]
34pub enum BitsPerPixel {
35 Three = 0b001,
37 Eight = 0b010,
39 Twelve = 0b011,
41 Sixteen = 0b101,
43 Eighteen = 0b110,
45 TwentyFour = 0b111,
47}
48
49#[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 pub const fn new(dpi: BitsPerPixel, dbi: BitsPerPixel) -> Self {
65 Self { dpi, dbi }
66 }
67
68 pub const fn with_all(bpp: BitsPerPixel) -> Self {
73 Self { dpi: bpp, dbi: bpp }
74 }
75
76 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}