53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use embedded_graphics::{
|
|
pixelcolor::Rgb888,
|
|
prelude::{DrawTarget, IntoStorage, OriginDimensions, Size},
|
|
Pixel,
|
|
};
|
|
|
|
pub struct Display {
|
|
fb: *mut u32,
|
|
size: Size,
|
|
}
|
|
|
|
unsafe impl Send for Display {}
|
|
impl DrawTarget for Display {
|
|
type Color = Rgb888;
|
|
type Error = BlitOutOfBoundsError;
|
|
|
|
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
|
where
|
|
I: IntoIterator<Item = Pixel<Self::Color>>,
|
|
{
|
|
for Pixel(pos, rgb) in pixels {
|
|
let pos_x = u32::try_from(pos.x).map_err(|_| BlitOutOfBoundsError)?;
|
|
let pos_y = u32::try_from(pos.y).map_err(|_| BlitOutOfBoundsError)?;
|
|
|
|
unsafe {
|
|
if pos_x >= self.size.width || pos_y >= self.size.height {
|
|
return Err(BlitOutOfBoundsError);
|
|
}
|
|
|
|
self.fb
|
|
.add(
|
|
(pos_y * self.size.width + pos_x)
|
|
.try_into()
|
|
.map_err(|_| BlitOutOfBoundsError)?,
|
|
)
|
|
.write_volatile(rgb.into_storage());
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl OriginDimensions for Display {
|
|
#[inline]
|
|
fn size(&self) -> Size {
|
|
self.size
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct BlitOutOfBoundsError;
|