mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
examples: log: begin refactoring for desktop
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
cf992e9b09
commit
88d8274a19
2 changed files with 203 additions and 22 deletions
|
|
@ -7,4 +7,5 @@ description = "Log output program"
|
|||
|
||||
[dependencies]
|
||||
xous = { path = "../../xous-rs" }
|
||||
|
||||
#bracket-terminal = "0.8.1"
|
||||
pancurses = "*"
|
||||
|
|
|
|||
|
|
@ -8,16 +8,168 @@ mod debug;
|
|||
mod log_string;
|
||||
mod start;
|
||||
|
||||
use log_string::LogString;
|
||||
use core::fmt::Write;
|
||||
use log_string::LogString;
|
||||
|
||||
#[cfg(bracket_terminal)]
|
||||
mod implementation {
|
||||
use bracket_terminal::prelude::*;
|
||||
use core::fmt::{Error, Write};
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::thread::{spawn, JoinHandle};
|
||||
|
||||
struct State {}
|
||||
|
||||
impl GameState for State {
|
||||
fn tick(&mut self, ctx: &mut BTerm) {
|
||||
ctx.print(1, 1, "Hello Bracket World");
|
||||
}
|
||||
}
|
||||
|
||||
enum ControlMessage {
|
||||
Text(String),
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub struct Output {
|
||||
stream: Sender<ControlMessage>,
|
||||
context: Option<BTerm>,
|
||||
gs: Option<State>,
|
||||
}
|
||||
|
||||
pub fn init() -> Output {
|
||||
let (tx, rx) = channel();
|
||||
|
||||
let context = BTermBuilder::simple80x50()
|
||||
.with_title("Hello Minimal Bracket World")
|
||||
.build()
|
||||
.unwrap();
|
||||
let gs: State = State {};
|
||||
Output {
|
||||
stream: tx,
|
||||
context: Some(context),
|
||||
gs: Some(gs),
|
||||
}
|
||||
}
|
||||
|
||||
impl Output {
|
||||
pub fn run(&mut self) {
|
||||
main_loop(self.context.take().unwrap(), self.gs.take().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Output {
|
||||
fn drop(&mut self) {
|
||||
self.stream.send(ControlMessage::Exit).unwrap();
|
||||
// self.finish.take().unwrap().join().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Output {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
// It would be nice if this worked with &str
|
||||
self.stream
|
||||
.send(ControlMessage::Text(s.to_owned()))
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(baremetal))]
|
||||
mod native_nostd {
|
||||
pub fn init() {}
|
||||
mod implementation {
|
||||
use core::fmt::{Error, Write};
|
||||
use pancurses::{endwin, initscr, Window};
|
||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||
|
||||
enum ControlMessage {
|
||||
Text(String),
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub struct Output {
|
||||
window: Option<Window>,
|
||||
tx: Sender<ControlMessage>,
|
||||
rx: Receiver<ControlMessage>,
|
||||
}
|
||||
|
||||
pub fn init() -> Output {
|
||||
let (tx, rx) = channel();
|
||||
let window = initscr();
|
||||
window.nodelay(true);
|
||||
|
||||
Output {
|
||||
tx,
|
||||
rx,
|
||||
window: Some(window),
|
||||
}
|
||||
}
|
||||
|
||||
impl Output {
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
match self.rx.recv_timeout(std::time::Duration::from_millis(50)) {
|
||||
Ok(msg) => {
|
||||
match msg {
|
||||
ControlMessage::Exit => break,
|
||||
ControlMessage::Text(s) => {
|
||||
self.window.as_ref().unwrap().printw(s);
|
||||
self.window.as_ref().unwrap().refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
// Calling `getch` refreshes the screen
|
||||
self.window.as_ref().unwrap().getch();
|
||||
}
|
||||
Err(e) => panic!("Error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_writer(&self) -> OutputWriter {
|
||||
OutputWriter { tx: self.tx.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Output {
|
||||
fn drop(&mut self) {
|
||||
self.tx.send(ControlMessage::Exit).unwrap();
|
||||
endwin();
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Output {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
// It would be nice if this worked with &str
|
||||
self.tx
|
||||
.send(ControlMessage::Text(s.to_owned()))
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OutputWriter {
|
||||
tx: Sender<ControlMessage>,
|
||||
}
|
||||
|
||||
impl Write for OutputWriter {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
// It would be nice if this worked with &str
|
||||
self.tx
|
||||
.send(ControlMessage::Text(s.to_owned()))
|
||||
.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
mod native_nostd {
|
||||
mod implementation {
|
||||
pub struct Output {
|
||||
addr: *mut u32,
|
||||
}
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[panic_handler]
|
||||
|
|
@ -37,7 +189,7 @@ mod native_nostd {
|
|||
println!();
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
pub fn init() -> Output {
|
||||
let uart = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(0xf000_4000),
|
||||
None,
|
||||
|
|
@ -57,44 +209,72 @@ mod native_nostd {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(baremetal, no_mangle)]
|
||||
fn main() {
|
||||
if cfg!(baremetal) {
|
||||
native_nostd::init();
|
||||
}
|
||||
fn reader_thread(output: &mut implementation::OutputWriter) {
|
||||
writeln!(output, "Xous Logging Server starting up...").unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
|
||||
println!("Starting server...");
|
||||
let server_addr =
|
||||
xous::syscall::create_server(xous::make_name!("📜")).expect("couldn't create server");
|
||||
println!("Server listening on address {:?}", server_addr);
|
||||
writeln!(output, "Starting log server...").unwrap();
|
||||
let server_addr = xous::create_server(b"xous-logs-output").unwrap();
|
||||
writeln!(output, "Server listening on address {:?}", server_addr).unwrap();
|
||||
|
||||
let mut counter: usize = 0;
|
||||
loop {
|
||||
if counter.trailing_zeros() >= 12 {
|
||||
println!("Counter tick: {}", counter);
|
||||
writeln!(output, "Counter tick: {}", counter).unwrap();
|
||||
}
|
||||
counter += 1;
|
||||
println!("Waiting for an event...");
|
||||
writeln!(output, "Waiting for an event...").unwrap();
|
||||
let mut envelope =
|
||||
xous::syscall::receive_message(server_addr).expect("couldn't get address");
|
||||
println!("Got message envelope: {:?}", envelope);
|
||||
writeln!(output, "Got message envelope: {:?}", envelope).unwrap();
|
||||
match &mut envelope.message {
|
||||
xous::Message::Scalar(msg) => {
|
||||
println!("Scalar message from {}: {:?}", envelope.sender, msg)
|
||||
writeln!(
|
||||
output,
|
||||
"Scalar message from {}: {:?}",
|
||||
envelope.sender, msg
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
xous::Message::Move(msg) => {
|
||||
let log_entry = LogString::from_message(msg);
|
||||
println!("Moved log message from {}: {}", envelope.sender, log_entry);
|
||||
writeln!(
|
||||
output,
|
||||
"Moved log message from {}: {}",
|
||||
envelope.sender, log_entry
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
xous::Message::Borrow(msg) => {
|
||||
let log_entry = LogString::from_message(msg);
|
||||
println!("Immutably borrowed log message from {}: {}", envelope.sender, log_entry);
|
||||
writeln!(
|
||||
output,
|
||||
"Immutably borrowed log message from {}: {}",
|
||||
envelope.sender, log_entry
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
xous::Message::MutableBorrow(msg) => {
|
||||
let mut log_entry = LogString::from_message(msg);
|
||||
println!("Immutably borrowed log message from {}: {}", envelope.sender, log_entry);
|
||||
writeln!(
|
||||
output,
|
||||
"Immutably borrowed log message from {}: {}",
|
||||
envelope.sender, log_entry
|
||||
)
|
||||
.unwrap();
|
||||
writeln!(log_entry, " << HELLO FROM THE SERVER").unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(baremetal, no_mangle)]
|
||||
fn main() -> Result<(), xous::Error> {
|
||||
let mut output = implementation::init();
|
||||
let mut writer = output.get_writer();
|
||||
|
||||
xous::create_thread(move || reader_thread(&mut writer)).unwrap();
|
||||
|
||||
output.run();
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue