Poll devices for input

This commit is contained in:
Marc Riera 2023-02-28 00:03:28 +01:00
parent 7e640edcc6
commit 382990f596
3 changed files with 29 additions and 28 deletions

View file

@ -3,8 +3,5 @@ name = "ddgo-pnp-controller"
version = "0.1.0"
edition = "2021"
[package.metadata.cross.target.arm-unknown-linux-musleabi]
pre-build = ["apt-get update && apt-get install -y python3"]
[dependencies]
evdev = "0.12.1"

View file

@ -11,11 +11,12 @@ use virtual_controller::ControllerModel;
fn main() -> Result<()> {
match physical_controller::init() {
Ok(mut dev) => {
Ok(dev) => {
// Wait 3 seconds and get current state of the controller
println!("Press a button to select the controller model...");
sleep(Duration::from_secs(3));
let mut controller_state = physical_controller::get_state(dev);
let mut controller_state = Default::default();
physical_controller::get_state(&mut controller_state, &dev);
let controller_model = virtual_controller::set_model(&controller_state);
// If no model selected, quit
@ -23,29 +24,18 @@ fn main() -> Result<()> {
return Result::Ok(());
}
// Stop main game
stop_game();
// Vibrate to end selection mode
physical_controller::set_rumble(true);
sleep(Duration::from_millis(500));
physical_controller::set_rumble(false);
// Stop main game
stop_game();
loop {
// Process events from input devices
/*for mut device in [&mut d1, &mut d2] {
let evs = device.fetch_events();
match evs {
Ok(evs) => {
for event in evs {
if event.event_type() == EventType::KEY {
physical_controller::read_input(&mut controller_state, Key(event.code()), event.value());
}
}
},
Err(_e) => (),
}
}*/
// Fetch events from input devices
physical_controller::get_state(&mut controller_state, &dev);
sleep(Duration::from_millis(100));
}
},
Err(_e) => println!("ERROR: Could not read input devices! Exiting."),

View file

@ -12,23 +12,37 @@ const USED_KEYS: [Key; 26] = [Key::KEY_0, Key::KEY_1, Key::KEY_2, Key::KEY_3, Ke
Key::KEY_SPACE, Key::KEY_ENTER, Key::KEY_A, Key::KEY_Z, Key::KEY_X, Key::KEY_S, Key::KEY_UP, Key::KEY_DOWN, Key::KEY_LEFT, Key::KEY_RIGHT];
pub fn init() -> Result<[Device; 2]> {
let d1 = Device::open("/dev/input/event14")?;
let d2 = Device::open("/dev/input/event14")?;
let d1 = Device::open("/dev/input/event1")?;
let d2 = Device::open("/dev/input/event2")?;
Ok([d1, d2])
}
pub fn get_state(dev: [Device; 2]) -> ControllerState {
let mut state: ControllerState = Default::default();
pub fn get_state(state: &mut ControllerState, dev: &[Device; 2]) {
for d in dev {
if let Ok(key_vals) = d.get_key_state() {
for key in USED_KEYS {
read_input(&mut state, key, key_vals.contains(key));
read_input(state, key, key_vals.contains(key));
}
}
}
return state;
}
/* pub fn fetch_input(state: &mut ControllerState, dev: &mut [Device; 2]) {
for device in dev {
let evs = device.fetch_events();
match evs {
Ok(evs) => {
for event in evs {
if event.event_type() == EventType::KEY {
read_input(state, Key(event.code()), event.value() != 0);
}
}
},
Err(_e) => (),
}
}
} */
fn read_input(controller: &mut ControllerState, key: Key, value: bool) {
// Save input status to object for processing
match key{