mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
factor out the shellchat callback, which mysterious breaks things
This commit is contained in:
parent
5ba5dc0340
commit
eda33b5de7
3 changed files with 33 additions and 85 deletions
|
|
@ -21,8 +21,8 @@ riscv = "0.5.6"
|
||||||
debug-print = []
|
debug-print = []
|
||||||
print-panics = []
|
print-panics = []
|
||||||
report-memory = ["stats_alloc"]
|
report-memory = ["stats_alloc"]
|
||||||
default = ["print-panics", "debug-print"]
|
#default = ["print-panics", "debug-print"]
|
||||||
#default = ["print-panics"]
|
default = ["print-panics"]
|
||||||
|
|
||||||
[target.'cfg(any(windows, unix))'.dependencies]
|
[target.'cfg(any(windows, unix))'.dependencies]
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,10 @@
|
||||||
use xous::{String, MessageEnvelope};
|
use xous::String;
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
/////////////////////////// Common items to all commands
|
/////////////////////////// Common items to all commands
|
||||||
pub trait ShellCmdApi<'a> {
|
pub trait ShellCmdApi<'a> {
|
||||||
// user implemented:
|
// user implemented:
|
||||||
// called to process the command with the remainder of the string attached
|
// 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>;
|
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
|
// created with cmd_api! macro
|
||||||
// checks if the command matches the current verb in question
|
// checks if the command matches the current verb in question
|
||||||
|
|
@ -33,7 +28,6 @@ macro_rules! cmd_api {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////// Command shell integration
|
/////////////////////////// Command shell integration
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CommonEnv {
|
pub struct CommonEnv {
|
||||||
|
|
@ -61,17 +55,14 @@ mod echo; use echo::*;
|
||||||
mod test; use test::*;
|
mod test; use test::*;
|
||||||
mod sleep; use sleep::*;
|
mod sleep; use sleep::*;
|
||||||
mod sensors; use sensors::*;
|
mod sensors; use sensors::*;
|
||||||
mod callback; use callback::*;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CmdEnv {
|
pub struct CmdEnv {
|
||||||
common_env: CommonEnv,
|
common_env: CommonEnv,
|
||||||
lastverb: String::<256>,
|
|
||||||
///// 2. declare storage for your command here.
|
///// 2. declare storage for your command here.
|
||||||
test_cmd: Test,
|
test_cmd: Test,
|
||||||
sleep_cmd: Sleep,
|
sleep_cmd: Sleep,
|
||||||
sensors_cmd: Sensors,
|
sensors_cmd: Sensors,
|
||||||
callback_cmd: CallBack,
|
|
||||||
}
|
}
|
||||||
impl CmdEnv {
|
impl CmdEnv {
|
||||||
pub fn new(gam: xous::CID) -> CmdEnv {
|
pub fn new(gam: xous::CID) -> CmdEnv {
|
||||||
|
|
@ -83,16 +74,14 @@ impl CmdEnv {
|
||||||
ticktimer: xous::connect(ticktimer_server_id).unwrap(),
|
ticktimer: xous::connect(ticktimer_server_id).unwrap(),
|
||||||
gam,
|
gam,
|
||||||
},
|
},
|
||||||
lastverb: String::<256>::new(),
|
|
||||||
///// 3. initialize your storage, by calling new()
|
///// 3. initialize your storage, by calling new()
|
||||||
test_cmd: Test::new(),
|
test_cmd: Test::new(),
|
||||||
sleep_cmd: Sleep::new(),
|
sleep_cmd: Sleep::new(),
|
||||||
sensors_cmd: Sensors::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 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)
|
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.test_cmd,
|
||||||
&mut self.sleep_cmd,
|
&mut self.sleep_cmd,
|
||||||
&mut self.sensors_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);
|
let mut cmd_ret: Result<Option<String::<1024>>, xous::Error> = Ok(None);
|
||||||
if let Some(verb_string) = maybe_verb {
|
if let Some(verb_string) = maybe_verb {
|
||||||
let verb = verb_string.to_str();
|
let verb = verb_string.to_str();
|
||||||
|
|
||||||
// search through the list of commands linearly until one matches,
|
// search through the list of commands linearly until one matches,
|
||||||
// then run it.
|
// then run it.
|
||||||
let mut match_found = false;
|
let mut match_found = false;
|
||||||
for cmd in commands.iter_mut() {
|
for cmd in commands.iter_mut() {
|
||||||
if cmd.matches(verb) {
|
if cmd.matches(verb) {
|
||||||
match_found = true;
|
match_found = true;
|
||||||
cmd_ret = cmd.process(*cmdline, &mut self.common_env);
|
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)
|
|
||||||
}
|
}
|
||||||
} else if let Some(callback) = maybe_callback {
|
|
||||||
let mut cmd_ret: Result<Option<String::<1024>>, xous::Error> = Ok(None);
|
// if none match, create a list of available commands
|
||||||
if self.lastverb.len() > 0 {
|
if !match_found {
|
||||||
let verb = self.lastverb.to_str();
|
let mut first = true;
|
||||||
for cmd in commands.iter_mut() {
|
write!(ret, "Commands: ").unwrap();
|
||||||
if cmd.matches(verb) {
|
for cmd in commands.iter() {
|
||||||
cmd_ret = cmd.callback(callback, &mut self.common_env);
|
if !first {
|
||||||
break;
|
ret.append(", ")?;
|
||||||
};
|
}
|
||||||
|
ret.append(cmd.verb())?;
|
||||||
|
first = false;
|
||||||
}
|
}
|
||||||
cmd_ret
|
Ok(Some(ret))
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
cmd_ret
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
#![cfg_attr(target_os = "none", no_main)]
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
use log::info;
|
use log::{error, info};
|
||||||
|
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
|
|
@ -16,8 +16,6 @@ use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, Gl
|
||||||
use heapless::spsc::Queue;
|
use heapless::spsc::Queue;
|
||||||
use heapless::consts::U16;
|
use heapless::consts::U16;
|
||||||
|
|
||||||
use xous::MessageEnvelope;
|
|
||||||
|
|
||||||
mod cmds;
|
mod cmds;
|
||||||
use cmds::*;
|
use cmds::*;
|
||||||
|
|
||||||
|
|
@ -34,8 +32,7 @@ struct Repl {
|
||||||
// optional structures that indicate new input to the Repl loop per iteration
|
// optional structures that indicate new input to the Repl loop per iteration
|
||||||
// an input string
|
// an input string
|
||||||
input: Option<xous::String<1024>>,
|
input: Option<xous::String<1024>>,
|
||||||
// messages from other servers
|
// examples of future inputs: returned values via other services
|
||||||
msg: Option<MessageEnvelope>,
|
|
||||||
|
|
||||||
// record our input history
|
// record our input history
|
||||||
history: Queue<History, U16>,
|
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");
|
let screensize = gam::get_canvas_bounds(gam_conn, content).expect("SHCH: couldn't get dimensions of content canvas");
|
||||||
Repl {
|
Repl {
|
||||||
input: None,
|
input: None,
|
||||||
msg: None,
|
|
||||||
history: Queue::new(),
|
history: Queue::new(),
|
||||||
content,
|
content,
|
||||||
gam: gam_conn,
|
gam: gam_conn,
|
||||||
|
|
@ -84,10 +80,6 @@ impl Repl{
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn msg(&mut self, message: MessageEnvelope) {
|
|
||||||
self.msg = Some(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn circular_push(&mut self, item: History) {
|
fn circular_push(&mut self, item: History) {
|
||||||
if self.history.len() == self.history.capacity() {
|
if self.history.len() == self.history.capacity() {
|
||||||
self.history.dequeue().expect("SHCH: couldn't dequeue historye");
|
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
|
// 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(mut local) = self.input {
|
||||||
if let Some(res) = self.env.dispatch(Some(&mut local), None).expect("SHCH: command dispatch 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 {
|
|
||||||
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();
|
let mut response = xous::String::<1024>::new();
|
||||||
write!(response, "{}", res).expect("SHCH: can't copy result to history");
|
write!(response, "{}", res).expect("SHCH: can't copy result to history");
|
||||||
let output_history = 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
|
// clear all the inputs to the loop, so we don't process them twice
|
||||||
self.input = None;
|
self.input = None;
|
||||||
self.msg = None;
|
|
||||||
// redraw UI now that we've responded
|
// redraw UI now that we've responded
|
||||||
self.redraw().expect("SHCH: can't redraw");
|
self.redraw().expect("SHCH: can't redraw");
|
||||||
|
|
||||||
|
|
@ -252,8 +233,7 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
repl.msg(envelope);
|
error!("SHCH: couldn't convert message");
|
||||||
update_repl = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if update_repl {
|
if update_repl {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue