Skip to main content

display_driver/eg/
utils.rs

1use embedded_graphics::{
2    geometry::Point,
3    mono_font::{ascii::FONT_8X13, MonoTextStyle},
4    pixelcolor::{PixelColor, Rgb565},
5    prelude::*,
6    primitives::{Line, Primitive, PrimitiveStyle, Triangle},
7    text::Text,
8};
9
10/// L-shaped corner markers for display offset verification
11pub struct LShapedMarkers<C> {
12    pub width: i32,
13    pub height: i32,
14    pub length: i32,
15    pub color: C,
16}
17
18impl<C: PixelColor> LShapedMarkers<C> {
19    pub fn new(width: i32, height: i32, length: i32, color: C) -> Self {
20        Self {
21            width,
22            height,
23            length,
24            color,
25        }
26    }
27}
28
29impl<C: PixelColor> Drawable for LShapedMarkers<C> {
30    type Color = C;
31    type Output = ();
32
33    fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
34    where
35        D: DrawTarget<Color = C>,
36    {
37        let style = PrimitiveStyle::with_stroke(self.color, 1);
38        let w = self.width;
39        let h = self.height;
40        let l = self.length;
41
42        // Top-left
43        Line::new(Point::new(0, 0), Point::new(l, 0))
44            .into_styled(style)
45            .draw(target)?;
46        Line::new(Point::new(0, 0), Point::new(0, l))
47            .into_styled(style)
48            .draw(target)?;
49
50        // Top-right
51        Line::new(Point::new(w - 1, 0), Point::new(w - 1 - l, 0))
52            .into_styled(style)
53            .draw(target)?;
54        Line::new(Point::new(w - 1, 0), Point::new(w - 1, l))
55            .into_styled(style)
56            .draw(target)?;
57
58        // Bottom-left
59        Line::new(Point::new(0, h - 1), Point::new(l, h - 1))
60            .into_styled(style)
61            .draw(target)?;
62        Line::new(Point::new(0, h - 1), Point::new(0, h - 1 - l))
63            .into_styled(style)
64            .draw(target)?;
65
66        // Bottom-right
67        Line::new(Point::new(w - 1, h - 1), Point::new(w - 1 - l, h - 1))
68            .into_styled(style)
69            .draw(target)?;
70        Line::new(Point::new(w - 1, h - 1), Point::new(w - 1, h - 1 - l))
71            .into_styled(style)
72            .draw(target)?;
73
74        Ok(())
75    }
76}
77
78/// Draw a rotation demo scene with orientation indicators
79///
80/// This function draws:
81/// - L-shaped corner markers (red) to verify display offsets
82/// - An upward-pointing arrow (green) indicating the visual top of the buffer
83/// - Rotation text showing the current orientation
84/// - A triangle marker in the top-right corner (red)
85pub fn draw_rotation_scene<D>(target: &mut D, w: usize, h: usize, rot_str: &str)
86where
87    D: DrawTarget<Color = Rgb565> + Dimensions,
88    D::Error: core::fmt::Debug,
89{
90    target.clear(Rgb565::BLACK).unwrap();
91
92    let text_style = MonoTextStyle::new(&FONT_8X13, Rgb565::WHITE);
93    let cx = (w / 2) as i32;
94    let cy = (h / 2) as i32;
95
96    // Draw L-shaped markers at the corners to verify offsets
97    LShapedMarkers::new(w as i32, h as i32, 5, Rgb565::RED)
98        .draw(target)
99        .unwrap();
100
101    // Draw Arrow pointing UP (Visual top of the current buffer)
102    // Shaft
103    Line::new(Point::new(cx, cy + 20), Point::new(cx, cy - 20))
104        .into_styled(PrimitiveStyle::with_stroke(Rgb565::GREEN, 1))
105        .draw(target)
106        .ok();
107
108    // Head
109    Triangle::new(
110        Point::new(cx, cy - 20),
111        Point::new(cx - 5, cy - 10),
112        Point::new(cx + 5, cy - 10),
113    )
114    .into_styled(PrimitiveStyle::with_fill(Rgb565::GREEN))
115    .draw(target)
116    .ok();
117
118    // Centered text
119    Text::new(rot_str, Point::new(10, 20), text_style)
120        .draw(target)
121        .ok();
122
123    // Triangle marker in top-right
124    Triangle::new(
125        Point::new((w as i32) - 10, 10),
126        Point::new((w as i32) - 20, 10),
127        Point::new((w as i32) - 10, 20),
128    )
129    .into_styled(PrimitiveStyle::with_stroke(Rgb565::RED, 1))
130    .draw(target)
131    .ok();
132}