2020-06-03 03:01:50 +00:00
|
|
|
#![cfg_attr(baremetal, no_main)]
|
|
|
|
|
#![cfg_attr(baremetal, no_std)]
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-06-03 03:01:50 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:46:05 +00:00
|
|
|
#[macro_use]
|
|
|
|
|
mod debug;
|
|
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
mod logstr;
|
2020-10-28 03:14:09 +00:00
|
|
|
mod timer;
|
2020-10-14 15:01:57 +00:00
|
|
|
use core::fmt::Write;
|
2020-10-30 13:27:35 +00:00
|
|
|
use log::{error, info};
|
2020-10-30 03:19:43 +00:00
|
|
|
use xous::String;
|
2020-11-22 16:40:40 +00:00
|
|
|
use graphics_server::Point;
|
2020-03-31 10:03:19 +00:00
|
|
|
|
|
|
|
|
// fn print_and_yield(index: *mut usize) -> ! {
|
|
|
|
|
// let num = index as usize;
|
|
|
|
|
// loop {
|
|
|
|
|
// println!("THREAD {}", num);
|
|
|
|
|
// xous::syscall::yield_slice();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2020-03-30 08:07:58 +00:00
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Rectangle {
|
|
|
|
|
/// Top left point of the rect
|
|
|
|
|
pub top_left: Point,
|
|
|
|
|
/// Bottom right point of the rect
|
|
|
|
|
pub bottom_right: Point,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Rectangle {
|
|
|
|
|
pub fn top_left(&self) -> Point {
|
|
|
|
|
self.top_left
|
|
|
|
|
}
|
|
|
|
|
pub fn bottom_right(&self) -> Point {
|
|
|
|
|
self.bottom_right
|
|
|
|
|
}
|
|
|
|
|
pub fn new(top_left: Point, bottom_right: Point) -> Self {
|
|
|
|
|
Rectangle {
|
|
|
|
|
top_left,
|
|
|
|
|
bottom_right,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-10-12 21:22:19 +00:00
|
|
|
use utralib::generated::*;
|
2020-10-11 19:56:10 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
fn move_lfsr(mut lfsr: u32) -> u32 {
|
|
|
|
|
lfsr ^= lfsr >> 7;
|
|
|
|
|
lfsr ^= lfsr << 9;
|
|
|
|
|
lfsr ^= lfsr >> 13;
|
|
|
|
|
lfsr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_connection(server: xous::SID) -> xous::CID {
|
|
|
|
|
loop {
|
2020-08-30 09:41:02 +00:00
|
|
|
if let Ok(cid) = xous::syscall::try_connect(server) {
|
2020-08-29 14:09:20 +00:00
|
|
|
return cid;
|
|
|
|
|
}
|
|
|
|
|
xous::syscall::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
pub struct Bounce {
|
|
|
|
|
vector: Point,
|
|
|
|
|
radius: u16,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
loc: Point,
|
|
|
|
|
lfsr: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Bounce {
|
|
|
|
|
pub fn new(radius: u16, bounds: Rectangle) -> Bounce {
|
|
|
|
|
Bounce {
|
|
|
|
|
vector: Point::new(2,3),
|
|
|
|
|
radius: radius,
|
|
|
|
|
bounds: bounds,
|
|
|
|
|
loc: Point::new((bounds.bottom_right.x - bounds.top_left.x)/2, (bounds.bottom_right.y - bounds.top_left.y)/2),
|
|
|
|
|
lfsr: 0xace1u32,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next_rand(&mut self) -> i16 {
|
|
|
|
|
let ret = move_lfsr(self.lfsr);
|
|
|
|
|
self.lfsr = ret;
|
|
|
|
|
|
|
|
|
|
(ret % 8) as i16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self) -> &mut Self {
|
|
|
|
|
let mut x: i16;
|
|
|
|
|
let mut y: i16;
|
|
|
|
|
// update the new ball location
|
|
|
|
|
x = self.loc.x + self.vector.x; y = self.loc.y + self.vector.y;
|
|
|
|
|
|
|
|
|
|
let r: i16 = self.radius as i16;
|
|
|
|
|
if (x >= (self.bounds.bottom_right().x - r)) ||
|
|
|
|
|
(x <= (self.bounds.top_left().x + r)) ||
|
|
|
|
|
(y >= (self.bounds.bottom_right().y - r)) ||
|
|
|
|
|
(y <= (self.bounds.top_left().y + r)) {
|
|
|
|
|
if x >= (self.bounds.bottom_right().x - r - 1) {
|
|
|
|
|
self.vector.x = -self.next_rand();
|
|
|
|
|
x = self.bounds.bottom_right().x - r;
|
|
|
|
|
}
|
|
|
|
|
if x <= self.bounds.top_left().x + r + 1 {
|
|
|
|
|
self.vector.x = self.next_rand();
|
|
|
|
|
x = self.bounds.top_left().x + r;
|
|
|
|
|
}
|
|
|
|
|
if y >= (self.bounds.bottom_right().y - r - 1) {
|
|
|
|
|
self.vector.y = -self.next_rand();
|
|
|
|
|
y = self.bounds.bottom_right().y - r;
|
|
|
|
|
}
|
|
|
|
|
if y <= (self.bounds.top_left().y + r + 1) {
|
|
|
|
|
self.vector.y = self.next_rand();
|
|
|
|
|
y = self.bounds.top_left().y + r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.loc.x = x;
|
|
|
|
|
self.loc.y = y;
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 09:00:48 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn shell_main() -> ! {
|
2020-03-10 04:46:05 +00:00
|
|
|
timer::init();
|
2020-10-30 13:27:35 +00:00
|
|
|
log_server::init_wait().unwrap();
|
2020-11-11 08:06:39 +00:00
|
|
|
let mut loops = 0;
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-10-04 11:07:53 +00:00
|
|
|
// let log_server_id = xous::SID::from_bytes(b"xous-logs-output").unwrap();
|
2020-08-29 14:09:20 +00:00
|
|
|
let graphics_server_id = xous::SID::from_bytes(b"graphics-server ").unwrap();
|
2020-10-12 11:08:16 +00:00
|
|
|
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
|
2020-10-14 15:01:57 +00:00
|
|
|
let log_server_id = xous::SID::from_bytes(b"xous-log-server ").unwrap();
|
2020-07-29 11:31:49 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
println!("SHELL: Attempting to connect to servers...");
|
2020-10-14 15:01:57 +00:00
|
|
|
let log_conn = ensure_connection(log_server_id);
|
2020-08-29 14:09:20 +00:00
|
|
|
let graphics_conn = ensure_connection(graphics_server_id);
|
2020-10-12 11:08:16 +00:00
|
|
|
let ticktimer_conn = ensure_connection(ticktimer_server_id);
|
2020-07-29 11:31:49 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
println!(
|
2020-10-12 11:08:16 +00:00
|
|
|
"SHELL: Connected to Log server: {} Graphics server: {} Ticktimer server: {}",
|
|
|
|
|
log_conn, graphics_conn, ticktimer_conn,
|
2020-08-29 14:09:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_ne!(
|
|
|
|
|
log_conn, graphics_conn,
|
|
|
|
|
"SHELL: graphics and log connections are the same!"
|
|
|
|
|
);
|
2020-03-19 09:26:16 +00:00
|
|
|
|
2020-10-12 11:08:16 +00:00
|
|
|
assert_ne!(
|
|
|
|
|
ticktimer_conn, graphics_conn,
|
|
|
|
|
"SHELL: graphics and ticktimer connections are the same!"
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-13 19:40:44 +00:00
|
|
|
// let mut counter: usize = 0;
|
2020-10-14 15:01:57 +00:00
|
|
|
let mut ls = logstr::LogStr::new();
|
2020-08-29 14:09:20 +00:00
|
|
|
let dark = graphics_server::Color::from(0);
|
|
|
|
|
let light = graphics_server::Color::from(!0);
|
2020-11-22 16:40:40 +00:00
|
|
|
let mut bouncyball = Bounce::new(14,
|
|
|
|
|
Rectangle::new(Point::new(0, 18 * 21),
|
|
|
|
|
Point::new(336, 536-1))); // TODO: make API call to get screen size
|
2020-10-12 21:22:19 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
#[cfg(baremetal)]
|
|
|
|
|
{
|
|
|
|
|
let gpio_base = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::gpio::HW_GPIO_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map GPIO CSR range");
|
|
|
|
|
let mut gpio = CSR::new(gpio_base.as_mut_ptr() as *mut u32);
|
|
|
|
|
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 2);
|
|
|
|
|
}
|
2020-10-12 21:22:19 +00:00
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
graphics_server::set_style(
|
|
|
|
|
graphics_conn,
|
|
|
|
|
1,
|
|
|
|
|
dark,
|
|
|
|
|
dark,
|
|
|
|
|
)
|
|
|
|
|
.expect("unable to draw to screen: {:?}");
|
|
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
let mut last_time: u64 = 0;
|
2020-10-30 03:19:43 +00:00
|
|
|
let mut string_buffer = String::new(4096);
|
2020-03-10 04:46:05 +00:00
|
|
|
loop {
|
2020-10-14 15:01:57 +00:00
|
|
|
// a message passing demo -- checking time
|
|
|
|
|
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
2020-10-30 13:27:35 +00:00
|
|
|
info!("SHELL: {}ms", elapsed_time);
|
2020-10-28 03:14:09 +00:00
|
|
|
if elapsed_time - last_time > 40 {
|
2020-10-14 15:01:57 +00:00
|
|
|
last_time = elapsed_time;
|
|
|
|
|
/*
|
|
|
|
|
xous::try_send_message(log_conn,
|
|
|
|
|
xous::Message::Scalar(xous::ScalarMessage{id:256, arg1: elapsed_time as usize, arg2: 257, arg3: 258, arg4: 259}));
|
|
|
|
|
*/
|
2020-10-30 13:27:35 +00:00
|
|
|
info!("Preparing a mutable borrow message");
|
2020-10-14 16:01:02 +00:00
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
ls.clear();
|
2020-10-28 03:14:09 +00:00
|
|
|
write!(
|
|
|
|
|
ls,
|
|
|
|
|
"Hello, Server! This memory is borrowed from another process. Elapsed: {}",
|
|
|
|
|
elapsed_time as usize
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't send hello message");
|
2020-10-14 15:01:57 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
let mm = ls
|
|
|
|
|
.as_memory_message(0)
|
|
|
|
|
.expect("couldn't form memory message");
|
2020-10-14 16:01:02 +00:00
|
|
|
|
2020-10-30 13:27:35 +00:00
|
|
|
info!("Sending a mutable borrow message");
|
2020-10-14 16:01:02 +00:00
|
|
|
|
2020-10-30 13:27:35 +00:00
|
|
|
xous::syscall::send_message(log_conn, xous::Message::MutableBorrow(mm))
|
2020-10-28 03:14:09 +00:00
|
|
|
.expect("couldn't send memory message");
|
2020-10-14 15:01:57 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-10-30 13:27:35 +00:00
|
|
|
error!("error requesting ticktimer!")
|
2020-10-14 15:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-22 16:40:40 +00:00
|
|
|
graphics_server::draw_circle(
|
2020-10-30 02:30:34 +00:00
|
|
|
graphics_conn,
|
2020-11-22 16:40:40 +00:00
|
|
|
bouncyball.loc,
|
|
|
|
|
bouncyball.radius as u16,
|
2020-10-30 02:30:34 +00:00
|
|
|
)
|
|
|
|
|
.expect("unable to draw to screen");
|
2020-08-29 14:09:20 +00:00
|
|
|
|
2020-10-30 02:30:34 +00:00
|
|
|
string_buffer.clear();
|
|
|
|
|
write!(&mut string_buffer, "Elapsed time: {}ms", last_time).expect("Can't write");
|
2020-10-30 13:27:35 +00:00
|
|
|
graphics_server::clear_region(graphics_conn, 0, 0, 300, 40)
|
|
|
|
|
.expect("unable to clear region");
|
|
|
|
|
info!("drawing string: {}", string_buffer);
|
2020-10-30 03:19:43 +00:00
|
|
|
graphics_server::draw_string(graphics_conn, &string_buffer).expect("unable to draw string");
|
2020-10-30 02:30:34 +00:00
|
|
|
graphics_server::flush(graphics_conn).expect("unable to draw to screen");
|
2020-11-11 08:06:39 +00:00
|
|
|
|
|
|
|
|
ticktimer_server::sleep_ms(ticktimer_conn, 2000 + loops).expect("couldn't sleep");
|
|
|
|
|
loops += 1;
|
2020-11-22 16:40:40 +00:00
|
|
|
bouncyball.update();
|
2020-03-10 04:46:05 +00:00
|
|
|
}
|
|
|
|
|
}
|