From cb2a3b442d511e5169c7a627be3f67114ca5c0d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Sat, 14 May 2022 11:17:26 +0200 Subject: [PATCH] intermediate commit --- Cargo.lock | 20 +++++++++++ Cargo.toml | 2 ++ services/keyboard-backlight/Cargo.toml | 26 ++++++++++++++ services/keyboard-backlight/src/main.rs | 48 +++++++++++++++++++++++++ services/keyboard/src/api.rs | 3 ++ services/keyboard/src/lib.rs | 10 ++++++ services/keyboard/src/main.rs | 31 +++++++++++++++- 7 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 services/keyboard-backlight/Cargo.toml create mode 100644 services/keyboard-backlight/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 597a2b327..4d2097e0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1491,6 +1491,26 @@ dependencies = [ "xous-names", ] +[[package]] +name = "keyboard-backlight" +version = "0.1.0" +dependencies = [ + "com", + "keyboard", + "log", + "log-server", + "net", + "num-derive", + "num-traits", + "rkyv", + "ticktimer-server", + "trng", + "utralib", + "xous", + "xous-ipc 0.9.7", + "xous-names", +] + [[package]] name = "lazy_static" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index 5c885f4a0..d3435a022 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ default-members = [ "services/ticktimer-server", "services/xous-names", "services/keyboard", + "services/keyboard-backlight", "services/com", "services/trng", "services/gam", @@ -44,6 +45,7 @@ members = [ "xous-rs", "tools", "macros", + "services/keyboard-backlight", "services/aes-test", "services/graphics-server", "services/log-server", diff --git a/services/keyboard-backlight/Cargo.toml b/services/keyboard-backlight/Cargo.toml new file mode 100644 index 000000000..19a196100 --- /dev/null +++ b/services/keyboard-backlight/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "keyboard-backlight" +version = "0.1.0" +edition = "2021" + +# Dependency policy: fully specify dependencies to the minor version number +[dependencies] +xous = { path = "../../xous-rs" } +log-server = { path = "../log-server" } +ticktimer-server = { path = "../ticktimer-server" } +xous-names = { path = "../xous-names" } +log = "0.4.14" +num-derive = {version = "0.3.3", default-features = false} +num-traits = {version = "0.2.14", default-features = false} +net = {path = "../net"} +xous-ipc = {path="../../xous-ipc"} +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} +trng = {path = "../trng"} +keyboard = {path = "../keyboard"} +com = {path = "../com"} + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} + +[features] +default = [] \ No newline at end of file diff --git a/services/keyboard-backlight/src/main.rs b/services/keyboard-backlight/src/main.rs new file mode 100644 index 000000000..59e0ee5f6 --- /dev/null +++ b/services/keyboard-backlight/src/main.rs @@ -0,0 +1,48 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +use num_traits::*; + +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum KbbOps { + Keypress, + TurnLightsOff, +} + +#[xous::xous_main] +fn xmain() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + let xns = xous_names::XousNames::new().unwrap(); + let kbb_sid = xns + .register_name("_Keyboard backlight_", Some(1)) + .expect("can't register server"); + + // connect to com + let com = com::Com::new(&xns).expect("cannot connect to com"); + + // connect to keyboard + let keyboard = keyboard::Keyboard::new(&xns).expect("cannot connect to keyboard server"); + keyboard.register_observer("_Keyboard backlight_", KbbOps::Keypress.to_u32().unwrap() as usize); + + //let backlight_period_on = + + loop { + let mut msg = xous::receive_message(kbb_sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(KbbOps::Keypress) => {}, + Some(KbbOps::TurnLightsOff) => { + com.set_backlight(0, 0).expect("cannot set backlight off"); + }, + _ => {}, + } + } +} + +fn turn_lights_on(com: &com::Com) { + com.set_backlight(255, 128).expect("cannot set backlight on"); + std::thread::sleep(std::time::Duration::from_secs(7)); + com.set_backlight(0, 0).expect("cannot set backlight off"); +} \ No newline at end of file diff --git a/services/keyboard/src/api.rs b/services/keyboard/src/api.rs index a37140911..58b5b5c50 100644 --- a/services/keyboard/src/api.rs +++ b/services/keyboard/src/api.rs @@ -59,6 +59,9 @@ pub(crate) enum Opcode { /// request for raw keyups/downs RegisterRawListener, + /// request for updates for *when* keyboard is pressed + RegisterKeyObserver, + /// set repeat delay, rate; both in ms SetRepeat, //(u32, u32), diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index de17d8ba3..bab739437 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -41,6 +41,16 @@ impl Keyboard { .expect("couldn't register listener"); } + pub fn register_observer(&self, server_name: &str, action_opcode: usize) { + let kr = KeyboardRegistration { + server_name: String::<64>::from_str(server_name), + listener_op_id: action_opcode + }; + let buf = Buffer::into_buf(kr).unwrap(); + buf.lend(self.conn, Opcode::RegisterKeyObserver.to_u32().unwrap()) + .expect("couldn't register listener"); + } + pub fn set_vibe(&self, enable: bool) -> Result<(), xous::Error> { let ena = if enable { 1 } diff --git a/services/keyboard/src/main.rs b/services/keyboard/src/main.rs index 26620d01c..a4051f4bb 100644 --- a/services/keyboard/src/main.rs +++ b/services/keyboard/src/main.rs @@ -735,8 +735,9 @@ fn xmain() -> ! { // - graphics (if building for hosted mode) // - oqc (for factory test) // - status sub system (for setting the layout) + // - keyboard-backlight (to start backlight when a key is pressed) #[cfg(any(target_os = "none", target_os = "xous"))] - let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(3)).expect("can't register server"); + let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(4)).expect("can't register server"); #[cfg(not(any(target_os = "none", target_os = "xous")))] let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(4)).expect("can't register server"); log::trace!("registered with NS -- {:?}", kbd_sid); @@ -755,6 +756,8 @@ fn xmain() -> ! { let mut listener_op: Option = None; let mut raw_listener_conn: Option = None; let mut raw_listener_op: Option = None; + let mut observer_conn: Option = None; + let mut observer_op: Option = None; let mut vibe = false; let llio = llio::Llio::new(&xns); @@ -809,6 +812,21 @@ fn xmain() -> ! { } } }, + Some(Opcode::RegisterKeyObserver) => { + let buffer = unsafe{Buffer::from_memory_message(msg.body.memory_message().unwrap())}; + let kr = buffer.as_flat::().unwrap(); + match xns.request_connection_blocking(kr.server_name.as_str()) { + Ok(cid) => { + observer_conn = Some(cid); + observer_op = Some(kr.listener_op_id as usize); + } + Err(e) => { + log::error!("couldn't connect to observer: {:?}", e); + observer_conn = None; + observer_op = None; + } + } + }, Some(Opcode::SelectKeyMap) => msg_scalar_unpack!(msg, km, _, _, _, { kbd.set_map(KeyMap::from(km)) }), @@ -946,6 +964,17 @@ fn xmain() -> ! { keys[3] as u32 as usize, ) ).expect("couldn't send key codes to listener"); + + log::trace!("sending observer key"); + xous::send_message(observer_conn.unwrap(), + xous::Message::new_scalar( + observer_op.unwrap(), + 0, + 0, + 0, + 0, + ) + ).expect("couldn't send key codes to listener"); } } // as long as we have a keydown, keep pinging the loop at a high rate. this consumes more power, but keydowns are relatively rare.