mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
add status thread mechanism to check charger state
This commit is contained in:
parent
87b6d9b6d7
commit
5d4e716e1b
6 changed files with 57 additions and 3 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -531,6 +531,7 @@ dependencies = [
|
||||||
"heapless 0.7.0",
|
"heapless 0.7.0",
|
||||||
"ime-plugin-api",
|
"ime-plugin-api",
|
||||||
"keyboard",
|
"keyboard",
|
||||||
|
"llio",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"num-derive",
|
"num-derive",
|
||||||
|
|
|
||||||
|
|
@ -65,13 +65,13 @@ pub(crate) enum Opcode {
|
||||||
ShipMode,
|
ShipMode,
|
||||||
|
|
||||||
/// Is the battery charging?
|
/// Is the battery charging?
|
||||||
//IsCharging,
|
IsCharging,
|
||||||
|
|
||||||
/// Set the backlight brightness
|
/// Set the backlight brightness
|
||||||
SetBackLight,
|
SetBackLight,
|
||||||
|
|
||||||
/// Request charging
|
/// Request charging
|
||||||
//RequestCharging,
|
RequestCharging,
|
||||||
|
|
||||||
/// Erase a region of EC FLASH
|
/// Erase a region of EC FLASH
|
||||||
//FlashErase,
|
//FlashErase,
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,26 @@ impl Com {
|
||||||
)
|
)
|
||||||
).map(|_| ())
|
).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_charging(&self) -> Result<bool, xous::Error> {
|
||||||
|
if let xous::Result::Scalar1(state) =
|
||||||
|
send_message(self.conn,
|
||||||
|
Message::new_blocking_scalar(Opcode::IsCharging.to_usize().unwrap(), 0, 0, 0, 0)).unwrap() {
|
||||||
|
if state != 0 {
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(xous::Error::InternalError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn request_charging(&self) -> Result<(), xous::Error> {
|
||||||
|
send_message(self.conn,
|
||||||
|
Message::new_scalar(Opcode::RequestCharging.to_usize().unwrap(), 0, 0, 0, 0
|
||||||
|
)).map(|_| ())
|
||||||
|
}
|
||||||
// note to future self: add other event listener registrations (such as network events) here
|
// note to future self: add other event listener registrations (such as network events) here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -329,6 +329,14 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
Some(Opcode::IsCharging) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||||
|
com.txrx(ComState::POWER_CHARGER_STATE.verb);
|
||||||
|
let result = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
||||||
|
xous::return_scalar(msg.sender, result as usize).expect("couldn't return charging state");
|
||||||
|
}),
|
||||||
|
Some(Opcode::RequestCharging) => msg_scalar_unpack!(msg, _, _, _, _, {
|
||||||
|
com.txrx(ComState::CHG_START.verb);
|
||||||
|
}),
|
||||||
Some(Opcode::StandbyCurrent) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
Some(Opcode::StandbyCurrent) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||||
if let Some(i) = com.stby_current() {
|
if let Some(i) = com.stby_current() {
|
||||||
xous::return_scalar2(msg.sender, 1, i as usize).expect("couldn't return StandbyCurrent");
|
xous::return_scalar2(msg.sender, 1, i as usize).expect("couldn't return StandbyCurrent");
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ log-server = {path = "../log-server"}
|
||||||
ticktimer-server = {path = "../ticktimer-server"}
|
ticktimer-server = {path = "../ticktimer-server"}
|
||||||
trng = {path = "../trng"}
|
trng = {path = "../trng"}
|
||||||
rtc = {path = "../rtc"}
|
rtc = {path = "../rtc"}
|
||||||
|
llio = {path = "../llio"}
|
||||||
xous = {path = "../../xous-rs"}
|
xous = {path = "../../xous-rs"}
|
||||||
xous-ipc = {path = "../../xous-ipc"}
|
xous-ipc = {path = "../../xous-ipc"}
|
||||||
xous-names = {path = "../xous-names"}
|
xous-names = {path = "../xous-names"}
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,13 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi
|
||||||
battstats_tv.draw_border = false;
|
battstats_tv.draw_border = false;
|
||||||
battstats_tv.margin = Point::new(0, 0);
|
battstats_tv.margin = Point::new(0, 0);
|
||||||
|
|
||||||
let mut stats: BattStats;
|
// initialize to some "sane" mid-point defaults, so we don't trigger errors later on before the first real battstat reading comes
|
||||||
|
let mut stats = BattStats {
|
||||||
|
voltage: 3700,
|
||||||
|
soc: 50,
|
||||||
|
current: 0,
|
||||||
|
remaining_capacity: 650,
|
||||||
|
};
|
||||||
let mut last_time: u64 = ticktimer.elapsed_ms();
|
let mut last_time: u64 = ticktimer.elapsed_ms();
|
||||||
let mut stats_phase: usize = 0;
|
let mut stats_phase: usize = 0;
|
||||||
let mut last_seconds: usize = ((last_time / 1000) % 60) as usize;
|
let mut last_seconds: usize = ((last_time / 1000) % 60) as usize;
|
||||||
|
|
@ -123,6 +129,9 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi
|
||||||
let mut datetime: Option<rtc::DateTime> = None;
|
let mut datetime: Option<rtc::DateTime> = None;
|
||||||
let mut dt_pump_modulus = 15;
|
let mut dt_pump_modulus = 15;
|
||||||
|
|
||||||
|
let mut charger_pump_modulus = 0;
|
||||||
|
let llio = llio::Llio::new(&xns).unwrap();
|
||||||
|
|
||||||
let secs_interval;
|
let secs_interval;
|
||||||
let batt_interval;
|
let batt_interval;
|
||||||
if cfg!(feature = "slowstatus") {
|
if cfg!(feature = "slowstatus") {
|
||||||
|
|
@ -155,6 +164,21 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi
|
||||||
let elapsed_time = ticktimer.elapsed_ms();
|
let elapsed_time = ticktimer.elapsed_ms();
|
||||||
let now_seconds: usize = ((elapsed_time / 1000) % 60) as usize;
|
let now_seconds: usize = ((elapsed_time / 1000) % 60) as usize;
|
||||||
if (now_seconds / secs_interval) != (last_seconds / secs_interval) {
|
if (now_seconds / secs_interval) != (last_seconds / secs_interval) {
|
||||||
|
charger_pump_modulus += 1;
|
||||||
|
if charger_pump_modulus > 60 {
|
||||||
|
// once a minute confirm that the charger is in the right state.
|
||||||
|
charger_pump_modulus = 0;
|
||||||
|
if stats.soc < 95 || stats.remaining_capacity < 1000 { // only request if we aren't fully charged, either by SOC or capacity metrics
|
||||||
|
if (llio.adc_vbus().unwrap() as f64) * 0.005033 > 4.45 { // 4.45V is our threshold for deciding if a cable is present
|
||||||
|
// charging cable is present
|
||||||
|
if !com.is_charging().expect("couldn't check charging state") {
|
||||||
|
// not charging, but cable is present
|
||||||
|
log::info!("Charger present, but not currently charging. Automatically requesting charge start.");
|
||||||
|
com.request_charging().expect("couldn't send charge request");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
dt_pump_modulus += 1;
|
dt_pump_modulus += 1;
|
||||||
if dt_pump_modulus > 15 {
|
if dt_pump_modulus > 15 {
|
||||||
dt_pump_modulus = 0;
|
dt_pump_modulus = 0;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue