2021-01-17 20:12:40 +00:00
|
|
|
use log::{error, info};
|
|
|
|
|
use com::api::BattStats;
|
2021-01-18 20:54:54 +00:00
|
|
|
use graphics_server::*;
|
2021-01-17 20:12:40 +00:00
|
|
|
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
use core::convert::TryFrom;
|
|
|
|
|
|
2021-01-20 08:22:38 +00:00
|
|
|
use blitstr_ref as blitstr;
|
|
|
|
|
|
2021-01-20 06:57:40 +00:00
|
|
|
pub fn status_thread(canvas_gid: [u32; 4]) {
|
2021-03-02 11:25:41 +00:00
|
|
|
let debug1 = false;
|
2021-01-20 06:57:40 +00:00
|
|
|
let status_gid: Gid = Gid::new(canvas_gid);
|
2021-03-10 20:43:58 +00:00
|
|
|
if debug1{info!("GAM|status: my canvas {:?}", status_gid)};
|
2021-01-20 06:57:40 +00:00
|
|
|
|
2021-03-02 13:47:23 +00:00
|
|
|
if debug1{info!("GAM|status: registering GAM|status thread");}
|
2021-01-17 20:12:40 +00:00
|
|
|
let status_sid = xous_names::register_name(xous::names::SERVER_NAME_STATUS).expect("GAM|status: can't register server");
|
|
|
|
|
|
|
|
|
|
let ticktimer_conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap()).unwrap();
|
|
|
|
|
let com_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_COM).expect("GAM|status: can't connect to COM");
|
|
|
|
|
let gam_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("GAM|status: can't connect to GAM");
|
|
|
|
|
|
2021-03-02 13:47:23 +00:00
|
|
|
if debug1{info!("GAM|status: getting screen size");}
|
2021-02-28 18:33:13 +00:00
|
|
|
let screensize = gam::get_canvas_bounds(gam_conn, status_gid).expect("GAM|status: Couldn't get canvas size");
|
2021-03-01 07:08:08 +00:00
|
|
|
//let screensize: Point = Point::new(0, 336);
|
2021-01-17 20:12:40 +00:00
|
|
|
|
2021-03-02 13:47:23 +00:00
|
|
|
if debug1{info!("GAM|status: building textview objects");}
|
2021-02-25 19:37:48 +00:00
|
|
|
// build uptime text view: left half of status bar
|
2021-01-19 19:18:13 +00:00
|
|
|
let mut uptime_tv = TextView::new(status_gid, 0,
|
2021-02-25 19:37:48 +00:00
|
|
|
TextBounds::BoundingBox(Rectangle::new(Point::new(0,0),
|
2021-02-28 18:33:13 +00:00
|
|
|
Point::new(screensize.x / 2, screensize.y - 1))));
|
2021-01-17 20:12:40 +00:00
|
|
|
uptime_tv.untrusted = false;
|
|
|
|
|
uptime_tv.style = blitstr::GlyphStyle::Small;
|
|
|
|
|
uptime_tv.draw_border = false;
|
2021-03-10 20:09:12 +00:00
|
|
|
uptime_tv.margin = Point::new(3, 0);
|
2021-01-19 19:18:13 +00:00
|
|
|
write!(uptime_tv, "Booting up...").expect("GAM|status: couldn't init uptime text");
|
2021-03-02 13:47:23 +00:00
|
|
|
if debug1{info!("GAM|status: screensize as reported: {:?}", screensize);}
|
|
|
|
|
if debug1{info!("GAM|status: uptime initialized to '{:?}'", uptime_tv);}
|
2021-01-17 20:12:40 +00:00
|
|
|
|
2021-02-25 19:37:48 +00:00
|
|
|
// build battstats text view: right half of status bar
|
2021-01-19 19:18:13 +00:00
|
|
|
let mut battstats_tv = TextView::new(status_gid, 0,
|
2021-02-25 19:37:48 +00:00
|
|
|
TextBounds::BoundingBox(Rectangle::new(Point::new(screensize.x / 2, 0),
|
2021-02-28 18:33:13 +00:00
|
|
|
Point::new(screensize.x, screensize.y - 1))));
|
2021-01-17 20:12:40 +00:00
|
|
|
battstats_tv.style = blitstr::GlyphStyle::Small;
|
|
|
|
|
battstats_tv.draw_border = false;
|
2021-02-28 17:49:22 +00:00
|
|
|
battstats_tv.margin = Point::new(0, 0);
|
2021-01-17 20:12:40 +00:00
|
|
|
|
2021-03-03 14:38:20 +00:00
|
|
|
let mut stats: BattStats;
|
2021-01-17 20:12:40 +00:00
|
|
|
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
|
|
|
|
|
let mut stats_phase: usize = 0;
|
2021-03-02 13:47:23 +00:00
|
|
|
let mut last_seconds: usize = ((last_time / 1000) % 60) as usize;
|
2021-02-28 20:01:33 +00:00
|
|
|
|
|
|
|
|
let style_dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1);
|
|
|
|
|
gam::draw_line(gam_conn, status_gid, Line::new_with_style(
|
|
|
|
|
Point::new(0, screensize.y),
|
|
|
|
|
Point::new(screensize.x, screensize.y),
|
|
|
|
|
style_dark
|
|
|
|
|
)).expect("GAM|status: Can't draw border line");
|
2021-03-01 07:08:08 +00:00
|
|
|
|
2021-03-02 13:15:40 +00:00
|
|
|
com::request_battstat_events(xous::names::SERVER_NAME_STATUS, com_conn).expect("GAM|status: couldn't request events from COM");
|
2021-03-02 13:57:52 +00:00
|
|
|
// prime the loop
|
|
|
|
|
com::get_batt_stats_nb(com_conn).expect("Can't get battery stats from COM");
|
|
|
|
|
last_seconds = last_seconds - 1; // this will force the uptime to redraw
|
2021-03-02 13:15:40 +00:00
|
|
|
info!("GAM|status: starting main loop");
|
2021-03-01 06:40:39 +00:00
|
|
|
loop {
|
2021-01-17 20:12:40 +00:00
|
|
|
let maybe_env = xous::try_receive_message(status_sid).unwrap();
|
|
|
|
|
match maybe_env {
|
|
|
|
|
Some(envelope) => {
|
2021-03-02 13:15:40 +00:00
|
|
|
//let envelope = xous::receive_message(status_sid).unwrap();
|
2021-03-02 11:25:41 +00:00
|
|
|
if debug1{info!("GAM|status: Message: {:?}", envelope);}
|
2021-01-17 20:12:40 +00:00
|
|
|
if let Ok(opcode) = com::api::Opcode::try_from(&envelope.body) {
|
|
|
|
|
match opcode {
|
|
|
|
|
com::api::Opcode::BattStatsEvent(s) => {
|
|
|
|
|
stats = s.clone();
|
2021-01-19 10:58:57 +00:00
|
|
|
battstats_tv.clear_str();
|
2021-02-25 19:37:48 +00:00
|
|
|
// toggle between two views of the data; duration of toggle is set by the modulus and thresholds below
|
2021-03-02 13:47:23 +00:00
|
|
|
if stats_phase > 3 {
|
2021-01-19 10:58:57 +00:00
|
|
|
write!(&mut battstats_tv, "{}mV {}mA", stats.voltage, stats.current).expect("GAM|status: can't write string");
|
2021-01-17 20:12:40 +00:00
|
|
|
} else {
|
2021-01-19 10:58:57 +00:00
|
|
|
write!(&mut battstats_tv, "{}mAh {}%", stats.remaining_capacity, stats.soc).expect("GAM|status: can't write string");
|
2021-01-17 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
stats_phase = (stats_phase + 1) % 8;
|
2021-01-19 19:18:13 +00:00
|
|
|
gam::post_textview(gam_conn, &mut battstats_tv).expect("GAM|status: can't draw battery stats");
|
2021-03-08 19:08:34 +00:00
|
|
|
gam::redraw(gam_conn).expect("GAM|status: couldn't redraw");
|
2021-01-17 20:12:40 +00:00
|
|
|
},
|
|
|
|
|
_ => error!("GAM|status received COM event opcode that wasn't expected"),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error!("GAM|status: couldn't convert opcode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => xous::yield_slice(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
2021-03-02 13:47:23 +00:00
|
|
|
let now_seconds: usize = ((elapsed_time / 1000) % 60) as usize;
|
|
|
|
|
if now_seconds != last_seconds {
|
|
|
|
|
last_seconds = now_seconds;
|
|
|
|
|
uptime_tv.clear_str();
|
|
|
|
|
write!(&mut uptime_tv, "Up {:02}:{:02}:{:02}",
|
|
|
|
|
(elapsed_time / 3_600_000), (elapsed_time / 60_000) % 60, now_seconds).expect("GAM|status: can't write string");
|
|
|
|
|
if debug1{info!("GAM|status: requesting draw of '{}'", uptime_tv);}
|
|
|
|
|
gam::post_textview(gam_conn, &mut uptime_tv).expect("GAM|status: can't draw uptime");
|
2021-03-08 19:08:34 +00:00
|
|
|
gam::redraw(gam_conn).expect("GAM|status: couldn't redraw");
|
2021-03-02 13:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-17 20:12:40 +00:00
|
|
|
if elapsed_time - last_time > 500 {
|
2021-02-28 17:49:22 +00:00
|
|
|
//info!("GAM|status: size of TextView type: {} bytes", core::mem::size_of::<TextView>());
|
2021-03-02 11:25:41 +00:00
|
|
|
if debug1{info!("GAM|status: periodic tasks: updating uptime, requesting battstats");}
|
2021-01-17 20:12:40 +00:00
|
|
|
last_time = elapsed_time;
|
|
|
|
|
com::get_batt_stats_nb(com_conn).expect("Can't get battery stats from COM");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error!("error requesting ticktimer!")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-16 20:02:08 +00:00
|
|
|
|
|
|
|
|
}
|