From a6b99f281716ce65cc5af572007a5c2d05fe1cc7 Mon Sep 17 00:00:00 2001 From: Marc Riera Date: Sun, 23 Mar 2025 21:21:45 +0100 Subject: [PATCH] Update to Rust 2024 and formatting changes --- Cargo.toml | 6 +- src/controller.rs | 2 +- src/controller/emulated.rs | 184 ++++++--- src/controller/emulated/dgoc44u.rs | 139 ++++--- src/controller/emulated/slph00051.rs | 400 ++++++++++++------- src/controller/emulated/sotp031201_p4b2b7.rs | 69 +++- src/controller/emulated/sotp031201_p4b7.rs | 71 +++- src/controller/emulated/sotp031201_p5b5.rs | 71 +++- src/controller/emulated/sotp031201_p5b7.rs | 71 +++- src/controller/emulated/tcpp20009.rs | 85 ++-- src/controller/emulated/tcpp20011.rs | 87 ++-- src/controller/emulated/zkns001.rs | 204 ++++++---- src/controller/physical.rs | 162 ++++++-- src/main.rs | 14 +- 14 files changed, 1067 insertions(+), 498 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c6baeda..1c12fe8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "ddgo-pnp-controller" -version = "1.2.0" -edition = "2021" +version = "1.2.1" +edition = "2024" [dependencies] bitflags = "1.3.2" -evdev = "0.12.1" +evdev = "0.12.2" diff --git a/src/controller.rs b/src/controller.rs index 4586bff..cb235eb 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,2 +1,2 @@ -pub mod physical; pub mod emulated; +pub mod physical; diff --git a/src/controller/emulated.rs b/src/controller/emulated.rs index 72d31d5..8f054b8 100644 --- a/src/controller/emulated.rs +++ b/src/controller/emulated.rs @@ -1,23 +1,23 @@ -use std::thread; -use std::thread::sleep; -use std::time::Duration; use std::fs; use std::fs::File; use std::io::{Read, Write}; -use std::process::Command; use std::path::Path; +use std::process::Command; +use std::thread; +use std::thread::sleep; +use std::time::Duration; use crate::controller::physical::ControllerState; mod dgoc44u; -mod tcpp20009; -mod tcpp20011; -mod sotp031201_p4b7; +mod slph00051; mod sotp031201_p4b2b7; +mod sotp031201_p4b7; mod sotp031201_p5b5; mod sotp031201_p5b7; +mod tcpp20009; +mod tcpp20011; mod zkns001; -mod slph00051; const FFS_MOUNT: &str = "/tmp/ffs"; const ENDPOINT0: &str = "/tmp/ffs/ep0"; @@ -55,49 +55,76 @@ pub fn set_model(state: &ControllerState) -> Option { if state.button_right { model_name = "DGOC44-U"; model = ControllerModel::DGOC44U; - descriptors = (&dgoc44u::DEVICE_DESCRIPTOR, &dgoc44u::DESCRIPTORS, &dgoc44u::STRINGS); - } - else if state.button_up { + descriptors = ( + &dgoc44u::DEVICE_DESCRIPTOR, + &dgoc44u::DESCRIPTORS, + &dgoc44u::STRINGS, + ); + } else if state.button_up { model_name = "ZKNS-001"; model = ControllerModel::ZKNS001; - descriptors = (&zkns001::DEVICE_DESCRIPTOR, &zkns001::DESCRIPTORS, &zkns001::STRINGS); - } - else if state.button_down && state.power == 0 { + descriptors = ( + &zkns001::DEVICE_DESCRIPTOR, + &zkns001::DESCRIPTORS, + &zkns001::STRINGS, + ); + } else if state.button_down && state.power == 0 { model_name = "SLPH-00051"; model = ControllerModel::SLPH00051; - descriptors = (&slph00051::DEVICE_DESCRIPTOR, &slph00051::DESCRIPTORS, &slph00051::STRINGS); - } - else if state.button_d { + descriptors = ( + &slph00051::DEVICE_DESCRIPTOR, + &slph00051::DESCRIPTORS, + &slph00051::STRINGS, + ); + } else if state.button_d { model_name = "TCPP-20009"; model = ControllerModel::TCPP20009; - descriptors = (&tcpp20009::DEVICE_DESCRIPTOR, &tcpp20009::DESCRIPTORS, &tcpp20009::STRINGS); - } - else if state.button_b { + descriptors = ( + &tcpp20009::DEVICE_DESCRIPTOR, + &tcpp20009::DESCRIPTORS, + &tcpp20009::STRINGS, + ); + } else if state.button_b { model_name = "TCPP-20011"; model = ControllerModel::TCPP20011; - descriptors = (&tcpp20011::DEVICE_DESCRIPTOR, &tcpp20011::DESCRIPTORS, &tcpp20011::STRINGS); - } - else if state.button_c && state.power == 0 { + descriptors = ( + &tcpp20011::DEVICE_DESCRIPTOR, + &tcpp20011::DESCRIPTORS, + &tcpp20011::STRINGS, + ); + } else if state.button_c && state.power == 0 { model_name = "SOTP-031201 (P4/B7 mode)"; model = ControllerModel::SOTP031201P4B7; - descriptors = (&sotp031201_p4b7::DEVICE_DESCRIPTOR, &sotp031201_p4b7::DESCRIPTORS, &sotp031201_p4b7::STRINGS); - } - else if state.button_c && state.power == 1 { + descriptors = ( + &sotp031201_p4b7::DEVICE_DESCRIPTOR, + &sotp031201_p4b7::DESCRIPTORS, + &sotp031201_p4b7::STRINGS, + ); + } else if state.button_c && state.power == 1 { model_name = "SOTP-031201 (P4/B2-B7 mode)"; model = ControllerModel::SOTP031201P4B2B7; - descriptors = (&sotp031201_p4b2b7::DEVICE_DESCRIPTOR, &sotp031201_p4b2b7::DESCRIPTORS, &sotp031201_p4b2b7::STRINGS); - } - else if state.button_c && state.power == 2 { + descriptors = ( + &sotp031201_p4b2b7::DEVICE_DESCRIPTOR, + &sotp031201_p4b2b7::DESCRIPTORS, + &sotp031201_p4b2b7::STRINGS, + ); + } else if state.button_c && state.power == 2 { model_name = "SOTP-031201 (P5/B5 mode)"; model = ControllerModel::SOTP031201P5B5; - descriptors = (&sotp031201_p5b5::DEVICE_DESCRIPTOR, &sotp031201_p5b5::DESCRIPTORS, &sotp031201_p5b5::STRINGS); - } - else if state.button_c && state.power == 3 { + descriptors = ( + &sotp031201_p5b5::DEVICE_DESCRIPTOR, + &sotp031201_p5b5::DESCRIPTORS, + &sotp031201_p5b5::STRINGS, + ); + } else if state.button_c && state.power == 3 { model_name = "SOTP-031201 (P5/B7 mode)"; model = ControllerModel::SOTP031201P5B7; - descriptors = (&sotp031201_p5b7::DEVICE_DESCRIPTOR, &sotp031201_p5b7::DESCRIPTORS, &sotp031201_p5b7::STRINGS); - } - else { + descriptors = ( + &sotp031201_p5b7::DEVICE_DESCRIPTOR, + &sotp031201_p5b7::DESCRIPTORS, + &sotp031201_p5b7::STRINGS, + ); + } else { println!("ddgo-pnp-controller: No controller selected, starting RNDIS gadget."); Command::new("rndis-gadget.sh").output().ok(); return None; @@ -166,31 +193,38 @@ pub fn handle_ctrl_transfer(model: ControllerModel, data: &[u8]) { } None => (), } - } - else { + } else { match model { ControllerModel::SLPH00051 => { slph00051::handle_ctrl_transfer(data); } - _ => () + _ => (), } } } -fn init_gadget(model: &ControllerModel, (device, descriptors, strings): (&DeviceDescriptor, &[u8], &[u8])) { +fn init_gadget( + model: &ControllerModel, + (device, descriptors, strings): (&DeviceDescriptor, &[u8], &[u8]), +) { // Init g_ffs kernel module - Command::new("modprobe").arg("g_ffs") - .arg(String::from("bDeviceClass=")+&device.b_device_class.to_string()) - .arg(String::from("bDeviceSubClass=")+&device.b_device_sub_class.to_string()) - .arg(String::from("idVendor=")+&device.id_vendor.to_string()) - .arg(String::from("idProduct=")+&device.id_product.to_string()) - .arg(String::from("bcdDevice=")+&device.bcd_device.to_string()) - .arg(String::from("iManufacturer=")+device.i_manufacturer) - .arg(String::from("iProduct=")+device.i_product) - .arg(String::from("iSerialNumber=")+device.i_serial_number) - .output().ok(); - Command::new("mkdir").args(["-p",&FFS_MOUNT]).output().ok(); - Command::new("mount").args(["-t","functionfs","ffs",&FFS_MOUNT]).output().ok(); + Command::new("modprobe") + .arg("g_ffs") + .arg(String::from("bDeviceClass=") + &device.b_device_class.to_string()) + .arg(String::from("bDeviceSubClass=") + &device.b_device_sub_class.to_string()) + .arg(String::from("idVendor=") + &device.id_vendor.to_string()) + .arg(String::from("idProduct=") + &device.id_product.to_string()) + .arg(String::from("bcdDevice=") + &device.bcd_device.to_string()) + .arg(String::from("iManufacturer=") + device.i_manufacturer) + .arg(String::from("iProduct=") + device.i_product) + .arg(String::from("iSerialNumber=") + device.i_serial_number) + .output() + .ok(); + Command::new("mkdir").args(["-p", &FFS_MOUNT]).output().ok(); + Command::new("mount") + .args(["-t", "functionfs", "ffs", &FFS_MOUNT]) + .output() + .ok(); let controller_model = model.clone(); @@ -219,14 +253,46 @@ fn init_gadget(model: &ControllerModel, (device, descriptors, strings): (&Device // Init Android Gadget for old 3.4 kernel let gadget = Path::new(&ANDROID_GADGET); if gadget.is_dir() { - fs::write(gadget.join(Path::new("bDeviceClass")), &device.b_device_class.to_string()).ok(); - fs::write(gadget.join(Path::new("bDeviceSubClass")), &device.b_device_sub_class.to_string()).ok(); - fs::write(gadget.join(Path::new("idVendor")), format!("{:x}", &device.id_vendor)).ok(); - fs::write(gadget.join(Path::new("idProduct")), format!("{:x}", &device.id_product)).ok(); - fs::write(gadget.join(Path::new("bcdDevice")), format!("{:x}", &device.bcd_device)).ok(); - fs::write(gadget.join(Path::new("iManufacturer")), &device.i_manufacturer.to_string()).ok(); - fs::write(gadget.join(Path::new("iProduct")), &device.i_product.to_string()).ok(); - fs::write(gadget.join(Path::new("iSerial")), &device.i_serial_number.to_string()).ok(); + fs::write( + gadget.join(Path::new("bDeviceClass")), + &device.b_device_class.to_string(), + ) + .ok(); + fs::write( + gadget.join(Path::new("bDeviceSubClass")), + &device.b_device_sub_class.to_string(), + ) + .ok(); + fs::write( + gadget.join(Path::new("idVendor")), + format!("{:x}", &device.id_vendor), + ) + .ok(); + fs::write( + gadget.join(Path::new("idProduct")), + format!("{:x}", &device.id_product), + ) + .ok(); + fs::write( + gadget.join(Path::new("bcdDevice")), + format!("{:x}", &device.bcd_device), + ) + .ok(); + fs::write( + gadget.join(Path::new("iManufacturer")), + &device.i_manufacturer.to_string(), + ) + .ok(); + fs::write( + gadget.join(Path::new("iProduct")), + &device.i_product.to_string(), + ) + .ok(); + fs::write( + gadget.join(Path::new("iSerial")), + &device.i_serial_number.to_string(), + ) + .ok(); fs::write(gadget.join(Path::new("functions")), "ffs").ok(); fs::write(gadget.join(Path::new("f_ffs/aliases")), "ffs").ok(); fs::write(gadget.join(Path::new("enable")), "1").ok(); diff --git a/src/controller/emulated/dgoc44u.rs b/src/controller/emulated/dgoc44u.rs index 07715ed..9dd81f7 100644 --- a/src/controller/emulated/dgoc44u.rs +++ b/src/controller/emulated/dgoc44u.rs @@ -1,54 +1,65 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 66] = [0x01, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x3F, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x05, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x3F, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x05]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 66] = [ + 0x01, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, + 0x3F, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x05, 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, + 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x3F, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, + 0x00, 0x05, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x0, b_device_sub_class: 0x0, id_vendor: 0x0AE4, id_product: 0x0003, bcd_device: 0x0102, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play", i_serial_number: "DGOC-44U"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x0, + b_device_sub_class: 0x0, + id_vendor: 0x0AE4, + id_product: 0x0003, + bcd_device: 0x0102, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play", + i_serial_number: "DGOC-44U", +}; pub const HID_REPORT_DESCRIPTOR: [u8; 63] = [ - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x09, 0x04, // Usage (Joystick) - 0xA1, 0x01, // Collection (Application) - 0x09, 0x01, // Usage (Pointer) - 0xA1, 0x00, // Collection (Physical) - 0x09, 0x30, // Usage (X) - 0x09, 0x31, // Usage (Y) - 0x15, 0x00, // Logical Minimum (0) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x75, 0x08, // Report Size (8) - 0x95, 0x02, // Report Count (2) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0xC0, // End Collection - 0x75, 0x08, // Report Size (8) - 0x95, 0x01, // Report Count (1) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x05, 0x09, // Usage Page (Button) - 0x19, 0x01, // Usage Minimum (0x01) - 0x29, 0x06, // Usage Maximum (0x06) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x35, 0x00, // Physical Minimum (0) - 0x45, 0x01, // Physical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x06, // Report Count (6) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x95, 0x02, // Report Count (2) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x75, 0x08, // Report Size (8) - 0x95, 0x02, // Report Count (2) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0xC0 // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x09, 0x04, // Usage (Joystick) + 0xA1, 0x01, // Collection (Application) + 0x09, 0x01, // Usage (Pointer) + 0xA1, 0x00, // Collection (Physical) + 0x09, 0x30, // Usage (X) + 0x09, 0x31, // Usage (Y) + 0x15, 0x00, // Logical Minimum (0) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x75, 0x08, // Report Size (8) + 0x95, 0x02, // Report Count (2) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0xC0, // End Collection + 0x75, 0x08, // Report Size (8) + 0x95, 0x01, // Report Count (1) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x05, 0x09, // Usage Page (Button) + 0x19, 0x01, // Usage Minimum (0x01) + 0x29, 0x06, // Usage Maximum (0x06) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x35, 0x00, // Physical Minimum (0) + 0x45, 0x01, // Physical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x06, // Report Count (6) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x95, 0x02, // Report Count (2) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x75, 0x08, // Report Size (8) + 0x95, 0x02, // Report Count (2) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0xC0, // End Collection ]; const POWER_NOTCHES: [u8; 6] = [0x81, 0x6D, 0x54, 0x3F, 0x21, 0x00]; @@ -77,20 +88,40 @@ pub fn update_gadget(state: &mut ControllerState) { // Calculate data for buttons let mut buttons = Buttons::NONE; - if state.button_a { buttons.insert(Buttons::A) } - if state.button_b { buttons.insert(Buttons::B) } - if state.button_c { buttons.insert(Buttons::C) } - if state.button_d { buttons.insert(Buttons::D) } - if state.button_select { buttons.insert(Buttons::SELECT) } - if state.button_start { buttons.insert(Buttons::START) } - if state.button_up { buttons.insert(Buttons::UP) } - if state.button_down { buttons.insert(Buttons::DOWN) } - if state.button_left { buttons.insert(Buttons::LEFT) } - if state.button_right { buttons.insert(Buttons::RIGHT) } + if state.button_a { + buttons.insert(Buttons::A) + } + if state.button_b { + buttons.insert(Buttons::B) + } + if state.button_c { + buttons.insert(Buttons::C) + } + if state.button_d { + buttons.insert(Buttons::D) + } + if state.button_select { + buttons.insert(Buttons::SELECT) + } + if state.button_start { + buttons.insert(Buttons::START) + } + if state.button_up { + buttons.insert(Buttons::UP) + } + if state.button_down { + buttons.insert(Buttons::DOWN) + } + if state.button_left { + buttons.insert(Buttons::LEFT) + } + if state.button_right { + buttons.insert(Buttons::RIGHT) + } // Assemble data and send it to gadget let data = [brake, power, 0, buttons.bits, 0, 0]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/emulated/slph00051.rs b/src/controller/emulated/slph00051.rs index aedda6f..4d351a0 100644 --- a/src/controller/emulated/slph00051.rs +++ b/src/controller/emulated/slph00051.rs @@ -1,112 +1,130 @@ -use std::fs::File; -use std::io::{Write}; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT0, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; +use std::fs::File; +use std::io::Write; -pub const DESCRIPTORS: [u8; 80] = [0x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x94, 0x00, -0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, -0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, -0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x94, 0x00, -0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, -0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; - -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x0, b_device_sub_class: 0x0, id_vendor: 0x054C, id_product: 0x0268, bcd_device: 0x0100, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (PS1 Two Handle mode)", i_serial_number: "SLPH-00051"}; - -pub const HID_REPORT_DESCRIPTOR: [u8; 148] = [ - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x09, 0x04, // Usage (Joystick) - 0xA1, 0x01, // Collection (Physical) - 0xA1, 0x02, // Collection (Application) - 0x85, 0x01, // Report ID (1) - 0x75, 0x08, // Report Size (8) - 0x95, 0x01, // Report Count (1) - 0x15, 0x00, // Logical Minimum (0) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - // NOTE: reserved byte - 0x75, 0x01, // Report Size (1) - 0x95, 0x13, // Report Count (19) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x35, 0x00, // Physical Minimum (0) - 0x45, 0x01, // Physical Maximum (1) - 0x05, 0x09, // Usage Page (Button) - 0x19, 0x01, // Usage Minimum (0x01) - 0x29, 0x13, // Usage Maximum (0x13) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x75, 0x01, // Report Size (1) - 0x95, 0x0D, // Report Count (13) - 0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00) - 0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - // NOTE: 32 bit integer, where 0:18 are buttons and 19:31 are reserved - 0x15, 0x00, // Logical Minimum (0) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x09, 0x01, // Usage (Pointer) - 0xA1, 0x00, // Collection (Undefined) - 0x75, 0x08, // Report Size (8) - 0x95, 0x04, // Report Count (4) - 0x35, 0x00, // Physical Minimum (0) - 0x46, 0xFF, 0x00, // Physical Maximum (255) - 0x09, 0x30, // Usage (X) - 0x09, 0x31, // Usage (Y) - 0x09, 0x32, // Usage (Z) - 0x09, 0x35, // Usage (Rz) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - // NOTE: four joysticks - 0xC0, // End Collection - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x75, 0x08, // Report Size (8) - 0x95, 0x27, // Report Count (39) - 0x09, 0x01, // Usage (Pointer) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x75, 0x08, // Report Size (8) - 0x95, 0x30, // Report Count (48) - 0x09, 0x01, // Usage (Pointer) - 0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0x75, 0x08, // Report Size (8) - 0x95, 0x30, // Report Count (48) - 0x09, 0x01, // Usage (Pointer) - 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0xC0, // End Collection - 0xA1, 0x02, // Collection (Application) - 0x85, 0x02, // Report ID (2) - 0x75, 0x08, // Report Size (8) - 0x95, 0x30, // Report Count (48) - 0x09, 0x01, // Usage (Pointer) - 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0xC0, // End Collection - 0xA1, 0x02, // Collection (Application) - 0x85, 0xEE, // Report ID (238) - 0x75, 0x08, // Report Size (8) - 0x95, 0x30, // Report Count (48) - 0x09, 0x01, // Usage (Pointer) - 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0xC0, // End Collection - 0xA1, 0x02, // Collection (Application) - 0x85, 0xEF, // Report ID (239) - 0x75, 0x08, // Report Size (8) - 0x95, 0x30, // Report Count (48) - 0x09, 0x01, // Usage (Pointer) - 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0xC0, // End Collection - 0xC0, // End Collection +pub const DESCRIPTORS: [u8; 80] = [ + 0x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, + 0x94, 0x00, 0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, 0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, + 0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, + 0x94, 0x00, 0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, 0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; -const F2_REPORT: [u8; 64] = [0xF2, 0xFF, 0xFF, 0x0, 0x0, 0x6, 0xF5, 0x48, 0xE2, 0x49, 0x0, 0x3, 0x50, 0x81, 0xD8, 0x1, -0x8A, 0x13, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x2, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x4, -0x4, 0x4, 0x4, 0x0, 0x0, 0x4, 0x0, 0x1, 0x2, 0x7, 0x0, 0x17, 0x0, 0x0, 0x0, 0x0, -0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x0, + b_device_sub_class: 0x0, + id_vendor: 0x054C, + id_product: 0x0268, + bcd_device: 0x0100, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (PS1 Two Handle mode)", + i_serial_number: "SLPH-00051", +}; -const F5_REPORT: [u8; 64] = [0x1, 0x0, 0x0, 0x23, 0x6, 0x7C, 0xB9, 0xB, 0xE2, 0x49, 0x0, 0x3, 0x50, 0x81, 0xD8, 0x1, -0x8A, 0x13, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x2, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x4, -0x4, 0x4, 0x4, 0x0, 0x0, 0x4, 0x0, 0x1, 0x2, 0x7, 0x0, 0x17, 0x0, 0x0, 0x0, 0x0, -0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]; +pub const HID_REPORT_DESCRIPTOR: [u8; 148] = [ + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x09, 0x04, // Usage (Joystick) + 0xA1, 0x01, // Collection (Physical) + 0xA1, 0x02, // Collection (Application) + 0x85, 0x01, // Report ID (1) + 0x75, 0x08, // Report Size (8) + 0x95, 0x01, // Report Count (1) + 0x15, 0x00, // Logical Minimum (0) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: reserved byte + 0x75, 0x01, // Report Size (1) + 0x95, 0x13, // Report Count (19) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x35, 0x00, // Physical Minimum (0) + 0x45, 0x01, // Physical Maximum (1) + 0x05, 0x09, // Usage Page (Button) + 0x19, 0x01, // Usage Minimum (0x01) + 0x29, 0x13, // Usage Maximum (0x13) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x75, 0x01, // Report Size (1) + 0x95, 0x0D, // Report Count (13) + 0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00) + 0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: 32 bit integer, where 0:18 are buttons and 19:31 are reserved + 0x15, 0x00, // Logical Minimum (0) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x09, 0x01, // Usage (Pointer) + 0xA1, 0x00, // Collection (Undefined) + 0x75, 0x08, // Report Size (8) + 0x95, 0x04, // Report Count (4) + 0x35, 0x00, // Physical Minimum (0) + 0x46, 0xFF, 0x00, // Physical Maximum (255) + 0x09, 0x30, // Usage (X) + 0x09, 0x31, // Usage (Y) + 0x09, 0x32, // Usage (Z) + 0x09, 0x35, // Usage (Rz) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + // NOTE: four joysticks + 0xC0, // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x75, 0x08, // Report Size (8) + 0x95, 0x27, // Report Count (39) + 0x09, 0x01, // Usage (Pointer) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x75, 0x08, // Report Size (8) + 0x95, 0x30, // Report Count (48) + 0x09, 0x01, // Usage (Pointer) + 0x91, + 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0x75, 0x08, // Report Size (8) + 0x95, 0x30, // Report Count (48) + 0x09, 0x01, // Usage (Pointer) + 0xB1, + 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0xC0, // End Collection + 0xA1, 0x02, // Collection (Application) + 0x85, 0x02, // Report ID (2) + 0x75, 0x08, // Report Size (8) + 0x95, 0x30, // Report Count (48) + 0x09, 0x01, // Usage (Pointer) + 0xB1, + 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0xC0, // End Collection + 0xA1, 0x02, // Collection (Application) + 0x85, 0xEE, // Report ID (238) + 0x75, 0x08, // Report Size (8) + 0x95, 0x30, // Report Count (48) + 0x09, 0x01, // Usage (Pointer) + 0xB1, + 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0xC0, // End Collection + 0xA1, 0x02, // Collection (Application) + 0x85, 0xEF, // Report ID (239) + 0x75, 0x08, // Report Size (8) + 0x95, 0x30, // Report Count (48) + 0x09, 0x01, // Usage (Pointer) + 0xB1, + 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0xC0, // End Collection + 0xC0, // End Collection +]; + +const F2_REPORT: [u8; 64] = [ + 0xF2, 0xFF, 0xFF, 0x0, 0x0, 0x6, 0xF5, 0x48, 0xE2, 0x49, 0x0, 0x3, 0x50, 0x81, 0xD8, 0x1, 0x8A, + 0x13, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x2, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, 0x0, + 0x0, 0x4, 0x0, 0x1, 0x2, 0x7, 0x0, 0x17, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, +]; + +const F5_REPORT: [u8; 64] = [ + 0x1, 0x0, 0x0, 0x23, 0x6, 0x7C, 0xB9, 0xB, 0xE2, 0x49, 0x0, 0x3, 0x50, 0x81, 0xD8, 0x1, 0x8A, + 0x13, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x2, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, 0x0, + 0x0, 0x4, 0x0, 0x1, 0x2, 0x7, 0x0, 0x17, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, +]; bitflags! { struct Buttons1: u8 { @@ -161,7 +179,7 @@ pub fn update_gadget(state: &mut ControllerState) { } _ => { buttons2.insert(Buttons2::TRIANGLE); - } + } } match state.brake { 0 => { @@ -191,41 +209,154 @@ pub fn update_gadget(state: &mut ControllerState) { 8 => { buttons2.insert(Buttons2::L2 | Buttons2::R1); } - _ => () + _ => (), } - } - else { + } else { // D-pad mode - if state.button_up { buttons1.insert(Buttons1::UP) } - if state.button_down { buttons1.insert(Buttons1::DOWN) } - if state.button_left { buttons1.insert(Buttons1::LEFT) } - if state.button_right { buttons1.insert(Buttons1::RIGHT) } + if state.button_up { + buttons1.insert(Buttons1::UP) + } + if state.button_down { + buttons1.insert(Buttons1::DOWN) + } + if state.button_left { + buttons1.insert(Buttons1::LEFT) + } + if state.button_right { + buttons1.insert(Buttons1::RIGHT) + } } // Calculate data for buttons - if state.button_a { buttons2.insert(Buttons2::SQUARE) } - if state.button_b { buttons2.insert(Buttons2::CROSS) } - if state.button_c { buttons2.insert(Buttons2::CIRCLE) } - if state.button_start { buttons1.insert(Buttons1::START) } - if state.button_select { buttons1.insert(Buttons1::SELECT) } + if state.button_a { + buttons2.insert(Buttons2::SQUARE) + } + if state.button_b { + buttons2.insert(Buttons2::CROSS) + } + if state.button_c { + buttons2.insert(Buttons2::CIRCLE) + } + if state.button_start { + buttons1.insert(Buttons1::START) + } + if state.button_select { + buttons1.insert(Buttons1::SELECT) + } - let btn_up = if buttons1.contains(Buttons1::UP) {0xFF} else {0x0}; - let btn_right = if buttons1.contains(Buttons1::RIGHT) {0xFF} else {0x0}; - let btn_down = if buttons1.contains(Buttons1::DOWN) {0xFF} else {0x0}; - let btn_left = if buttons1.contains(Buttons1::LEFT) {0xFF} else {0x0}; - let btn_l2 = if buttons2.contains(Buttons2::L2) {0xFF} else {0x0}; - let btn_r2 = if buttons2.contains(Buttons2::R2) {0xFF} else {0x0}; - let btn_l1 = if buttons2.contains(Buttons2::L1) {0xFF} else {0x0}; - let btn_r1 = if buttons2.contains(Buttons2::R1) {0xFF} else {0x0}; - let btn_triangle = if buttons2.contains(Buttons2::TRIANGLE) {0xFF} else {0x0}; - let btn_circle = if buttons2.contains(Buttons2::CIRCLE) {0xFF} else {0x0}; - let btn_cross = if buttons2.contains(Buttons2::CROSS) {0xFF} else {0x0}; - let btn_square = if buttons2.contains(Buttons2::SQUARE) {0xFF} else {0x0}; + let btn_up = if buttons1.contains(Buttons1::UP) { + 0xFF + } else { + 0x0 + }; + let btn_right = if buttons1.contains(Buttons1::RIGHT) { + 0xFF + } else { + 0x0 + }; + let btn_down = if buttons1.contains(Buttons1::DOWN) { + 0xFF + } else { + 0x0 + }; + let btn_left = if buttons1.contains(Buttons1::LEFT) { + 0xFF + } else { + 0x0 + }; + let btn_l2 = if buttons2.contains(Buttons2::L2) { + 0xFF + } else { + 0x0 + }; + let btn_r2 = if buttons2.contains(Buttons2::R2) { + 0xFF + } else { + 0x0 + }; + let btn_l1 = if buttons2.contains(Buttons2::L1) { + 0xFF + } else { + 0x0 + }; + let btn_r1 = if buttons2.contains(Buttons2::R1) { + 0xFF + } else { + 0x0 + }; + let btn_triangle = if buttons2.contains(Buttons2::TRIANGLE) { + 0xFF + } else { + 0x0 + }; + let btn_circle = if buttons2.contains(Buttons2::CIRCLE) { + 0xFF + } else { + 0x0 + }; + let btn_cross = if buttons2.contains(Buttons2::CROSS) { + 0xFF + } else { + 0x0 + }; + let btn_square = if buttons2.contains(Buttons2::SQUARE) { + 0xFF + } else { + 0x0 + }; // Assemble data and send it to gadget - let data = [0x1, 0x0, buttons1.bits, buttons2.bits, 0x0, 0x0, 0x80, 0x80, 0x80, 0x80, - 0x0, 0x0, 0x0, 0x0, btn_up, btn_right, btn_down, btn_left, btn_l2, btn_r2, btn_l1, btn_r1, btn_triangle, btn_circle, btn_cross, btn_square, 0x0, 0x0, 0x0, - 0x3, 0xEF, 0x14, 0x0, 0x0, 0x0, 0x0, 0x23, 0x1A, 0x77, 0x1, 0x81, 0x1, 0xFE, 0x1, 0xFE, 0x1, 0xFE, 0x1, 0xFE]; + let data = [ + 0x1, + 0x0, + buttons1.bits, + buttons2.bits, + 0x0, + 0x0, + 0x80, + 0x80, + 0x80, + 0x80, + 0x0, + 0x0, + 0x0, + 0x0, + btn_up, + btn_right, + btn_down, + btn_left, + btn_l2, + btn_r2, + btn_l1, + btn_r1, + btn_triangle, + btn_circle, + btn_cross, + btn_square, + 0x0, + 0x0, + 0x0, + 0x3, + 0xEF, + 0x14, + 0x0, + 0x0, + 0x0, + 0x0, + 0x23, + 0x1A, + 0x77, + 0x1, + 0x81, + 0x1, + 0xFE, + 0x1, + 0xFE, + 0x1, + 0xFE, + 0x1, + 0xFE, + ]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } @@ -237,11 +368,10 @@ pub fn handle_ctrl_transfer(data: &[u8]) { if let Ok(mut file) = File::create(ENDPOINT0) { file.write(&F2_REPORT).unwrap(); } - } - else if data[1] == 1 && data[2] == 0xF5 { + } else if data[1] == 1 && data[2] == 0xF5 { // Init 2 if let Ok(mut file) = File::create(ENDPOINT0) { file.write(&F5_REPORT).unwrap(); } } -} \ No newline at end of file +} diff --git a/src/controller/emulated/sotp031201_p4b2b7.rs b/src/controller/emulated/sotp031201_p4b2b7.rs index 8e54370..56222c3 100644 --- a/src/controller/emulated/sotp031201_p4b2b7.rs +++ b/src/controller/emulated/sotp031201_p4b2b7.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x00, b_device_sub_class: 0x0, id_vendor: 0x0AE4, id_product: 0x0101, bcd_device: 0x0400, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (MTC P4/B2-B7 mode)", i_serial_number: "SOTP-031201"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x00, + b_device_sub_class: 0x0, + id_vendor: 0x0AE4, + id_product: 0x0101, + bcd_device: 0x0400, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (MTC P4/B2-B7 mode)", + i_serial_number: "SOTP-031201", +}; const POWER_NOTCHES: [u8; 6] = [0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0C]; const BRAKE_NOTCHES: [u8; 10] = [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x02, 0x02, 0x01]; @@ -68,16 +79,36 @@ pub fn update_gadget(state: &mut ControllerState) { state.reverser = 0x40; state.combo = true; } - if !state.combo && state.button_a { buttons1.insert(Buttons1::A) } - if state.button_b { buttons1.insert(Buttons1::B) } - if state.button_c { buttons1.insert(Buttons1::C) } - if !state.combo && state.button_d { buttons1.insert(Buttons1::D) } - if !state.combo && state.button_up { buttons2.insert(Buttons2::UP) } - if !state.combo && state.button_down { buttons2.insert(Buttons2::DOWN) } - if !state.combo && state.button_left { buttons2.insert(Buttons2::LEFT) } - if state.button_right { buttons2.insert(Buttons2::RIGHT) } - if state.button_start { buttons2.insert(Buttons2::START) } - if !state.combo && state.button_select_hold { buttons2.insert(Buttons2::SELECT) } + if !state.combo && state.button_a { + buttons1.insert(Buttons1::A) + } + if state.button_b { + buttons1.insert(Buttons1::B) + } + if state.button_c { + buttons1.insert(Buttons1::C) + } + if !state.combo && state.button_d { + buttons1.insert(Buttons1::D) + } + if !state.combo && state.button_up { + buttons2.insert(Buttons2::UP) + } + if !state.combo && state.button_down { + buttons2.insert(Buttons2::DOWN) + } + if !state.combo && state.button_left { + buttons2.insert(Buttons2::LEFT) + } + if state.button_right { + buttons2.insert(Buttons2::RIGHT) + } + if state.button_start { + buttons2.insert(Buttons2::START) + } + if !state.combo && state.button_select_hold { + buttons2.insert(Buttons2::SELECT) + } // Assemble data and send it to endpoint let data = [0x1, state.reverser + handle, buttons1.bits, buttons2.bits]; diff --git a/src/controller/emulated/sotp031201_p4b7.rs b/src/controller/emulated/sotp031201_p4b7.rs index 2d6427b..e13623c 100644 --- a/src/controller/emulated/sotp031201_p4b7.rs +++ b/src/controller/emulated/sotp031201_p4b7.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x00, b_device_sub_class: 0x0, id_vendor: 0x0AE4, id_product: 0x0101, bcd_device: 0x0300, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (MTC P4/B7 mode)", i_serial_number: "SOTP-031201"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x00, + b_device_sub_class: 0x0, + id_vendor: 0x0AE4, + id_product: 0x0101, + bcd_device: 0x0300, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (MTC P4/B7 mode)", + i_serial_number: "SOTP-031201", +}; const POWER_NOTCHES: [u8; 6] = [0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0D]; const BRAKE_NOTCHES: [u8; 10] = [0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x02, 0x01]; @@ -68,20 +79,40 @@ pub fn update_gadget(state: &mut ControllerState) { state.reverser = 0x40; state.combo = true; } - if !state.combo && state.button_a { buttons1.insert(Buttons1::A) } - if state.button_b { buttons1.insert(Buttons1::B) } - if state.button_c { buttons1.insert(Buttons1::C) } - if !state.combo && state.button_d { buttons1.insert(Buttons1::D) } - if !state.combo && state.button_up { buttons2.insert(Buttons2::UP) } - if !state.combo && state.button_down { buttons2.insert(Buttons2::DOWN) } - if !state.combo && state.button_left { buttons2.insert(Buttons2::LEFT) } - if state.button_right { buttons2.insert(Buttons2::RIGHT) } - if state.button_start { buttons2.insert(Buttons2::START) } - if !state.combo && state.button_select_hold { buttons2.insert(Buttons2::SELECT) } + if !state.combo && state.button_a { + buttons1.insert(Buttons1::A) + } + if state.button_b { + buttons1.insert(Buttons1::B) + } + if state.button_c { + buttons1.insert(Buttons1::C) + } + if !state.combo && state.button_d { + buttons1.insert(Buttons1::D) + } + if !state.combo && state.button_up { + buttons2.insert(Buttons2::UP) + } + if !state.combo && state.button_down { + buttons2.insert(Buttons2::DOWN) + } + if !state.combo && state.button_left { + buttons2.insert(Buttons2::LEFT) + } + if state.button_right { + buttons2.insert(Buttons2::RIGHT) + } + if state.button_start { + buttons2.insert(Buttons2::START) + } + if !state.combo && state.button_select_hold { + buttons2.insert(Buttons2::SELECT) + } // Assemble data and send it to endpoint let data = [0x1, state.reverser + handle, buttons1.bits, buttons2.bits]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/emulated/sotp031201_p5b5.rs b/src/controller/emulated/sotp031201_p5b5.rs index ff30f65..344a2a5 100644 --- a/src/controller/emulated/sotp031201_p5b5.rs +++ b/src/controller/emulated/sotp031201_p5b5.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x00, b_device_sub_class: 0x0, id_vendor: 0x1C06, id_product: 0x77A7, bcd_device: 0x0202, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (MTC P5/B5 mode)", i_serial_number: "SOTP-031201"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x00, + b_device_sub_class: 0x0, + id_vendor: 0x1C06, + id_product: 0x77A7, + bcd_device: 0x0202, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (MTC P5/B5 mode)", + i_serial_number: "SOTP-031201", +}; const POWER_NOTCHES: [u8; 6] = [0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C]; const BRAKE_NOTCHES: [u8; 10] = [0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01]; @@ -68,20 +79,40 @@ pub fn update_gadget(state: &mut ControllerState) { state.reverser = 0x10; state.combo = true; } - if !state.combo && state.button_a { buttons1.insert(Buttons1::A) } - if state.button_b { buttons1.insert(Buttons1::B) } - if state.button_c { buttons1.insert(Buttons1::C) } - if !state.combo && state.button_d { buttons1.insert(Buttons1::D) } - if !state.combo && state.button_up { buttons2.insert(Buttons2::UP) } - if !state.combo && state.button_down { buttons2.insert(Buttons2::DOWN) } - if !state.combo && state.button_left { buttons2.insert(Buttons2::LEFT) } - if state.button_right { buttons2.insert(Buttons2::RIGHT) } - if state.button_start { buttons2.insert(Buttons2::START) } - if !state.combo && state.button_select_hold { buttons2.insert(Buttons2::SELECT) } + if !state.combo && state.button_a { + buttons1.insert(Buttons1::A) + } + if state.button_b { + buttons1.insert(Buttons1::B) + } + if state.button_c { + buttons1.insert(Buttons1::C) + } + if !state.combo && state.button_d { + buttons1.insert(Buttons1::D) + } + if !state.combo && state.button_up { + buttons2.insert(Buttons2::UP) + } + if !state.combo && state.button_down { + buttons2.insert(Buttons2::DOWN) + } + if !state.combo && state.button_left { + buttons2.insert(Buttons2::LEFT) + } + if state.button_right { + buttons2.insert(Buttons2::RIGHT) + } + if state.button_start { + buttons2.insert(Buttons2::START) + } + if !state.combo && state.button_select_hold { + buttons2.insert(Buttons2::SELECT) + } // Assemble data and send it to endpoint let data = [0x1, state.reverser + handle, buttons1.bits, buttons2.bits]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/emulated/sotp031201_p5b7.rs b/src/controller/emulated/sotp031201_p5b7.rs index 5cb5ee3..9b1f1b7 100644 --- a/src/controller/emulated/sotp031201_p5b7.rs +++ b/src/controller/emulated/sotp031201_p5b7.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x00, b_device_sub_class: 0x0, id_vendor: 0x0AE4, id_product: 0x0101, bcd_device: 0x0800, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (MTC P5/B7 mode)", i_serial_number: "SOTP-031201"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x00, + b_device_sub_class: 0x0, + id_vendor: 0x0AE4, + id_product: 0x0101, + bcd_device: 0x0800, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (MTC P5/B7 mode)", + i_serial_number: "SOTP-031201", +}; const POWER_NOTCHES: [u8; 6] = [0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E]; const BRAKE_NOTCHES: [u8; 10] = [0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x02, 0x01]; @@ -68,20 +79,40 @@ pub fn update_gadget(state: &mut ControllerState) { state.reverser = 0x40; state.combo = true; } - if !state.combo && state.button_a { buttons1.insert(Buttons1::A) } - if state.button_b { buttons1.insert(Buttons1::B) } - if state.button_c { buttons1.insert(Buttons1::C) } - if !state.combo && state.button_d { buttons1.insert(Buttons1::D) } - if !state.combo && state.button_up { buttons2.insert(Buttons2::UP) } - if !state.combo && state.button_down { buttons2.insert(Buttons2::DOWN) } - if !state.combo && state.button_left { buttons2.insert(Buttons2::LEFT) } - if state.button_right { buttons2.insert(Buttons2::RIGHT) } - if state.button_start { buttons2.insert(Buttons2::START) } - if !state.combo && state.button_select_hold { buttons2.insert(Buttons2::SELECT) } + if !state.combo && state.button_a { + buttons1.insert(Buttons1::A) + } + if state.button_b { + buttons1.insert(Buttons1::B) + } + if state.button_c { + buttons1.insert(Buttons1::C) + } + if !state.combo && state.button_d { + buttons1.insert(Buttons1::D) + } + if !state.combo && state.button_up { + buttons2.insert(Buttons2::UP) + } + if !state.combo && state.button_down { + buttons2.insert(Buttons2::DOWN) + } + if !state.combo && state.button_left { + buttons2.insert(Buttons2::LEFT) + } + if state.button_right { + buttons2.insert(Buttons2::RIGHT) + } + if state.button_start { + buttons2.insert(Buttons2::START) + } + if !state.combo && state.button_select_hold { + buttons2.insert(Buttons2::SELECT) + } // Assemble data and send it to endpoint let data = [0x1, state.reverser + handle, buttons1.bits, buttons2.bits]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/emulated/tcpp20009.rs b/src/controller/emulated/tcpp20009.rs index f36217d..cf11812 100644 --- a/src/controller/emulated/tcpp20009.rs +++ b/src/controller/emulated/tcpp20009.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0xFF, b_device_sub_class: 0x4, id_vendor: 0x0AE4, id_product: 0x0004, bcd_device: 0x0102, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (Type 2 mode)", i_serial_number: "TCPP20010"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0xFF, + b_device_sub_class: 0x4, + id_vendor: 0x0AE4, + id_product: 0x0004, + bcd_device: 0x0102, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (Type 2 mode)", + i_serial_number: "TCPP20010", +}; const POWER_NOTCHES: [u8; 6] = [0x81, 0x6D, 0x54, 0x3F, 0x21, 0x00]; const BRAKE_NOTCHES: [u8; 10] = [0x79, 0x8A, 0x94, 0x9A, 0xA2, 0xA8, 0xAF, 0xB2, 0xB5, 0xB9]; @@ -36,23 +47,51 @@ pub fn update_gadget(state: &mut ControllerState) { // Calculate data for buttons let mut buttons = Buttons::NONE; - if state.button_a { buttons.insert(Buttons::A) } - if state.button_b { buttons.insert(Buttons::B) } - if state.button_c { buttons.insert(Buttons::C) } - if state.button_d { buttons.insert(Buttons::D) } - if state.button_select { buttons.insert(Buttons::SELECT) } - if state.button_start { buttons.insert(Buttons::START) } + if state.button_a { + buttons.insert(Buttons::A) + } + if state.button_b { + buttons.insert(Buttons::B) + } + if state.button_c { + buttons.insert(Buttons::C) + } + if state.button_d { + buttons.insert(Buttons::D) + } + if state.button_select { + buttons.insert(Buttons::SELECT) + } + if state.button_start { + buttons.insert(Buttons::START) + } // Calculate data for D-pad let mut dpad: u8 = 0x8; - if state.button_up { dpad = 0x0 } - if state.button_down { dpad = 0x4 } - if state.button_left { dpad = 0x6 } - if state.button_right { dpad = 0x2 } - if state.button_up & state.button_left { dpad = 0x7 } - if state.button_up & state.button_right { dpad = 0x1 } - if state.button_down & state.button_left { dpad = 0x5 } - if state.button_down & state.button_right { dpad = 0x3 } + if state.button_up { + dpad = 0x0 + } + if state.button_down { + dpad = 0x4 + } + if state.button_left { + dpad = 0x6 + } + if state.button_right { + dpad = 0x2 + } + if state.button_up & state.button_left { + dpad = 0x7 + } + if state.button_up & state.button_right { + dpad = 0x1 + } + if state.button_down & state.button_left { + dpad = 0x5 + } + if state.button_down & state.button_right { + dpad = 0x3 + } // Assemble data and send it to endpoint let data = [0x1, brake, power, 0xFF, dpad, buttons.bits]; diff --git a/src/controller/emulated/tcpp20011.rs b/src/controller/emulated/tcpp20011.rs index 5136585..8c3f0da 100644 --- a/src/controller/emulated/tcpp20011.rs +++ b/src/controller/emulated/tcpp20011.rs @@ -1,18 +1,29 @@ use std::fs::File; use std::io::Write; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; -pub const DESCRIPTORS: [u8; 48] = [0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, -0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, -0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 48] = [ + 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, + 0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x14, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0xFF, b_device_sub_class: 0x5, id_vendor: 0x0AE4, id_product: 0x0005, bcd_device: 0x0102, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (Shinkansen mode)", i_serial_number: "TCPP20011"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0xFF, + b_device_sub_class: 0x5, + id_vendor: 0x0AE4, + id_product: 0x0005, + bcd_device: 0x0102, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (Shinkansen mode)", + i_serial_number: "TCPP20011", +}; const POWER_NOTCHES: [u8; 6] = [0x12, 0x36, 0x5A, 0x90, 0xC6, 0xFB]; const BRAKE_NOTCHES: [u8; 10] = [0x1C, 0x38, 0x54, 0x70, 0x8B, 0xA7, 0xC3, 0xDF, 0xDF, 0xFB]; @@ -36,27 +47,55 @@ pub fn update_gadget(state: &mut ControllerState) { // Calculate data for buttons let mut buttons = Buttons::NONE; - if state.button_a { buttons.insert(Buttons::A) } - if state.button_b { buttons.insert(Buttons::B) } - if state.button_c { buttons.insert(Buttons::C) } - if state.button_d { buttons.insert(Buttons::D) } - if state.button_select { buttons.insert(Buttons::SELECT) } - if state.button_start { buttons.insert(Buttons::START) } + if state.button_a { + buttons.insert(Buttons::A) + } + if state.button_b { + buttons.insert(Buttons::B) + } + if state.button_c { + buttons.insert(Buttons::C) + } + if state.button_d { + buttons.insert(Buttons::D) + } + if state.button_select { + buttons.insert(Buttons::SELECT) + } + if state.button_start { + buttons.insert(Buttons::START) + } // Calculate data for D-pad let mut dpad: u8 = 0x8; - if state.button_up { dpad = 0x0 } - if state.button_down { dpad = 0x4 } - if state.button_left { dpad = 0x6 } - if state.button_right { dpad = 0x2 } - if state.button_up & state.button_left { dpad = 0x7 } - if state.button_up & state.button_right { dpad = 0x1 } - if state.button_down & state.button_left { dpad = 0x5 } - if state.button_down & state.button_right { dpad = 0x3 } + if state.button_up { + dpad = 0x0 + } + if state.button_down { + dpad = 0x4 + } + if state.button_left { + dpad = 0x6 + } + if state.button_right { + dpad = 0x2 + } + if state.button_up & state.button_left { + dpad = 0x7 + } + if state.button_up & state.button_right { + dpad = 0x1 + } + if state.button_down & state.button_left { + dpad = 0x5 + } + if state.button_down & state.button_right { + dpad = 0x3 + } // Assemble data and send it to endpoint let data = [brake, power, 0xFF, dpad, buttons.bits, 0x0]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/emulated/zkns001.rs b/src/controller/emulated/zkns001.rs index 211b7fd..627f5eb 100644 --- a/src/controller/emulated/zkns001.rs +++ b/src/controller/emulated/zkns001.rs @@ -1,68 +1,79 @@ -use std::fs::File; -use std::io::{Write}; -use bitflags::bitflags; -use crate::controller::physical::ControllerState; use crate::controller::emulated::{DeviceDescriptor, ENDPOINT1}; +use crate::controller::physical::ControllerState; +use bitflags::bitflags; +use std::fs::File; +use std::io::Write; -pub const DESCRIPTORS: [u8; 80] = [0x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, -0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x5E, 0x00, -0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, -0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, -0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, -0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, 0x5E, 0x00, -0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, -0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05]; -pub const STRINGS: [u8; 16] = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; +pub const DESCRIPTORS: [u8; 80] = [ + 0x01, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, + 0x5E, 0x00, 0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, 0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, + 0x09, 0x04, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x09, 0x21, 0x11, 0x01, 0x00, 0x01, 0x22, + 0x5E, 0x00, 0x07, 0x05, 0x02, 0x03, 0x40, 0x00, 0x05, 0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x05, +]; +pub const STRINGS: [u8; 16] = [ + 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +]; -pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor{b_device_class: 0x0, b_device_sub_class: 0x0, id_vendor: 0x33DD, id_product: 0x0001, bcd_device: 0x0106, i_manufacturer: "TAITO", i_product: "Densha de Go! Plug & Play (NS One Handle mode)", i_serial_number: "ZKNS-001"}; +pub const DEVICE_DESCRIPTOR: DeviceDescriptor = DeviceDescriptor { + b_device_class: 0x0, + b_device_sub_class: 0x0, + id_vendor: 0x33DD, + id_product: 0x0001, + bcd_device: 0x0106, + i_manufacturer: "TAITO", + i_product: "Densha de Go! Plug & Play (NS One Handle mode)", + i_serial_number: "ZKNS-001", +}; pub const HID_REPORT_DESCRIPTOR: [u8; 94] = [ - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x09, 0x05, // Usage (Game Pad) - 0xA1, 0x01, // Collection (Application) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x35, 0x00, // Physical Minimum (0) - 0x45, 0x01, // Physical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x0E, // Report Count (14) - 0x05, 0x09, // Usage Page (Button) - 0x19, 0x01, // Usage Minimum (0x01) - 0x29, 0x0E, // Usage Maximum (0x0E) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x95, 0x02, // Report Count (2) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) - 0x25, 0x07, // Logical Maximum (7) - 0x46, 0x3B, 0x01, // Physical Maximum (315) - 0x75, 0x04, // Report Size (4) - 0x95, 0x01, // Report Count (1) - 0x65, 0x14, // Unit (System: English Rotation, Length: Centimeter) - 0x09, 0x39, // Usage (Hat switch) - 0x81, 0x42, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,Null State) - 0x65, 0x00, // Unit (None) - 0x95, 0x01, // Report Count (1) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x46, 0xFF, 0x00, // Physical Maximum (255) - 0x09, 0x30, // Usage (X) - 0x09, 0x31, // Usage (Y) - 0x09, 0x32, // Usage (Z) - 0x09, 0x35, // Usage (Rz) - 0x75, 0x08, // Report Size (8) - 0x95, 0x04, // Report Count (4) - 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x75, 0x08, // Report Size (8) - 0x95, 0x01, // Report Count (1) - 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - 0x0A, 0x4F, 0x48, // Usage (0x484F) - 0x75, 0x08, // Report Size (8) - 0x95, 0x08, // Report Count (8) - 0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0x0A, 0x4F, 0x48, // Usage (0x484F) - 0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - 0xC0, // End Collection + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x09, 0x05, // Usage (Game Pad) + 0xA1, 0x01, // Collection (Application) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x35, 0x00, // Physical Minimum (0) + 0x45, 0x01, // Physical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x0E, // Report Count (14) + 0x05, 0x09, // Usage Page (Button) + 0x19, 0x01, // Usage Minimum (0x01) + 0x29, 0x0E, // Usage Maximum (0x0E) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x95, 0x02, // Report Count (2) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) + 0x25, 0x07, // Logical Maximum (7) + 0x46, 0x3B, 0x01, // Physical Maximum (315) + 0x75, 0x04, // Report Size (4) + 0x95, 0x01, // Report Count (1) + 0x65, 0x14, // Unit (System: English Rotation, Length: Centimeter) + 0x09, 0x39, // Usage (Hat switch) + 0x81, 0x42, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,Null State) + 0x65, 0x00, // Unit (None) + 0x95, 0x01, // Report Count (1) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x26, 0xFF, 0x00, // Logical Maximum (255) + 0x46, 0xFF, 0x00, // Physical Maximum (255) + 0x09, 0x30, // Usage (X) + 0x09, 0x31, // Usage (Y) + 0x09, 0x32, // Usage (Z) + 0x09, 0x35, // Usage (Rz) + 0x75, 0x08, // Report Size (8) + 0x95, 0x04, // Report Count (4) + 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x75, 0x08, // Report Size (8) + 0x95, 0x01, // Report Count (1) + 0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + 0x0A, 0x4F, 0x48, // Usage (0x484F) + 0x75, 0x08, // Report Size (8) + 0x95, 0x08, // Report Count (8) + 0xB1, + 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0x0A, 0x4F, 0x48, // Usage (0x484F) + 0x91, + 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + 0xC0, // End Collection ]; const POWER_NOTCHES: [u8; 6] = [0x80, 0x9F, 0xB7, 0xCE, 0xE6, 0xFF]; @@ -109,28 +120,67 @@ pub fn update_gadget(state: &mut ControllerState) { buttons2.insert(Buttons2::HOME); state.combo = true; } - if state.button_a { buttons1.insert(Buttons1::Y) } - if state.button_b { buttons1.insert(Buttons1::B) } - if state.button_c { buttons1.insert(Buttons1::A) } - if state.button_d { buttons1.insert(Buttons1::X) } - if state.brake == 9 { buttons1.insert(Buttons1::ZL) } - if !state.combo && state.button_start { buttons2.insert(Buttons2::START) } - if !state.combo && state.button_select_hold { buttons2.insert(Buttons2::SELECT) } + if state.button_a { + buttons1.insert(Buttons1::Y) + } + if state.button_b { + buttons1.insert(Buttons1::B) + } + if state.button_c { + buttons1.insert(Buttons1::A) + } + if state.button_d { + buttons1.insert(Buttons1::X) + } + if state.brake == 9 { + buttons1.insert(Buttons1::ZL) + } + if !state.combo && state.button_start { + buttons2.insert(Buttons2::START) + } + if !state.combo && state.button_select_hold { + buttons2.insert(Buttons2::SELECT) + } // Calculate data for D-pad let mut dpad: u8 = 0xF; - if state.button_up { dpad = 0x0 } - if state.button_down { dpad = 0x4 } - if !state.combo && state.button_left { dpad = 0x6 } - if !state.combo && state.button_right { dpad = 0x2 } - if !state.combo && state.button_up & state.button_left { dpad = 0x7 } - if !state.combo && state.button_up & state.button_right { dpad = 0x1 } - if !state.combo && state.button_down & state.button_left { dpad = 0x5 } - if !state.combo && state.button_down & state.button_right { dpad = 0x3 } + if state.button_up { + dpad = 0x0 + } + if state.button_down { + dpad = 0x4 + } + if !state.combo && state.button_left { + dpad = 0x6 + } + if !state.combo && state.button_right { + dpad = 0x2 + } + if !state.combo && state.button_up & state.button_left { + dpad = 0x7 + } + if !state.combo && state.button_up & state.button_right { + dpad = 0x1 + } + if !state.combo && state.button_down & state.button_left { + dpad = 0x5 + } + if !state.combo && state.button_down & state.button_right { + dpad = 0x3 + } // Assemble data and send it to gadget - let data = [buttons1.bits, buttons2.bits, dpad, 0x80, handle, 0x80, 0x80, 0x00]; + let data = [ + buttons1.bits, + buttons2.bits, + dpad, + 0x80, + handle, + 0x80, + 0x80, + 0x00, + ]; if let Ok(mut file) = File::create(ENDPOINT1) { file.write(&data).ok(); } -} \ No newline at end of file +} diff --git a/src/controller/physical.rs b/src/controller/physical.rs index c995b9f..4e0fb19 100644 --- a/src/controller/physical.rs +++ b/src/controller/physical.rs @@ -1,7 +1,7 @@ use std::fs::File; -use std::io::Write; use std::io::Result; -use std::time::{Instant,Duration}; +use std::io::Write; +use std::time::{Duration, Instant}; use evdev::Device; use evdev::Key; @@ -30,9 +30,34 @@ pub struct ControllerState { const HOLD_DELAY: Duration = Duration::from_millis(750); -const USED_KEYS: [Key; 26] = [Key::KEY_0, Key::KEY_1, Key::KEY_2, Key::KEY_3, Key::KEY_4, Key::KEY_5, - Key::KEY_B, Key::KEY_C, Key::KEY_D, Key::KEY_E, Key::KEY_F, Key::KEY_G, Key::KEY_H, Key::KEY_I, Key::KEY_J, Key::KEY_P, - 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]; +const USED_KEYS: [Key; 26] = [ + Key::KEY_0, + Key::KEY_1, + Key::KEY_2, + Key::KEY_3, + Key::KEY_4, + Key::KEY_5, + Key::KEY_B, + Key::KEY_C, + Key::KEY_D, + Key::KEY_E, + Key::KEY_F, + Key::KEY_G, + Key::KEY_H, + Key::KEY_I, + Key::KEY_J, + Key::KEY_P, + 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/event1")?; @@ -55,27 +80,90 @@ pub fn get_state(state: &mut ControllerState, dev: &[Device; 2]) { fn read_input(controller: &mut ControllerState, key: Key, value: bool) { // Save input status to object for processing match key { - Key::KEY_0=>if value {controller.power = 0}, - Key::KEY_1=>if value {controller.power = 1}, - Key::KEY_2=>if value {controller.power = 2}, - Key::KEY_3=>if value {controller.power = 3}, - Key::KEY_4=>if value {controller.power = 4}, - Key::KEY_5=>if value {controller.power = 5}, - Key::KEY_B=>if value {controller.brake = 0}, - Key::KEY_C=>if value {controller.brake = 1}, - Key::KEY_D=>if value {controller.brake = 2}, - Key::KEY_E=>if value {controller.brake = 3}, - Key::KEY_F=>if value {controller.brake = 4}, - Key::KEY_G=>if value {controller.brake = 5}, - Key::KEY_H=>if value {controller.brake = 6}, - Key::KEY_I=>if value {controller.brake = 7}, - Key::KEY_J=>if value {controller.brake = 8}, - Key::KEY_P=>if value {controller.brake = 9}, - Key::KEY_SPACE=> { + Key::KEY_0 => { + if value { + controller.power = 0 + } + } + Key::KEY_1 => { + if value { + controller.power = 1 + } + } + Key::KEY_2 => { + if value { + controller.power = 2 + } + } + Key::KEY_3 => { + if value { + controller.power = 3 + } + } + Key::KEY_4 => { + if value { + controller.power = 4 + } + } + Key::KEY_5 => { + if value { + controller.power = 5 + } + } + Key::KEY_B => { + if value { + controller.brake = 0 + } + } + Key::KEY_C => { + if value { + controller.brake = 1 + } + } + Key::KEY_D => { + if value { + controller.brake = 2 + } + } + Key::KEY_E => { + if value { + controller.brake = 3 + } + } + Key::KEY_F => { + if value { + controller.brake = 4 + } + } + Key::KEY_G => { + if value { + controller.brake = 5 + } + } + Key::KEY_H => { + if value { + controller.brake = 6 + } + } + Key::KEY_I => { + if value { + controller.brake = 7 + } + } + Key::KEY_J => { + if value { + controller.brake = 8 + } + } + Key::KEY_P => { + if value { + controller.brake = 9 + } + } + Key::KEY_SPACE => { if !controller.button_select && value { controller.button_select_time = Some(Instant::now()); - } - else if !value { + } else if !value { controller.button_select_time = None; controller.combo = false; } @@ -84,23 +172,23 @@ fn read_input(controller: &mut ControllerState, key: Key, value: bool) { if let Some(time) = controller.button_select_time { controller.button_select_hold = time.elapsed() > HOLD_DELAY && !controller.combo; } - }, - Key::KEY_ENTER=>controller.button_start = value, - Key::KEY_A=>controller.button_a = value, - Key::KEY_Z=>controller.button_b = value, - Key::KEY_X=>controller.button_c = value, - Key::KEY_S=>controller.button_d = value, - Key::KEY_UP=>controller.button_up = value, - Key::KEY_DOWN=>controller.button_down = value, - Key::KEY_LEFT=>controller.button_left = value, - Key::KEY_RIGHT=>controller.button_right = value, - _=>(), + } + Key::KEY_ENTER => controller.button_start = value, + Key::KEY_A => controller.button_a = value, + Key::KEY_Z => controller.button_b = value, + Key::KEY_X => controller.button_c = value, + Key::KEY_S => controller.button_d = value, + Key::KEY_UP => controller.button_up = value, + Key::KEY_DOWN => controller.button_down = value, + Key::KEY_LEFT => controller.button_left = value, + Key::KEY_RIGHT => controller.button_right = value, + _ => (), } } pub fn set_lamp(status: bool) { if let Ok(mut out) = File::create("/sys/class/leds/led2/brightness") { - out.write(if status {b"1"} else {b"0"}).ok(); + out.write(if status { b"1" } else { b"0" }).ok(); } /*else { println!("WARNING: Could not set door lamp status!") @@ -109,7 +197,7 @@ pub fn set_lamp(status: bool) { pub fn set_rumble(status: bool) { if let Ok(mut out) = File::create("/sys/class/leds/led1/brightness") { - out.write(if status {b"1"} else {b"0"}).ok(); + out.write(if status { b"1" } else { b"0" }).ok(); } /*else { println!("WARNING: Could not set rumble motor status!") diff --git a/src/main.rs b/src/main.rs index e3419dd..e07bbb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,10 @@ mod controller; use std::io::Result; use std::process::Command; -use std::time::Duration; use std::thread::sleep; +use std::time::Duration; -use controller::physical::{set_lamp,set_rumble}; +use controller::physical::{set_lamp, set_rumble}; fn main() -> Result<()> { match controller::physical::init() { @@ -18,7 +18,6 @@ fn main() -> Result<()> { // Check selected controller model if let Some(controller_model) = controller::emulated::set_model(&controller_state) { - // Stop main game stop_game(); @@ -43,12 +42,15 @@ fn main() -> Result<()> { } } return Result::Ok(()); - }, + } Err(_e) => println!("ddgo-pnp-controller: ERROR: Could not read input devices! Exiting."), } Ok(()) } fn stop_game() { - Command::new("/etc/init.d/S99dgtype3").arg("stop").output().ok(); -} \ No newline at end of file + Command::new("/etc/init.d/S99dgtype3") + .arg("stop") + .output() + .ok(); +}