RKTK API Docs RKTK Home Repo

rktk/task/display/
default_display.rs

1use embassy_futures::select::{Either, select};
2use embedded_graphics::{
3    mono_font::{MonoTextStyleBuilder, ascii::FONT_8X13},
4    pixelcolor::BinaryColor,
5    prelude::*,
6    text::{Baseline, Text},
7};
8use images::*;
9
10use crate::{
11    drivers::interface::{display::DisplayDriver, reporter::Output},
12    utils::{Channel, Signal},
13};
14
15use super::{DisplayConfig, DisplayMessage};
16
17mod images;
18
19fn get_last_digit_str(n: u8) -> &'static str {
20    let digit = n % 10;
21    const DIGIT_STRS: [&str; 10] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
22    DIGIT_STRS[digit as usize]
23}
24
25pub struct DefaultDisplayConfig;
26impl DisplayConfig for DefaultDisplayConfig {
27    async fn start<D: DisplayDriver, const N1: usize, const N2: usize>(
28        &mut self,
29        display: &mut D,
30        display_controller: &Channel<DisplayMessage, N1>,
31        display_dynamic_message_controller: &Signal<heapless::String<N2>>,
32    ) {
33        loop {
34            match select(
35                display_controller.receive(),
36                display_dynamic_message_controller.wait(),
37            )
38            .await
39            {
40                Either::First(mes) => match mes {
41                    DisplayMessage::Clear => {
42                        let _ = display.as_mut().clear(BinaryColor::Off);
43                    }
44                    DisplayMessage::Message(_msg) => {
45                        // TODO: Implement this
46                    }
47                    DisplayMessage::Output(output) => {
48                        let image = match output {
49                            Output::Usb => IMAGE_USB,
50                            Output::Ble => IMAGE_BLUETOOTH,
51                        };
52                        let _ = image.translate(Point::new(8, 0)).draw(display.as_mut());
53                    }
54                    DisplayMessage::LayerState(layers) => {
55                        for (i, a) in layers.iter().enumerate() {
56                            let _ = Text::with_baseline(
57                                get_last_digit_str(i as u8),
58                                Point::new(0, 20 + i as i32 * 13),
59                                MonoTextStyleBuilder::new()
60                                    .font(&FONT_8X13)
61                                    .text_color(if *a {
62                                        BinaryColor::Off
63                                    } else {
64                                        BinaryColor::On
65                                    })
66                                    .background_color(if *a {
67                                        BinaryColor::On
68                                    } else {
69                                        BinaryColor::Off
70                                    })
71                                    .build(),
72                                Baseline::Top,
73                            )
74                            .draw(display.as_mut());
75                        }
76                    }
77                    DisplayMessage::MouseAvailable(mouse) => {
78                        if mouse {
79                            let _ = IMAGE_MOUSE
80                                .translate(Point::new(16, 35))
81                                .draw(display.as_mut());
82                        }
83                    }
84                    DisplayMessage::NumLock(num_lock) => {
85                        let _ = Text::with_baseline(
86                            "N",
87                            Point::new(14, 20),
88                            MonoTextStyleBuilder::new()
89                                .font(&FONT_8X13)
90                                .text_color(if num_lock {
91                                    BinaryColor::Off
92                                } else {
93                                    BinaryColor::On
94                                })
95                                .background_color(if num_lock {
96                                    BinaryColor::On
97                                } else {
98                                    BinaryColor::Off
99                                })
100                                .build(),
101                            Baseline::Top,
102                        )
103                        .draw(display.as_mut());
104                    }
105                    DisplayMessage::CapsLock(caps_lock) => {
106                        let _ = Text::with_baseline(
107                            "C",
108                            Point::new(24, 20),
109                            MonoTextStyleBuilder::new()
110                                .font(&FONT_8X13)
111                                .text_color(if caps_lock {
112                                    BinaryColor::Off
113                                } else {
114                                    BinaryColor::On
115                                })
116                                .background_color(if caps_lock {
117                                    BinaryColor::On
118                                } else {
119                                    BinaryColor::Off
120                                })
121                                .build(),
122                            Baseline::Top,
123                        )
124                        .draw(display.as_mut());
125                    }
126                    DisplayMessage::Brightness(brightness) => {
127                        let _ = display.set_brightness(brightness).await;
128                    }
129                    DisplayMessage::On(on) => {
130                        let _ = display.set_display_on(on).await;
131                    }
132                    _ => {}
133                },
134                Either::Second(_str) => {
135                    // TODO: Implement this
136                }
137            }
138
139            let _ = display.flush().await;
140        }
141    }
142}