mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
shell: use new String struct from xous
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
9192775e5e
commit
f46f7c6e81
1 changed files with 3 additions and 66 deletions
|
|
@ -8,6 +8,7 @@ mod debug;
|
||||||
mod logstr;
|
mod logstr;
|
||||||
mod timer;
|
mod timer;
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use xous::String;
|
||||||
|
|
||||||
// fn print_and_yield(index: *mut usize) -> ! {
|
// fn print_and_yield(index: *mut usize) -> ! {
|
||||||
// let num = index as usize;
|
// let num = index as usize;
|
||||||
|
|
@ -36,69 +37,6 @@ fn ensure_connection(server: xous::SID) -> xous::CID {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StringBuffer<'a> {
|
|
||||||
raw_slice: &'a mut [u8],
|
|
||||||
pub s: &'a str,
|
|
||||||
pub len: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> StringBuffer<'a> {
|
|
||||||
pub fn new(max: usize) -> StringBuffer<'a> {
|
|
||||||
let mem = xous::map_memory(None, None, max, xous::MemoryFlags::R | xous::MemoryFlags::W).unwrap();
|
|
||||||
let p = mem.as_mut_ptr();
|
|
||||||
for i in 0..max {
|
|
||||||
unsafe { p.add(i).write_volatile(0) };
|
|
||||||
}
|
|
||||||
StringBuffer {
|
|
||||||
raw_slice: unsafe { core::slice::from_raw_parts_mut(mem.as_mut_ptr(), max)},
|
|
||||||
s: unsafe {
|
|
||||||
core::str::from_utf8_unchecked(core::slice::from_raw_parts(mem.as_ptr(), 0))
|
|
||||||
},
|
|
||||||
len: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Perform an immutable lend of this Carton to the specified server.
|
|
||||||
/// This function will block until the server returns.
|
|
||||||
pub fn lend(&self, connection: xous::CID, id: usize) -> Result<xous::Result, xous::Error> {
|
|
||||||
let memory_range = xous::MemoryRange::new(self.raw_slice.as_ptr() as _, self.raw_slice.len()).unwrap();
|
|
||||||
let msg = xous::MemoryMessage {
|
|
||||||
id,
|
|
||||||
buf: memory_range,
|
|
||||||
offset: None,
|
|
||||||
valid: xous::MemorySize::new(self.len).map(|x| Some(x)).unwrap_or(None),
|
|
||||||
};
|
|
||||||
xous::send_message(connection, xous::Message::Borrow(msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
self.len = 0;
|
|
||||||
self.s = unsafe {
|
|
||||||
core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.raw_slice.as_ptr(), self.len))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> core::fmt::Display for StringBuffer<'a> {
|
|
||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
||||||
write!(f, "{}", self.s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> core::fmt::Write for StringBuffer<'a> {
|
|
||||||
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
|
|
||||||
for c in s.bytes() {
|
|
||||||
if self.len < self.raw_slice.len() {
|
|
||||||
self.raw_slice[self.len] = c;
|
|
||||||
self.len += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.s = unsafe {
|
|
||||||
core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.raw_slice.as_ptr(), self.len))
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[xous::xous_main]
|
#[xous::xous_main]
|
||||||
|
|
@ -150,7 +88,7 @@ fn shell_main() -> ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut last_time: u64 = 0;
|
let mut last_time: u64 = 0;
|
||||||
let mut string_buffer = StringBuffer::new(4096);
|
let mut string_buffer = String::new(4096);
|
||||||
loop {
|
loop {
|
||||||
// a message passing demo -- checking time
|
// a message passing demo -- checking time
|
||||||
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
||||||
|
|
@ -211,8 +149,7 @@ fn shell_main() -> ! {
|
||||||
string_buffer.clear();
|
string_buffer.clear();
|
||||||
write!(&mut string_buffer, "Elapsed time: {}ms", last_time).expect("Can't write");
|
write!(&mut string_buffer, "Elapsed time: {}ms", last_time).expect("Can't write");
|
||||||
graphics_server::clear_region(graphics_conn, 0, 0, 300, 40).expect("unable to clear region");
|
graphics_server::clear_region(graphics_conn, 0, 0, 300, 40).expect("unable to clear region");
|
||||||
string_buffer.lend(graphics_conn, 1).expect("unable to draw string");
|
graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string");
|
||||||
|
|
||||||
graphics_server::flush(graphics_conn).expect("unable to draw to screen");
|
graphics_server::flush(graphics_conn).expect("unable to draw to screen");
|
||||||
|
|
||||||
// let lfsr = move_lfsr(lfsr);
|
// let lfsr = move_lfsr(lfsr);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue