mirror of
https://github.com/LegitCamper/picocalc-os-rs.git
synced 2025-12-27 07:45:28 +00:00
works
This commit is contained in:
@@ -4,9 +4,9 @@ extern crate alloc;
|
|||||||
|
|
||||||
use abi::{
|
use abi::{
|
||||||
display::Display,
|
display::Display,
|
||||||
fs::{Entries, FileName},
|
|
||||||
get_key,
|
get_key,
|
||||||
keyboard::{KeyCode, KeyState},
|
keyboard::{KeyCode, KeyState},
|
||||||
|
print, sleep,
|
||||||
};
|
};
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
@@ -26,13 +26,13 @@ use embedded_text::TextBox;
|
|||||||
|
|
||||||
pub struct SelectionUi<'a> {
|
pub struct SelectionUi<'a> {
|
||||||
selection: usize,
|
selection: usize,
|
||||||
items: &'a Entries,
|
items: &'a [&'a str],
|
||||||
error: &'a str,
|
error: &'a str,
|
||||||
last_bounds: Option<Rectangle>,
|
last_bounds: Option<Rectangle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SelectionUi<'a> {
|
impl<'a> SelectionUi<'a> {
|
||||||
pub fn new(items: &'a Entries, error: &'a str) -> Self {
|
pub fn new(items: &'a [&'a str], error: &'a str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
selection: 0,
|
selection: 0,
|
||||||
items,
|
items,
|
||||||
@@ -47,6 +47,7 @@ impl<'a> SelectionUi<'a> {
|
|||||||
loop {
|
loop {
|
||||||
let key = get_key();
|
let key = get_key();
|
||||||
if key.state == KeyState::Pressed {
|
if key.state == KeyState::Pressed {
|
||||||
|
print!("Got Key press: {:?}", key.key);
|
||||||
if let Some(s) = self.update(display, key.key)? {
|
if let Some(s) = self.update(display, key.key)? {
|
||||||
selection = Some(s);
|
selection = Some(s);
|
||||||
break;
|
break;
|
||||||
@@ -61,13 +62,13 @@ impl<'a> SelectionUi<'a> {
|
|||||||
pub fn update(&mut self, display: &mut Display, key: KeyCode) -> Result<Option<usize>, ()> {
|
pub fn update(&mut self, display: &mut Display, key: KeyCode) -> Result<Option<usize>, ()> {
|
||||||
match key {
|
match key {
|
||||||
KeyCode::JoyUp => {
|
KeyCode::JoyUp => {
|
||||||
let _ = self.selection.saturating_sub(1);
|
self.selection = self.selection.saturating_sub(1);
|
||||||
}
|
}
|
||||||
KeyCode::JoyDown => {
|
KeyCode::JoyDown => {
|
||||||
let _ = self.selection.saturating_add(1);
|
self.selection = self.selection.saturating_add(1);
|
||||||
}
|
}
|
||||||
KeyCode::Enter | KeyCode::JoyRight => return Ok(Some(self.selection)),
|
KeyCode::Enter | KeyCode::JoyRight => return Ok(Some(self.selection)),
|
||||||
_ => (),
|
_ => return Ok(Some(self.selection)),
|
||||||
};
|
};
|
||||||
self.draw(display)?;
|
self.draw(display)?;
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@@ -77,9 +78,7 @@ impl<'a> SelectionUi<'a> {
|
|||||||
let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
||||||
let display_area = display.bounding_box();
|
let display_area = display.bounding_box();
|
||||||
|
|
||||||
let entries = self.items.entries();
|
if self.items.is_empty() {
|
||||||
|
|
||||||
if entries.is_empty() {
|
|
||||||
TextBox::new(
|
TextBox::new(
|
||||||
&self.error,
|
&self.error,
|
||||||
Rectangle::new(
|
Rectangle::new(
|
||||||
@@ -94,8 +93,8 @@ impl<'a> SelectionUi<'a> {
|
|||||||
|
|
||||||
let mut views: Vec<Text<MonoTextStyle<Rgb565>>> = Vec::new();
|
let mut views: Vec<Text<MonoTextStyle<Rgb565>>> = Vec::new();
|
||||||
|
|
||||||
for i in &entries {
|
for i in self.items {
|
||||||
views.push(Text::new(i.full_name(), Point::zero(), text_style));
|
views.push(Text::new(i, Point::zero(), text_style));
|
||||||
}
|
}
|
||||||
|
|
||||||
let views_group = Views::new(views.as_mut_slice());
|
let views_group = Views::new(views.as_mut_slice());
|
||||||
|
|||||||
@@ -32,11 +32,12 @@ pub fn main() {
|
|||||||
print!("Starting Gif app");
|
print!("Starting Gif app");
|
||||||
let mut display = Display;
|
let mut display = Display;
|
||||||
|
|
||||||
let mut gifs = Entries::new();
|
let mut entries = Entries::new();
|
||||||
list_dir("/gifs", &mut gifs);
|
list_dir("/gifs", &mut entries);
|
||||||
|
|
||||||
gifs.entries()
|
let mut files = entries.entries();
|
||||||
.retain(|e| e.extension().unwrap_or("") == "gif");
|
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 mut selection_ui = SelectionUi::new(&gifs, "No Gif files found in /gifs");
|
||||||
let selection = selection_ui
|
let selection = selection_ui
|
||||||
@@ -44,9 +45,10 @@ pub fn main() {
|
|||||||
.expect("failed to draw")
|
.expect("failed to draw")
|
||||||
.expect("Failed to get user selection");
|
.expect("Failed to get user selection");
|
||||||
|
|
||||||
let size = file_len(&format!("/gifs/{}.gif", gifs.entries()[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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user