lcd_async/models/
gc9107.rs

1use embedded_graphics_core::pixelcolor::Rgb565;
2use embedded_hal_async::delay::DelayNs;
3
4use crate::{
5    dcs::{
6        BitsPerPixel, ExitSleepMode, InterfaceExt, PixelFormat, SetAddressMode, SetDisplayOn,
7        SetInvertMode, SetPixelFormat,
8    },
9    interface::{Interface, InterfaceKind},
10    models::{Model, ModelInitError},
11    options::ModelOptions,
12    ConfigurationError,
13};
14
15/// GC9107 display in Rgb565 color mode.
16pub struct GC9107;
17
18impl Model for GC9107 {
19    type ColorFormat = Rgb565;
20    const FRAMEBUFFER_SIZE: (u16, u16) = (128, 160);
21
22    async fn init<DELAY, DI>(
23        &mut self,
24        di: &mut DI,
25        delay: &mut DELAY,
26        options: &ModelOptions,
27    ) -> Result<SetAddressMode, ModelInitError<DI::Error>>
28    where
29        DELAY: DelayNs,
30        DI: Interface,
31    {
32        if !matches!(
33            DI::KIND,
34            InterfaceKind::Serial4Line | InterfaceKind::Parallel8Bit
35        ) {
36            return Err(ModelInitError::InvalidConfiguration(
37                ConfigurationError::UnsupportedInterface,
38            ));
39        }
40
41        delay.delay_ms(200).await;
42
43        di.write_raw(0xFE, &[]).await?;
44        delay.delay_ms(5).await;
45        di.write_raw(0xEF, &[]).await?;
46        delay.delay_ms(5).await;
47
48        di.write_raw(0xB0, &[0xC0]).await?;
49        di.write_raw(0xB2, &[0x2F]).await?;
50        di.write_raw(0xB3, &[0x03]).await?;
51        di.write_raw(0xB6, &[0x19]).await?;
52        di.write_raw(0xB7, &[0x01]).await?;
53
54        let madctl = SetAddressMode::from(options);
55        di.write_command(madctl).await?;
56
57        di.write_raw(0xAC, &[0xCB]).await?;
58        di.write_raw(0xAB, &[0x0E]).await?;
59
60        di.write_raw(0xB4, &[0x04]).await?;
61
62        di.write_raw(0xA8, &[0x19]).await?;
63
64        let pf = PixelFormat::with_all(BitsPerPixel::from_rgb_color::<Self::ColorFormat>());
65        di.write_command(SetPixelFormat::new(pf)).await?;
66
67        di.write_raw(0xB8, &[0x08]).await?;
68
69        di.write_raw(0xE8, &[0x24]).await?;
70
71        di.write_raw(0xE9, &[0x48]).await?;
72
73        di.write_raw(0xEA, &[0x22]).await?;
74
75        di.write_raw(0xC6, &[0x30]).await?;
76        di.write_raw(0xC7, &[0x18]).await?;
77
78        di.write_raw(
79            0xF0,
80            &[
81                0x01, 0x2b, 0x23, 0x3c, 0xb7, 0x12, 0x17, 0x60, 0x00, 0x06, 0x0c, 0x17, 0x12, 0x1f,
82            ],
83        )
84        .await?;
85
86        di.write_raw(
87            0xF1,
88            &[
89                0x05, 0x2e, 0x2d, 0x44, 0xd6, 0x15, 0x17, 0xa0, 0x02, 0x0d, 0x0d, 0x1a, 0x18, 0x1f,
90            ],
91        )
92        .await?;
93
94        di.write_command(SetInvertMode::new(options.invert_colors))
95            .await?;
96
97        di.write_command(ExitSleepMode).await?; // turn off sleep
98        delay.delay_ms(120).await;
99
100        di.write_command(SetDisplayOn).await?; // turn on display
101
102        Ok(madctl)
103    }
104}