device-envoy-core 0.1.3

Shared traits and data types for device-envoy platform crates
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::pixel_target::{
    PixelTarget, PixelTargetAdapter, fill_ellipse_pixels, pixel_put, pixel_put_565,
};
use embedded_graphics::{
    Drawable,
    pixelcolor::{Rgb565, Rgb888, raw::RawU16},
    prelude::{IntoStorage, Point, Size},
    primitives::Rectangle,
    primitives::{Circle, Line, Primitive, PrimitiveStyle},
};

/// A read-only view of all or part of a statically stored RGB565 image.
///
/// A view borrows the original pixels without copying or allocating. Create a
/// full-image view with [`Image565Fixed::view`](super::tga::Image565Fixed::view),
/// or select a rectangular portion with
/// [`Image565Fixed::view_rect`](super::tga::Image565Fixed::view_rect).
///
/// Coordinates passed to [`Image565View::pixel_at`] are local to the view, so
/// `(0, 0)` addresses the crop's top-left pixel rather than the original
/// image's top-left pixel.
///
/// Views contain only color pixels and draw opaquely. For color-key
/// transparency, draw an [`Image565Fixed`](super::Image565Fixed) with a
/// [`MaskFixed`](super::MaskFixed), as shown in the
/// [`MaskFixed` example](super::MaskFixed).
///
/// # Example
#[cfg_attr(
    feature = "doc-images",
    doc = ::embed_doc_image::embed_image!(
        "image565_view",
        "docs/assets/image565_view.png"
    )
)]
#[cfg_attr(
    feature = "host",
    doc = r#"

```rust
use device_envoy_core::cyd::{
    Cyd, CydDisplay,
    display::{CydFrame, DrawItem, Image565Fixed, tga},
};
use embedded_graphics::{
    prelude::{Point, Size},
    primitives::Rectangle,
};

const IMAGE: Image565Fixed<45, 73, { 45 * 73 }> = tga!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/docs/assets/cyd_fill_contiguous.tga"
))
.to_565();

async fn draw<C: Cyd>(cyd: &mut C) -> Result<(), C::Error> {
    let full_view = IMAGE.view();
    let source = Rectangle::new(Point::new(2, 35), Size::new(41, 36));
    let cropped_view = IMAGE.view_rect(source);

    assert_eq!(cropped_view.size(), source.size);
    assert_eq!(
        cropped_view.pixel_at(Point::zero()),
        full_view.pixel_at(source.top_left),
    );
    assert_eq!(
        cropped_view.rgb565_iter().count(),
        source.size.width as usize * source.size.height as usize,
    );

    let display = cyd.display();
    let mut frame = display.full_frame_mut();
    DrawItem::Bitmap {
        view: full_view,
        top_left: Point::new(80, 84),
    }
    .draw(&mut frame);
    DrawItem::Bitmap {
        view: cropped_view,
        top_left: Point::new(200, 102),
    }
    .draw(&mut frame);
    frame.flush().await
}

# use device_envoy_core::memory::{CydMemory, assert_framebuffer_matches_expected_png};
# use embedded_graphics::{
#     mono_font::ascii::FONT_9X15_BOLD,
#     pixelcolor::Rgb888,
#     prelude::RgbColor,
# };
# let mut cyd_memory = CydMemory::new(
#     Size::new(320, 240),
#     Rgb888::BLACK,
#     Rgb888::WHITE,
#     &FONT_9X15_BOLD,
# );
# futures_executor::block_on(draw(&mut cyd_memory))?;
# let golden_result = assert_framebuffer_matches_expected_png(
#     &cyd_memory,
#     env!("CARGO_MANIFEST_DIR"),
#     "image565_view.png",
# );
# assert!(golden_result.is_ok(), "{golden_result:?}");
# Ok::<(), device_envoy_core::memory::Error>(())
```

The complete image is shown on the left and its cropped view on the right:

![A complete RGB565 image beside a cropped view of the same image][image565_view]
"#
)]
#[derive(Clone, Copy, Debug)]
pub struct Image565View {
    pixels: &'static [u16],
    stride: u32,
    source: Rectangle,
}

impl Image565View {
    /// Creates a full-image view from a row-major RGB565 pixel slice.
    ///
    /// Use this when pixels are already available as packed RGB565 values. For
    /// a compile-time TGA image, prefer [`Image565Fixed::view`](super::tga::Image565Fixed::view),
    /// as shown in the [`Image565View` example](Image565View).
    ///
    /// Panics if `pixels.len() != size.width * size.height`.
    #[must_use]
    pub const fn new(pixels: &'static [u16], size: Size) -> Self {
        assert!(
            pixels.len() == size.width as usize * size.height as usize,
            "Image565View pixels must match width * height"
        );
        Self {
            pixels,
            stride: size.width,
            source: Rectangle::new(Point::zero(), size),
        }
    }

    /// Cropped view — `source` is in image coordinates, `stride` is the full
    /// image row width. Prefer [`Image565Fixed::view_rect`] at call sites.
    #[must_use]
    pub(crate) const fn new_cropped(
        pixels: &'static [u16],
        stride: u32,
        source: Rectangle,
    ) -> Self {
        Self {
            pixels,
            stride,
            source,
        }
    }

    /// Returns this view's dimensions.
    ///
    /// For a cropped view, these are the crop dimensions rather than the full
    /// image dimensions. See the [`Image565View` example](Image565View).
    #[must_use]
    pub const fn size(&self) -> Size {
        self.source.size
    }

    /// Returns the pixel at a view-local coordinate.
    ///
    /// `(0, 0)` is the top-left of this view, not necessarily the top-left of
    /// the underlying image. See the [`Image565View` example](Image565View).
    ///
    /// Panics if `point` is outside the view.
    #[must_use]
    pub fn pixel_at(&self, point: Point) -> Rgb565 {
        assert!(
            point.x >= 0 && point.y >= 0,
            "Image565View pixel coordinate must be non-negative"
        );
        let vx = point.x as usize;
        let vy = point.y as usize;
        assert!(
            vx < self.source.size.width as usize && vy < self.source.size.height as usize,
            "Image565View pixel coordinate must be inside the view"
        );
        let source_x = self.source.top_left.x as usize + vx;
        let source_y = self.source.top_left.y as usize + vy;
        let index = source_y * self.stride as usize + source_x;
        Rgb565::from(RawU16::new(self.pixels[index]))
    }

    /// Iterate over the view's pixels in row-major order as `Rgb565` values.
    ///
    /// Cropped views skip the pixels outside the view while preserving the
    /// view's local row order.
    ///
    /// See the [`Image565View` example](Image565View).
    pub fn rgb565_iter(&self) -> impl Iterator<Item = Rgb565> + '_ {
        Image565ViewPixels {
            view: *self,
            index: 0,
        }
    }
}

struct Image565ViewPixels {
    view: Image565View,
    index: usize,
}

impl Iterator for Image565ViewPixels {
    type Item = Rgb565;

    fn next(&mut self) -> Option<Self::Item> {
        let width = self.view.source.size.width as usize;
        let height = self.view.source.size.height as usize;
        if self.index >= width * height {
            return None;
        }

        let view_x = self.index % width;
        let view_y = self.index / width;
        let source_x = self.view.source.top_left.x as usize + view_x;
        let source_y = self.view.source.top_left.y as usize + view_y;
        let source_index = source_y * self.view.stride as usize + source_x;
        self.index += 1;
        Some(Rgb565::from(RawU16::new(self.view.pixels[source_index])))
    }
}

/// A 2D drawing command that can be rendered onto a [`PixelTarget`].
///
/// `DrawItem` is a compact, [`Copy`] representation for a heterogeneous scene.
/// Its floating-point geometry is convenient for calculated or projected
/// coordinates, but projection is not required. The same items can be passed to
/// [`CydDisplay::draw_items`](crate::cyd::CydDisplay::draw_items), which
/// composites and streams them without a pixel frame buffer, or rendered
/// directly with [`DrawItem::draw`] when a frame is available.
///
/// For ordinary imperative drawing into a
/// [`CydFrame`](crate::cyd::display::CydFrame), embedded-graphics primitives are
/// also appropriate, particularly when their integer-coordinate geometry and
/// styling API fit the scene. `DrawItem::draw` uses embedded-graphics internally
/// for strokes and circles as an implementation detail; `DrawItem` does not
/// replace the broader embedded-graphics API.
///
/// Coordinates and sizes are measured in display pixels. Colors are specified
/// as [`Rgb888`], and the target converts them to its native pixel format when
/// needed.
///
/// # Example
///
/// This example loads a TGA bitmap at compile time and draws one of each item
/// variant onto a full-screen frame.
#[cfg_attr(
    feature = "doc-images",
    doc = ::embed_doc_image::embed_image!(
        "draw_item_bitmap",
        "docs/assets/draw_item_bitmap.png"
    )
)]
#[cfg_attr(
    feature = "host",
    doc = r#"

```rust
use device_envoy_core::cyd::{
    Cyd, CydDisplay,
    display::{CydFrame, DrawItem, Image565Fixed, tga},
};
use embedded_graphics::{
    pixelcolor::Rgb888,
    prelude::{Point, RgbColor},
};

const BITMAP: Image565Fixed<45, 73, { 45 * 73 }> = tga!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/docs/assets/cyd_fill_contiguous.tga"
))
.to_565();

async fn draw<C: Cyd>(cyd: &mut C) -> Result<(), C::Error> {
    let display = cyd.display();
    let mut frame = display.full_frame_mut();
    let draw_items = [
        DrawItem::Bitmap {
            view: BITMAP.view(),
            top_left: Point::new(35, 84),
        },
        DrawItem::Circle {
            center: (125.0, 120.0),
            pixel_radius: 32.0,
            color: Rgb888::CYAN,
        },
        DrawItem::Ellipse {
            center: (215.0, 120.0),
            axis_a: (38.0, 0.0),
            axis_b: (12.0, 24.0),
            color: Rgb888::GREEN,
        },
        DrawItem::Stroke {
            start: (280.0, 80.0),
            end: (300.0, 160.0),
            color: Rgb888::YELLOW,
            pixel_width: 8.0,
        },
    ];
    for draw_item in draw_items {
        draw_item.draw(&mut frame);
    }
    frame.flush().await
}

# use device_envoy_core::memory::{CydMemory, assert_framebuffer_matches_expected_png};
# use embedded_graphics::{mono_font::ascii::FONT_9X15_BOLD, prelude::Size};
# let mut cyd_memory = CydMemory::new(
#     Size::new(320, 240),
#     Rgb888::BLACK,
#     Rgb888::WHITE,
#     &FONT_9X15_BOLD,
# );
# futures_executor::block_on(draw(&mut cyd_memory))?;
# let golden_result = assert_framebuffer_matches_expected_png(
#     &cyd_memory,
#     env!("CARGO_MANIFEST_DIR"),
#     "draw_item_bitmap.png",
# );
# assert!(golden_result.is_ok(), "{golden_result:?}");
# Ok::<(), device_envoy_core::memory::Error>(())
```

![Examples of all four DrawItem variants][draw_item_bitmap]
"#
)]
#[derive(Clone, Copy, Debug)]
pub enum DrawItem {
    /// A line stroke from `start` to `end` with the given pixel width.
    Stroke {
        /// Start point in display coordinates.
        start: (f32, f32),
        /// End point in display coordinates.
        end: (f32, f32),
        /// Stroke color.
        color: Rgb888,
        /// Stroke width in pixels.
        pixel_width: f32,
    },
    /// A filled ellipse. It can also represent a projected disk.
    ///
    /// The ellipse is the locus of `center + s·axis_a + t·axis_b` with `s²+t² ≤ 1`.
    Ellipse {
        /// Center in display coordinates.
        center: (f32, f32),
        /// First radius vector, measured in pixels.
        axis_a: (f32, f32),
        /// Second radius vector, measured in pixels.
        axis_b: (f32, f32),
        /// Fill color.
        color: Rgb888,
    },
    /// A filled circle. It can also represent a projected sphere.
    Circle {
        /// Center in display coordinates.
        center: (f32, f32),
        /// Radius in pixels.
        pixel_radius: f32,
        /// Fill color.
        color: Rgb888,
    },
    /// A statically stored RGB565 bitmap placed at a display position.
    Bitmap {
        /// Bitmap pixels and dimensions.
        view: Image565View,
        /// Top-left corner in display coordinates.
        top_left: Point,
    },
}

impl DrawItem {
    /// Draw this item onto a [`PixelTarget`].
    ///
    /// Strokes use the embedded-graphics
    /// [`Line`](https://docs.rs/embedded-graphics/latest/embedded_graphics/primitives/struct.Line.html)
    /// primitive and circles use
    /// [`Circle`](https://docs.rs/embedded-graphics/latest/embedded_graphics/primitives/struct.Circle.html);
    /// the general ellipse is rasterized with
    /// [`fill_ellipse_pixels`].
    ///
    /// See the [`DrawItem` example](DrawItem).
    pub fn draw<T: PixelTarget>(&self, target: &mut T) {
        match *self {
            DrawItem::Stroke {
                start,
                end,
                color,
                pixel_width,
            } => {
                let width = ((pixel_width + 0.5) as u32).max(1);
                Line::new(
                    embedded_graphics::prelude::Point::new(start.0 as i32, start.1 as i32),
                    embedded_graphics::prelude::Point::new(end.0 as i32, end.1 as i32),
                )
                .into_styled(PrimitiveStyle::with_stroke(color, width))
                .draw(&mut PixelTargetAdapter(target))
                .expect("drawing onto a PixelTargetAdapter is Infallible");
            }
            DrawItem::Ellipse {
                center,
                axis_a,
                axis_b,
                color,
            } => {
                fill_ellipse_pixels(center, axis_a, axis_b, |position_x, position_y| {
                    pixel_put(target, position_x, position_y, color);
                });
            }
            DrawItem::Circle {
                center,
                pixel_radius,
                color,
            } => {
                let diameter = (((pixel_radius * 2.0) + 0.5) as u32).max(1);
                Circle::with_center(
                    embedded_graphics::prelude::Point::new(center.0 as i32, center.1 as i32),
                    diameter,
                )
                .into_styled(PrimitiveStyle::with_fill(color))
                .draw(&mut PixelTargetAdapter(target))
                .expect("drawing onto a PixelTargetAdapter is Infallible");
            }
            DrawItem::Bitmap { view, top_left } => {
                let size = view.size();
                for dy in 0..size.height as i32 {
                    for dx in 0..size.width as i32 {
                        let view_point = Point::new(dx, dy);
                        let target_point = top_left + view_point;
                        pixel_put_565(
                            target,
                            target_point.x,
                            target_point.y,
                            view.pixel_at(view_point).into_storage(),
                        );
                    }
                }
            }
        }
    }
}