status: preference-based backlight timeout

Also rename some variables for clarity.
This commit is contained in:
Gianguido Sorà 2022-10-18 11:52:04 +02:00
parent 412b8bdd5e
commit 59c4fbd5e9

View file

@ -384,12 +384,12 @@ fn wrapped_main() -> ! {
StatusOpcode::Keypress.to_u32().unwrap() as usize, StatusOpcode::Keypress.to_u32().unwrap() as usize,
); );
let enabled = Arc::new(Mutex::new(false)); let autobacklight_enabled = Arc::new(Mutex::new(false));
let (tx, rx): (Sender<BacklightThreadOps>, Receiver<BacklightThreadOps>) = unbounded(); let (tx, rx): (Sender<BacklightThreadOps>, Receiver<BacklightThreadOps>) = unbounded();
let rx = Box::new(rx); let rx = Box::new(rx);
let thread_already_running = Arc::new(Mutex::new(false)); let autobacklight_thread_already_running = Arc::new(Mutex::new(false));
let thread_conn = xous::connect(status_sid).unwrap(); let thread_conn = xous::connect(status_sid).unwrap();
// ------------------------ check firmware status and apply updates // ------------------------ check firmware status and apply updates
@ -694,7 +694,7 @@ fn wrapped_main() -> ! {
log::debug!("{:?}", opcode); log::debug!("{:?}", opcode);
match opcode { match opcode {
Some(StatusOpcode::EnableAutomaticBacklight) => { Some(StatusOpcode::EnableAutomaticBacklight) => {
*enabled.lock().unwrap() = true; *autobacklight_enabled.lock().unwrap() = true;
// second: delete the first three elements off the menu // second: delete the first three elements off the menu
menu_manager.delete_item(t!("mainmenu.backlighton", xous::LANG)); menu_manager.delete_item(t!("mainmenu.backlighton", xous::LANG));
@ -711,7 +711,7 @@ fn wrapped_main() -> ! {
}, 0); }, 0);
} }
Some(StatusOpcode::DisableAutomaticBacklight) => { Some(StatusOpcode::DisableAutomaticBacklight) => {
*enabled.lock().unwrap() = false; *autobacklight_enabled.lock().unwrap() = false;
tx.send(BacklightThreadOps::Stop).unwrap(); tx.send(BacklightThreadOps::Stop).unwrap();
// second: delete the first element off the menu. // second: delete the first element off the menu.
@ -1074,11 +1074,11 @@ fn wrapped_main() -> ! {
}, },
Some(StatusOpcode::Keypress) => { Some(StatusOpcode::Keypress) => {
if !*enabled.lock().unwrap() { if !*autobacklight_enabled.lock().unwrap() {
log::trace!("ignoring keypress, automatic backlight is disabled"); log::trace!("ignoring keypress, automatic backlight is disabled");
continue continue
} }
let mut run_lock = thread_already_running.lock().unwrap(); let mut run_lock = autobacklight_thread_already_running.lock().unwrap();
match *run_lock { match *run_lock {
true => { true => {
log::trace!("renewing backlight timer"); log::trace!("renewing backlight timer");
@ -1087,10 +1087,22 @@ fn wrapped_main() -> ! {
}, },
false => { false => {
*run_lock = true; *run_lock = true;
// TODO(gsora): this code queries PDDB every time autobacklight timeout expired
// and user presses a button.
// Too intensive? Needs a dirty bit+cache?
let abl_timeout = match prefs.autobacklight_timeout() {
Ok(timeout) => timeout,
Err(error) => {
log::error!("cannot fetch autobacklight timeout, {:?}, defaulting to 10s", error);
10
}
};
com.set_backlight(255, 128).expect("cannot set backlight on"); com.set_backlight(255, 128).expect("cannot set backlight on");
std::thread::spawn({ std::thread::spawn({
let rx = rx.clone(); let rx = rx.clone();
move || turn_lights_on(rx, thread_conn) move || turn_lights_on(rx, thread_conn, abl_timeout)
}); });
}, },
} }
@ -1101,7 +1113,7 @@ fn wrapped_main() -> ! {
}, },
Some(StatusOpcode::TurnLightsOff) => { Some(StatusOpcode::TurnLightsOff) => {
log::trace!("turning lights off"); log::trace!("turning lights off");
let mut run_lock = thread_already_running.lock().unwrap(); let mut run_lock = autobacklight_thread_already_running.lock().unwrap();
*run_lock = false; *run_lock = false;
com.set_backlight(0, 0).expect("cannot set backlight off"); com.set_backlight(0, 0).expect("cannot set backlight off");
}, },
@ -1155,8 +1167,8 @@ enum BacklightThreadOps {
Stop, Stop,
} }
fn turn_lights_on(rx: Box<Receiver<BacklightThreadOps>>, cid: xous::CID) { fn turn_lights_on(rx: Box<Receiver<BacklightThreadOps>>, cid: xous::CID, timeout: u64) {
let standard_duration = std::time::Duration::from_secs(10); let standard_duration = std::time::Duration::from_secs(timeout);
let mut timeout = std::time::Instant::now() + standard_duration; let mut timeout = std::time::Instant::now() + standard_duration;