mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-27 15:55:25 +00:00
userlib now implements embedded_graphics::DrawTarget
This commit is contained in:
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -15,6 +15,15 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "abi"
|
name = "abi"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"abi_sys",
|
||||||
|
"embedded-graphics",
|
||||||
|
"shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "abi_sys"
|
||||||
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"embedded-graphics",
|
"embedded-graphics",
|
||||||
"shared",
|
"shared",
|
||||||
@@ -208,6 +217,7 @@ name = "calculator"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"abi",
|
"abi",
|
||||||
|
"embedded-graphics",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1163,7 +1173,7 @@ dependencies = [
|
|||||||
name = "kernel"
|
name = "kernel"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"abi",
|
"abi_sys",
|
||||||
"bitflags 2.9.1",
|
"bitflags 2.9.1",
|
||||||
"bt-hci",
|
"bt-hci",
|
||||||
"cortex-m",
|
"cortex-m",
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
embedded-graphics = "0.8.1"
|
embedded-graphics = "0.8.1"
|
||||||
shared = { path = "../shared" }
|
shared = { path = "../shared" }
|
||||||
|
abi_sys = { path = "../abi_sys" }
|
||||||
|
|||||||
@@ -1,28 +1,76 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
pub use embedded_graphics::{
|
use abi_sys::{Syscall, call_abi};
|
||||||
Pixel,
|
|
||||||
geometry::Point,
|
|
||||||
pixelcolor::{Rgb565, RgbColor},
|
|
||||||
};
|
|
||||||
use shared::keyboard::{KeyCode, KeyEvent, KeyState, Modifiers};
|
use shared::keyboard::{KeyCode, KeyEvent, KeyState, Modifiers};
|
||||||
|
|
||||||
// Instead of extern, declare a static pointer in a dedicated section
|
pub mod display {
|
||||||
#[unsafe(no_mangle)]
|
use crate::{Syscall, call_abi};
|
||||||
#[unsafe(link_section = ".user_reloc")]
|
use embedded_graphics::{
|
||||||
#[allow(non_upper_case_globals)]
|
Pixel,
|
||||||
pub static mut call_abi_ptr: usize = 0;
|
geometry::{Dimensions, Point},
|
||||||
|
pixelcolor::{Rgb565, RgbColor},
|
||||||
|
prelude::{DrawTarget, Size},
|
||||||
|
primitives::Rectangle,
|
||||||
|
};
|
||||||
|
|
||||||
// Helper to call it
|
pub const SCREEN_WIDTH: usize = 320;
|
||||||
pub unsafe fn call_abi(call: *const Syscall) {
|
pub const SCREEN_HEIGHT: usize = 320;
|
||||||
let f: extern "C" fn(*const Syscall) = unsafe { core::mem::transmute(call_abi_ptr) };
|
|
||||||
f(call);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
pub type Pixel565 = Pixel<Rgb565>;
|
||||||
pub enum Syscall {
|
|
||||||
DrawIter {
|
pub struct Display;
|
||||||
pixels: *const Pixel<Rgb565>,
|
|
||||||
len: usize,
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
abi_sys/Cargo.toml
Normal file
8
abi_sys/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "abi_sys"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
shared = { path = "../shared" }
|
||||||
28
abi_sys/src/lib.rs
Normal file
28
abi_sys/src/lib.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#![no_std]
|
||||||
|
|
||||||
|
pub use embedded_graphics::{
|
||||||
|
Pixel,
|
||||||
|
geometry::Point,
|
||||||
|
pixelcolor::{Rgb565, RgbColor},
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub enum Syscall {
|
||||||
|
DrawIter {
|
||||||
|
pixels: *const Pixel<Rgb565>,
|
||||||
|
len: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -87,4 +87,4 @@ talc = "4.4.3"
|
|||||||
spin = "0.10.0"
|
spin = "0.10.0"
|
||||||
|
|
||||||
shared = { path = "../shared" }
|
shared = { path = "../shared" }
|
||||||
abi = { path = "../abi" }
|
abi_sys = { path = "../abi_sys" }
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use abi::Syscall;
|
use abi_sys::Syscall;
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_futures::block_on;
|
use embassy_futures::block_on;
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
abi = { path = "../../abi" }
|
abi = { path = "../../abi" }
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use abi::{Pixel, Point, Rgb565, RgbColor, Syscall, call_abi};
|
use abi::display::Display;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
use embedded_graphics::{
|
||||||
|
Drawable,
|
||||||
|
geometry::{Dimensions, Point},
|
||||||
|
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
||||||
|
pixelcolor::Rgb565,
|
||||||
|
prelude::RgbColor,
|
||||||
|
text::{Alignment, Text},
|
||||||
|
};
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_info: &PanicInfo) -> ! {
|
fn panic(_info: &PanicInfo) -> ! {
|
||||||
@@ -11,23 +19,18 @@ fn panic(_info: &PanicInfo) -> ! {
|
|||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn _start() {
|
pub extern "C" fn _start() {
|
||||||
// Local pixel buffer
|
let mut display = Display;
|
||||||
let mut pixels = [Pixel(Point { x: 50, y: 50 }, Rgb565::RED); 300];
|
|
||||||
for (i, p) in pixels.iter_mut().enumerate() {
|
|
||||||
*p = Pixel(
|
|
||||||
Point {
|
|
||||||
x: i as i32,
|
|
||||||
y: i as i32,
|
|
||||||
},
|
|
||||||
Rgb565::RED,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct syscall with raw pointer + length
|
let character_style = MonoTextStyle::new(&FONT_6X10, Rgb565::RED);
|
||||||
let call = Syscall::DrawIter {
|
|
||||||
pixels: pixels.as_ptr(), // raw pointer
|
|
||||||
len: pixels.len(), // number of elements
|
|
||||||
};
|
|
||||||
|
|
||||||
unsafe { call_abi(&call) };
|
// Draw centered text.
|
||||||
|
let text = "embedded-graphics";
|
||||||
|
Text::with_alignment(
|
||||||
|
text,
|
||||||
|
display.bounding_box().center() + Point::new(0, 15),
|
||||||
|
character_style,
|
||||||
|
Alignment::Center,
|
||||||
|
)
|
||||||
|
.draw(&mut display)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user