first somewhat working code

This commit is contained in:
Gianguido Sorà 2022-05-20 12:40:32 +02:00
parent cb2a3b442d
commit 9b58fc6bf8
6 changed files with 146 additions and 32 deletions

View file

@ -3,3 +3,6 @@ xtask = "run --package xtask --"
# [net]
# offline = true # do not access the network
[build]
rustflags = ["--cfg", "crossbeam_no_atomic_64"]

31
Cargo.lock generated
View file

@ -568,10 +568,24 @@ dependencies = [
]
[[package]]
name = "crossbeam-channel"
version = "0.5.2"
name = "crossbeam"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa"
checksum = "4ae5588f6b3c3cb05239e90bd110f257254aecd01e4635400391aeae07497845"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
@ -602,6 +616,16 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f25d8400f4a7a5778f0e4e52384a48cbd9b5c495d110786187fc750075277a2"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.6"
@ -1496,6 +1520,7 @@ name = "keyboard-backlight"
version = "0.1.0"
dependencies = [
"com",
"crossbeam",
"keyboard",
"log",
"log-server",

View file

@ -18,9 +18,10 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics
trng = {path = "../trng"}
keyboard = {path = "../keyboard"}
com = {path = "../com"}
crossbeam = "0.8.1"
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}
[features]
default = []
default = []

View file

@ -1,48 +1,117 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
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,
}
#[xous::xous_main]
fn xmain() -> ! {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Trace);
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))
.register_name("_Keyboard backlight_", Some(2))
.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 =
let keyboard = keyboard::Keyboard::new(&xns).expect("cannot connect to keyboard server");
keyboard.register_observer(
"_Keyboard backlight_",
KbbOps::Keypress.to_u32().unwrap() as usize,
);
// TODO: wire this to a real activation routine
let enabled = Arc::new(RwLock::new(true));
let (tx, rx): (Sender<ThreadOps>, Receiver<ThreadOps>) = unbounded();
let rx = Box::new(rx);
let thread_already_running = Arc::new(Mutex::new(false));
let thread_conn = xous::connect(kbb_sid).unwrap();
loop {
let mut msg = xous::receive_message(kbb_sid).unwrap();
let msg = xous::receive_message(kbb_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(KbbOps::Keypress) => {},
Some(KbbOps::Keypress) => {
if !*enabled.read().unwrap() {
continue
}
let mut run_lock = thread_already_running.lock().unwrap();
match *run_lock {
true => {
log::trace!("renewing backlight timer");
tx.send(ThreadOps::Renew).unwrap();
continue
},
false => {
*run_lock = true;
com.set_backlight(255, 128).expect("cannot set backlight on");
std::thread::spawn({
let rx = rx.clone();
move || turn_lights_on(rx, thread_conn)
});
},
}
}
Some(KbbOps::TurnLightsOn) => {
log::trace!("turning lights on");
com.set_backlight(255, 128).expect("cannot set backlight on");
},
Some(KbbOps::TurnLightsOff) => {
log::trace!("turning lights off");
let mut run_lock = thread_already_running.lock().unwrap();
*run_lock = false;
com.set_backlight(0, 0).expect("cannot set backlight off");
},
_ => {},
Some(KbbOps::EnableAutomaticBacklight) => {
*enabled.write().unwrap() = true;
}
Some(KbbOps::DisableAutomaticBacklight) => {
*enabled.write().unwrap() = false;
}
_ => {}
}
}
}
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");
}
fn turn_lights_on(rx: Box<Receiver<ThreadOps>>, cid: xous::CID) {
let standard_duration = std::time::Duration::from_secs(10);
let mut timeout = std::time::Instant::now() + standard_duration;
let mut total_waited = 0;
loop {
select! {
recv(rx) -> _ => {
timeout = std::time::Instant::now() + standard_duration;
total_waited += 1
},
recv(at(timeout)) -> _ => {
log::trace!("timeout finished, total re-waited {}, returning!", total_waited);
xous::send_message(cid, xous::Message::new_scalar(KbbOps::TurnLightsOff.to_usize().unwrap(), 0,0,0,0)).unwrap();
break;
}
};
}
}

View file

@ -737,9 +737,9 @@ fn xmain() -> ! {
// - 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(4)).expect("can't register server");
let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(5)).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");
let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(5)).expect("can't register server");
log::trace!("registered with NS -- {:?}", kbd_sid);
// Create a new kbd object
@ -911,6 +911,19 @@ fn xmain() -> ! {
).unwrap();
}
}
if observer_conn.is_some() && observer_op.is_some() {
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");
}
}),
Some(Opcode::HandlerTrigger) => {
let rawstates = kbd.update();
@ -933,6 +946,19 @@ fn xmain() -> ! {
buf.lend(raw_listener_conn.unwrap(), raw_listener_op.unwrap()).expect("couldn't send raw scancodes");
}
if observer_conn.is_some() && observer_op.is_some() {
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");
}
// interpret scancodes
// the track_* functions track the keyup/keydowns to modify keys with shift, hold, and chord state
let kc: Vec<char> = match kbd.get_map() {
@ -964,17 +990,6 @@ 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.

View file

@ -58,6 +58,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"com",
"xous-names",
"keyboard",
"keyboard-backlight",
"trng",
"llio",
"susres",