From 382990f596d994dfb44be6c0e85ed7da11cc8386 Mon Sep 17 00:00:00 2001 From: Marc Riera Date: Tue, 28 Feb 2023 00:03:28 +0100 Subject: [PATCH] Poll devices for input --- Cargo.toml | 3 --- src/main.rs | 28 +++++++++------------------- src/physical_controller.rs | 26 ++++++++++++++++++++------ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ca56bd..8124fcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index 2ac10d9..20ba255 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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."), diff --git a/src/physical_controller.rs b/src/physical_controller.rs index 9806a98..735b9bb 100644 --- a/src/physical_controller.rs +++ b/src/physical_controller.rs @@ -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{