intermediate commit

This commit is contained in:
Gianguido Sorà 2022-05-14 11:17:26 +02:00
parent 9a11056340
commit cb2a3b442d
7 changed files with 139 additions and 1 deletions

20
Cargo.lock generated
View file

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

View file

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

View file

@ -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 = []

View file

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

View file

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

View file

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

View file

@ -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<usize> = None;
let mut raw_listener_conn: Option<CID> = None;
let mut raw_listener_op: Option<u32> = None;
let mut observer_conn: Option<CID> = None;
let mut observer_op: Option<usize> = 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::<KeyboardRegistration, _>().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.