Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Joystick and Trigger movements successful in steam on win10
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosw committed Sep 15, 2023
1 parent 1eed4af commit c7d5520
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gamepad-bridge"
version = "0.5.2-0"
version = "0.5.2-7"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Binary file modified aarch64/gamepad-bridge
Binary file not shown.
8 changes: 2 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ extern crate termion;
use ctrlc::set_handler;
use hidapi::HidApi;
use std::env;
use std::fs::File;
use std::io::Write;
use std::process::exit;
use std::process::Command;
use std::sync::atomic::AtomicBool;
Expand Down Expand Up @@ -46,10 +44,8 @@ fn main() {
println!("\nGamepad-Bridge started: v{:}", version!());
println!("This program needs to be run as root user. Please set uuid accordingly.\n");

DUALSENSE.configure_device();
DUALSENSE.write_output_once(&UniversalGamepad::nothing_pressed(), 0, 0);

exit(0);
// DUALSENSE.configure_device();
DUALSENSE.write_continously_testing();

// _read_gamepad_input();
}
Expand Down
46 changes: 46 additions & 0 deletions src/usb_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use std::{
fs::{self, File},
io::Write,
process::exit,
thread,
time::Duration,
};

use crate::{
Expand Down Expand Up @@ -42,6 +44,50 @@ pub struct UsbGadgetDescriptor<'a> {
}

impl UsbGadgetDescriptor<'_> {
/// Moves all triggers and joysticks and presses and releases all buttons
pub fn write_continously_testing(&self) -> ! {
let mut gamepad: UniversalGamepad = UniversalGamepad::nothing_pressed();

const OSCILLATE_UPPER: u8 = 192;
const OSCILLATE_LOWER: u8 = 64;
let mut oscillate: u8 = OSCILLATE_LOWER;
let mut up: bool = true;

// println!("sleeping 10s");
// thread::sleep(Duration::from_secs(10));
println!("lets go");

loop {
// This counts one byte at a time from OSCILLATE_LOWER to OSCILLATE_UPPER and back to OSCILLATE_LOWER

match oscillate {
OSCILLATE_LOWER => up = true,
OSCILLATE_UPPER => up = false,
_ => (),
}

if up && (oscillate < OSCILLATE_UPPER) {
oscillate += 1;
}
if !up && (oscillate > OSCILLATE_LOWER) {
oscillate -= 1;
}

gamepad.sticks.left.x = oscillate;
gamepad.sticks.left.y = oscillate;
gamepad.sticks.right.x = oscillate;
gamepad.sticks.right.y = oscillate;
gamepad.triggers.left = oscillate;
gamepad.triggers.right = oscillate;
println!("{oscillate}");

self.write_output_once(&gamepad, 0, 0);

// TODO achieve a real timed interval
thread::sleep(Duration::from_millis(4));
}
}

/// Calls the function pointer `write_output_once` (was provided at instantiation)
pub fn write_output_once(&self, gamepad: &UniversalGamepad, counter: u8, seconds: u8) {
(self.write_output_once)(gamepad, counter, seconds);
Expand Down
179 changes: 60 additions & 119 deletions src/usb_gamepad_ps5.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::fs::File;
use std::io::Write;
use std::process::exit;
use std::thread;
use std::time::Duration;
use rand::Rng;

use crate::universal_gamepad::UniversalGamepad;
use crate::usb_gadget::*;
Expand Down Expand Up @@ -34,7 +31,7 @@ pub const DUALSENSE: UsbGadgetDescriptor = UsbGadgetDescriptor {
functions_hid: UsbGadgetFunctionsHid {
hid_subclass: 0,
protocol: 0,
report_length: REPORT_LENGTH,
report_length: 64,
report_descriptor: &[
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x05, // Usage (Game Pad)
Expand Down Expand Up @@ -204,7 +201,7 @@ pub const DUALSENSE: UsbGadgetDescriptor = UsbGadgetDescriptor {
],
},
};
fn _write_output_once(gamepad: &UniversalGamepad, counter: u8, seconds: u8) {
fn _write_output_once(gamepad: &UniversalGamepad, _counter: u8, _seconds: u8) {
let mut hidg0 = match File::options().write(true).append(false).open("/dev/hidg0") {
Ok(file) => file,
Err(err) => {
Expand All @@ -221,123 +218,67 @@ fn _write_output_once(gamepad: &UniversalGamepad, counter: u8, seconds: u8) {
gamepad.sticks.right.y,
gamepad.triggers.left,
gamepad.triggers.right,
counter,
0, // Buttons and DPad
0, // Special Buttons, Bumpers, Triggers and Sticks (only WHAT is pressed, for triggers not value)
0, // Logo / Touchpad
0, // always 0
counter, //
seconds, //
0xee, // might be charging state (in %) (unlikely, changes drastically after reconnect)
0xad, // ??
0x00, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0xff, // gyroskop here (seems to be relative, not absolute)
0xff, // gyroskop here (seems to be relative, not absolute)
0x02, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0x06, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0x81, // gyroskop here (seems to be relative, not absolute)
0x1f, // gyroskop here (seems to be relative, not absolute)
0x07, // gyroskop here (seems to be relative, not absolute)
0x06, // gyroskop here (seems to be relative, not absolute)
0x46, // gyroskop here (seems to be relative, not absolute)
0x66, // gyroskop here (seems to be relative, not absolute)
counter, // this is a really slow counter (goes up every ~10s)
0x00, // ??
0x14, // ??
0x80, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x80, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x09, // ??
0x09, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0xe3, // random?
0x79, // random?
0xab, // random?
0x00, // slow counter
0x17, // constant?
0x08, // constant?
0x00, // constant?
0x5b, // random?
0x7f, // random?
0xef, // random?
0x9c, // random?
0xac, // random?
0x03, // random?
0x92, // random?
0x30, // random?
0, // counter
0, // Buttons and DPad
0, // Special Buttons, Bumpers, Triggers and Sticks (only WHAT is pressed, for triggers not value)
0, // Logo / Touchpad
0, // always 0
0, // counter
0, // seconds ?
0xee, // might be charging state (in %) (unlikely, changes drastically after reconnect)
0xad, // ??
0x00, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0xff, // gyroskop here (seems to be relative, not absolute)
0xff, // gyroskop here (seems to be relative, not absolute)
0x02, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0x06, // gyroskop here (seems to be relative, not absolute)
0x00, // gyroskop here (seems to be relative, not absolute)
0x81, // gyroskop here (seems to be relative, not absolute)
0x1f, // gyroskop here (seems to be relative, not absolute)
0x07, // gyroskop here (seems to be relative, not absolute)
0x06, // gyroskop here (seems to be relative, not absolute)
0x46, // gyroskop here (seems to be relative, not absolute)
0x66, // gyroskop here (seems to be relative, not absolute)
0, // this is a really slow counter (goes up every ~10s)
0x00, // ??
0x14, // ??
0x80, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x80, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x09, // ??
0x09, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0x00, // ??
0xe3, // random?
0x79, // random?
0xab, // random?
0x00, // slow counter
0x17, // constant?
0x08, // constant?
0x00, // constant?
0x5b, // random?
0x7f, // random?
0xef, // random?
0x9c, // random?
0xac, // random?
0x03, // random?
0x92, // random?
0x30, // random?
];

match hidg0.write_all(&out) {
Ok(_) => (),
Err(err) => {
println!("write to hidg0 failed: {:?}", err);
}
}
}

// TODO Generalize this function and add to usb_gadget.rs as pub fn
fn ___() {
const REPORT_LENGTH: usize = DUALSENSE.b_max_packet_size0 as usize;
const DURATION_MS: u64 = 4000;

let mut dummy: UniversalGamepad = UniversalGamepad::nothing_pressed();
let mut counter: u8 = 0;
let mut seconds: u8 = 0;

let mut oscillate = 0;
let mut up: bool = true;

println!("sleeping 10s");
thread::sleep(Duration::from_secs(10));
println!("lets go");

loop {
counter += 1;

if counter == 255 {
// This is roughly a second for 4ms interval
// 4ms * 250 would be 1s, but maybe the real value also isnt a second, but counts the overflows?
seconds += 1
}

let mut rng = rand::thread_rng();
let _logo_touchpad_byte: u8 = rng.gen_range(0..=2);

// This counts one byte at a time from 0 to 255 and back to 0

if oscillate == 255 {
up = false;
}
while (oscillate < 255) && up {
oscillate += 1;
}
while (oscillate > 0) && !up {
oscillate -= 1;
}

dummy.sticks.left.x = oscillate;
dummy.sticks.left.y = oscillate;
dummy.sticks.right.x = oscillate;
dummy.sticks.right.y = oscillate;
dummy.triggers.left = oscillate;
dummy.triggers.right = oscillate;

_write_output_once(&dummy, counter, seconds);

// TODO achieve a real timed interval
thread::sleep(Duration::from_millis(4));
Err(err) => println!("write to hidg0 failed: {:?}", err),
}
}

0 comments on commit c7d5520

Please sign in to comment.