mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
add callback test back into shellchat
This commit is contained in:
parent
2d5c1dd6db
commit
9c49940e1d
2 changed files with 82 additions and 31 deletions
|
|
@ -1,10 +1,15 @@
|
|||
use xous::String;
|
||||
use xous::{String, MessageEnvelope};
|
||||
use core::fmt::Write;
|
||||
/////////////////////////// Common items to all commands
|
||||
pub trait ShellCmdApi<'a> {
|
||||
// user implemented:
|
||||
// called to process the command with the remainder of the string attached
|
||||
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error>;
|
||||
// called to process incoming messages that may have been origniated by the most recently issued command
|
||||
fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
log::info!("SHCH: received unhandled message {:?}", msg);
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
// created with cmd_api! macro
|
||||
// checks if the command matches the current verb in question
|
||||
|
|
@ -28,6 +33,7 @@ macro_rules! cmd_api {
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////// Command shell integration
|
||||
#[derive(Debug)]
|
||||
pub struct CommonEnv {
|
||||
|
|
@ -55,14 +61,17 @@ mod echo; use echo::*;
|
|||
mod test; use test::*;
|
||||
mod sleep; use sleep::*;
|
||||
mod sensors; use sensors::*;
|
||||
mod callback; use callback::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CmdEnv {
|
||||
common_env: CommonEnv,
|
||||
lastverb: String::<256>,
|
||||
///// 2. declare storage for your command here.
|
||||
test_cmd: Test,
|
||||
sleep_cmd: Sleep,
|
||||
sensors_cmd: Sensors,
|
||||
callback_cmd: CallBack,
|
||||
}
|
||||
impl CmdEnv {
|
||||
pub fn new(gam: xous::CID) -> CmdEnv {
|
||||
|
|
@ -74,14 +83,16 @@ impl CmdEnv {
|
|||
ticktimer: xous::connect(ticktimer_server_id).unwrap(),
|
||||
gam,
|
||||
},
|
||||
lastverb: String::<256>::new(),
|
||||
///// 3. initialize your storage, by calling new()
|
||||
test_cmd: Test::new(),
|
||||
sleep_cmd: Sleep::new(),
|
||||
sensors_cmd: Sensors::new(),
|
||||
callback_cmd: CallBack::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dispatch(&mut self, cmdline: &mut String::<1024>) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
pub fn dispatch(&mut self, maybe_cmdline: Option<&mut String::<1024>>, maybe_callback: Option<&MessageEnvelope>) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
let mut ret = String::<1024>::new();
|
||||
|
||||
let mut echo_cmd = Echo {}; // this command has no persistent storage, so we can "create" it every time we call dispatch (but it's a zero-cost absraction so this doesn't actually create any instructions)
|
||||
|
|
@ -91,38 +102,59 @@ impl CmdEnv {
|
|||
&mut self.test_cmd,
|
||||
&mut self.sleep_cmd,
|
||||
&mut self.sensors_cmd,
|
||||
&mut self.callback_cmd,
|
||||
];
|
||||
|
||||
let maybe_verb = tokenize(cmdline);
|
||||
if let Some(cmdline) = maybe_cmdline {
|
||||
let maybe_verb = tokenize(cmdline);
|
||||
|
||||
let mut cmd_ret: Result<Option<String::<1024>>, xous::Error> = Ok(None);
|
||||
if let Some(verb_string) = maybe_verb {
|
||||
let verb = verb_string.to_str();
|
||||
let mut cmd_ret: Result<Option<String::<1024>>, xous::Error> = Ok(None);
|
||||
if let Some(verb_string) = maybe_verb {
|
||||
let verb = verb_string.to_str();
|
||||
|
||||
// search through the list of commands linearly until one matches,
|
||||
// then run it.
|
||||
let mut match_found = false;
|
||||
for cmd in commands.iter_mut() {
|
||||
if cmd.matches(verb) {
|
||||
match_found = true;
|
||||
cmd_ret = cmd.process(*cmdline, &mut self.common_env);
|
||||
};
|
||||
}
|
||||
|
||||
// if none match, create a list of available commands
|
||||
if !match_found {
|
||||
let mut first = true;
|
||||
write!(ret, "Commands: ").unwrap();
|
||||
for cmd in commands.iter() {
|
||||
if !first {
|
||||
ret.append(", ")?;
|
||||
}
|
||||
ret.append(cmd.verb())?;
|
||||
first = false;
|
||||
// search through the list of commands linearly until one matches,
|
||||
// then run it.
|
||||
let mut match_found = false;
|
||||
for cmd in commands.iter_mut() {
|
||||
if cmd.matches(verb) {
|
||||
match_found = true;
|
||||
cmd_ret = cmd.process(*cmdline, &mut self.common_env);
|
||||
self.lastverb.clear();
|
||||
write!(self.lastverb, "{}", verb).expect("SHCH: couldn't record last verb");
|
||||
};
|
||||
}
|
||||
|
||||
// if none match, create a list of available commands
|
||||
if !match_found {
|
||||
let mut first = true;
|
||||
write!(ret, "Commands: ").unwrap();
|
||||
for cmd in commands.iter() {
|
||||
if !first {
|
||||
ret.append(", ")?;
|
||||
}
|
||||
ret.append(cmd.verb())?;
|
||||
first = false;
|
||||
}
|
||||
Ok(Some(ret))
|
||||
} else {
|
||||
cmd_ret
|
||||
}
|
||||
Ok(Some(ret))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else if let Some(callback) = maybe_callback {
|
||||
let mut cmd_ret: Result<Option<String::<1024>>, 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;
|
||||
};
|
||||
}
|
||||
cmd_ret
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
#![cfg_attr(target_os = "none", no_main)]
|
||||
|
||||
use log::{error, info};
|
||||
use log::info;
|
||||
|
||||
use core::fmt::Write;
|
||||
use core::convert::TryFrom;
|
||||
|
|
@ -12,6 +12,7 @@ use core::pin::Pin;
|
|||
|
||||
use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd};
|
||||
use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, GlyphStyle, PixelColor};
|
||||
use xous::MessageEnvelope;
|
||||
|
||||
use heapless::spsc::Queue;
|
||||
use heapless::consts::U16;
|
||||
|
|
@ -32,7 +33,8 @@ struct Repl {
|
|||
// optional structures that indicate new input to the Repl loop per iteration
|
||||
// an input string
|
||||
input: Option<xous::String<1024>>,
|
||||
// examples of future inputs: returned values via other services
|
||||
// messages from other servers
|
||||
msg: Option<MessageEnvelope>,
|
||||
|
||||
// record our input history
|
||||
history: Queue<History, U16>,
|
||||
|
|
@ -57,6 +59,7 @@ impl Repl{
|
|||
let screensize = gam::get_canvas_bounds(gam_conn, content).expect("SHCH: couldn't get dimensions of content canvas");
|
||||
Repl {
|
||||
input: None,
|
||||
msg: None,
|
||||
history: Queue::new(),
|
||||
content,
|
||||
gam: gam_conn,
|
||||
|
|
@ -80,6 +83,10 @@ impl Repl{
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn msg(&mut self, message: MessageEnvelope) {
|
||||
self.msg = Some(message);
|
||||
}
|
||||
|
||||
fn circular_push(&mut self, item: History) {
|
||||
if self.history.len() == self.history.capacity() {
|
||||
self.history.dequeue().expect("SHCH: couldn't dequeue historye");
|
||||
|
|
@ -108,7 +115,17 @@ impl Repl{
|
|||
|
||||
// take the input and pass it on to the various command parsers, and attach result
|
||||
if let Some(mut local) = self.input {
|
||||
if let Some(res) = self.env.dispatch(&mut local).expect("SHCH: command dispatch failed") {
|
||||
if let Some(res) = self.env.dispatch(Some(&mut local), None).expect("SHCH: command dispatch failed") {
|
||||
let mut response = xous::String::<1024>::new();
|
||||
write!(response, "{}", res).expect("SHCH: can't copy result to history");
|
||||
let output_history = History {
|
||||
text: response,
|
||||
is_input: false
|
||||
};
|
||||
self.circular_push(output_history);
|
||||
}
|
||||
} else if let Some(msg) = &self.msg {
|
||||
if let Some(res) = self.env.dispatch(None, Some(msg)).expect("SCHC: callback failed") {
|
||||
let mut response = xous::String::<1024>::new();
|
||||
write!(response, "{}", res).expect("SHCH: can't copy result to history");
|
||||
let output_history = History {
|
||||
|
|
@ -121,6 +138,7 @@ impl Repl{
|
|||
|
||||
// clear all the inputs to the loop, so we don't process them twice
|
||||
self.input = None;
|
||||
self.msg = None;
|
||||
// redraw UI now that we've responded
|
||||
self.redraw().expect("SHCH: can't redraw");
|
||||
|
||||
|
|
@ -233,7 +251,8 @@ fn xmain() -> ! {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
error!("SHCH: couldn't convert message");
|
||||
repl.msg(envelope);
|
||||
update_repl = true;
|
||||
}
|
||||
|
||||
if update_repl {
|
||||
|
|
|
|||
Loading…
Reference in a new issue