fix warnings in initial API

This commit is contained in:
bunnie 2021-03-03 23:27:30 +08:00
parent e62256b3df
commit 2b768e9b86
8 changed files with 57 additions and 47 deletions

1
Cargo.lock generated
View file

@ -532,6 +532,7 @@ version = "0.1.0"
dependencies = [
"graphics-server",
"heapless",
"keyboard",
"log",
"log-server",
"rkyv",

View file

@ -136,11 +136,10 @@ fn xmain() -> ! {
xous::create_thread_simple(status_thread, chatlayout.status.gid()).expect("GAM: couldn't create status thread");
// connect to the IME front end, and set its canvas
/*
info!("GAM: acquiring connection to IMEF...");
let imef_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_IME_FRONT).expect("GAM: can't connect to the IME front end");
ime_frontend::set_canvas(imef_conn, chatlayout.input).expect("GAM: couldn't set IMEF canvas");
*/
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
info!("GAM: entering main loop");
loop {

View file

@ -14,6 +14,7 @@ xous-names = { path = "../xous-names" }
graphics-server = {path = "../graphics-server"}
log = "0.4"
rkyv = { version = "0.3.1", default_features = false }
keyboard = {path="../keyboard"}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -11,6 +11,10 @@ use log::{error, info};
use graphics_server::Gid;
use heapless::Vec;
use heapless::consts::U32;
use core::pin::Pin;
use rkyv::Unarchive;
use rkyv::archived_value;
/*
what else do we need:
@ -20,12 +24,13 @@ what else do we need:
fn draw_canvas(gam_conn: xous::CID, canvas: Gid, pred_conn: xous::CID, newkeys: [char; 4]) {
// this is just reference to remind me how to decode the key array
/*
for &k in newkeys.iter() {
if k != '\u{0000}' {
key_queue.push(k).unwrap();
if debug1{info!("IMEF: got key '{}'", k);}
}
}
}*/
}
@ -56,6 +61,7 @@ fn xmain() -> ! {
match opcode {
Opcode::SetCanvas(g) => {
canvas = g;
break;
},
_ => info!("IMEF: expected canvas Gid, but got {:?}", opcode)
}
@ -68,6 +74,7 @@ fn xmain() -> ! {
info!("IMEF: entering main loop");
loop {
let envelope = xous::receive_message(imef_sid).unwrap();
if debug1{info!("IMEF: got message {:?}", envelope);}
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
match opcode {
Opcode::SetCanvas(g) => {
@ -77,7 +84,7 @@ fn xmain() -> ! {
},
_ => info!("IMEF: unhandled opcode {:?}", opcode)
}
} else if let xous::Message::Borrow(m) = &msg.body {
} else if let xous::Message::Borrow(m) = &envelope.body {
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let bytes = Pin::new(buf.as_ref());
let value = unsafe {
@ -86,7 +93,10 @@ fn xmain() -> ! {
match &*value {
rkyv::Archived::<api::Opcode>::SetPrediction(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
prediction_conn = xous_names::request_connection(s.as_str());
match xous_names::request_connection(s.as_str().expect("IMEF: SetPrediction received malformed server name")) {
Ok(pc) => prediction_conn = pc,
_ => error!("IMEF: can't find predictive engine {}, retaining existign one.", s.as_str().expect("IMEF: SetPrediction received malformed server name")),
}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
};

View file

@ -1,19 +1,17 @@
use xous::{Message, ScalarMessage};
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub struct Prediction {
index: u32,
string: xous::String,
pub index: u32,
pub string: xous::String<4096>,
}
#[allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
pub enum Opcode {
/// update with the latest input candidate. Replaces the previous input.
Input(xous::String),
Input(xous::String<4096>),
/// feed back to the IME plugin as to what was picked, so predictions can be updated
Picked(xous::String),
Picked(xous::String<4096>),
/// fetch the prediction at a given index, where the index is ordered from 0..N, where 0 is the most likely prediction
/// if there is no prediction available, just return an empty string

View file

@ -1,44 +1,43 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::{CID, send_message, String};
use xous::{CID, String};
pub fn set_input(cid: CID, s: String) -> Result<(), xous::Error> {
pub fn set_input(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
let mut rkyv_input = api::Opcode::Input(*s);
let rkyv_input = api::Opcode::Input(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_s).expect("IMES|API: couldn't archive input string");
let mut xous_buffer = writer.into_inner();
let pos = writer.archive(&rkyv_input).expect("IMES|API: couldn't archive input string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IMES|API: set_input operation failure");
Ok(())
}
pub fn feedback_picked(cid: CID, s: String) -> Result<(), xous::Error> {
pub fn feedback_picked(cid: CID, s: String<4096>) -> Result<(), xous::Error> {
use rkyv::Write;
let mut rkyv_input = api::Opcode::Picked(*s);
let rkyv_picked = api::Opcode::Picked(s);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_s).expect("IMES|API: couldn't archive picked string");
let mut xous_buffer = writer.into_inner();
let pos = writer.archive(&rkyv_picked).expect("IMES|API: couldn't archive picked string");
let xous_buffer = writer.into_inner();
xous_buffer.lend(cid, pos as u32).expect("IMES|API: feedback_picked operation failure");
Ok(())
}
pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String, xous::Error> {
pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String<4096>, xous::Error> {
use rkyv::Write;
use rkyv::Unarchive;
let mut mutstr = xous::String::new();
let mut prediction = api::Prediction {
let prediction = api::Prediction {
index,
string: mutstr
string: xous::String::<4096>::new(),
};
let mut pred_op = api::Opcode::Prediction(*prediction);
let pred_op = api::Opcode::Prediction(prediction);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&pred_op).expect("IMES|API: couldn't archive prediction request");
@ -50,11 +49,11 @@ pub fn get_prediction(cid: CID, index: u32) -> Result<xous::String, xous::Error>
if let rkyv::Archived::<api::Opcode>::Prediction(result) = returned {
let pred_r: api::Prediction = result.unarchive();
let retstring: xous::String = pred_r.string.clone();
let retstring: xous::String<4096> = pred_r.string.clone();
Ok(retstring)
} else {
let r = returned.unarchive();
info!("get_prediciton saw an unhandled return of {:?}", r);
log::info!("get_prediciton saw an unhandled return of {:?}", r);
Err(xous::Error::InvalidString)
}
}

View file

@ -4,13 +4,14 @@
mod api;
use api::*;
use core::convert::TryFrom;
use log::{error, info};
use heapless::spsc::Queue;
use heapless::consts::U3;
use rkyv::Unarchive;
use core::pin::Pin;
use rkyv::{archived_value, archived_value_mut};
#[xous::xous_main]
fn xmain() -> ! {
let debug1 = true;
@ -24,12 +25,12 @@ fn xmain() -> ! {
info!("IME_SH: ready to accept requests");
let mut history: Queue<xous::String, U3> = Queue::new(); // this has 2^3 elements = 8
let mut history: Queue<xous::String<4096>, U3> = Queue::new(); // this has 2^3 elements = 8
let history_max = 8;
loop {
let envelope = xous::receive_message(ime_sh_sid).unwrap();
if let xous::Message::Borrow(m) = &msg.body {
if let xous::Message::Borrow(m) = &envelope.body {
let buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let bytes = Pin::new(buf.as_ref());
let value = unsafe {
@ -39,7 +40,7 @@ fn xmain() -> ! {
rkyv::Archived::<api::Opcode>::Input(rkyv_s) => {
let s: xous::String<4096> = rkyv_s.unarchive();
if history.len() == history_max {
history.dequeue()?;
history.dequeue().expect("IME_SH: couldn't dequeue history");
}
history.enqueue(s).expect("IME_SH: couldn't store history");
},
@ -47,9 +48,9 @@ fn xmain() -> ! {
let s: xous::String<4096> = rkyv_s.unarchive();
if debug1{info!("Received prediction feedback but ignored: {}", s);}
},
_ => panic!("IME_SH: invalid response from server -- corruption occurred in MemoryMessage")
_ => error!("IME_SH: unknown Borrow message")
};
} else if let xous::Message::MutableBorrow(m) = &msg.body {
} else if let xous::Message::MutableBorrow(m) = &envelope.body {
let mut buf = unsafe { xous::XousBuffer::from_memory_message(m) };
let value = unsafe {
archived_value_mut::<api::Opcode>(Pin::new(buf.as_mut()), m.id as usize)
@ -59,28 +60,29 @@ fn xmain() -> ! {
let mut prediction: api::Prediction = pred_r.unarchive();
if history.len() > 0 {
let mut index = prediction.index;
if index >= history.len() {
index = history.len() - 1;
if index >= history.len() as u32 {
index = history.len() as u32 - 1;
}
let mut i = history_len();
let mut i = history.len() as u32;
let mut retstr = xous::String::new();
for s in history.iter() {
for &s in history.iter() {
// iterator is from oldest to newest, so do some math to go from newest to oldest
if (history.len() - i) == index {
if (history.len() as u32 - i) == index {
retstr = s;
break;
}
i = i + 1;
}
prediction.string = s;
prediction.string = retstr;
} else { // there is no history
// return the empty string
prediction.string = xous::String::new();
}
}
},
_ => error!("IME_SH: unknown MutableBorrow message"),
}
} else {
info!("IME_SH: received unknown message type {}", envelope);
info!("IME_SH: received unknown message type {:?}", envelope);
panic!("IME_SH: received unknown message type");
}
}

View file

@ -39,8 +39,8 @@ fn try_main() -> Result<(), DynError> {
let hw_pkgs = [
"gam",
//"shell",
//"ime-frontend",
//"ime-plugin-shell",
"ime-frontend",
"ime-plugin-shell",
"graphics-server",
"ticktimer-server",
"log-server",
@ -192,8 +192,8 @@ fn run(debug: bool) -> Result<(), DynError> {
let init = [
//"shell",
"gam",
//"ime-frontend",
//"ime-plugin-shell",
"ime-frontend",
"ime-plugin-shell",
//"benchmark",
//"benchmark-target",
"log-server",