userlib now implements embedded_graphics::DrawTarget

This commit is contained in:
2025-08-27 17:12:51 -06:00
parent 64254831e8
commit f3c67beb00
9 changed files with 141 additions and 42 deletions

View File

@@ -6,3 +6,4 @@ edition = "2024"
[dependencies]
embedded-graphics = "0.8.1"
shared = { path = "../shared" }
abi_sys = { path = "../abi_sys" }

View File

@@ -1,28 +1,76 @@
#![no_std]
pub use embedded_graphics::{
Pixel,
geometry::Point,
pixelcolor::{Rgb565, RgbColor},
};
use abi_sys::{Syscall, call_abi};
use shared::keyboard::{KeyCode, KeyEvent, KeyState, Modifiers};
// Instead of extern, declare a static pointer in a dedicated section
#[unsafe(no_mangle)]
#[unsafe(link_section = ".user_reloc")]
#[allow(non_upper_case_globals)]
pub static mut call_abi_ptr: usize = 0;
pub mod display {
use crate::{Syscall, call_abi};
use embedded_graphics::{
Pixel,
geometry::{Dimensions, Point},
pixelcolor::{Rgb565, RgbColor},
prelude::{DrawTarget, Size},
primitives::Rectangle,
};
// Helper to call it
pub unsafe fn call_abi(call: *const Syscall) {
let f: extern "C" fn(*const Syscall) = unsafe { core::mem::transmute(call_abi_ptr) };
f(call);
}
pub const SCREEN_WIDTH: usize = 320;
pub const SCREEN_HEIGHT: usize = 320;
#[repr(C)]
pub enum Syscall {
DrawIter {
pixels: *const Pixel<Rgb565>,
len: usize,
},
pub type Pixel565 = Pixel<Rgb565>;
pub struct Display;
impl Display {
fn syscall_draw(&self, pixels: &[Pixel565]) {
let syscall = Syscall::DrawIter {
pixels: pixels.as_ptr(),
len: pixels.len(),
};
unsafe {
call_abi(&syscall);
}
}
}
impl Dimensions for Display {
fn bounding_box(&self) -> Rectangle {
Rectangle {
top_left: Point { x: 0, y: 0 },
size: Size {
width: SCREEN_WIDTH as u32,
height: SCREEN_HEIGHT as u32,
},
}
}
}
impl DrawTarget for Display {
type Color = Rgb565;
type Error = ();
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
const BUF_SIZE: usize = 1024; // tune this for performance
let mut buf: [Pixel565; BUF_SIZE] = [Pixel(Point::new(0, 0), Rgb565::BLACK); BUF_SIZE];
let mut count = 0;
for p in pixels {
buf[count] = p;
count += 1;
if count == BUF_SIZE {
self.syscall_draw(&buf[..count]);
count = 0;
}
}
if count > 0 {
self.syscall_draw(&buf[..count]);
}
Ok(())
}
}
}