mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-28 08:15:59 +00:00
Compare commits
7 Commits
doom
...
selection-
| Author | SHA1 | Date | |
|---|---|---|---|
| 513042b561 | |||
| 375b5d1d47 | |||
| dd26e3528b | |||
| 74ec86da8f | |||
| cc463a34be | |||
| 355be5cde3 | |||
| 1bbb892fcc |
13
Cargo.lock
generated
13
Cargo.lock
generated
@@ -18,7 +18,6 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"abi_sys",
|
"abi_sys",
|
||||||
"embedded-graphics",
|
"embedded-graphics",
|
||||||
"embedded-sdmmc",
|
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rand_core 0.9.3",
|
"rand_core 0.9.3",
|
||||||
]
|
]
|
||||||
@@ -31,7 +30,6 @@ dependencies = [
|
|||||||
"cbindgen",
|
"cbindgen",
|
||||||
"defmt 0.3.100",
|
"defmt 0.3.100",
|
||||||
"embedded-graphics",
|
"embedded-graphics",
|
||||||
"embedded-sdmmc",
|
|
||||||
"strum",
|
"strum",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1289,6 +1287,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"abi",
|
"abi",
|
||||||
"embedded-graphics",
|
"embedded-graphics",
|
||||||
|
"selection_ui",
|
||||||
"tinygif",
|
"tinygif",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2191,6 +2190,16 @@ version = "0.13.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1257cd4248b4132760d6524d6dda4e053bc648c9070b960929bf50cfb1e7add"
|
checksum = "c1257cd4248b4132760d6524d6dda4e053bc648c9070b960929bf50cfb1e7add"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "selection_ui"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"abi",
|
||||||
|
"embedded-graphics",
|
||||||
|
"embedded-layout",
|
||||||
|
"embedded-text",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "semver"
|
name = "semver"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ members = [
|
|||||||
"kernel",
|
"kernel",
|
||||||
"abi_sys",
|
"abi_sys",
|
||||||
"abi",
|
"abi",
|
||||||
|
"selection_ui",
|
||||||
"user-apps/calculator",
|
"user-apps/calculator",
|
||||||
"user-apps/snake",
|
"user-apps/snake",
|
||||||
"user-apps/gallery",
|
"user-apps/gallery",
|
||||||
@@ -26,4 +27,4 @@ panic = "abort"
|
|||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
lto = true
|
lto = true
|
||||||
opt-level = "z"
|
opt-level = "s"
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embedded-sdmmc = { version = "0.9.0", default-features = false }
|
|
||||||
embedded-graphics = "0.8.1"
|
|
||||||
abi_sys = { path = "../abi_sys" }
|
abi_sys = { path = "../abi_sys" }
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
once_cell = { version = "1", default-features = false }
|
once_cell = { version = "1", default-features = false }
|
||||||
rand_core = "0.9.3"
|
rand_core = "0.9.3"
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ pub fn get_key() -> KeyEvent {
|
|||||||
|
|
||||||
pub mod display {
|
pub mod display {
|
||||||
use abi_sys::CPixel;
|
use abi_sys::CPixel;
|
||||||
use alloc::{vec, vec::Vec};
|
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
Pixel,
|
Pixel,
|
||||||
geometry::{Dimensions, Point},
|
geometry::{Dimensions, Point},
|
||||||
@@ -54,7 +53,6 @@ pub mod display {
|
|||||||
prelude::{DrawTarget, Size},
|
prelude::{DrawTarget, Size},
|
||||||
primitives::Rectangle,
|
primitives::Rectangle,
|
||||||
};
|
};
|
||||||
use once_cell::unsync::Lazy;
|
|
||||||
|
|
||||||
pub const SCREEN_WIDTH: usize = 320;
|
pub const SCREEN_WIDTH: usize = 320;
|
||||||
pub const SCREEN_HEIGHT: usize = 320;
|
pub const SCREEN_HEIGHT: usize = 320;
|
||||||
@@ -142,7 +140,9 @@ impl RngCore for Rng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod fs {
|
pub mod fs {
|
||||||
use embedded_sdmmc::DirEntry;
|
use core::fmt::Display;
|
||||||
|
|
||||||
|
use alloc::{format, vec::Vec};
|
||||||
|
|
||||||
pub fn read_file(file: &str, read_from: usize, buf: &mut [u8]) -> usize {
|
pub fn read_file(file: &str, read_from: usize, buf: &mut [u8]) -> usize {
|
||||||
abi_sys::read_file(
|
abi_sys::read_file(
|
||||||
@@ -154,8 +154,85 @@ pub mod fs {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_dir(path: &str, files: &mut [Option<DirEntry>]) -> usize {
|
pub struct FileName<'a> {
|
||||||
abi_sys::list_dir(path.as_ptr(), path.len(), files.as_mut_ptr(), files.len())
|
full: &'a str,
|
||||||
|
base: &'a str,
|
||||||
|
ext: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> FileName<'a> {
|
||||||
|
pub fn full_name(&self) -> &str {
|
||||||
|
self.full
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn base(&self) -> &str {
|
||||||
|
self.base
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extension(&self) -> Option<&str> {
|
||||||
|
self.ext
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Display for FileName<'a> {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "{}", self.full_name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for FileName<'a> {
|
||||||
|
fn from(s: &'a str) -> FileName<'a> {
|
||||||
|
let full = s;
|
||||||
|
|
||||||
|
// Split on last dot for extension
|
||||||
|
let (base, ext) = match s.rfind('.') {
|
||||||
|
Some(idx) => (&s[..idx], Some(&s[idx + 1..])),
|
||||||
|
None => (s, None),
|
||||||
|
};
|
||||||
|
|
||||||
|
FileName { full, base, ext }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_ENTRY_NAME_LEN: usize = 25;
|
||||||
|
const MAX_ENTRIES: usize = 25;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct Entries([[u8; MAX_ENTRY_NAME_LEN]; MAX_ENTRIES]);
|
||||||
|
|
||||||
|
impl Entries {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self([[0; MAX_ENTRY_NAME_LEN]; MAX_ENTRIES])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get list of file names after listing
|
||||||
|
pub fn entries<'a>(&'a self) -> Vec<FileName<'a>> {
|
||||||
|
self.0
|
||||||
|
.iter()
|
||||||
|
.filter_map(|buf| {
|
||||||
|
let nul_pos = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
|
||||||
|
Some(core::str::from_utf8(&buf[..nul_pos]).ok()?.into())
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_ptrs(&mut self) -> [*mut u8; MAX_ENTRIES] {
|
||||||
|
let mut ptrs: [*mut u8; MAX_ENTRIES] = [core::ptr::null_mut(); MAX_ENTRIES];
|
||||||
|
for (i, buf) in self.0.iter_mut().enumerate() {
|
||||||
|
ptrs[i] = buf.as_mut_ptr();
|
||||||
|
}
|
||||||
|
ptrs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_dir(path: &str, entries: &mut Entries) -> usize {
|
||||||
|
abi_sys::list_dir(
|
||||||
|
path.as_ptr(),
|
||||||
|
path.len(),
|
||||||
|
entries.as_ptrs().as_mut_ptr(),
|
||||||
|
MAX_ENTRIES,
|
||||||
|
MAX_ENTRY_NAME_LEN,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_len(str: &str) -> usize {
|
pub fn file_len(str: &str) -> usize {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ defmt = ["dep:defmt"]
|
|||||||
strum = { version = "0.27.2", default-features = false, features = ["derive"] }
|
strum = { version = "0.27.2", default-features = false, features = ["derive"] }
|
||||||
bitflags = "2.9.4"
|
bitflags = "2.9.4"
|
||||||
embedded-graphics = "0.8.1"
|
embedded-graphics = "0.8.1"
|
||||||
embedded-sdmmc = { version = "0.9.0", default-features = false }
|
|
||||||
defmt = { version = "0.3", optional = true }
|
defmt = { version = "0.3", optional = true }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
use core::alloc::Layout;
|
use core::alloc::Layout;
|
||||||
|
|
||||||
|
use core::ffi::c_char;
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
Pixel,
|
Pixel,
|
||||||
pixelcolor::{Rgb565, raw::RawU16},
|
pixelcolor::{Rgb565, raw::RawU16},
|
||||||
prelude::{IntoStorage, Point},
|
prelude::{IntoStorage, Point},
|
||||||
};
|
};
|
||||||
use embedded_sdmmc::DirEntry;
|
|
||||||
use strum::{EnumCount, EnumIter};
|
use strum::{EnumCount, EnumIter};
|
||||||
|
|
||||||
pub type EntryFn = fn();
|
pub type EntryFn = fn();
|
||||||
@@ -396,21 +395,23 @@ pub extern "C" fn gen_rand(req: &mut RngRequest) {
|
|||||||
pub type ListDir = extern "C" fn(
|
pub type ListDir = extern "C" fn(
|
||||||
str: *const u8,
|
str: *const u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
files: *mut Option<DirEntry>,
|
entries: *mut *mut c_char,
|
||||||
file_len: usize,
|
file_len: usize,
|
||||||
|
max_entry_str_len: usize,
|
||||||
) -> usize;
|
) -> usize;
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn list_dir(
|
pub extern "C" fn list_dir(
|
||||||
str: *const u8,
|
str: *const u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
files: *mut Option<DirEntry>,
|
entries: *mut *mut c_char,
|
||||||
file_len: usize,
|
entry_count: usize,
|
||||||
|
max_entry_str_len: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = CALL_ABI_TABLE[CallTable::ListDir as usize];
|
let ptr = CALL_ABI_TABLE[CallTable::ListDir as usize];
|
||||||
let f: ListDir = core::mem::transmute(ptr);
|
let f: ListDir = core::mem::transmute(ptr);
|
||||||
f(str, len, files, file_len)
|
f(str, len, entries, entry_count, max_entry_str_len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ use abi_sys::{
|
|||||||
PrintAbi, ReadFile, RngRequest, SleepMsAbi, keyboard::*,
|
PrintAbi, ReadFile, RngRequest, SleepMsAbi, keyboard::*,
|
||||||
};
|
};
|
||||||
use alloc::{string::ToString, vec::Vec};
|
use alloc::{string::ToString, vec::Vec};
|
||||||
use core::{alloc::GlobalAlloc, sync::atomic::Ordering};
|
use core::{alloc::GlobalAlloc, ffi::c_char, ptr, sync::atomic::Ordering};
|
||||||
use embassy_rp::clocks::{RoscRng, clk_sys_freq};
|
use embassy_rp::clocks::{RoscRng, clk_sys_freq};
|
||||||
use embassy_time::Instant;
|
use embassy_time::Instant;
|
||||||
use embedded_graphics::draw_target::DrawTarget;
|
use embedded_graphics::draw_target::DrawTarget;
|
||||||
use embedded_sdmmc::{DirEntry, LfnBuffer};
|
use embedded_sdmmc::LfnBuffer;
|
||||||
use heapless::spsc::Queue;
|
use heapless::spsc::Queue;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -116,11 +116,27 @@ pub extern "C" fn gen_rand(req: &mut RngRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_dir_entries(dir: &Dir, files: &mut [Option<DirEntry>]) -> usize {
|
unsafe fn copy_entry_to_user_buf(name: &[u8], dest: *mut c_char, max_str_len: usize) {
|
||||||
|
if !dest.is_null() {
|
||||||
|
let len = name.len().min(max_str_len - 1);
|
||||||
|
unsafe {
|
||||||
|
ptr::copy_nonoverlapping(name.as_ptr(), dest as *mut u8, len);
|
||||||
|
*dest.add(len) = 0; // nul terminator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn get_dir_entries(dir: &Dir, entries: &mut [*mut c_char], max_str_len: usize) -> usize {
|
||||||
|
let mut b = [0; 25];
|
||||||
|
let mut buf = LfnBuffer::new(&mut b);
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
dir.iterate_dir(|entry| {
|
dir.iterate_dir_lfn(&mut buf, |entry, lfn_name| {
|
||||||
if i < files.len() {
|
if i < entries.len() {
|
||||||
files[i] = Some(entry.clone());
|
if let Some(name) = lfn_name {
|
||||||
|
unsafe { copy_entry_to_user_buf(name.as_bytes(), entries[i], max_str_len) };
|
||||||
|
} else {
|
||||||
|
unsafe { copy_entry_to_user_buf(entry.name.base_name(), entries[i], max_str_len) };
|
||||||
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -128,24 +144,30 @@ fn get_dir_entries(dir: &Dir, files: &mut [Option<DirEntry>]) -> usize {
|
|||||||
i
|
i
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recurse_dir(dir: &Dir, dirs: &[&str], files: &mut [Option<DirEntry>]) -> usize {
|
unsafe fn recurse_dir(
|
||||||
|
dir: &Dir,
|
||||||
|
dirs: &[&str],
|
||||||
|
entries: &mut [*mut c_char],
|
||||||
|
max_str_len: usize,
|
||||||
|
) -> usize {
|
||||||
if dirs.is_empty() {
|
if dirs.is_empty() {
|
||||||
return get_dir_entries(dir, files);
|
return unsafe { get_dir_entries(dir, entries, max_str_len) };
|
||||||
}
|
}
|
||||||
|
|
||||||
let dir = dir.open_dir(dirs[0]).unwrap();
|
let dir = dir.open_dir(dirs[0]).unwrap();
|
||||||
recurse_dir(&dir, &dirs[1..], files)
|
unsafe { recurse_dir(&dir, &dirs[1..], entries, max_str_len) }
|
||||||
}
|
}
|
||||||
|
|
||||||
const _: ListDir = list_dir;
|
const _: ListDir = list_dir;
|
||||||
pub extern "C" fn list_dir(
|
pub extern "C" fn list_dir(
|
||||||
dir: *const u8,
|
dir: *const u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
files: *mut Option<DirEntry>,
|
entries: *mut *mut c_char,
|
||||||
files_len: usize,
|
files_len: usize,
|
||||||
|
max_entry_str_len: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
// SAFETY: caller guarantees `ptr` is valid for `len` bytes
|
// SAFETY: caller guarantees `ptr` is valid for `len` bytes
|
||||||
let files = unsafe { core::slice::from_raw_parts_mut(files, files_len) };
|
let files = unsafe { core::slice::from_raw_parts_mut(entries, files_len) };
|
||||||
// SAFETY: caller guarantees `ptr` is valid for `len` bytes
|
// SAFETY: caller guarantees `ptr` is valid for `len` bytes
|
||||||
let dir = unsafe { core::str::from_raw_parts(dir, len) };
|
let dir = unsafe { core::str::from_raw_parts(dir, len) };
|
||||||
let dirs: Vec<&str> = dir.split('/').collect();
|
let dirs: Vec<&str> = dir.split('/').collect();
|
||||||
@@ -156,10 +178,12 @@ pub extern "C" fn list_dir(
|
|||||||
let mut wrote = 0;
|
let mut wrote = 0;
|
||||||
sd.access_root_dir(|root| {
|
sd.access_root_dir(|root| {
|
||||||
if dirs[0] == "" && dirs.len() >= 2 {
|
if dirs[0] == "" && dirs.len() >= 2 {
|
||||||
|
unsafe {
|
||||||
if dir == "/" {
|
if dir == "/" {
|
||||||
wrote = get_dir_entries(&root, files);
|
wrote = get_dir_entries(&root, files, max_entry_str_len);
|
||||||
} else {
|
} else {
|
||||||
wrote = recurse_dir(&root, &dirs[1..], files);
|
wrote = recurse_dir(&root, &dirs[1..], files, max_entry_str_len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ pub async fn display_handler(mut display: DISPLAY) {
|
|||||||
FRAMEBUFFER
|
FRAMEBUFFER
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.safe_draw(&mut display)
|
.partial_draw(&mut display)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use crate::display::{SCREEN_HEIGHT, SCREEN_WIDTH};
|
use crate::display::{SCREEN_HEIGHT, SCREEN_WIDTH};
|
||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use embassy_sync::lazy_lock::LazyLock;
|
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
draw_target::DrawTarget,
|
draw_target::DrawTarget,
|
||||||
pixelcolor::{
|
pixelcolor::{
|
||||||
@@ -14,32 +13,33 @@ use embedded_hal_2::digital::OutputPin;
|
|||||||
use embedded_hal_async::{delay::DelayNs, spi::SpiDevice};
|
use embedded_hal_async::{delay::DelayNs, spi::SpiDevice};
|
||||||
use st7365p_lcd::ST7365P;
|
use st7365p_lcd::ST7365P;
|
||||||
|
|
||||||
pub const TILE_SIZE: usize = 16; // 16x16 tile
|
const TILE_SIZE: usize = 16; // 16x16 tile
|
||||||
pub const TILE_COUNT: usize = (SCREEN_WIDTH / TILE_SIZE) * (SCREEN_HEIGHT / TILE_SIZE); // 400 tiles
|
const TILE_COUNT: usize = (SCREEN_WIDTH / TILE_SIZE) * (SCREEN_HEIGHT / TILE_SIZE); // 400 tiles
|
||||||
|
const NUM_TILE_ROWS: usize = SCREEN_WIDTH / TILE_SIZE;
|
||||||
|
const NUM_TILE_COLS: usize = SCREEN_WIDTH / TILE_SIZE;
|
||||||
|
|
||||||
// Group of tiles for batching
|
const MAX_BATCH_TILES: usize = (SCREEN_WIDTH / TILE_SIZE) * 2;
|
||||||
pub const MAX_META_TILES: usize = (SCREEN_WIDTH / TILE_SIZE) * 2; // max number of meta tiles in buffer
|
type BatchTileBuf = [u16; MAX_BATCH_TILES * TILE_SIZE * TILE_SIZE];
|
||||||
type MetaTileVec = heapless::Vec<Rectangle, { TILE_COUNT / MAX_META_TILES }>;
|
|
||||||
|
|
||||||
pub const SIZE: usize = SCREEN_HEIGHT * SCREEN_WIDTH;
|
pub const SIZE: usize = SCREEN_HEIGHT * SCREEN_WIDTH;
|
||||||
|
|
||||||
pub static FB_PAUSED: AtomicBool = AtomicBool::new(false);
|
pub static FB_PAUSED: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
static mut DIRTY_TILES: LazyLock<heapless::Vec<AtomicBool, TILE_COUNT>> = LazyLock::new(|| {
|
|
||||||
let mut tiles = heapless::Vec::new();
|
|
||||||
for _ in 0..TILE_COUNT {
|
|
||||||
tiles.push(AtomicBool::new(true)).unwrap();
|
|
||||||
}
|
|
||||||
tiles
|
|
||||||
});
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct AtomicFrameBuffer<'a>(&'a mut [u16]);
|
pub struct AtomicFrameBuffer<'a> {
|
||||||
|
fb: &'a mut [u16],
|
||||||
|
dirty_tiles: [AtomicBool; TILE_COUNT],
|
||||||
|
batch_tile_buf: BatchTileBuf,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> AtomicFrameBuffer<'a> {
|
impl<'a> AtomicFrameBuffer<'a> {
|
||||||
pub fn new(buffer: &'a mut [u16]) -> Self {
|
pub fn new(buffer: &'a mut [u16]) -> Self {
|
||||||
assert!(buffer.len() == SIZE);
|
assert!(buffer.len() == SIZE);
|
||||||
Self(buffer)
|
Self {
|
||||||
|
fb: buffer,
|
||||||
|
dirty_tiles: core::array::from_fn(|_| AtomicBool::new(true)),
|
||||||
|
batch_tile_buf: [0; MAX_BATCH_TILES * TILE_SIZE * TILE_SIZE],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mark_tiles_dirty(&mut self, rect: Rectangle) {
|
fn mark_tiles_dirty(&mut self, rect: Rectangle) {
|
||||||
@@ -52,7 +52,7 @@ impl<'a> AtomicFrameBuffer<'a> {
|
|||||||
for ty in start_ty..=end_ty {
|
for ty in start_ty..=end_ty {
|
||||||
for tx in start_tx..=end_tx {
|
for tx in start_tx..=end_tx {
|
||||||
let tile_idx = ty * tiles_x + tx;
|
let tile_idx = ty * tiles_x + tx;
|
||||||
unsafe { DIRTY_TILES.get_mut()[tile_idx].store(true, Ordering::Release) };
|
self.dirty_tiles[tile_idx].store(true, Ordering::Release);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ impl<'a> AtomicFrameBuffer<'a> {
|
|||||||
for y in sy..=ey {
|
for y in sy..=ey {
|
||||||
for x in sx..=ex {
|
for x in sx..=ex {
|
||||||
if let Some(color) = color_iter.next() {
|
if let Some(color) = color_iter.next() {
|
||||||
self.0[(y as usize * SCREEN_WIDTH) + x as usize] = color;
|
self.fb[(y as usize * SCREEN_WIDTH) + x as usize] = color;
|
||||||
} else {
|
} else {
|
||||||
return Err(()); // Not enough data
|
return Err(()); // Not enough data
|
||||||
}
|
}
|
||||||
@@ -93,60 +93,17 @@ impl<'a> AtomicFrameBuffer<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// walk the dirty tiles and mark groups of tiles(meta-tiles) for batched updates
|
// Checks if a full draw would be faster than individual tile batches
|
||||||
fn find_meta_tiles(&mut self, tiles_x: usize, tiles_y: usize) -> MetaTileVec {
|
fn should_full_draw(&self) -> bool {
|
||||||
let mut meta_tiles: MetaTileVec = heapless::Vec::new();
|
let threshold_pixels = SIZE * 80 / 100;
|
||||||
|
let mut dirty_pixels = 0;
|
||||||
|
|
||||||
for ty in 0..tiles_y {
|
self.dirty_tiles.iter().any(|tile| {
|
||||||
let mut tx = 0;
|
if tile.load(Ordering::Acquire) {
|
||||||
while tx < tiles_x {
|
dirty_pixels += TILE_SIZE * TILE_SIZE;
|
||||||
let idx = ty * tiles_x + tx;
|
|
||||||
if !unsafe { DIRTY_TILES.get()[idx].load(Ordering::Acquire) } {
|
|
||||||
tx += 1;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
dirty_pixels >= threshold_pixels
|
||||||
// Start meta-tile at this tile
|
})
|
||||||
let mut width_tiles = 1;
|
|
||||||
let height_tiles = 1;
|
|
||||||
|
|
||||||
// Grow horizontally, but keep under MAX_TILES_PER_METATILE
|
|
||||||
while tx + width_tiles < tiles_x
|
|
||||||
&& unsafe {
|
|
||||||
DIRTY_TILES.get()[ty * tiles_x + tx + width_tiles].load(Ordering::Acquire)
|
|
||||||
}
|
|
||||||
&& (width_tiles + height_tiles) <= MAX_META_TILES
|
|
||||||
{
|
|
||||||
width_tiles += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: for simplicity, skipped vertical growth
|
|
||||||
|
|
||||||
for x_off in 0..width_tiles {
|
|
||||||
unsafe {
|
|
||||||
DIRTY_TILES.get()[ty * tiles_x + tx + x_off]
|
|
||||||
.store(false, Ordering::Release);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// new meta-tile pos
|
|
||||||
let rect = Rectangle::new(
|
|
||||||
Point::new((tx * TILE_SIZE) as i32, (ty * TILE_SIZE) as i32),
|
|
||||||
Size::new(
|
|
||||||
(width_tiles * TILE_SIZE) as u32,
|
|
||||||
(height_tiles * TILE_SIZE) as u32,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
if meta_tiles.push(rect).is_err() {
|
|
||||||
return meta_tiles;
|
|
||||||
};
|
|
||||||
|
|
||||||
tx += width_tiles;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
meta_tiles
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends the entire framebuffer to the display
|
/// Sends the entire framebuffer to the display
|
||||||
@@ -165,71 +122,40 @@ impl<'a> AtomicFrameBuffer<'a> {
|
|||||||
0,
|
0,
|
||||||
self.size().width as u16 - 1,
|
self.size().width as u16 - 1,
|
||||||
self.size().height as u16 - 1,
|
self.size().height as u16 - 1,
|
||||||
&self.0[..],
|
&self.fb[..],
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
unsafe {
|
for tile in self.dirty_tiles.iter() {
|
||||||
for tile in DIRTY_TILES.get_mut().iter() {
|
|
||||||
tile.store(false, Ordering::Release);
|
tile.store(false, Ordering::Release);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn safe_draw<SPI, DC, RST, DELAY>(
|
// copy N tiles horizontally to the right into batch tile buf
|
||||||
|
fn append_tiles_to_batch(
|
||||||
&mut self,
|
&mut self,
|
||||||
display: &mut ST7365P<SPI, DC, RST, DELAY>,
|
tile_x: u16,
|
||||||
) -> Result<(), ()>
|
tile_y: u16,
|
||||||
where
|
total_tiles: u16, // number of tiles being written to buf
|
||||||
SPI: SpiDevice,
|
) {
|
||||||
DC: OutputPin,
|
debug_assert!(total_tiles as usize <= NUM_TILE_COLS);
|
||||||
RST: OutputPin,
|
for batch_row_num in 0..TILE_SIZE {
|
||||||
DELAY: DelayNs,
|
let batch_row_offset = batch_row_num * total_tiles as usize * TILE_SIZE;
|
||||||
{
|
let batch_row = &mut self.batch_tile_buf
|
||||||
let tiles_x = SCREEN_WIDTH / TILE_SIZE;
|
[batch_row_offset..batch_row_offset + (total_tiles as usize * TILE_SIZE)];
|
||||||
let _tiles_y = SCREEN_HEIGHT / TILE_SIZE;
|
|
||||||
|
|
||||||
let tiles = unsafe { DIRTY_TILES.get_mut() };
|
let fb_row_offset = (tile_y as usize * TILE_SIZE + batch_row_num) * SCREEN_WIDTH
|
||||||
let mut pixel_buffer: heapless::Vec<u16, { TILE_SIZE * TILE_SIZE }> = heapless::Vec::new();
|
+ tile_x as usize * TILE_SIZE;
|
||||||
|
let fb_row =
|
||||||
|
&self.fb[fb_row_offset..fb_row_offset + (total_tiles as usize * TILE_SIZE)];
|
||||||
|
|
||||||
for tile_idx in 0..TILE_COUNT {
|
batch_row.copy_from_slice(fb_row);
|
||||||
if tiles[tile_idx].swap(false, Ordering::AcqRel) {
|
|
||||||
let tx = tile_idx % tiles_x;
|
|
||||||
let ty = tile_idx / tiles_x;
|
|
||||||
|
|
||||||
let x_start = tx * TILE_SIZE;
|
|
||||||
let y_start = ty * TILE_SIZE;
|
|
||||||
|
|
||||||
let x_end = (x_start + TILE_SIZE).min(SCREEN_WIDTH);
|
|
||||||
let y_end = (y_start + TILE_SIZE).min(SCREEN_HEIGHT);
|
|
||||||
|
|
||||||
pixel_buffer.clear();
|
|
||||||
|
|
||||||
for y in y_start..y_end {
|
|
||||||
let start = y * SCREEN_WIDTH + x_start;
|
|
||||||
let end = y * SCREEN_WIDTH + x_end;
|
|
||||||
pixel_buffer.extend_from_slice(&self.0[start..end]).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
display
|
|
||||||
.set_pixels_buffered(
|
|
||||||
x_start as u16,
|
|
||||||
y_start as u16,
|
|
||||||
(x_end - 1) as u16,
|
|
||||||
(y_end - 1) as u16,
|
|
||||||
&pixel_buffer,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
// Pushes tiles to the display in batches to avoid full frame pushes (unless needed)
|
||||||
}
|
|
||||||
|
|
||||||
/// Sends only dirty tiles (16x16px) in batches to the display
|
|
||||||
pub async fn partial_draw<SPI, DC, RST, DELAY>(
|
pub async fn partial_draw<SPI, DC, RST, DELAY>(
|
||||||
&mut self,
|
&mut self,
|
||||||
display: &mut ST7365P<SPI, DC, RST, DELAY>,
|
display: &mut ST7365P<SPI, DC, RST, DELAY>,
|
||||||
@@ -240,55 +166,53 @@ impl<'a> AtomicFrameBuffer<'a> {
|
|||||||
RST: OutputPin,
|
RST: OutputPin,
|
||||||
DELAY: DelayNs,
|
DELAY: DelayNs,
|
||||||
{
|
{
|
||||||
if unsafe { DIRTY_TILES.get().iter().any(|p| p.load(Ordering::Acquire)) } {
|
if self.should_full_draw() {
|
||||||
let tiles_x = (SCREEN_WIDTH + TILE_SIZE - 1) / TILE_SIZE;
|
return self.draw(display).await;
|
||||||
let tiles_y = (SCREEN_HEIGHT + TILE_SIZE - 1) / TILE_SIZE;
|
|
||||||
|
|
||||||
let meta_tiles = self.find_meta_tiles(tiles_x, tiles_y);
|
|
||||||
|
|
||||||
// buffer for copying meta tiles before sending to display
|
|
||||||
let mut pixel_buffer: heapless::Vec<u16, { MAX_META_TILES * TILE_SIZE * TILE_SIZE }> =
|
|
||||||
heapless::Vec::new();
|
|
||||||
|
|
||||||
for rect in meta_tiles {
|
|
||||||
let rect_width = rect.size.width as usize;
|
|
||||||
let rect_height = rect.size.height as usize;
|
|
||||||
let rect_x = rect.top_left.x as usize;
|
|
||||||
let rect_y = rect.top_left.y as usize;
|
|
||||||
|
|
||||||
pixel_buffer.clear();
|
|
||||||
|
|
||||||
for row in 0..rect_height {
|
|
||||||
let y = rect_y + row;
|
|
||||||
let start = y * SCREEN_WIDTH + rect_x;
|
|
||||||
let end = start + rect_width;
|
|
||||||
|
|
||||||
// Safe: we guarantee buffer will not exceed MAX_META_TILE_PIXELS
|
|
||||||
pixel_buffer.extend_from_slice(&self.0[start..end]).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for tile_row in 0..NUM_TILE_ROWS {
|
||||||
|
let row_start_idx = tile_row * NUM_TILE_COLS;
|
||||||
|
let mut col = 0;
|
||||||
|
|
||||||
|
while col < NUM_TILE_COLS {
|
||||||
|
// Check for dirty tile
|
||||||
|
if self.dirty_tiles[row_start_idx + col].swap(false, Ordering::Acquire) {
|
||||||
|
let run_start = col;
|
||||||
|
let mut run_len = 1;
|
||||||
|
|
||||||
|
// Extend run while contiguous dirty tiles and within MAX_BATCH_TILES
|
||||||
|
while col + 1 < NUM_TILE_COLS
|
||||||
|
&& self.dirty_tiles[row_start_idx + col + 1].load(Ordering::Acquire)
|
||||||
|
&& run_len < MAX_BATCH_TILES
|
||||||
|
{
|
||||||
|
col += 1;
|
||||||
|
run_len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the whole horizontal run into the batch buffer in one call
|
||||||
|
let tile_x = run_start;
|
||||||
|
let tile_y = tile_row;
|
||||||
|
self.append_tiles_to_batch(tile_x as u16, tile_y as u16, run_len as u16);
|
||||||
|
|
||||||
|
// Compute coordinates for display write
|
||||||
|
let start_x = tile_x * TILE_SIZE;
|
||||||
|
let end_x = start_x + run_len * TILE_SIZE - 1;
|
||||||
|
let start_y = tile_y * TILE_SIZE;
|
||||||
|
let end_y = start_y + TILE_SIZE - 1;
|
||||||
|
|
||||||
|
// Send batch to display
|
||||||
display
|
display
|
||||||
.set_pixels_buffered(
|
.set_pixels_buffered(
|
||||||
rect_x as u16,
|
start_x as u16,
|
||||||
rect_y as u16,
|
start_y as u16,
|
||||||
(rect_x + rect_width - 1) as u16,
|
end_x as u16,
|
||||||
(rect_y + rect_height - 1) as u16,
|
end_y as u16,
|
||||||
&pixel_buffer,
|
&self.batch_tile_buf[..run_len * TILE_SIZE * TILE_SIZE],
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// walk the meta-tile and set as clean
|
|
||||||
let start_tx = rect_x / TILE_SIZE;
|
|
||||||
let start_ty = rect_y / TILE_SIZE;
|
|
||||||
let end_tx = (rect_x + rect_width - 1) / TILE_SIZE;
|
|
||||||
let end_ty = (rect_y + rect_height - 1) / TILE_SIZE;
|
|
||||||
|
|
||||||
for ty in start_ty..=end_ty {
|
|
||||||
for tx in start_tx..=end_tx {
|
|
||||||
let tile_idx = ty * tiles_x + tx;
|
|
||||||
unsafe { DIRTY_TILES.get_mut()[tile_idx].store(false, Ordering::Release) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
col += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,14 +233,14 @@ impl<'a> DrawTarget for AtomicFrameBuffer<'a> {
|
|||||||
|
|
||||||
for Pixel(coord, color) in pixels {
|
for Pixel(coord, color) in pixels {
|
||||||
if coord.x >= 0 && coord.y >= 0 {
|
if coord.x >= 0 && coord.y >= 0 {
|
||||||
let x = coord.x as i32;
|
let x = coord.x;
|
||||||
let y = coord.y as i32;
|
let y = coord.y;
|
||||||
|
|
||||||
if (x as usize) < SCREEN_WIDTH && (y as usize) < SCREEN_HEIGHT {
|
if (x as usize) < SCREEN_WIDTH && (y as usize) < SCREEN_HEIGHT {
|
||||||
let idx = (y as usize) * SCREEN_WIDTH + (x as usize);
|
let idx = (y as usize) * SCREEN_WIDTH + (x as usize);
|
||||||
let raw_color = RawU16::from(color).into_inner();
|
let raw_color = RawU16::from(color).into_inner();
|
||||||
if self.0[idx] != raw_color {
|
if self.fb[idx] != raw_color {
|
||||||
self.0[idx] = raw_color;
|
self.fb[idx] = raw_color;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,8 +289,8 @@ impl<'a> DrawTarget for AtomicFrameBuffer<'a> {
|
|||||||
if let Some(color) = colors.next() {
|
if let Some(color) = colors.next() {
|
||||||
let idx = (p.y as usize * SCREEN_WIDTH) + (p.x as usize);
|
let idx = (p.y as usize * SCREEN_WIDTH) + (p.x as usize);
|
||||||
let raw_color = RawU16::from(color).into_inner();
|
let raw_color = RawU16::from(color).into_inner();
|
||||||
if self.0[idx] != raw_color {
|
if self.fb[idx] != raw_color {
|
||||||
self.0[idx] = raw_color;
|
self.fb[idx] = raw_color;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -404,7 +328,7 @@ impl<'a> DrawTarget for AtomicFrameBuffer<'a> {
|
|||||||
.take((self.size().width * self.size().height) as usize),
|
.take((self.size().width * self.size().height) as usize),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for tile in unsafe { DIRTY_TILES.get_mut() }.iter() {
|
for tile in self.dirty_tiles.iter() {
|
||||||
tile.store(true, Ordering::Release);
|
tile.store(true, Ordering::Release);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,10 @@ mod abi;
|
|||||||
mod display;
|
mod display;
|
||||||
mod elf;
|
mod elf;
|
||||||
mod framebuffer;
|
mod framebuffer;
|
||||||
|
#[allow(unused)]
|
||||||
mod heap;
|
mod heap;
|
||||||
mod peripherals;
|
mod peripherals;
|
||||||
|
#[allow(unused)]
|
||||||
mod psram;
|
mod psram;
|
||||||
mod scsi;
|
mod scsi;
|
||||||
mod storage;
|
mod storage;
|
||||||
@@ -27,6 +29,7 @@ use crate::{heap::init_qmi_psram_heap, psram::init_psram_qmi};
|
|||||||
use crate::{
|
use crate::{
|
||||||
abi::{KEY_CACHE, MS_SINCE_LAUNCH},
|
abi::{KEY_CACHE, MS_SINCE_LAUNCH},
|
||||||
display::{FRAMEBUFFER, display_handler, init_display},
|
display::{FRAMEBUFFER, display_handler, init_display},
|
||||||
|
heap::HEAP,
|
||||||
peripherals::{
|
peripherals::{
|
||||||
conf_peripherals,
|
conf_peripherals,
|
||||||
keyboard::{KeyState, read_keyboard_fifo},
|
keyboard::{KeyState, read_keyboard_fifo},
|
||||||
@@ -225,6 +228,7 @@ struct Sd {
|
|||||||
cs: Peri<'static, PIN_17>,
|
cs: Peri<'static, PIN_17>,
|
||||||
det: Peri<'static, PIN_22>,
|
det: Peri<'static, PIN_22>,
|
||||||
}
|
}
|
||||||
|
#[allow(dead_code)]
|
||||||
struct Psram {
|
struct Psram {
|
||||||
pio: Peri<'static, PIO0>,
|
pio: Peri<'static, PIO0>,
|
||||||
sclk: Peri<'static, PIN_21>,
|
sclk: Peri<'static, PIN_21>,
|
||||||
@@ -328,10 +332,14 @@ async fn kernel_task(
|
|||||||
|
|
||||||
setup_mcu(mcu).await;
|
setup_mcu(mcu).await;
|
||||||
|
|
||||||
|
defmt::info!("setting up psram");
|
||||||
|
Timer::after_millis(100).await;
|
||||||
|
|
||||||
// setup_psram(psram).await;
|
// setup_psram(psram).await;
|
||||||
#[cfg(feature = "pimoroni2w")]
|
#[cfg(feature = "pimoroni2w")]
|
||||||
setup_qmi_psram().await;
|
setup_qmi_psram().await;
|
||||||
|
|
||||||
|
Timer::after_millis(100).await;
|
||||||
setup_display(display, spawner).await;
|
setup_display(display, spawner).await;
|
||||||
setup_sd(sd).await;
|
setup_sd(sd).await;
|
||||||
|
|
||||||
|
|||||||
@@ -549,13 +549,13 @@ pub fn init_psram_qmi(
|
|||||||
let min_deselect: u32 = ((18 * 1_000_000 + (clock_period_fs - 1)) / clock_period_fs
|
let min_deselect: u32 = ((18 * 1_000_000 + (clock_period_fs - 1)) / clock_period_fs
|
||||||
- u64::from(divisor + 1) / 2) as u32;
|
- u64::from(divisor + 1) / 2) as u32;
|
||||||
|
|
||||||
#[cfg(feature = "defmt")]
|
// #[cfg(feature = "defmt")]
|
||||||
defmt::info!(
|
// defmt::info!(
|
||||||
"clock_period_fs={} max_select={} min_deselect={}",
|
// "clock_period_fs={} max_select={} min_deselect={}",
|
||||||
clock_period_fs,
|
// clock_period_fs,
|
||||||
max_select,
|
// max_select,
|
||||||
min_deselect
|
// min_deselect
|
||||||
);
|
// );
|
||||||
|
|
||||||
qmi.direct_csr().write(|w| {
|
qmi.direct_csr().write(|w| {
|
||||||
w.set_clkdiv(10);
|
w.set_clkdiv(10);
|
||||||
|
|||||||
10
selection_ui/Cargo.toml
Normal file
10
selection_ui/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "selection_ui"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
abi = { path = "../abi" }
|
||||||
|
embedded-graphics = "0.8.1"
|
||||||
|
embedded-layout = "0.4.2"
|
||||||
|
embedded-text = "0.7.3"
|
||||||
119
selection_ui/src/lib.rs
Normal file
119
selection_ui/src/lib.rs
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#![no_std]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use abi::{
|
||||||
|
display::Display,
|
||||||
|
get_key,
|
||||||
|
keyboard::{KeyCode, KeyState},
|
||||||
|
print, sleep,
|
||||||
|
};
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
use embedded_graphics::{
|
||||||
|
Drawable,
|
||||||
|
mono_font::{MonoTextStyle, ascii::FONT_10X20},
|
||||||
|
pixelcolor::Rgb565,
|
||||||
|
prelude::{Dimensions, Point, Primitive, RgbColor, Size},
|
||||||
|
primitives::{PrimitiveStyle, Rectangle},
|
||||||
|
text::Text,
|
||||||
|
};
|
||||||
|
use embedded_layout::{
|
||||||
|
align::{horizontal, vertical},
|
||||||
|
layout::linear::{FixedMargin, LinearLayout},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
use embedded_text::TextBox;
|
||||||
|
|
||||||
|
pub struct SelectionUi<'a> {
|
||||||
|
selection: usize,
|
||||||
|
items: &'a [&'a str],
|
||||||
|
error: &'a str,
|
||||||
|
last_bounds: Option<Rectangle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> SelectionUi<'a> {
|
||||||
|
pub fn new(items: &'a [&'a str], error: &'a str) -> Self {
|
||||||
|
Self {
|
||||||
|
selection: 0,
|
||||||
|
items,
|
||||||
|
error,
|
||||||
|
last_bounds: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_selection_ui(&mut self, display: &mut Display) -> Result<Option<usize>, ()> {
|
||||||
|
self.draw(display)?;
|
||||||
|
let selection;
|
||||||
|
loop {
|
||||||
|
let key = get_key();
|
||||||
|
if key.state == KeyState::Pressed {
|
||||||
|
print!("Got Key press: {:?}", key.key);
|
||||||
|
if let Some(s) = self.update(display, key.key)? {
|
||||||
|
selection = Some(s);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(selection)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// updates the display with a new keypress.
|
||||||
|
/// returns selection idx if selected
|
||||||
|
pub fn update(&mut self, display: &mut Display, key: KeyCode) -> Result<Option<usize>, ()> {
|
||||||
|
match key {
|
||||||
|
KeyCode::JoyUp => {
|
||||||
|
self.selection = self.selection.saturating_sub(1);
|
||||||
|
}
|
||||||
|
KeyCode::JoyDown => {
|
||||||
|
self.selection = self.selection.saturating_add(1);
|
||||||
|
}
|
||||||
|
KeyCode::Enter | KeyCode::JoyRight => return Ok(Some(self.selection)),
|
||||||
|
_ => return Ok(Some(self.selection)),
|
||||||
|
};
|
||||||
|
self.draw(display)?;
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(&mut self, display: &mut Display) -> Result<(), ()> {
|
||||||
|
let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
||||||
|
let display_area = display.bounding_box();
|
||||||
|
|
||||||
|
if self.items.is_empty() {
|
||||||
|
TextBox::new(
|
||||||
|
&self.error,
|
||||||
|
Rectangle::new(
|
||||||
|
Point::new(25, 25),
|
||||||
|
Size::new(display_area.size.width - 50, display_area.size.width - 50),
|
||||||
|
),
|
||||||
|
text_style,
|
||||||
|
)
|
||||||
|
.draw(display)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut views: Vec<Text<MonoTextStyle<Rgb565>>> = Vec::new();
|
||||||
|
|
||||||
|
for i in self.items {
|
||||||
|
views.push(Text::new(i, Point::zero(), text_style));
|
||||||
|
}
|
||||||
|
|
||||||
|
let views_group = Views::new(views.as_mut_slice());
|
||||||
|
|
||||||
|
let layout = LinearLayout::vertical(views_group)
|
||||||
|
.with_alignment(horizontal::Center)
|
||||||
|
.with_spacing(FixedMargin(5))
|
||||||
|
.arrange()
|
||||||
|
.align_to(&display_area, horizontal::Center, vertical::Center);
|
||||||
|
|
||||||
|
// draw selected box
|
||||||
|
let selected_bounds = layout.inner().get(self.selection).ok_or(())?.bounding_box();
|
||||||
|
Rectangle::new(selected_bounds.top_left, selected_bounds.size)
|
||||||
|
.into_styled(PrimitiveStyle::with_stroke(Rgb565::WHITE, 1))
|
||||||
|
.draw(display)?;
|
||||||
|
|
||||||
|
self.last_bounds = Some(layout.bounds());
|
||||||
|
|
||||||
|
layout.draw(display)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,12 +5,12 @@
|
|||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
use abi::{
|
use abi::{
|
||||||
display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
|
display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
|
||||||
fs::{list_dir, read_file},
|
fs::{Entries, list_dir, read_file},
|
||||||
get_key,
|
get_key,
|
||||||
keyboard::{KeyCode, KeyState},
|
keyboard::{KeyCode, KeyState},
|
||||||
print,
|
print,
|
||||||
};
|
};
|
||||||
use alloc::{format, string::ToString, vec};
|
use alloc::{format, vec};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
Drawable, image::Image, mono_font::MonoTextStyle, mono_font::ascii::FONT_6X10,
|
Drawable, image::Image, mono_font::MonoTextStyle, mono_font::ascii::FONT_6X10,
|
||||||
@@ -34,7 +34,6 @@ pub fn main() {
|
|||||||
let mut bmp_buf = vec![0_u8; 100_000];
|
let mut bmp_buf = vec![0_u8; 100_000];
|
||||||
let mut display = Display;
|
let mut display = Display;
|
||||||
|
|
||||||
// Grid parameters
|
|
||||||
let grid_cols = 3;
|
let grid_cols = 3;
|
||||||
let grid_rows = 3;
|
let grid_rows = 3;
|
||||||
let cell_width = SCREEN_WIDTH as i32 / grid_cols;
|
let cell_width = SCREEN_WIDTH as i32 / grid_cols;
|
||||||
@@ -42,20 +41,19 @@ pub fn main() {
|
|||||||
|
|
||||||
let mut images_drawn = 0;
|
let mut images_drawn = 0;
|
||||||
|
|
||||||
let mut files = [const { None }; 18];
|
let mut entries = Entries::new();
|
||||||
let files_num = list_dir("/images", &mut files);
|
let files_num = list_dir("/images", &mut entries);
|
||||||
|
|
||||||
for file in &files[2..files_num] {
|
for file in &entries.entries()[2..files_num] {
|
||||||
if images_drawn >= grid_cols * grid_rows {
|
if images_drawn >= grid_cols * grid_rows {
|
||||||
break; // only draw 3x3
|
break; // only draw 3x3
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(f) = file {
|
print!("file: {}", file);
|
||||||
print!("file: {}", f.name);
|
if file.extension().unwrap_or("") == "bmp" || file.extension().unwrap_or("") == "BMP" {
|
||||||
if f.name.extension() == b"bmp" || f.name.extension() == b"BMP" {
|
let file_path = format!("/images/{}", file);
|
||||||
let file = format!("/images/{}", f.name);
|
|
||||||
|
|
||||||
let read = read_file(&file, 0, &mut &mut bmp_buf[..]);
|
let read = read_file(&file_path, 0, &mut &mut bmp_buf[..]);
|
||||||
if read > 0 {
|
if read > 0 {
|
||||||
let bmp = Bmp::from_slice(&bmp_buf).expect("failed to parse bmp");
|
let bmp = Bmp::from_slice(&bmp_buf).expect("failed to parse bmp");
|
||||||
|
|
||||||
@@ -76,11 +74,7 @@ pub fn main() {
|
|||||||
|
|
||||||
let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
||||||
let text_y = y + bmp_h + 2; // 2px gap under image
|
let text_y = y + bmp_h + 2; // 2px gap under image
|
||||||
Text::new(
|
Text::new(&file.base(), Point::new(cell_x + 2, text_y), text_style)
|
||||||
f.name.to_string().as_str(),
|
|
||||||
Point::new(cell_x + 2, text_y),
|
|
||||||
text_style,
|
|
||||||
)
|
|
||||||
.draw(&mut display)
|
.draw(&mut display)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -88,7 +82,6 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let event = get_key();
|
let event = get_key();
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
abi = { path = "../../abi" }
|
abi = { path = "../../abi" }
|
||||||
embedded-graphics = "0.8.1"
|
embedded-graphics = "0.8.1"
|
||||||
|
selection_ui = { path = "../../selection_ui" }
|
||||||
tinygif = { git = "https://github.com/LegitCamper/tinygif" }
|
tinygif = { git = "https://github.com/LegitCamper/tinygif" }
|
||||||
|
|||||||
@@ -4,16 +4,17 @@
|
|||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
use abi::{
|
use abi::{
|
||||||
display::Display,
|
display::Display,
|
||||||
fs::{file_len, read_file},
|
fs::{Entries, file_len, list_dir, read_file},
|
||||||
get_key, get_ms,
|
get_key, get_ms,
|
||||||
keyboard::{KeyCode, KeyState},
|
keyboard::{KeyCode, KeyState},
|
||||||
print, sleep,
|
print, sleep,
|
||||||
};
|
};
|
||||||
use alloc::vec;
|
use alloc::{format, vec, vec::Vec};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
image::ImageDrawable, pixelcolor::Rgb565, prelude::Point, transform::Transform,
|
image::ImageDrawable, pixelcolor::Rgb565, prelude::Point, transform::Transform,
|
||||||
};
|
};
|
||||||
|
use selection_ui::SelectionUi;
|
||||||
use tinygif::Gif;
|
use tinygif::Gif;
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
@@ -31,13 +32,27 @@ pub fn main() {
|
|||||||
print!("Starting Gif app");
|
print!("Starting Gif app");
|
||||||
let mut display = Display;
|
let mut display = Display;
|
||||||
|
|
||||||
let size = file_len("/gifs/bad_apple.gif");
|
let mut entries = Entries::new();
|
||||||
|
list_dir("/gifs", &mut entries);
|
||||||
|
|
||||||
|
let mut files = entries.entries();
|
||||||
|
files.retain(|e| e.extension().unwrap_or("") == "gif");
|
||||||
|
let gifs = &files.iter().map(|e| e.full_name()).collect::<Vec<&str>>();
|
||||||
|
|
||||||
|
let mut selection_ui = SelectionUi::new(&gifs, "No Gif files found in /gifs");
|
||||||
|
let selection = selection_ui
|
||||||
|
.run_selection_ui(&mut display)
|
||||||
|
.expect("failed to draw")
|
||||||
|
.expect("Failed to get user selection");
|
||||||
|
|
||||||
|
let file_name = format!("/gifs/{}", gifs[selection]);
|
||||||
|
let size = file_len(&file_name);
|
||||||
let mut buf = vec![0_u8; size];
|
let mut buf = vec![0_u8; size];
|
||||||
let read = read_file("/gifs/bad_apple.gif", 0, &mut buf);
|
let read = read_file(&file_name, 0, &mut buf);
|
||||||
print!("read: {}, file size: {}", read, size);
|
print!("read: {}, file size: {}", read, size);
|
||||||
assert!(read == size);
|
assert!(read == size);
|
||||||
|
|
||||||
let gif = Gif::<Rgb565>::from_slice(&buf).unwrap();
|
let gif = Gif::<Rgb565>::from_slice(&buf).expect("Failed to parse gif");
|
||||||
let height = gif.height();
|
let height = gif.height();
|
||||||
|
|
||||||
let mut frame_num = 0;
|
let mut frame_num = 0;
|
||||||
@@ -51,11 +66,14 @@ pub fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
frame_num += 1;
|
frame_num += 1;
|
||||||
|
|
||||||
if frame_num % 100 == 0 {
|
if frame_num % 5 == 0 {
|
||||||
let event = get_key();
|
let event = get_key();
|
||||||
if event.state != KeyState::Idle {
|
if event.state != KeyState::Idle {
|
||||||
match event.key {
|
match event.key {
|
||||||
KeyCode::Esc => return,
|
KeyCode::Esc => {
|
||||||
|
drop(buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user