feat: wire keyboard-backlight to status menu

This commit is contained in:
Gianguido Sorà 2022-06-10 14:33:33 +02:00
parent 2fee46753e
commit 4115c9b2cd
8 changed files with 114 additions and 21 deletions

1
Cargo.lock generated
View file

@ -3262,6 +3262,7 @@ dependencies = [
"gam",
"graphics-server",
"keyboard",
"keyboard-backlight",
"llio",
"locales",
"log",

View file

@ -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,
}

View file

@ -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<Self, xous::Error> {
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<bool, xous::Error> {
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)
}
}
}
}

View file

@ -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<ThreadOps>, Receiver<ThreadOps>) = 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<Receiver<ThreadOps>>, 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);

View file

@ -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}

View file

@ -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": "ルートキーの初期化",

View file

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

View file

@ -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<Mutex<RootKeys>>, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID) {
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) {
let key_conn = keys.lock().unwrap().conn();
let mut menuitems = Vec::<MenuItem>::new();
@ -32,6 +33,24 @@ pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, 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),