factor out the shellchat callback, which mysterious breaks things

This commit is contained in:
bunnie 2021-03-16 18:03:12 +08:00
parent 5ba5dc0340
commit eda33b5de7
3 changed files with 33 additions and 85 deletions

View file

@ -21,8 +21,8 @@ riscv = "0.5.6"
debug-print = []
print-panics = []
report-memory = ["stats_alloc"]
default = ["print-panics", "debug-print"]
#default = ["print-panics"]
#default = ["print-panics", "debug-print"]
default = ["print-panics"]
[target.'cfg(any(windows, unix))'.dependencies]
lazy_static = "1.4"

View file

@ -1,15 +1,10 @@
use xous::{String, MessageEnvelope};
use xous::String;
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
@ -33,7 +28,6 @@ macro_rules! cmd_api {
};
}
/////////////////////////// Command shell integration
#[derive(Debug)]
pub struct CommonEnv {
@ -61,17 +55,14 @@ 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 {
@ -83,16 +74,14 @@ 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, maybe_cmdline: Option<&mut String::<1024>>, maybe_callback: Option<&MessageEnvelope>) -> Result<Option<String::<1024>>, xous::Error> {
pub fn dispatch(&mut self, cmdline: &mut String::<1024>) -> 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)
@ -102,59 +91,38 @@ impl CmdEnv {
&mut self.test_cmd,
&mut self.sleep_cmd,
&mut self.sensors_cmd,
&mut self.callback_cmd,
];
if let Some(cmdline) = maybe_cmdline {
let maybe_verb = tokenize(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);
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
}
} else {
Ok(None)
// 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);
};
}
} 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;
};
// 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;
}
cmd_ret
Ok(Some(ret))
} else {
Ok(None)
cmd_ret
}
} else {
Ok(None)

View file

@ -1,7 +1,7 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
use log::info;
use log::{error, info};
use core::fmt::Write;
use core::convert::TryFrom;
@ -16,8 +16,6 @@ use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, Gl
use heapless::spsc::Queue;
use heapless::consts::U16;
use xous::MessageEnvelope;
mod cmds;
use cmds::*;
@ -34,8 +32,7 @@ struct Repl {
// optional structures that indicate new input to the Repl loop per iteration
// an input string
input: Option<xous::String<1024>>,
// messages from other servers
msg: Option<MessageEnvelope>,
// examples of future inputs: returned values via other services
// record our input history
history: Queue<History, U16>,
@ -60,7 +57,6 @@ 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,
@ -84,10 +80,6 @@ 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");
@ -116,17 +108,7 @@ 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(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") {
if let Some(res) = self.env.dispatch(&mut local).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 {
@ -139,7 +121,6 @@ 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");
@ -252,8 +233,7 @@ fn xmain() -> ! {
}
}
} else {
repl.msg(envelope);
update_repl = true;
error!("SHCH: couldn't convert message");
}
if update_repl {