From 1ebf3316e7909b9358dddee3f549cb1bf393face Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Fri, 10 Jun 2022 17:14:38 +0200 Subject: [PATCH] feat: wire keyboard backlight controls to main menu --- services/keyboard-backlight/src/lib.rs | 22 +++++++++ services/status/src/main.rs | 63 +++++++++++++++++++++++++- services/status/src/mainmenu.rs | 25 +++++----- 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/services/keyboard-backlight/src/lib.rs b/services/keyboard-backlight/src/lib.rs index 263d2f6ae..0703279da 100644 --- a/services/keyboard-backlight/src/lib.rs +++ b/services/keyboard-backlight/src/lib.rs @@ -19,6 +19,28 @@ impl KeyboardBacklight { pub fn cid(&self) -> xous::CID { self.conn } + + pub fn enable(&self) { + xous::send_message(self.conn, + xous::Message::new_scalar(KbbOps::EnableAutomaticBacklight.to_usize().unwrap(), + 0, + 0, + 0, + 0 + ) + ).map(|_|()).unwrap(); + } + + pub fn disable(&self) { + xous::send_message(self.conn, + xous::Message::new_scalar(KbbOps::DisableAutomaticBacklight.to_usize().unwrap(), + 0, + 0, + 0, + 0 + ) + ).map(|_|()).unwrap(); + } pub fn enabled(&self) -> Result { match xous::send_message(self.conn, diff --git a/services/status/src/main.rs b/services/status/src/main.rs index c37920975..cf84aefbf 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -56,6 +56,12 @@ pub(crate) enum StatusOpcode { /// Set the keyboard map SetKeyboard, + /// Sets automatic backlight on + EnableAutomaticBacklight, + + /// Sets automatic backlight off + DisableAutomaticBacklight, + /// Suspend handler from the main menu TrySuspend, /// Ship mode handler for the main menu @@ -335,7 +341,9 @@ fn main() -> ! { 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, &keyboard_backlight); + let main_menu_sid = xous::create_server().unwrap(); + let status_cid = xous::connect(status_sid).unwrap(); + let menu_manager = create_main_menu(keys.clone(), main_menu_sid, status_cid, &com, time_cid); 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); @@ -366,6 +374,59 @@ fn main() -> ! { let msg = xous::receive_message(status_sid).unwrap(); log::trace!("|status: Message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { + // TODO: handle tts + Some(StatusOpcode::EnableAutomaticBacklight) => { + // first: enable automatic backlight. + keyboard_backlight.enable(); + + // second: delete the first three elements off the menu + menu_manager.delete_item(t!("mainmenu.backlighton", xous::LANG)); + menu_manager.delete_item(t!("mainmenu.backlightoff", xous::LANG)); + menu_manager.delete_item(t!("mainmenu.autobacklighton", xous::LANG)); + + // third: add a "disable automatic backlight element" + menu_manager.insert_item(gam::MenuItem { + name: xous_ipc::String::from_str(t!("mainmenu.autobacklightoff", xous::LANG)), + action_conn: Some(status_cid), + action_opcode: StatusOpcode::DisableAutomaticBacklight.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }, 0); + } + Some(StatusOpcode::DisableAutomaticBacklight) => { + // first: disable automatic backlight. + keyboard_backlight.disable(); + + // second: delete the first element off the menu. + menu_manager.delete_item(t!("mainmenu.autobacklightoff", xous::LANG)); + + // third: construct an array of the new elements to add to the menu. + let new_elems = [ + gam::MenuItem { + name: xous_ipc::String::from_str(t!("mainmenu.backlighton", xous::LANG)), + action_conn: Some(com.conn()), + action_opcode: com.getop_backlight(), + action_payload: gam::MenuPayload::Scalar([191 >> 3, 191 >> 3, 0, 0]), + close_on_select: true, + }, + gam::MenuItem { + name: xous_ipc::String::from_str(t!("mainmenu.backlightoff", xous::LANG)), + action_conn: Some(com.conn()), + action_opcode: com.getop_backlight(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }, + gam::MenuItem { + name: xous_ipc::String::from_str(t!("mainmenu.autobacklighton", xous::LANG)), + action_conn: Some(status_cid), + action_opcode: StatusOpcode::EnableAutomaticBacklight.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + } + ]; + + new_elems.iter().enumerate().for_each(|(index, element)| {let _ = menu_manager.insert_item(*element, index);}); + } Some(StatusOpcode::BattStats) => msg_scalar_unpack!(msg, lo, hi, _, _, { stats = [lo, hi].into(); battstats_tv.clear_str(); diff --git a/services/status/src/mainmenu.rs b/services/status/src/mainmenu.rs index 0111e4cfe..30d96c1c3 100644 --- a/services/status/src/mainmenu.rs +++ b/services/status/src/mainmenu.rs @@ -4,12 +4,11 @@ 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, kbb: &keyboard_backlight::KeyboardBacklight) { +pub fn create_main_menu(keys: Arc>, menu_management_sid: xous::SID, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID) -> MenuMatic { let key_conn = keys.lock().unwrap().conn(); let mut menuitems = Vec::::new(); @@ -36,20 +35,20 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: #[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_conn: Some(status_conn), + action_opcode: StatusOpcode::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, - }); + // #[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)), @@ -147,5 +146,5 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: close_on_select: true, }); - menu_matic(menuitems, MAIN_MENU_NAME, None); + menu_matic(menuitems, MAIN_MENU_NAME, Some(menu_management_sid)).unwrap() }