fix framebuffer crash
This commit is contained in:
@@ -24,17 +24,23 @@ pub extern "Rust" fn print(msg: &str) {
|
||||
defmt::info!("{:?}", msg);
|
||||
}
|
||||
|
||||
// TODO: maybe return result
|
||||
pub extern "Rust" fn draw_iter(pixels: &[Pixel<Rgb565>]) {
|
||||
let framebuffer = block_on(FRAMEBUFFER.lock());
|
||||
framebuffer
|
||||
.borrow_mut()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.draw_iter(pixels.iter().copied())
|
||||
.unwrap();
|
||||
for _ in 0..10 {
|
||||
if let Some(mut framebuffer) = FRAMEBUFFER.try_lock().ok() {
|
||||
for _ in 0..10 {
|
||||
// kernel takes() framebuffer
|
||||
if let Some(framebuffer) = framebuffer.as_mut() {
|
||||
framebuffer.draw_iter(pixels.iter().copied()).unwrap();
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
cortex_m::asm::nop();
|
||||
}
|
||||
}
|
||||
|
||||
pub extern "Rust" fn get_key() -> Option<KeyEvent> {
|
||||
defmt::info!("get key called");
|
||||
unsafe { KEY_CACHE.dequeue() }
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use core::cell::RefCell;
|
||||
use embassy_rp::{
|
||||
gpio::{Level, Output},
|
||||
peripherals::{PIN_13, PIN_14, PIN_15, SPI1},
|
||||
spi::{Async, Spi},
|
||||
};
|
||||
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
|
||||
use embassy_time::{Delay, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use st7365p_lcd::{FrameBuffer, ST7365P};
|
||||
@@ -22,8 +21,7 @@ pub const SCREEN_HEIGHT: usize = 320;
|
||||
|
||||
type FB = FrameBuffer<SCREEN_WIDTH, SCREEN_HEIGHT, { SCREEN_WIDTH * SCREEN_HEIGHT }>;
|
||||
static FRAMEBUFFER_CELL: StaticCell<FB> = StaticCell::new();
|
||||
pub static FRAMEBUFFER: Mutex<ThreadModeRawMutex, RefCell<Option<&'static mut FB>>> =
|
||||
Mutex::new(RefCell::new(None));
|
||||
pub static FRAMEBUFFER: Mutex<CriticalSectionRawMutex, Option<&'static mut FB>> = Mutex::new(None);
|
||||
|
||||
pub async fn init_display(
|
||||
spi: Spi<'static, SPI1, Async>,
|
||||
@@ -45,25 +43,23 @@ pub async fn init_display(
|
||||
display.set_custom_orientation(0x40).await.unwrap();
|
||||
framebuffer.draw(&mut display).await.unwrap();
|
||||
display.set_on().await.unwrap();
|
||||
FRAMEBUFFER
|
||||
.lock()
|
||||
.await
|
||||
.swap(&RefCell::new(Some(framebuffer)));
|
||||
FRAMEBUFFER.lock().await.replace(framebuffer);
|
||||
|
||||
display
|
||||
}
|
||||
|
||||
pub async fn display_handler(mut display: DISPLAY) {
|
||||
loop {
|
||||
FRAMEBUFFER
|
||||
.lock()
|
||||
.await
|
||||
.borrow_mut()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.partial_draw_batched(&mut display)
|
||||
.await
|
||||
.unwrap();
|
||||
let fb: &mut FB = {
|
||||
let mut guard = FRAMEBUFFER.lock().await;
|
||||
guard.take().unwrap() // take ownership
|
||||
}; // guard dropped
|
||||
|
||||
fb.partial_draw_batched(&mut display).await.unwrap();
|
||||
|
||||
// Put it back
|
||||
FRAMEBUFFER.lock().await.replace(fb);
|
||||
|
||||
Timer::after_millis(32).await; // 30 fps
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,8 +181,6 @@ async fn kernel_task(display: Display, sd: Sd, mcu: Mcu, usb: USB) {
|
||||
);
|
||||
let display = init_display(spi, display.cs, display.data, display.reset).await;
|
||||
|
||||
DRIVERS_READY.signal(());
|
||||
|
||||
let display_fut = display_handler(display);
|
||||
|
||||
{
|
||||
@@ -204,6 +202,7 @@ async fn kernel_task(display: Display, sd: Sd, mcu: Mcu, usb: USB) {
|
||||
let usb_fut = usb_handler(usb);
|
||||
|
||||
ENABLE_SCSI.store(true, core::sync::atomic::Ordering::Relaxed);
|
||||
DRIVERS_READY.signal(());
|
||||
join3(usb_fut, display_fut, async {
|
||||
loop {
|
||||
Timer::after_millis(100).await;
|
||||
|
||||
@@ -51,6 +51,9 @@ impl<const MAX_SELECTIONS: usize, const MAX_STR_LEN: usize> UI<MAX_SELECTIONS, M
|
||||
}
|
||||
|
||||
async fn draw_selection(&mut self) {
|
||||
let mut fb_lock = FRAMEBUFFER.lock().await;
|
||||
let fb = fb_lock.as_mut().unwrap();
|
||||
|
||||
let text_style = MonoTextStyle::new(&FONT_9X15, Rgb565::WHITE);
|
||||
|
||||
let selection = Rectangle::new(
|
||||
@@ -62,7 +65,7 @@ impl<const MAX_SELECTIONS: usize, const MAX_STR_LEN: usize> UI<MAX_SELECTIONS, M
|
||||
|
||||
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(*FRAMEBUFFER.lock().await.borrow_mut().as_mut().unwrap()).unwrap();
|
||||
Point::zero(), text_style).draw(*fb).unwrap();
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -72,18 +75,8 @@ impl<const MAX_SELECTIONS: usize, const MAX_STR_LEN: usize> UI<MAX_SELECTIONS, M
|
||||
LinearLayout::vertical(chain)
|
||||
.with_alignment(horizontal::Center)
|
||||
.arrange()
|
||||
.align_to(
|
||||
&FRAMEBUFFER
|
||||
.lock()
|
||||
.await
|
||||
.borrow_mut()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.bounding_box(),
|
||||
horizontal::Center,
|
||||
vertical::Center,
|
||||
)
|
||||
.draw(*FRAMEBUFFER.lock().await.borrow_mut().as_mut().unwrap())
|
||||
.align_to(&fb.bounding_box(), horizontal::Center, vertical::Center)
|
||||
.draw(*fb)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user