hal_core/framebuffer/
embedded_graphics.rs1use super::*;
2use embedded_graphics_core::{draw_target, geometry, pixelcolor, primitives::Rectangle, Pixel};
3
4#[derive(Copy, Clone, Debug)]
5pub struct DrawTarget<D>(D);
6
7impl<D: Draw> DrawTarget<D> {
8 pub fn new(draw: D) -> Self {
9 Self(draw)
10 }
11
12 pub fn size(&self) -> geometry::Size {
13 geometry::Size {
14 height: self.0.height() as u32,
15 width: self.0.width() as u32,
16 }
17 }
18
19 pub fn inner_mut(&mut self) -> &mut D {
20 &mut self.0
21 }
22}
23
24impl<D: Draw> geometry::Dimensions for DrawTarget<D> {
25 fn bounding_box(&self) -> Rectangle {
26 Rectangle {
27 top_left: geometry::Point { x: 0, y: 0 },
28 size: self.size(),
29 }
30 }
31}
32
33impl<D> draw_target::DrawTarget for DrawTarget<D>
34where
35 D: Draw,
36{
37 type Error = core::convert::Infallible;
38 type Color = pixelcolor::Rgb888;
39
40 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
41 where
42 I: IntoIterator<Item = Pixel<Self::Color>>,
43 {
44 for Pixel(geometry::Point { x, y }, color) in pixels {
45 self.0.set_pixel(x as usize, y as usize, color.into());
46 }
47
48 Ok(())
49 }
50
51 fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
52 self.0.fill(color.into());
53 Ok(())
54 }
55}
56
57impl<C> From<C> for RgbColor
58where
59 C: pixelcolor::RgbColor,
60{
61 #[inline]
62 fn from(c: C) -> Self {
63 Self {
64 red: c.r(),
65 green: c.g(),
66 blue: c.b(),
67 }
68 }
69}