Skip to main content
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    type Color = BinaryColor;
28
29    async fn start<D: DisplayDriver<Color = BinaryColor>, const N1: usize, const N2: usize>(
30        &mut self,
31        display: &mut D,
32        display_controller: &Channel<DisplayMessage, N1>,
33        display_dynamic_message_controller: &Signal<heapless::String<N2>>,
34    ) {
35        loop {
36            match select(display_controller.receive(), display_dynamic_message_controller.wait())
37                .await
38            {
39                Either::First(mes) => match mes {
40                    DisplayMessage::Clear => {
41                        let _ = display.draw_target().clear(BinaryColor::Off);
42                    }
43                    DisplayMessage::Message(_msg) => {
44                        // TODO: Implement this
45                    }
46                    DisplayMessage::Output(output) => {
47                        let image = match output {
48                            Output::Usb => IMAGE_USB,
49                            Output::Ble => IMAGE_BLUETOOTH,
50                        };
51                        let _ = image.translate(Point::new(8, 0)).draw(display.draw_target());
52                    }
53                    DisplayMessage::LayerState(layers) => {
54                        for (i, a) in layers.iter().enumerate() {
55                            let _ = Text::with_baseline(
56                                get_last_digit_str(i as u8),
57                                Point::new(0, 20 + i as i32 * 13),
58                                MonoTextStyleBuilder::new()
59                                    .font(&FONT_8X13)
60                                    .text_color(if *a { BinaryColor::Off } else { BinaryColor::On })
61                                    .background_color(if *a {
62                                        BinaryColor::On
63                                    } else {
64                                        BinaryColor::Off
65                                    })
66                                    .build(),
67                                Baseline::Top,
68                            )
69                            .draw(display.draw_target());
70                        }
71                    }
72                    DisplayMessage::MouseAvailable(mouse) => {
73                        if mouse {
74                            let _ =
75                                IMAGE_MOUSE.translate(Point::new(16, 35)).draw(display.draw_target());
76                        }
77                    }
78                    DisplayMessage::NumLock(num_lock) => {
79                        let _ = Text::with_baseline(
80                            "N",
81                            Point::new(14, 20),
82                            MonoTextStyleBuilder::new()
83                                .font(&FONT_8X13)
84                                .text_color(if num_lock {
85                                    BinaryColor::Off
86                                } else {
87                                    BinaryColor::On
88                                })
89                                .background_color(if num_lock {
90                                    BinaryColor::On
91                                } else {
92                                    BinaryColor::Off
93                                })
94                                .build(),
95                            Baseline::Top,
96                        )
97                        .draw(display.draw_target());
98                    }
99                    DisplayMessage::CapsLock(caps_lock) => {
100                        let _ = Text::with_baseline(
101                            "C",
102                            Point::new(24, 20),
103                            MonoTextStyleBuilder::new()
104                                .font(&FONT_8X13)
105                                .text_color(if caps_lock {
106                                    BinaryColor::Off
107                                } else {
108                                    BinaryColor::On
109                                })
110                                .background_color(if caps_lock {
111                                    BinaryColor::On
112                                } else {
113                                    BinaryColor::Off
114                                })
115                                .build(),
116                            Baseline::Top,
117                        )
118                        .draw(display.draw_target());
119                    }
120                    DisplayMessage::Brightness(brightness) => {
121                        let _ = display.set_brightness(brightness).await;
122                    }
123                    DisplayMessage::On(on) => {
124                        let _ = display.set_display_on(on).await;
125                    }
126                    _ => {}
127                },
128                Either::Second(_str) => {
129                    // TODO: Implement this
130                }
131            }
132
133            let _ = display.flush().await;
134        }
135    }
136}