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

Commit

Permalink
moved write functions for keyboard and ps5 to their files
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosw committed Sep 14, 2023
1 parent 1de3389 commit 1eed4af
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 146 deletions.
78 changes: 24 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(dead_code, unreachable_code)]
#![allow(dead_code)]

#[macro_use]
extern crate version;
Expand All @@ -12,6 +12,7 @@ use std::env;
use std::fs::File;
use std::io::Write;
use std::process::exit;
use std::process::Command;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
Expand All @@ -31,7 +32,6 @@ mod usb_gamepad_ps5;
use crate::bluetooth_fn::*;
use crate::hidapi_fn::{get_hid_gamepad, process_input};
use crate::universal_gamepad::UniversalGamepad;
use crate::usb_gamepad_ps4::DUALSHOCK;
use crate::usb_gamepad_ps5::DUALSENSE;

// if working inside a docker container: (started with the docker-compose from project root)
Expand All @@ -42,68 +42,18 @@ use crate::usb_gamepad_ps5::DUALSENSE;

pub const HID_ARRAY_SIZE: usize = 75;

struct Test {}

impl Test {}

fn main() {
println!("\nGamepad-Bridge started: v{:}", version!());
println!("This program needs to be run as root user. Please set uuid accordingly.\n");

DUALSHOCK.configure_device();
DUALSHOCK.write_output_once(&UniversalGamepad::nothing_pressed());

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

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

exit(0);

// _read_gamepad_input();
}

fn _generate_output_keyboard() {
// Currently simulating keyboard

const REPORT_LENGTH: usize = 8; // see usb_gamepads.rs or Gamepad Info Collection.md
const DURATION_MS: u64 = 4000;

loop {
let mut hidg0 = match File::options().write(true).append(false).open("/dev/hidg0") {
Ok(file) => file,
Err(err) => {
println!("Could not open file hidg0 {err}");
exit(1);
}
};

let out: [u8; REPORT_LENGTH] = [0x11, 0x22, 0x33, 0x44, 0x55, 0xFF, 0xAA, 0x00];

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

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

let out: [u8; REPORT_LENGTH] = [0; REPORT_LENGTH];

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

thread::sleep(Duration::from_millis(150));
}
}

fn _read_gamepad_input() -> ! {
let api = match HidApi::new() {
Ok(api) => api,
Expand Down Expand Up @@ -181,7 +131,27 @@ fn _bt_program_flow() {
}
};

bt_power_on();
{
let output_power_on = match Command::new("bluetoothctl").args(["power", "on"]).output() {
Ok(out) => out,
Err(err) => {
println!("unwrapping the output failed: {:?}", err);
exit(1);
}
};

let stdout = String::from_utf8(output_power_on.stdout).ok();
let stderr = String::from_utf8(output_power_on.stderr).ok();

if !output_power_on.status.success() {
println!("bluetoothctl power on failed:");
println!("{:?}", stderr);
exit(1);
}

println!("Stdout: {:?}", stdout);
println!("Stderr: {:?}", stderr);
};
let (shared_mem_scan_output, thread_handle) = bt_scan_on_threaded();

// find new controllers
Expand Down
6 changes: 3 additions & 3 deletions src/usb_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub struct UsbGadgetDescriptor<'a> {
pub strings_0x409: UsbGadgetStrings<'a>,
pub configs_c1: UsbGadgetConfigs<'a>,
pub functions_hid: UsbGadgetFunctionsHid,
pub write_output_once: fn(&UniversalGamepad),
pub write_output_once: fn(&UniversalGamepad, u8, u8),
}

impl UsbGadgetDescriptor<'_> {
/// Calls the function pointer `write_output_once` (was provided at instantiation)
pub fn write_output_once(&self, input: &UniversalGamepad) {
(self.write_output_once)(input);
pub fn write_output_once(&self, gamepad: &UniversalGamepad, counter: u8, seconds: u8) {
(self.write_output_once)(gamepad, counter, seconds);
}

/// Using linux' ConfigFS, create the given usb device
Expand Down
43 changes: 41 additions & 2 deletions src/usb_gamepad_keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::fs::File;
use std::io::Write;
use std::process::exit;
use std::thread;
use std::time::Duration;

use crate::universal_gamepad::UniversalGamepad;
use crate::usb_gadget::*;
use crate::UsbGadgetDescriptor;
Expand Down Expand Up @@ -63,6 +69,39 @@ pub const GENERIC_KEYBOARD: UsbGadgetDescriptor = UsbGadgetDescriptor {
},
};

fn _write_output_once(input: &UniversalGamepad) {
todo!();
fn _write_output_once(_gamepad: &UniversalGamepad, _counter: u8, _seconds: u8) {
const REPORT_LENGTH: usize = 8;

let mut hidg0 = match File::options().write(true).append(false).open("/dev/hidg0") {
Ok(file) => file,
Err(err) => {
println!("Could not open file hidg0 {err}");
exit(1);
}
};

let out: [u8; REPORT_LENGTH] = [0x11, 0x22, 0x33, 0x44, 0x55, 0xFF, 0xAA, 0x00];

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

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

let out: [u8; REPORT_LENGTH] = [0; REPORT_LENGTH];

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

thread::sleep(Duration::from_millis(150));
}
2 changes: 1 addition & 1 deletion src/usb_gamepad_ps4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,6 @@ pub const DUALSHOCK: UsbGadgetDescriptor = UsbGadgetDescriptor {
},
};

fn _write_output_once(input: &UniversalGamepad) {
fn _write_output_once(_gamepad: &UniversalGamepad, _counter: u8, _seconds: u8) {
todo!();
}
Loading

0 comments on commit 1eed4af

Please sign in to comment.