2021-03-03 06:53:33 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
use ime_plugin_api::*;
|
|
|
|
|
use core::convert::TryFrom;
|
2021-03-03 06:53:33 +00:00
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
use heapless::spsc::Queue;
|
|
|
|
|
use heapless::consts::U3;
|
|
|
|
|
|
2021-03-03 15:27:30 +00:00
|
|
|
use rkyv::Unarchive;
|
|
|
|
|
use core::pin::Pin;
|
|
|
|
|
use rkyv::{archived_value, archived_value_mut};
|
|
|
|
|
|
2021-03-03 06:53:33 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
let debug1 = true;
|
|
|
|
|
log_server::init_wait().unwrap();
|
|
|
|
|
info!("IME_SH: my PID is {}", xous::process::id());
|
|
|
|
|
|
|
|
|
|
let ime_sh_sid = xous_names::register_name(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("IME_SH: can't register server");
|
|
|
|
|
info!("IME_SH: registered with NS -- {:?}", ime_sh_sid);
|
|
|
|
|
|
|
|
|
|
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("IME_SH: can't connect to IMEF");
|
|
|
|
|
|
|
|
|
|
info!("IME_SH: ready to accept requests");
|
|
|
|
|
|
2021-03-03 15:27:30 +00:00
|
|
|
let mut history: Queue<xous::String<4096>, U3> = Queue::new(); // this has 2^3 elements = 8
|
2021-03-03 06:53:33 +00:00
|
|
|
let history_max = 8;
|
|
|
|
|
|
2021-03-03 19:14:26 +00:00
|
|
|
let mytriggers = PredictionTriggers {
|
|
|
|
|
trigger_newline: true,
|
|
|
|
|
trigger_punctuation: false,
|
|
|
|
|
trigger_whitespace: false,
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-03 06:53:33 +00:00
|
|
|
loop {
|
|
|
|
|
let envelope = xous::receive_message(ime_sh_sid).unwrap();
|
2021-03-03 19:14:26 +00:00
|
|
|
if debug1{info!("IME_SH: received message {:?}", envelope);}
|
2021-03-03 15:27:30 +00:00
|
|
|
if let xous::Message::Borrow(m) = &envelope.body {
|
2021-03-03 06:53:33 +00:00
|
|
|
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let bytes = Pin::new(buf.as_ref());
|
|
|
|
|
let value = unsafe {
|
2021-03-03 19:14:26 +00:00
|
|
|
archived_value::<Opcode>(&bytes, m.id as usize)
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
|
|
|
|
match &*value {
|
2021-03-03 19:14:26 +00:00
|
|
|
rkyv::Archived::<Opcode>::Input(_rkyv_s) => {
|
|
|
|
|
// shell does nothing with the input, it only keeps track of
|
|
|
|
|
// the picked results
|
|
|
|
|
},
|
|
|
|
|
rkyv::Archived::<Opcode>::Picked(rkyv_s) => {
|
2021-03-03 06:53:33 +00:00
|
|
|
let s: xous::String<4096> = rkyv_s.unarchive();
|
|
|
|
|
if history.len() == history_max {
|
2021-03-03 15:27:30 +00:00
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue history");
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
|
|
|
|
history.enqueue(s).expect("IME_SH: couldn't store history");
|
|
|
|
|
},
|
2021-03-03 15:27:30 +00:00
|
|
|
_ => error!("IME_SH: unknown Borrow message")
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
2021-03-03 15:27:30 +00:00
|
|
|
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
|
2021-03-03 06:53:33 +00:00
|
|
|
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
|
|
|
|
|
let value = unsafe {
|
2021-03-03 19:14:26 +00:00
|
|
|
archived_value_mut::<Opcode>(Pin::new(buf.as_mut()), m.id as usize)
|
2021-03-03 06:53:33 +00:00
|
|
|
};
|
|
|
|
|
match &*value {
|
2021-03-03 19:14:26 +00:00
|
|
|
rkyv::Archived::<Opcode>::Prediction(pred_r) => {
|
|
|
|
|
let mut prediction: Prediction = pred_r.unarchive();
|
2021-03-03 06:53:33 +00:00
|
|
|
if history.len() > 0 {
|
|
|
|
|
let mut index = prediction.index;
|
2021-03-03 15:27:30 +00:00
|
|
|
if index >= history.len() as u32 {
|
|
|
|
|
index = history.len() as u32 - 1;
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
2021-03-03 15:27:30 +00:00
|
|
|
let mut i = history.len() as u32;
|
2021-03-03 06:53:33 +00:00
|
|
|
let mut retstr = xous::String::new();
|
2021-03-03 15:27:30 +00:00
|
|
|
for &s in history.iter() {
|
2021-03-03 06:53:33 +00:00
|
|
|
// iterator is from oldest to newest, so do some math to go from newest to oldest
|
2021-03-03 15:27:30 +00:00
|
|
|
if (history.len() as u32 - i) == index {
|
2021-03-03 06:53:33 +00:00
|
|
|
retstr = s;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i = i + 1;
|
|
|
|
|
}
|
2021-03-03 15:27:30 +00:00
|
|
|
prediction.string = retstr;
|
2021-03-03 06:53:33 +00:00
|
|
|
} else { // there is no history
|
|
|
|
|
// return the empty string
|
|
|
|
|
prediction.string = xous::String::new();
|
|
|
|
|
}
|
2021-03-03 15:27:30 +00:00
|
|
|
},
|
|
|
|
|
_ => error!("IME_SH: unknown MutableBorrow message"),
|
2021-03-03 06:53:33 +00:00
|
|
|
}
|
2021-03-03 19:14:26 +00:00
|
|
|
} else if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
Opcode::Unpick => {
|
|
|
|
|
if history.len() == 1 {
|
|
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (1)");
|
|
|
|
|
} else if history.len() > 1 {
|
|
|
|
|
// rotate everything around except the last entry
|
|
|
|
|
for _ in 0 .. history.len() - 1 {
|
|
|
|
|
let s = history.dequeue().expect("IME_SH: couldn't dequee in Unpick (>1)");
|
|
|
|
|
history.enqueue(s).expect("IME_SH: couldn't enqueue in Unpick");
|
|
|
|
|
}
|
|
|
|
|
// discard the last entry
|
|
|
|
|
history.dequeue().expect("IME_SH: couldn't dequeue in Unpick (>1 last)");
|
|
|
|
|
}
|
|
|
|
|
// in case of 0 length, do nothing
|
|
|
|
|
},
|
|
|
|
|
Opcode::GetPredictionTriggers => {
|
|
|
|
|
xous::return_scalar(envelope.sender, mytriggers.into()).expect("IME_SH: couldn't return GetPredictionTriggers");
|
|
|
|
|
},
|
|
|
|
|
_ => error!("IME_SH: unknown Opcode"),
|
|
|
|
|
}
|
2021-03-03 06:53:33 +00:00
|
|
|
} else {
|
2021-03-03 15:27:30 +00:00
|
|
|
info!("IME_SH: received unknown message type {:?}", envelope);
|
2021-03-03 06:53:33 +00:00
|
|
|
panic!("IME_SH: received unknown message type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|