mirror of
https://github.com/marcriera/ddgo-pnp-controller.git
synced 2025-04-18 09:39:28 +02:00
Implement controller model check
This commit is contained in:
parent
5f53051bcf
commit
e282ef4ccd
1 changed files with 59 additions and 2 deletions
61
src/main.rs
61
src/main.rs
|
@ -1,3 +1,7 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use std::thread::sleep;
|
||||||
use evdev_rs::Device;
|
use evdev_rs::Device;
|
||||||
use evdev_rs::InputEvent;
|
use evdev_rs::InputEvent;
|
||||||
use evdev_rs::ReadFlag;
|
use evdev_rs::ReadFlag;
|
||||||
|
@ -20,25 +24,66 @@ struct Controller {
|
||||||
button_right: bool,
|
button_right: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum ControllerModel {
|
||||||
|
NONE,
|
||||||
|
DGOC44U,
|
||||||
|
TYPE2,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let d1 = Device::new_from_path("/dev/input/event1").unwrap();
|
let d1 = Device::new_from_path("/dev/input/event1").unwrap();
|
||||||
let d2 = Device::new_from_path("/dev/input/event2").unwrap();
|
let d2 = Device::new_from_path("/dev/input/event2").unwrap();
|
||||||
|
|
||||||
let mut controller: Controller = Default::default();
|
let mut controller: Controller = Default::default();
|
||||||
|
let mut controller_model: ControllerModel = ControllerModel::NONE;
|
||||||
|
|
||||||
|
// Save current time and 5 seconds in the future to check for pressed buttons later
|
||||||
|
let start_time = Instant::now();
|
||||||
|
let init_time = start_time + Duration::from_secs(5);
|
||||||
|
|
||||||
|
// Turn on door light to indicate selection mode
|
||||||
|
set_door(true);
|
||||||
|
println!("Hold a button to select the controller model...");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
// Process events from both input devices
|
||||||
for device in [&d1, &d2] {
|
for device in [&d1, &d2] {
|
||||||
let ev = device.next_event(ReadFlag::NORMAL).map(|val| val.1);
|
let ev = device.next_event(ReadFlag::NORMAL).map(|val| val.1);
|
||||||
|
|
||||||
match ev {
|
match ev {
|
||||||
Ok(ev) => read_input(ev, &mut controller),
|
Ok(ev) => read_input(ev, &mut controller),
|
||||||
Err(_e) => (),
|
Err(_e) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If init time has passed, try to select model or quit
|
||||||
|
if controller_model == ControllerModel::NONE && Instant::now() >= init_time {
|
||||||
|
if controller.button_right {
|
||||||
|
controller_model = ControllerModel::DGOC44U;
|
||||||
|
println!("Selected controller DGOC44-U, starting gadget...");
|
||||||
|
}
|
||||||
|
else if controller.button_left {
|
||||||
|
controller_model = ControllerModel::TYPE2;
|
||||||
|
println!("Selected controller TCPP-20009, starting gadget...");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Turn off door light and quit
|
||||||
|
set_door(false);
|
||||||
|
println!("No controller selected, exiting...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Turn off door light and vibrate to end selection mode
|
||||||
|
set_door(false);
|
||||||
|
set_rumble(true);
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
set_rumble(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_input(event: InputEvent, controller: &mut Controller) {
|
fn read_input(event: InputEvent, controller: &mut Controller) {
|
||||||
|
// Save input status to object for easier processing
|
||||||
match event.event_code{
|
match event.event_code{
|
||||||
EventCode::EV_KEY(EV_KEY::KEY_0)=>if event.value == 1 {controller.power = 0},
|
EventCode::EV_KEY(EV_KEY::KEY_0)=>if event.value == 1 {controller.power = 0},
|
||||||
EventCode::EV_KEY(EV_KEY::KEY_1)=>if event.value == 1 {controller.power = 1},
|
EventCode::EV_KEY(EV_KEY::KEY_1)=>if event.value == 1 {controller.power = 1},
|
||||||
|
@ -68,4 +113,16 @@ fn read_input(event: InputEvent, controller: &mut Controller) {
|
||||||
EventCode::EV_KEY(EV_KEY::KEY_RIGHT)=>controller.button_right = event.value != 0,
|
EventCode::EV_KEY(EV_KEY::KEY_RIGHT)=>controller.button_right = event.value != 0,
|
||||||
_=>(),
|
_=>(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_door(status: bool) -> std::io::Result<()> {
|
||||||
|
let mut out = File::create("/sys/class/leds/led2/brightness")?;
|
||||||
|
out.write(if status {b"1"} else {b"0"})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_rumble(status: bool) -> std::io::Result<()> {
|
||||||
|
let mut out = File::create("/sys/class/leds/led1/brightness")?;
|
||||||
|
out.write(if status {b"1"} else {b"0"})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue