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-11-23 07:42:58 +00:00
|
|
|
use graphics_server::GlyphSet;
|
2020-11-24 21:11:32 +00:00
|
|
|
use com::api::BattStats;
|
|
|
|
|
use com::*;
|
2020-03-31 10:03:19 +00:00
|
|
|
|
2020-11-25 11:58:24 +00:00
|
|
|
use core::convert::TryFrom;
|
|
|
|
|
|
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-08-29 14:09:20 +00:00
|
|
|
fn move_lfsr(mut lfsr: u32) -> u32 {
|
|
|
|
|
lfsr ^= lfsr >> 7;
|
|
|
|
|
lfsr ^= lfsr << 9;
|
|
|
|
|
lfsr ^= lfsr >> 13;
|
|
|
|
|
lfsr
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2020-11-24 14:45:10 +00:00
|
|
|
let mut ret = move_lfsr(self.lfsr);
|
2020-11-22 16:40:40 +00:00
|
|
|
self.lfsr = ret;
|
2020-11-24 14:45:10 +00:00
|
|
|
ret *= 2; // make the ball move faster
|
2020-11-22 16:40:40 +00:00
|
|
|
|
|
|
|
|
(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-11-26 16:35:29 +00:00
|
|
|
use core::sync::atomic::{AtomicU16, AtomicI16, AtomicU8, Ordering};
|
|
|
|
|
|
|
|
|
|
// need atomic global constants to pass data between threads
|
|
|
|
|
// as we do not yet have a "Mutex" in Xous
|
|
|
|
|
static BATT_STATS_VOLTAGE: AtomicU16 = AtomicU16::new(3700);
|
|
|
|
|
static BATT_STATS_CURRENT: AtomicI16 = AtomicI16::new(-150);
|
|
|
|
|
static BATT_STATS_SOC: AtomicU8 = AtomicU8::new(50);
|
|
|
|
|
static BATT_STATS_REMAINING: AtomicU16 = AtomicU16::new(750);
|
2020-11-25 16:39:15 +00:00
|
|
|
|
2020-11-26 16:35:29 +00:00
|
|
|
fn com_thread(_arg: Option<u32>) {
|
2020-11-25 16:39:15 +00:00
|
|
|
let shell_server = xous::create_server(b"shell ").expect("Couldn't create Shell server");
|
2020-11-26 16:35:29 +00:00
|
|
|
let shell_id = xous::SID::from_bytes(b"shell ").unwrap();
|
|
|
|
|
info!("SHELL|com_thread: starting COM response handler thread");
|
2020-11-25 16:39:15 +00:00
|
|
|
loop {
|
|
|
|
|
let mut envelope =
|
|
|
|
|
xous::syscall::receive_message(shell_id).expect("couldn't get address");
|
2020-11-26 16:35:29 +00:00
|
|
|
info!("SHELL|com_thread: got message {:?}", envelope);
|
2020-11-25 16:39:15 +00:00
|
|
|
if let Ok(opcode) = com::api::Opcode::try_from(&envelope.body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
com::api::Opcode::BattStatsReturn(stats) => {
|
2020-11-26 16:35:29 +00:00
|
|
|
BATT_STATS_VOLTAGE.store(stats.voltage, Ordering::Relaxed);
|
|
|
|
|
BATT_STATS_CURRENT.store(stats.current, Ordering::Relaxed);
|
|
|
|
|
BATT_STATS_SOC.store(stats.soc, Ordering::Relaxed);
|
|
|
|
|
BATT_STATS_REMAINING.store(stats.remaining_capacity, Ordering::Relaxed);
|
2020-11-25 16:39:15 +00:00
|
|
|
},
|
|
|
|
|
_ => error!("shell received an opcode that wasn't expected")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error!("couldn't convert opcode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-03-10 04:46:05 +00:00
|
|
|
|
2020-11-26 16:35:29 +00:00
|
|
|
// make a thread to catch responses from the COM
|
|
|
|
|
xous::create_thread_simple(com_thread, None).unwrap();
|
|
|
|
|
|
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-11-24 14:45:10 +00:00
|
|
|
let com_id = xous::SID::from_bytes(b"com ").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-11-23 12:05:36 +00:00
|
|
|
let log_conn = xous::connect(log_server_id).unwrap();
|
|
|
|
|
let graphics_conn = xous::connect(graphics_server_id).unwrap();
|
|
|
|
|
let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap();
|
2020-11-24 14:45:10 +00:00
|
|
|
let com_conn = xous::connect(com_id).unwrap();
|
2020-07-29 11:31:49 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
println!(
|
2020-11-24 14:45:10 +00:00
|
|
|
"SHELL: Connected to Log server: {} Graphics server: {} Ticktimer server: {} Com: {}",
|
|
|
|
|
log_conn, graphics_conn, ticktimer_conn, com_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-11-23 06:55:56 +00:00
|
|
|
let screensize = graphics_server::screen_size(graphics_conn).expect("Couldn't get screen size");
|
|
|
|
|
|
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),
|
2020-11-23 06:55:56 +00:00
|
|
|
Point::new(screensize.x as _, screensize.y as i16 - 1)));
|
2020-11-22 19:18:43 +00:00
|
|
|
bouncyball.update();
|
2020-10-12 21:22:19 +00:00
|
|
|
|
2020-11-24 21:11:32 +00:00
|
|
|
let mut batt_stats: BattStats = BattStats::default();
|
|
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-11-24 21:11:32 +00:00
|
|
|
{ // use this to select which UART to monitor in the main loop
|
2020-11-23 12:05:36 +00:00
|
|
|
use utralib::generated::*;
|
2020-10-28 03:14:09 +00:00
|
|
|
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);
|
2020-11-25 15:06:57 +00:00
|
|
|
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 1);
|
2020-10-28 03:14:09 +00:00
|
|
|
}
|
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-11-23 06:55:56 +00:00
|
|
|
ticktimer_server::reset(ticktimer_conn).unwrap();
|
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-11-24 21:11:32 +00:00
|
|
|
// update battery status periodically
|
2020-10-14 15:01:57 +00:00
|
|
|
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
2020-11-24 21:11:32 +00:00
|
|
|
if elapsed_time - last_time > 500 {
|
2020-10-14 15:01:57 +00:00
|
|
|
last_time = elapsed_time;
|
2020-11-25 15:06:57 +00:00
|
|
|
info!("Requesting batt stats from COM");
|
|
|
|
|
get_batt_stats_nb(com_conn).expect("Can't get battery stats from COM");
|
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-10-30 02:30:34 +00:00
|
|
|
string_buffer.clear();
|
2020-11-26 16:35:29 +00:00
|
|
|
write!(&mut string_buffer, "{}mV Uptime: {:.2}s", /*BATT_STATS_VOLTAGE.load(Ordering::Relaxed)*/ -1, last_time as f32 / 1000f32).expect("Can't write");
|
2020-11-23 07:42:58 +00:00
|
|
|
graphics_server::set_glyph(graphics_conn, GlyphSet::Small).expect("unable to set glyph");
|
|
|
|
|
let (_, h) = graphics_server::query_glyph(graphics_conn).expect("unable to query glyph");
|
|
|
|
|
graphics_server::clear_region(graphics_conn, 0, 0, screensize.x as usize - 1, h)
|
2020-10-30 13:27:35 +00:00
|
|
|
.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-11-11 08:06:39 +00:00
|
|
|
|
2020-11-23 11:02:02 +00:00
|
|
|
// ticktimer_server::sleep_ms(ticktimer_conn, 500).expect("couldn't sleep");
|
2020-11-22 19:18:43 +00:00
|
|
|
|
|
|
|
|
// draw the ball
|
2020-11-22 16:40:40 +00:00
|
|
|
bouncyball.update();
|
2020-11-22 19:18:43 +00:00
|
|
|
graphics_server::clear_region(graphics_conn,
|
|
|
|
|
bouncyball.bounds.top_left().x as _, bouncyball.bounds.top_left().y as _,
|
2020-11-22 19:49:09 +00:00
|
|
|
bouncyball.bounds.bottom_right().x as _, bouncyball.bounds.bottom_right().y as usize + 1)
|
2020-11-22 19:18:43 +00:00
|
|
|
.expect("unable to clear region");
|
|
|
|
|
graphics_server::draw_circle(
|
|
|
|
|
graphics_conn,
|
|
|
|
|
bouncyball.loc,
|
|
|
|
|
bouncyball.radius as u16,
|
|
|
|
|
)
|
|
|
|
|
.expect("unable to draw to screen");
|
|
|
|
|
|
|
|
|
|
graphics_server::flush(graphics_conn).expect("unable to draw to screen");
|
2020-11-25 11:58:24 +00:00
|
|
|
|
2020-11-25 16:39:15 +00:00
|
|
|
/*
|
2020-11-25 15:06:57 +00:00
|
|
|
let envelope = xous::try_receive_message(shell_server).unwrap();
|
|
|
|
|
if envelope.is_some() {
|
|
|
|
|
if let Ok(opcode) = com::api::Opcode::try_from(&envelope.unwrap().body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
com::api::Opcode::BattStatsReturn(stats) => {
|
|
|
|
|
batt_stats = stats;
|
|
|
|
|
},
|
|
|
|
|
_ => error!("SHELL: received unexpected opcode"),
|
|
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
}
|
2020-11-25 16:39:15 +00:00
|
|
|
}*/
|
2020-03-10 04:46:05 +00:00
|
|
|
}
|
|
|
|
|
}
|