lcd_async/dcs/
set_pixel_format.rs1use super::DcsCommand;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct SetPixelFormat(PixelFormat);
8
9impl SetPixelFormat {
10 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[repr(u8)]
32pub enum BitsPerPixel {
33 Three = 0b001,
35 Eight = 0b010,
37 Twelve = 0b011,
39 Sixteen = 0b101,
41 Eighteen = 0b110,
43 TwentyFour = 0b111,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct PixelFormat {
52 dpi: BitsPerPixel,
53 dbi: BitsPerPixel,
54}
55
56impl PixelFormat {
57 pub const fn new(dpi: BitsPerPixel, dbi: BitsPerPixel) -> Self {
62 Self { dpi, dbi }
63 }
64
65 pub const fn with_all(bpp: BitsPerPixel) -> Self {
70 Self { dpi: bpp, dbi: bpp }
71 }
72
73 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}