selection ui for wav_player

This commit is contained in:
2025-11-17 15:02:32 -07:00
parent 8c20864392
commit 0e7e330998
3 changed files with 61 additions and 22 deletions

1
Cargo.lock generated
View File

@@ -2762,6 +2762,7 @@ dependencies = [
"embedded-audio",
"embedded-graphics",
"rand",
"selection_ui",
]
[[package]]

View File

@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
abi = { path = "../../abi" }
selection_ui = { path = "../../selection_ui" }
embedded-graphics = "0.8.1"
rand = { version = "0.9.0", default-features = false }
embedded-audio = { git = "https://github.com/LegitCamper/embedded-audio" }

View File

@@ -5,14 +5,21 @@ extern crate alloc;
use abi::{
audio::{AUDIO_BUFFER_LEN, audio_buffer_ready, send_audio_buffer},
display::Display,
fs::{file_len, read_file},
format,
fs::{Entries, file_len, list_dir, read_file},
get_key,
keyboard::{KeyCode, KeyState},
println,
};
use alloc::string::String;
use alloc::{string::String, vec::Vec};
use core::panic::PanicInfo;
use embedded_audio::{AudioFile, PlatformFile, PlatformFileError, wav::Wav};
use embedded_graphics::{
mono_font::{MonoTextStyle, ascii::FONT_6X10},
pixelcolor::Rgb565,
prelude::RgbColor,
};
use selection_ui::{SelectionUi, SelectionUiError, draw_text_center};
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
@@ -27,19 +34,48 @@ pub extern "Rust" fn _start() {
pub fn main() {
println!("Starting Wav player app");
let mut _display = Display::take();
let mut display = Display::take().unwrap();
let mut buf = [0_u8; AUDIO_BUFFER_LEN];
loop {
let mut entries = Entries::new();
list_dir("/music", &mut entries);
let file = File::new(String::from("/music/test.wav"));
let mut files = entries.entries();
files.retain(|e| e.extension().unwrap_or("") == "wav");
let mut wavs = files.iter().map(|e| e.full_name()).collect::<Vec<&str>>();
wavs.sort();
let mut selection_ui = SelectionUi::new(&mut wavs);
let selection = match selection_ui.run_selection_ui(&mut display) {
Ok(maybe_sel) => maybe_sel,
Err(e) => match e {
SelectionUiError::SelectionListEmpty => {
draw_text_center(
&mut display,
"No Wavs were found in /gifs",
MonoTextStyle::new(&FONT_6X10, Rgb565::RED),
)
.expect("Display Error");
None
}
SelectionUiError::DisplayError(_) => panic!("Display Error"),
},
};
assert!(selection.is_some());
let file_name = format!("/music/{}", wavs[selection.unwrap()]);
let file = File::new(String::from(file_name));
let mut wav = Wav::new(file).unwrap();
println!("sample rate: {}", wav.sample_rate());
println!("channels: {:?}", wav.channels() as u8);
let mut buf = [0_u8; AUDIO_BUFFER_LEN];
loop {
if audio_buffer_ready() {
if wav.is_eof() {
wav.restart().unwrap()
break;
}
let _read = wav.read(&mut buf).unwrap();
@@ -55,6 +91,7 @@ pub fn main() {
}
}
}
}
struct File {
current_pos: usize,