mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-27 07:45:28 +00:00
dont use mutex on framebuffer
This commit is contained in:
@@ -12,7 +12,7 @@ use embedded_graphics::{
|
|||||||
};
|
};
|
||||||
use shared::keyboard::KeyEvent;
|
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
|
// ensure the abi and the kernel fn signatures are the same
|
||||||
const _: PrintAbi = print;
|
const _: PrintAbi = print;
|
||||||
@@ -25,17 +25,8 @@ pub extern "Rust" fn print(msg: &str) {
|
|||||||
|
|
||||||
// TODO: maybe return result
|
// TODO: maybe return result
|
||||||
pub extern "Rust" fn draw_iter(pixels: &[Pixel<Rgb565>]) {
|
pub extern "Rust" fn draw_iter(pixels: &[Pixel<Rgb565>]) {
|
||||||
loop {
|
let fb = framebuffer_mut();
|
||||||
if access_framebuffer_blocking(|fb| {
|
fb.draw_iter(pixels.iter().copied()).unwrap();
|
||||||
fb.draw_iter(pixels.iter().copied()).unwrap();
|
|
||||||
})
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
cortex_m::asm::nop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub extern "Rust" fn get_key() -> Option<KeyEvent> {
|
pub extern "Rust" fn get_key() -> Option<KeyEvent> {
|
||||||
|
|||||||
@@ -20,24 +20,10 @@ pub const SCREEN_WIDTH: usize = 320;
|
|||||||
pub const SCREEN_HEIGHT: usize = 320;
|
pub const SCREEN_HEIGHT: usize = 320;
|
||||||
|
|
||||||
type FB = FrameBuffer<SCREEN_WIDTH, SCREEN_HEIGHT, { SCREEN_WIDTH * SCREEN_HEIGHT }>;
|
type FB = FrameBuffer<SCREEN_WIDTH, SCREEN_HEIGHT, { SCREEN_WIDTH * SCREEN_HEIGHT }>;
|
||||||
static FRAMEBUFFER_CELL: StaticCell<FB> = StaticCell::new();
|
static mut FRAMEBUFFER: Option<FB> = None;
|
||||||
static FRAMEBUFFER: Mutex<CriticalSectionRawMutex, Option<&'static mut FB>> = Mutex::new(None);
|
|
||||||
|
|
||||||
pub fn access_framebuffer_blocking(mut access: impl FnMut(&mut FB)) -> Result<(), ()> {
|
pub fn framebuffer_mut() -> &'static mut FB {
|
||||||
let mut guard = FRAMEBUFFER.try_lock().ok().ok_or(())?;
|
unsafe { FRAMEBUFFER.as_mut().unwrap() }
|
||||||
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 async fn init_display(
|
pub async fn init_display(
|
||||||
@@ -58,19 +44,17 @@ pub async fn init_display(
|
|||||||
|
|
||||||
display.init().await.unwrap();
|
display.init().await.unwrap();
|
||||||
display.set_custom_orientation(0x40).await.unwrap();
|
display.set_custom_orientation(0x40).await.unwrap();
|
||||||
let mut framebuffer = FRAMEBUFFER_CELL.init(FrameBuffer::new());
|
unsafe { FRAMEBUFFER.replace(FrameBuffer::new()) };
|
||||||
display.draw(&mut framebuffer).await.unwrap();
|
display.draw(framebuffer_mut()).await.unwrap();
|
||||||
display.set_on().await.unwrap();
|
display.set_on().await.unwrap();
|
||||||
FRAMEBUFFER.lock().await.replace(framebuffer);
|
|
||||||
|
|
||||||
display
|
display
|
||||||
}
|
}
|
||||||
|
|
||||||
static DISPLAYREF: StaticCell<DISPLAY> = StaticCell::new();
|
|
||||||
|
|
||||||
pub async fn display_handler(mut display: DISPLAY) {
|
pub async fn display_handler(mut display: DISPLAY) {
|
||||||
let mut guard = FRAMEBUFFER.lock().await;
|
let fb = framebuffer_mut();
|
||||||
if let Some(fb) = guard.as_mut() {
|
loop {
|
||||||
display.partial_draw_batched(fb).await.unwrap();
|
display.partial_draw_batched(fb).await.unwrap();
|
||||||
|
embassy_time::Timer::after_millis(32).await; // 30 fps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ mod usb;
|
|||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{access_framebuffer, display_handler, init_display},
|
display::{display_handler, framebuffer_mut, init_display},
|
||||||
peripherals::{
|
peripherals::{
|
||||||
conf_peripherals,
|
conf_peripherals,
|
||||||
keyboard::{KeyCode, KeyState, read_keyboard_fifo},
|
keyboard::{KeyCode, KeyState, read_keyboard_fifo},
|
||||||
@@ -139,11 +139,10 @@ async fn userland_task() {
|
|||||||
TASK_STATE_CHANGED.signal(());
|
TASK_STATE_CHANGED.signal(());
|
||||||
}
|
}
|
||||||
|
|
||||||
access_framebuffer(|fb| {
|
{
|
||||||
|
let fb = framebuffer_mut();
|
||||||
fb.clear(Rgb565::BLACK).unwrap();
|
fb.clear(Rgb565::BLACK).unwrap();
|
||||||
})
|
}
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
defmt::info!("running entry");
|
defmt::info!("running entry");
|
||||||
entry().await;
|
entry().await;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
BINARY_CH, TASK_STATE, TaskState,
|
BINARY_CH, TASK_STATE, TaskState,
|
||||||
display::{SCREEN_HEIGHT, SCREEN_WIDTH, access_framebuffer},
|
display::{SCREEN_HEIGHT, SCREEN_WIDTH, framebuffer_mut},
|
||||||
elf::load_binary,
|
elf::load_binary,
|
||||||
format,
|
format,
|
||||||
peripherals::keyboard,
|
peripherals::keyboard,
|
||||||
@@ -102,59 +102,50 @@ async fn draw_selection() {
|
|||||||
guard.selections.clone()
|
guard.selections.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
let fb = framebuffer_mut();
|
||||||
if access_framebuffer(|fb|{
|
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
|
||||||
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
|
let display_area = fb.bounding_box();
|
||||||
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";
|
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();
|
let no_bins = String::from_str(NO_BINS).unwrap();
|
||||||
|
|
||||||
if file_names.is_empty() {
|
if file_names.is_empty() {
|
||||||
TextBox::new(
|
TextBox::new(
|
||||||
&no_bins,
|
&no_bins,
|
||||||
Rectangle::new(
|
Rectangle::new(
|
||||||
Point::new(25, 25),
|
Point::new(25, 25),
|
||||||
Size::new(display_area.size.width - 50, display_area.size.width - 50),
|
Size::new(display_area.size.width - 50, display_area.size.width - 50),
|
||||||
),
|
),
|
||||||
text_style,
|
text_style,
|
||||||
)
|
)
|
||||||
.draw(fb)
|
.draw(fb)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
let mut file_names = file_names.iter();
|
let mut file_names = file_names.iter();
|
||||||
let Some(first) = file_names.next() else {
|
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",
|
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();
|
Point::zero(), text_style).draw(fb).unwrap();
|
||||||
|
|
||||||
return;
|
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();
|
|
||||||
};
|
};
|
||||||
}
|
|
||||||
).await.is_ok() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
defmt::warn!("Failed to draw selection: framebuffer unavailable");
|
let chain = Chain::new(Text::new(&first.long_name, Point::zero(), text_style));
|
||||||
Timer::after_micros(10).await;
|
|
||||||
}
|
// 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)]
|
#[derive(Clone)]
|
||||||
|
|||||||
Reference in New Issue
Block a user