diff --git a/Cargo.lock b/Cargo.lock index c419f6c43..05a35e868 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1402,7 +1402,7 @@ dependencies = [ "content-plugin-api", "gam", "graphics-server", - "heapless 0.5.6", + "heapless 0.7.0", "ime-plugin-api", "keyboard", "llio", @@ -1414,6 +1414,7 @@ dependencies = [ "rtc", "susres", "ticktimer-server", + "trng", "utralib", "xous", "xous-ipc", diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index c81e49745..eeecc747b 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -10,7 +10,6 @@ com = {path = "../com"} content-plugin-api = {path = "../content-plugin-api"}# all content canvas providers must provide this API gam = {path = "../gam"} graphics-server = {path = "../graphics-server"} -heapless = "0.5.6" ime-plugin-api = {path = "../ime-plugin-api"} llio = {path = "../llio"} rtc = {path = "../rtc"} @@ -24,6 +23,9 @@ keyboard = {path = "../keyboard"} susres = {path = "../susres"} codec = {path = "../codec"} +heapless = "0.7.0" +trng = {path = "../trng"} + num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 076f49b9f..0cb7d8938 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -1,6 +1,8 @@ use xous::{MessageEnvelope}; use xous_ipc::String; use core::fmt::Write; + +use heapless::FnvIndexMap; /////////////////////////// Common items to all commands pub trait ShellCmdApi<'a> { // user implemented: @@ -42,6 +44,22 @@ pub struct CommonEnv { com: com::Com, ticktimer: ticktimer_server::Ticktimer, gam: gam::Gam, + cb_registrations: heapless::FnvIndexMap::, 8>, + trng: trng::Trng, +} +impl CommonEnv { + pub fn register_handler(&mut self, verb: String::<256>) -> u32 { + let mut key: u32; + loop { + key = self.trng.get_u32().unwrap(); + // reserve the bottom 1000 IDs for the main loop enums. + if !self.cb_registrations.contains_key(&key) && (key > 1000) { + break; + } + } + self.cb_registrations.insert(key, verb).unwrap(); + key + } } /* @@ -97,6 +115,8 @@ impl CmdEnv { com: com::Com::new(&xns).expect("could't connect to COM"), ticktimer: ticktimer, gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"), + cb_registrations: FnvIndexMap::new(), + trng: trng::Trng::new(&xns).unwrap(), }, lastverb: String::<256>::new(), ///// 3. initialize your storage, by calling new() @@ -173,14 +193,25 @@ impl CmdEnv { } } else if let Some(callback) = maybe_callback { let mut cmd_ret: Result>, xous::Error> = Ok(None); - if self.lastverb.len() > 0 { - let verb = self.lastverb.to_str(); - for cmd in commands.iter_mut() { - if cmd.matches(verb) { - cmd_ret = cmd.callback(callback, &mut self.common_env); - break; - }; + // first check and see if we have a callback registration; if not, just map to the last verb + let verb = match self.common_env.cb_registrations.get(&(callback.body.id() as u32)) { + Some(verb) => { + verb.to_str() + }, + None => { + self.lastverb.to_str() } + }; + // now dispatch + let mut verbfound = false; + for cmd in commands.iter_mut() { + if cmd.matches(verb) { + cmd_ret = cmd.callback(callback, &mut self.common_env); + verbfound = true; + break; + }; + } + if verbfound { cmd_ret } else { Ok(None) diff --git a/services/shellchat/src/cmds/audio.rs b/services/shellchat/src/cmds/audio.rs index baa844d0b..11079a7b4 100644 --- a/services/shellchat/src/cmds/audio.rs +++ b/services/shellchat/src/cmds/audio.rs @@ -14,6 +14,8 @@ pub struct Audio { raw_len_bytes: u32, play_ptr_bytes: usize, framecount: u32, + callback_id: Option, + callback_conn: u32, } impl Audio { pub fn new(xns: &xous_names::XousNames) -> Self { @@ -30,15 +32,10 @@ impl Audio { unsafe{ raw_header[i] = (*samples)[i] }; } - log::trace!("setting up audio stream"); + log::trace!("setting up codec hardware parameters"); codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults"); - log::trace!("getting a callback ID"); - let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(); - log::trace!("hooking frame callback"); - codec.hook_frame_callback(0xDEAD_BEEF, callback_conn).unwrap(); // any non-handled IDs get routed to our callback port - log::trace!("returning from setup"); - let mut audio = Audio { + let audio = Audio { codec, sample, header: Header::from(raw_header), @@ -46,33 +43,10 @@ impl Audio { raw_len_bytes: unsafe{*(sample.as_ptr().add(40) as *const u32)}, play_ptr_bytes: 0, framecount: 0, - }; - /* - // load the initial sample data - let (play_free, _) = audio.codec.free_frames().unwrap(); + callback_id: None, + callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(), - let mut frames: FrameRing = FrameRing::new(); - let frames_to_push = if frames.writeable_count() < play_free { - frames.writeable_count() - } else { - play_free }; - log::debug!("loading up {} frames", frames_to_push); - audio.framecount += frames_to_push as u32; - for i in 0..frames_to_push { - let mut frame: [u32; codec::FIFO_DEPTH] = [codec::ZERO_PCM as u32 | (codec::ZERO_PCM as u32) << 16; codec::FIFO_DEPTH]; - for sample in frame.iter_mut() { - *sample = unsafe{audio.raw_data.add(i).read_volatile()}; - } - audio.play_ptr_bytes += codec::FIFO_DEPTH * 4; - frames.nq_frame(frame).unwrap(); - } - log::debug!("pushing frames"); - audio.codec.swap_frames(&mut frames).unwrap(); - // start the playing - log::debug!("starting playback"); - audio.codec.resume().unwrap(); - */ audio } } @@ -80,7 +54,7 @@ impl Audio { impl<'a> ShellCmdApi<'a> for Audio { cmd_api!(audio); - fn process(&mut self, args: String::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { + fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; let mut ret = String::<1024>::new(); @@ -91,6 +65,13 @@ impl<'a> ShellCmdApi<'a> for Audio { if let Some(sub_cmd) = tokens.next() { match sub_cmd { "play" => { + if self.callback_id.is_none() { + let cb_id = env.register_handler(String::<256>::from_str(self.verb())); + log::trace!("hooking frame callback with ID {}", cb_id); + self.codec.hook_frame_callback(cb_id, self.callback_conn).unwrap(); // any non-handled IDs get routed to our callback port + self.callback_id = Some(cb_id); + } + write!(ret, "Playing sample...").unwrap(); // load the initial sample data let (play_free, _) = self.codec.free_frames().unwrap(); diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index 81fa182dd..6b43809d3 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -11,7 +11,6 @@ use xous::MessageEnvelope; use xous_ipc::{String, Buffer}; use heapless::spsc::Queue; -use heapless::consts::U16; mod cmds; use cmds::*; @@ -33,7 +32,7 @@ struct Repl { msg: Option, // record our input history - history: Queue, + history: Queue::, content: Gid, gam: gam::Gam, @@ -227,7 +226,7 @@ use num_traits::{ToPrimitive, FromPrimitive}; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] enum ShellOpcode { /// a line of text has arrived - Line, + Line = 0, // make sure we occupy opcodes with discriminants < 1000, as the rest are used for callbacks /// redraw our UI Redraw, /// exit the application @@ -259,7 +258,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 42d164a5f..51e3fda86 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -4,6 +4,7 @@ pub mod api; use xous::{CID, send_message}; use num_traits::ToPrimitive; +#[derive(Debug)] pub struct Trng { conn: CID, }