feat: wire keyboard backlight controls to main menu

This commit is contained in:
Gianguido Sorà 2022-06-10 17:14:38 +02:00
parent 0ff4e569c1
commit 1ebf3316e7
3 changed files with 96 additions and 14 deletions

View file

@ -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<bool, xous::Error> {
match xous::send_message(self.conn,

View file

@ -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();

View file

@ -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<Mutex<RootKeys>>, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID, kbb: &keyboard_backlight::KeyboardBacklight) {
pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, 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::<MenuItem>::new();
@ -36,20 +35,20 @@ pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, 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<Mutex<RootKeys>>, 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()
}