2021-03-10 17:45:02 +00:00
|
|
|
use crate::{ShellCmdApi,CommonEnv};
|
2021-03-26 12:22:33 +00:00
|
|
|
use xous_ipc::String;
|
2021-03-10 17:45:02 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Sleep {
|
|
|
|
|
}
|
|
|
|
|
impl Sleep {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Sleep {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> ShellCmdApi<'a> for Sleep {
|
2021-03-11 14:31:38 +00:00
|
|
|
cmd_api!(sleep); // inserts boilerplate for command API
|
|
|
|
|
|
2021-03-11 14:54:24 +00:00
|
|
|
fn process(&mut self, _args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
|
2021-03-10 17:45:02 +00:00
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
|
|
|
|
let mut ret = String::<1024>::new();
|
2021-04-05 15:50:00 +00:00
|
|
|
if ((llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
2021-03-10 20:09:47 +00:00
|
|
|
// if power is plugged in, deny powerdown request
|
|
|
|
|
write!(ret, "System can't sleep while charging. Unplug charging cable and try again.").unwrap();
|
|
|
|
|
Ok(Some(ret))
|
|
|
|
|
} else {
|
2021-04-05 15:50:00 +00:00
|
|
|
if Ok(true) == env.gam.powerdown_request() {
|
|
|
|
|
env.ticktimer.sleep_ms(500).unwrap(); // let the screen redraw
|
2021-03-10 20:09:47 +00:00
|
|
|
|
|
|
|
|
// allow EC to snoop, so that it can wake up the system
|
2021-04-05 15:50:00 +00:00
|
|
|
env.llio.allow_ec_snoop(true).unwrap();
|
2021-03-10 20:09:47 +00:00
|
|
|
// allow the EC to power me down
|
2021-04-05 15:50:00 +00:00
|
|
|
env.llio.allow_power_off(true).unwrap();
|
2021-03-10 20:09:47 +00:00
|
|
|
// now send the power off command
|
2021-04-05 15:50:00 +00:00
|
|
|
env.com.power_off_soc().unwrap();
|
2021-03-10 20:09:47 +00:00
|
|
|
|
|
|
|
|
log::info!("CMD: powering down now!");
|
|
|
|
|
// pause execution, nothing after this should be reachable
|
2021-04-05 15:50:00 +00:00
|
|
|
env.ticktimer.sleep_ms(2000).unwrap(); // should power off within 2 seconds
|
2021-03-10 20:09:47 +00:00
|
|
|
log::info!("CMD: if you can read this, power down failed!");
|
|
|
|
|
}
|
|
|
|
|
write!(ret, "Powerdown request denied").unwrap();
|
|
|
|
|
Ok(Some(ret))
|
|
|
|
|
}
|
2021-03-10 17:45:02 +00:00
|
|
|
}
|
2021-03-26 12:22:33 +00:00
|
|
|
}
|