mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-27 07:45:28 +00:00
selection ui for user apps
works
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
extern crate alloc;
|
||||
use abi::{
|
||||
display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
|
||||
fs::{list_dir, read_file},
|
||||
fs::{Entries, list_dir, read_file},
|
||||
get_key,
|
||||
keyboard::{KeyCode, KeyState},
|
||||
print,
|
||||
};
|
||||
use alloc::{format, string::ToString, vec};
|
||||
use alloc::{format, vec};
|
||||
use core::panic::PanicInfo;
|
||||
use embedded_graphics::{
|
||||
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 display = Display;
|
||||
|
||||
// Grid parameters
|
||||
let grid_cols = 3;
|
||||
let grid_rows = 3;
|
||||
let cell_width = SCREEN_WIDTH as i32 / grid_cols;
|
||||
@@ -42,50 +41,44 @@ pub fn main() {
|
||||
|
||||
let mut images_drawn = 0;
|
||||
|
||||
let mut files = [const { None }; 18];
|
||||
let files_num = list_dir("/images", &mut files);
|
||||
let mut entries = Entries::new();
|
||||
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 {
|
||||
break; // only draw 3x3
|
||||
}
|
||||
|
||||
if let Some(f) = file {
|
||||
print!("file: {}", f.name);
|
||||
if f.name.extension() == b"bmp" || f.name.extension() == b"BMP" {
|
||||
let file = format!("/images/{}", f.name);
|
||||
print!("file: {}", file);
|
||||
if file.extension().unwrap_or("") == "bmp" || file.extension().unwrap_or("") == "BMP" {
|
||||
let file_path = format!("/images/{}", file);
|
||||
|
||||
let read = read_file(&file, 0, &mut &mut bmp_buf[..]);
|
||||
if read > 0 {
|
||||
let bmp = Bmp::from_slice(&bmp_buf).expect("failed to parse bmp");
|
||||
let read = read_file(&file_path, 0, &mut &mut bmp_buf[..]);
|
||||
if read > 0 {
|
||||
let bmp = Bmp::from_slice(&bmp_buf).expect("failed to parse bmp");
|
||||
|
||||
let row = images_drawn / grid_cols;
|
||||
let col = images_drawn % grid_cols;
|
||||
let cell_x = col * cell_width;
|
||||
let cell_y = row * cell_height;
|
||||
let row = images_drawn / grid_cols;
|
||||
let col = images_drawn % grid_cols;
|
||||
let cell_x = col * cell_width;
|
||||
let cell_y = row * cell_height;
|
||||
|
||||
// Center image inside cell
|
||||
let bmp_w = bmp.size().width as i32;
|
||||
let bmp_h = bmp.size().height as i32;
|
||||
let x = cell_x + (cell_width - bmp_w) / 2;
|
||||
let y = cell_y + 5; // 5px top margin
|
||||
// Center image inside cell
|
||||
let bmp_w = bmp.size().width as i32;
|
||||
let bmp_h = bmp.size().height as i32;
|
||||
let x = cell_x + (cell_width - bmp_w) / 2;
|
||||
let y = cell_y + 5; // 5px top margin
|
||||
|
||||
Image::new(&bmp, Point::new(x, y))
|
||||
.draw(&mut display)
|
||||
.unwrap();
|
||||
|
||||
let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
||||
let text_y = y + bmp_h + 2; // 2px gap under image
|
||||
Text::new(
|
||||
f.name.to_string().as_str(),
|
||||
Point::new(cell_x + 2, text_y),
|
||||
text_style,
|
||||
)
|
||||
Image::new(&bmp, Point::new(x, y))
|
||||
.draw(&mut display)
|
||||
.unwrap();
|
||||
|
||||
images_drawn += 1;
|
||||
}
|
||||
let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
||||
let text_y = y + bmp_h + 2; // 2px gap under image
|
||||
Text::new(&file.base(), Point::new(cell_x + 2, text_y), text_style)
|
||||
.draw(&mut display)
|
||||
.unwrap();
|
||||
|
||||
images_drawn += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@ edition = "2024"
|
||||
[dependencies]
|
||||
abi = { path = "../../abi" }
|
||||
embedded-graphics = "0.8.1"
|
||||
selection_ui = { path = "../../selection_ui" }
|
||||
tinygif = { git = "https://github.com/LegitCamper/tinygif" }
|
||||
|
||||
@@ -4,16 +4,17 @@
|
||||
extern crate alloc;
|
||||
use abi::{
|
||||
display::Display,
|
||||
fs::{file_len, read_file},
|
||||
fs::{Entries, file_len, list_dir, read_file},
|
||||
get_key, get_ms,
|
||||
keyboard::{KeyCode, KeyState},
|
||||
print, sleep,
|
||||
};
|
||||
use alloc::vec;
|
||||
use alloc::{format, vec, vec::Vec};
|
||||
use core::panic::PanicInfo;
|
||||
use embedded_graphics::{
|
||||
image::ImageDrawable, pixelcolor::Rgb565, prelude::Point, transform::Transform,
|
||||
};
|
||||
use selection_ui::SelectionUi;
|
||||
use tinygif::Gif;
|
||||
|
||||
#[panic_handler]
|
||||
@@ -31,13 +32,27 @@ pub fn main() {
|
||||
print!("Starting Gif app");
|
||||
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 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);
|
||||
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 mut frame_num = 0;
|
||||
|
||||
Reference in New Issue
Block a user