embedded_gfx/
framebuffer.rs1use embedded_graphics_core::{
2 draw_target::DrawTarget,
3 geometry::{OriginDimensions, Point},
4 pixelcolor::{IntoStorage, Rgb565},
5};
6
7pub struct DmaReadyFramebuffer<const W: usize, const H: usize> {
8 pub framebuffer: *mut [[u16; W]; H], big_endian: bool,
10}
11
12impl<const W: usize, const H: usize> DmaReadyFramebuffer<W, H> {
13 pub fn new(
14 raw_framebuffer: *mut ::core::ffi::c_void,
15 big_endian: bool,
16 ) -> DmaReadyFramebuffer<W, H> {
17 if raw_framebuffer.is_null() {
18 panic!("Failed to allocate framebuffer");
19 }
20
21 DmaReadyFramebuffer {
22 framebuffer: raw_framebuffer as *mut [[u16; W]; H],
23 big_endian,
24 }
25 }
26
27 pub fn set_pixel(&mut self, point: Point, color: Rgb565) {
28 if point.x >= 0 && point.x < W as i32 && point.y >= 0 && point.y < H as i32 {
29 unsafe {
30 let framebuffer = &mut *self.framebuffer;
31
32 if self.big_endian {
33 framebuffer[point.y as usize][point.x as usize] = color.into_storage().to_be();
34 } else {
35 framebuffer[point.y as usize][point.x as usize] = color.into_storage();
36 }
37 }
38 }
39 }
40
41 pub fn as_slice(&self) -> &[u16] {
42 unsafe { core::slice::from_raw_parts(self.framebuffer as *const u16, W * H) }
43 }
44
45 pub fn as_mut_slice(&mut self) -> &mut [u16] {
46 unsafe { core::slice::from_raw_parts_mut(self.framebuffer as *mut u16, W * H) }
47 }
48
49 pub fn as_mut_ptr(&mut self) -> *mut [u16] {
50 self.as_slice() as *const [u16] as *mut [u16]
51 }
52}
53
54impl<const W: usize, const H: usize> DrawTarget for DmaReadyFramebuffer<W, H> {
55 type Color = Rgb565;
56 type Error = core::convert::Infallible;
57
58 fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
59 where
60 I: IntoIterator<Item = embedded_graphics_core::prelude::Pixel<Self::Color>>,
61 {
62 for pixel in pixels {
63 let embedded_graphics_core::prelude::Pixel(point, color) = pixel;
64
65 self.set_pixel(point, color);
66 }
67 Ok(())
68 }
69
70 fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
71 if self.big_endian {
72 self.as_mut_slice().fill(color.into_storage().to_be());
73 } else {
74 self.as_mut_slice().fill(color.into_storage());
75 }
76
77 Ok(())
78 }
79}
80
81impl<const W: usize, const H: usize> OriginDimensions for DmaReadyFramebuffer<W, H> {
82 fn size(&self) -> embedded_graphics_core::geometry::Size {
83 embedded_graphics_core::geometry::Size::new(W as u32, H as u32)
84 }
85}