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" version = "0.1.0"
edition = "2021" edition = "2021"
[package.metadata.cross.target.arm-unknown-linux-musleabi]
pre-build = ["apt-get update && apt-get install -y python3"]
[dependencies] [dependencies]
evdev = "0.12.1" evdev = "0.12.1"

View file

@ -11,11 +11,12 @@ use virtual_controller::ControllerModel;
fn main() -> Result<()> { fn main() -> Result<()> {
match physical_controller::init() { match physical_controller::init() {
Ok(mut dev) => { Ok(dev) => {
// Wait 3 seconds and get current state of the controller // Wait 3 seconds and get current state of the controller
println!("Press a button to select the controller model..."); println!("Press a button to select the controller model...");
sleep(Duration::from_secs(3)); 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); let controller_model = virtual_controller::set_model(&controller_state);
// If no model selected, quit // If no model selected, quit
@ -23,29 +24,18 @@ fn main() -> Result<()> {
return Result::Ok(()); return Result::Ok(());
} }
// Stop main game
stop_game();
// Vibrate to end selection mode // Vibrate to end selection mode
physical_controller::set_rumble(true); physical_controller::set_rumble(true);
sleep(Duration::from_millis(500)); sleep(Duration::from_millis(500));
physical_controller::set_rumble(false); physical_controller::set_rumble(false);
// Stop main game
stop_game();
loop { loop {
// Process events from input devices // Fetch events from input devices
/*for mut device in [&mut d1, &mut d2] { physical_controller::get_state(&mut controller_state, &dev);
let evs = device.fetch_events(); sleep(Duration::from_millis(100));
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) => (),
}
}*/
} }
}, },
Err(_e) => println!("ERROR: Could not read input devices! Exiting."), 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]; 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]> { pub fn init() -> Result<[Device; 2]> {
let d1 = Device::open("/dev/input/event14")?; let d1 = Device::open("/dev/input/event1")?;
let d2 = Device::open("/dev/input/event14")?; let d2 = Device::open("/dev/input/event2")?;
Ok([d1, d2]) Ok([d1, d2])
} }
pub fn get_state(dev: [Device; 2]) -> ControllerState { pub fn get_state(state: &mut ControllerState, dev: &[Device; 2]) {
let mut state: ControllerState = Default::default();
for d in dev { for d in dev {
if let Ok(key_vals) = d.get_key_state() { if let Ok(key_vals) = d.get_key_state() {
for key in USED_KEYS { 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) { fn read_input(controller: &mut ControllerState, key: Key, value: bool) {
// Save input status to object for processing // Save input status to object for processing
match key{ match key{