dont use mutex on framebuffer

This commit is contained in:
2025-09-11 23:39:47 -06:00
parent 718bcd6b5b
commit 49b6a99ea8
4 changed files with 54 additions and 89 deletions

View File

@@ -12,7 +12,7 @@ use embedded_graphics::{
};
use shared::keyboard::KeyEvent;
use crate::{KEY_CACHE, display::access_framebuffer_blocking};
use crate::{KEY_CACHE, display::framebuffer_mut};
// ensure the abi and the kernel fn signatures are the same
const _: PrintAbi = print;
@@ -25,17 +25,8 @@ pub extern "Rust" fn print(msg: &str) {
// TODO: maybe return result
pub extern "Rust" fn draw_iter(pixels: &[Pixel<Rgb565>]) {
loop {
if access_framebuffer_blocking(|fb| {
fb.draw_iter(pixels.iter().copied()).unwrap();
})
.is_ok()
{
break;
}
cortex_m::asm::nop();
}
let fb = framebuffer_mut();
fb.draw_iter(pixels.iter().copied()).unwrap();
}
pub extern "Rust" fn get_key() -> Option<KeyEvent> {

View File

@@ -20,24 +20,10 @@ pub const SCREEN_WIDTH: usize = 320;
pub const SCREEN_HEIGHT: usize = 320;
type FB = FrameBuffer<SCREEN_WIDTH, SCREEN_HEIGHT, { SCREEN_WIDTH * SCREEN_HEIGHT }>;
static FRAMEBUFFER_CELL: StaticCell<FB> = StaticCell::new();
static FRAMEBUFFER: Mutex<CriticalSectionRawMutex, Option<&'static mut FB>> = Mutex::new(None);
static mut FRAMEBUFFER: Option<FB> = None;
pub fn access_framebuffer_blocking(mut access: impl FnMut(&mut FB)) -> Result<(), ()> {
let mut guard = FRAMEBUFFER.try_lock().ok().ok_or(())?;
let fb = guard.as_mut().ok_or(())?;
access(fb);
Ok(())
}
pub async fn access_framebuffer(mut access: impl FnMut(&mut FB)) -> Result<(), ()> {
let mut guard = FRAMEBUFFER.lock().await;
let fb: Option<&mut &'static mut FB> = guard.as_mut();
if let Some(fb) = fb {
access(&mut *fb);
return Ok(());
}
Err(())
pub fn framebuffer_mut() -> &'static mut FB {
unsafe { FRAMEBUFFER.as_mut().unwrap() }
}
pub async fn init_display(
@@ -58,19 +44,17 @@ pub async fn init_display(
display.init().await.unwrap();
display.set_custom_orientation(0x40).await.unwrap();
let mut framebuffer = FRAMEBUFFER_CELL.init(FrameBuffer::new());
display.draw(&mut framebuffer).await.unwrap();
unsafe { FRAMEBUFFER.replace(FrameBuffer::new()) };
display.draw(framebuffer_mut()).await.unwrap();
display.set_on().await.unwrap();
FRAMEBUFFER.lock().await.replace(framebuffer);
display
}
static DISPLAYREF: StaticCell<DISPLAY> = StaticCell::new();
pub async fn display_handler(mut display: DISPLAY) {
let mut guard = FRAMEBUFFER.lock().await;
if let Some(fb) = guard.as_mut() {
let fb = framebuffer_mut();
loop {
display.partial_draw_batched(fb).await.unwrap();
embassy_time::Timer::after_millis(32).await; // 30 fps
}
}

View File

@@ -18,7 +18,7 @@ mod usb;
mod utils;
use crate::{
display::{access_framebuffer, display_handler, init_display},
display::{display_handler, framebuffer_mut, init_display},
peripherals::{
conf_peripherals,
keyboard::{KeyCode, KeyState, read_keyboard_fifo},
@@ -139,11 +139,10 @@ async fn userland_task() {
TASK_STATE_CHANGED.signal(());
}
access_framebuffer(|fb| {
{
let fb = framebuffer_mut();
fb.clear(Rgb565::BLACK).unwrap();
})
.await
.unwrap();
}
defmt::info!("running entry");
entry().await;

View File

@@ -1,6 +1,6 @@
use crate::{
BINARY_CH, TASK_STATE, TaskState,
display::{SCREEN_HEIGHT, SCREEN_WIDTH, access_framebuffer},
display::{SCREEN_HEIGHT, SCREEN_WIDTH, framebuffer_mut},
elf::load_binary,
format,
peripherals::keyboard,
@@ -102,59 +102,50 @@ async fn draw_selection() {
guard.selections.clone()
};
loop {
if access_framebuffer(|fb|{
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
let display_area = fb.bounding_box();
let fb = framebuffer_mut();
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
let display_area = fb.bounding_box();
const NO_BINS: &str = "No Programs found on SD Card. Ensure programs end with '.bin', and are located in the root directory";
let no_bins = String::from_str(NO_BINS).unwrap();
const NO_BINS: &str = "No Programs found on SD Card. Ensure programs end with '.bin', and are located in the root directory";
let no_bins = String::from_str(NO_BINS).unwrap();
if file_names.is_empty() {
TextBox::new(
&no_bins,
Rectangle::new(
Point::new(25, 25),
Size::new(display_area.size.width - 50, display_area.size.width - 50),
),
text_style,
)
.draw(fb)
.unwrap();
} else {
let mut file_names = file_names.iter();
let Some(first) = file_names.next() else {
Text::new("No Programs found on SD Card\nEnsure programs end with '.bin',\nand are located in the root directory",
if file_names.is_empty() {
TextBox::new(
&no_bins,
Rectangle::new(
Point::new(25, 25),
Size::new(display_area.size.width - 50, display_area.size.width - 50),
),
text_style,
)
.draw(fb)
.unwrap();
} else {
let mut file_names = file_names.iter();
let Some(first) = file_names.next() else {
Text::new("No Programs found on SD Card\nEnsure programs end with '.bin',\nand are located in the root directory",
Point::zero(), text_style).draw(fb).unwrap();
return;
};
let chain = Chain::new(Text::new(&first.long_name, Point::zero(), text_style));
// for _ in 0..file_names.len() {
// let chain = chain.append(Text::new(
// file_names.next().unwrap(),
// Point::zero(),
// text_style,
// ));
// }
LinearLayout::vertical(chain)
.with_alignment(horizontal::Center)
.arrange()
.align_to(&display_area, horizontal::Center, vertical::Center)
.draw(fb)
.unwrap();
return;
};
}
).await.is_ok() {
break;
}
defmt::warn!("Failed to draw selection: framebuffer unavailable");
Timer::after_micros(10).await;
}
let chain = Chain::new(Text::new(&first.long_name, Point::zero(), text_style));
// for _ in 0..file_names.len() {
// let chain = chain.append(Text::new(
// file_names.next().unwrap(),
// Point::zero(),
// text_style,
// ));
// }
LinearLayout::vertical(chain)
.with_alignment(horizontal::Center)
.arrange()
.align_to(&display_area, horizontal::Center, vertical::Center)
.draw(fb)
.unwrap();
};
}
#[derive(Clone)]