diff --git a/Cargo.lock b/Cargo.lock index 2d9f48e9f..c77b2fd35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3262,6 +3262,7 @@ dependencies = [ "gam", "graphics-server", "keyboard", + "keyboard-backlight", "llio", "locales", "log", diff --git a/services/keyboard-backlight/src/api.rs b/services/keyboard-backlight/src/api.rs new file mode 100644 index 000000000..d8b9789df --- /dev/null +++ b/services/keyboard-backlight/src/api.rs @@ -0,0 +1,11 @@ +pub const KBB_SERVER_NAME: &str = "_Keyboard backlight_"; + +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +pub enum KbbOps { + Keypress, + TurnLightsOff, + TurnLightsOn, + EnableAutomaticBacklight, + DisableAutomaticBacklight, + Status, +} \ No newline at end of file diff --git a/services/keyboard-backlight/src/lib.rs b/services/keyboard-backlight/src/lib.rs new file mode 100644 index 000000000..263d2f6ae --- /dev/null +++ b/services/keyboard-backlight/src/lib.rs @@ -0,0 +1,44 @@ +pub mod api; +use api::*; + +use num_traits::*; + + +#[derive(Debug)] +pub struct KeyboardBacklight { + conn: xous::CID, +} +impl KeyboardBacklight { + pub fn new(xns: &xous_names::XousNames) -> Result { + let conn = xns.request_connection_blocking(api::KBB_SERVER_NAME).expect("Can't connect to KBD"); + Ok(KeyboardBacklight { + conn, + }) + } + + pub fn cid(&self) -> xous::CID { + self.conn + } + + pub fn enabled(&self) -> Result { + match xous::send_message(self.conn, + xous::Message::new_blocking_scalar(KbbOps::Status.to_usize().unwrap(), + 0, + 0, + 0, + 0 + ) + ) { + Ok(xous::Result::Scalar1(last_state)) => { + if last_state == 1 { + Ok(true) + } else { + Ok(false) + } + } + _ => { + Err(xous::Error::InternalError) + } + } + } +} \ No newline at end of file diff --git a/services/keyboard-backlight/src/main.rs b/services/keyboard-backlight/src/main.rs index 807f20514..9e128050d 100644 --- a/services/keyboard-backlight/src/main.rs +++ b/services/keyboard-backlight/src/main.rs @@ -1,23 +1,17 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] +mod api; +use api::*; + use crossbeam::channel::{at, select, unbounded, Receiver, Sender}; use num_traits::*; use std::sync::Arc; use std::sync::Mutex; -use std::sync::RwLock; - -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -enum KbbOps { - Keypress, - TurnLightsOff, - TurnLightsOn, - EnableAutomaticBacklight, - DisableAutomaticBacklight, -} enum ThreadOps { Renew, + Stop, } fn main() -> ! { @@ -27,7 +21,7 @@ fn main() -> ! { let xns = xous_names::XousNames::new().unwrap(); let kbb_sid = xns - .register_name("_Keyboard backlight_", Some(2)) + .register_name(api::KBB_SERVER_NAME, Some(2)) .expect("can't register server"); // connect to com @@ -39,8 +33,7 @@ fn main() -> ! { KbbOps::Keypress.to_u32().unwrap() as usize, ); - // TODO: wire this to a real activation routine - let enabled = Arc::new(RwLock::new(true)); + let enabled = Arc::new(Mutex::new(false)); let (tx, rx): (Sender, Receiver) = unbounded(); let rx = Box::new(rx); @@ -52,7 +45,8 @@ fn main() -> ! { let msg = xous::receive_message(kbb_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { Some(KbbOps::Keypress) => { - if !*enabled.read().unwrap() { + if !*enabled.lock().unwrap() { + log::trace!("ignoring keypress, automatic backlight is disabled"); continue } let mut run_lock = thread_already_running.lock().unwrap(); @@ -83,11 +77,16 @@ fn main() -> ! { com.set_backlight(0, 0).expect("cannot set backlight off"); }, Some(KbbOps::EnableAutomaticBacklight) => { - *enabled.write().unwrap() = true; + *enabled.lock().unwrap() = true; } Some(KbbOps::DisableAutomaticBacklight) => { - *enabled.write().unwrap() = false; + *enabled.lock().unwrap() = false; + tx.send(ThreadOps::Stop).unwrap(); } + Some(KbbOps::Status) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { + let status = *enabled.lock().unwrap(); + xous::return_scalar(msg.sender, status.into()).unwrap(); + }), _ => {} } } @@ -102,9 +101,17 @@ fn turn_lights_on(rx: Box>, cid: xous::CID) { loop { select! { - recv(rx) -> _ => { - timeout = std::time::Instant::now() + standard_duration; - total_waited += 1 + recv(rx) -> op => { + match op.unwrap() { + ThreadOps::Renew => { + timeout = std::time::Instant::now() + standard_duration; + total_waited += 1; + }, + ThreadOps::Stop => { + log::trace!("received Stop op, killing background backlight thread"); + return; + }, + } }, recv(at(timeout)) -> _ => { log::trace!("timeout finished, total re-waited {}, returning!", total_waited); diff --git a/services/status/Cargo.toml b/services/status/Cargo.toml index 92aefdb7e..bf841d444 100644 --- a/services/status/Cargo.toml +++ b/services/status/Cargo.toml @@ -27,6 +27,7 @@ pddb = {path = "../pddb"} net = {path = "../net"} keyboard = {path = "../keyboard"} usb-device-xous = {path="../usb-device-xous"} +keyboard-backlight = {path = "../keyboard-backlight"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/status/locales/i18n.json b/services/status/locales/i18n.json index 915c5a702..e0325ce54 100644 --- a/services/status/locales/i18n.json +++ b/services/status/locales/i18n.json @@ -90,6 +90,14 @@ "zh": "背光关闭", "en-tts": "🔇" }, + "mainmenu.autobacklighton": { + "en": "Automatic backlight on", + "en-tts": "🔇" + }, + "mainmenu.autobacklightoff": { + "en": "Automatic backlight off", + "en-tts": "🔇" + }, "mainmenu.init_keys": { "en": "Initialize root keys", "ja": "ルートキーの初期化", diff --git a/services/status/src/main.rs b/services/status/src/main.rs index bf042c287..c37920975 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -332,8 +332,10 @@ fn main() -> ! { // used to show notifications, e.g. can't sleep while power is engaged. let modals = modals::Modals::new(&xns).unwrap(); + let keyboard_backlight = keyboard_backlight::KeyboardBacklight::new(&xns).unwrap(); + log::debug!("starting main menu thread"); - create_main_menu(keys.clone(), xous::connect(status_sid).unwrap(), &com, time_cid); + create_main_menu(keys.clone(), xous::connect(status_sid).unwrap(), &com, time_cid, &keyboard_backlight); create_app_menu(xous::connect(status_sid).unwrap()); let kbd_mgr = xous::create_server().unwrap(); let kbd_menumatic = create_kbd_menu(xous::connect(status_sid).unwrap(), kbd_mgr); diff --git a/services/status/src/mainmenu.rs b/services/status/src/mainmenu.rs index e53719d30..0111e4cfe 100644 --- a/services/status/src/mainmenu.rs +++ b/services/status/src/mainmenu.rs @@ -4,11 +4,12 @@ use root_keys::RootKeys; use std::sync::{Arc, Mutex}; use xous_ipc::String; use num_traits::*; +use keyboard_backlight::api::*; use crate::StatusOpcode; #[allow(unused_variables)] // quiets a warning about unused com that is emitted in tts config. Would be nice to make this more targeted... -pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID) { +pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID, kbb: &keyboard_backlight::KeyboardBacklight) { let key_conn = keys.lock().unwrap().conn(); let mut menuitems = Vec::::new(); @@ -32,6 +33,24 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: close_on_select: true, }); + #[cfg(not(feature="tts"))] + menuitems.push(MenuItem { + name: String::from_str(t!("mainmenu.autobacklighton", xous::LANG)), + action_conn: Some(kbb.cid()), + action_opcode: KbbOps::EnableAutomaticBacklight.to_u32().unwrap(), + action_payload: MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }); + + #[cfg(not(feature="tts"))] + menuitems.push(MenuItem { + name: String::from_str(t!("mainmenu.autobacklightoff", xous::LANG)), + action_conn: Some(kbb.cid()), + action_opcode: KbbOps::DisableAutomaticBacklight.to_u32().unwrap(), + action_payload: MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }); + menuitems.push(MenuItem { name: String::from_str(t!("mainmenu.sleep", xous::LANG)), action_conn: Some(status_conn),