use embedded_graphics_core::pixelcolor::raw::RawU24;
use embedded_graphics_core::{
draw_target::DrawTarget,
geometry::{OriginDimensions, Point},
pixelcolor::{Bgr888, IntoStorage},
};
pub struct DmaReadyFramebuffer {
pub width: usize,
pub height: usize,
pub framebuffer: Box<[u32]>,
big_endian: bool,
}
impl DmaReadyFramebuffer {
pub fn new(width: usize, height: usize, big_endian: bool) -> DmaReadyFramebuffer {
DmaReadyFramebuffer {
framebuffer: vec![0u32; width * height].into_boxed_slice(),
width,
height,
big_endian,
}
}
pub fn set_pixel(&mut self, point: Point, color: Bgr888) {
if point.x >= 0
&& point.x < self.width as i32
&& point.y >= 0
&& point.y < self.height as i32
{
let framebuffer = &mut *self.framebuffer;
if self.big_endian {
framebuffer[point.y as usize * self.width + point.x as usize] =
color.into_storage().to_be();
} else {
framebuffer[point.y as usize * self.width + point.x as usize] =
color.into_storage();
}
}
}
pub fn get_pixel(&mut self, point: Point) -> Option<Bgr888> {
if point.x >= 0
&& point.x < self.width as i32
&& point.y >= 0
&& point.y < self.height as i32
{
if self.big_endian {
Some(Bgr888::from(RawU24::new(u32::from_be(
self.framebuffer[point.y as usize * self.width + point.x as usize],
))))
} else {
Some(Bgr888::from(RawU24::new(
self.framebuffer[point.y as usize * self.width + point.x as usize],
)))
}
} else {
None
}
}
}
impl DrawTarget for DmaReadyFramebuffer {
type Color = Bgr888;
type Error = core::convert::Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics_core::prelude::Pixel<Self::Color>>,
{
for pixel in pixels {
let embedded_graphics_core::prelude::Pixel(point, color) = pixel;
self.set_pixel(point, color);
}
Ok(())
}
fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
if self.big_endian {
self.framebuffer.fill(color.into_storage().to_be());
} else {
self.framebuffer.fill(color.into_storage());
}
Ok(())
}
}
impl OriginDimensions for DmaReadyFramebuffer {
fn size(&self) -> embedded_graphics_core::geometry::Size {
embedded_graphics_core::geometry::Size::new(self.width as u32, self.height as u32)
}
}
unsafe impl Send for DmaReadyFramebuffer {}
#[cfg(test)]
mod tests {
use super::*;
use embedded_graphics_core::prelude::*;
#[test]
fn test_framebuffer_creation() {
const WIDTH: usize = 64;
const HEIGHT: usize = 32;
let fb_le = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
assert!(!fb_le.big_endian);
let fb_be = DmaReadyFramebuffer::new(WIDTH, HEIGHT, true);
assert!(fb_be.big_endian);
}
#[test]
fn test_set_pixel() {
const WIDTH: usize = 32;
const HEIGHT: usize = 32;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let color = Bgr888::new(64, 128, 255); let point = Point::new(5, 10);
fb.set_pixel(point, color);
let value = fb.framebuffer[10 * WIDTH + 5];
let expected = color.into_storage();
assert_eq!(value, expected);
}
#[test]
fn test_set_pixel_big_endian() {
const WIDTH: usize = 32;
const HEIGHT: usize = 32;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, true);
let color = Bgr888::new(64, 128, 255); let point = Point::new(5, 10);
fb.set_pixel(point, color);
let value = fb.framebuffer[10 * WIDTH + 5];
let expected = color.into_storage().to_be();
assert_eq!(value, expected);
}
#[test]
fn test_get_pixel_roundtrip() {
const WIDTH: usize = 32;
const HEIGHT: usize = 32;
let mut fb_le = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let color = Bgr888::new(64, 128, 255);
let point = Point::new(5, 10);
fb_le.set_pixel(point, color);
assert_eq!(fb_le.get_pixel(point), Some(color));
let mut fb_be = DmaReadyFramebuffer::new(WIDTH, HEIGHT, true);
fb_be.set_pixel(point, color);
assert_eq!(fb_be.get_pixel(point), Some(color));
}
#[test]
fn test_set_pixel_out_of_bounds() {
const WIDTH: usize = 32;
const HEIGHT: usize = 32;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let color = Bgr888::new(255, 0, 0);
fb.set_pixel(Point::new(-1, 10), color);
fb.set_pixel(Point::new(WIDTH as i32, 10), color);
fb.set_pixel(Point::new(5, -1), color);
fb.set_pixel(Point::new(5, HEIGHT as i32), color);
}
#[test]
fn test_as_slice() {
const WIDTH: usize = 4;
const HEIGHT: usize = 2;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
fb.set_pixel(Point::new(0, 0), Bgr888::new(0, 0, 1)); fb.set_pixel(Point::new(1, 0), Bgr888::new(0, 0, 2)); fb.set_pixel(Point::new(0, 1), Bgr888::new(0, 0, 3));
let slice = fb.framebuffer;
assert_eq!(slice.len(), WIDTH * HEIGHT);
assert_eq!(slice[0], Bgr888::new(0, 0, 1).into_storage()); assert_eq!(slice[1], Bgr888::new(0, 0, 2).into_storage()); assert_eq!(slice[WIDTH], Bgr888::new(0, 0, 3).into_storage()); }
#[test]
fn test_as_mut_slice() {
const WIDTH: usize = 4;
const HEIGHT: usize = 2;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let slice = &mut fb.framebuffer;
assert_eq!(slice.len(), WIDTH * HEIGHT);
slice[0] = 0x00FF0000; slice[1] = 0x0000FF00;
let check_slice = fb.framebuffer;
assert_eq!(check_slice[0], 0x00FF0000);
assert_eq!(check_slice[1], 0x0000FF00);
}
#[test]
fn test_draw_target_draw_iter() {
const WIDTH: usize = 32;
const HEIGHT: usize = 32;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let pixels = [
Pixel(Point::new(1, 1), Bgr888::new(0, 0, 255)), Pixel(Point::new(2, 1), Bgr888::new(0, 255, 0)), Pixel(Point::new(3, 1), Bgr888::new(255, 0, 0)), ];
fb.draw_iter(pixels).unwrap();
let slice = fb.framebuffer;
assert_eq!(slice[1 * WIDTH + 1], Bgr888::new(0, 0, 255).into_storage());
assert_eq!(slice[1 * WIDTH + 2], Bgr888::new(0, 255, 0).into_storage());
assert_eq!(slice[1 * WIDTH + 3], Bgr888::new(255, 0, 0).into_storage());
}
#[test]
fn test_draw_target_clear() {
const WIDTH: usize = 16;
const HEIGHT: usize = 16;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
fb.set_pixel(Point::new(0, 0), Bgr888::new(255, 0, 0));
fb.set_pixel(Point::new(1, 1), Bgr888::new(0, 255, 0));
fb.clear(Bgr888::new(0, 0, 128)).unwrap();
let slice = fb.framebuffer;
let blue_value = Bgr888::new(0, 0, 128).into_storage();
for pixel in slice {
assert_eq!(pixel, blue_value);
}
}
#[test]
fn test_draw_target_clear_big_endian() {
const WIDTH: usize = 16;
const HEIGHT: usize = 16;
let mut fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, true);
let color = Bgr888::new(10, 20, 30);
fb.clear(color).unwrap();
let slice = fb.framebuffer;
let expected_value = color.into_storage().to_be();
for pixel in slice {
assert_eq!(pixel, expected_value);
}
}
#[test]
fn test_origin_dimensions() {
const WIDTH: usize = 64;
const HEIGHT: usize = 32;
let fb = DmaReadyFramebuffer::new(WIDTH, HEIGHT, false);
let size = fb.framebuffer.len();
assert_eq!(size, WIDTH * HEIGHT);
}
}