Skip to content

Commit

Permalink
Minor changes, github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
radu781 committed Jan 8, 2024
1 parent 8616568 commit a2d4947
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 52 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Rust setup
uses: ATiltedTree/setup-rust@v1
with:
rust-version: stable
- name: Install libudev
run: |
sudo apt-get install libudev-dev
- name: Check build errors
run: cargo build
14 changes: 14 additions & 0 deletions .github/workflows/check_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: check format
on: [push]
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Rust setup
uses: ATiltedTree/setup-rust@v1
with:
rust-version: stable
components: rustfmt
- name: Check formatting
run: cargo fmt --check
14 changes: 14 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: run linter
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Rust setup
uses: ATiltedTree/setup-rust@v1
with:
rust-version: stable
components: clippy
- name: Check clippy
run: cargo clippy --no-deps
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dualsense-rs"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
license = "MIT"
description = "Rust programmatic wrapper over HID messages sent and received by the PS5 DualSense controller."
Expand Down
24 changes: 19 additions & 5 deletions examples/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ fn main() {
// controller.on_r2_changed(&|r2| println!("right pad y: {r2}"));

// controller.on_symbols_changed(&|sym| println!("pressed symbol {}", sym as u8));

controller.on_gyro_x_changed(&|val| println!("gyro x: {val}"));
controller.on_gyro_y_changed(&|val| println!("gyro y: {val}"));
controller.on_gyro_z_changed(&|val| println!("gyro z: {val}"));
// controller.on_l1_changed(&|pressed| println!("l1 {pressed}"));
// controller.on_r1_changed(&|pressed| println!("r1 {pressed}"));
// controller.on_l3_changed(&|pressed| println!("l3 {pressed}"));
// controller.on_r3_changed(&|pressed| println!("r3 {pressed}"));
// controller.on_options_changed(&|pressed| println!("options {pressed}"));
// controller.on_share_changed(&|pressed| println!("share {pressed}"));
//
// controller.on_touchpad_changed(&|pressed| println!("touchpad {pressed}"));
// controller.on_mute_changed(&|pressed| println!("mute {pressed}"));
// controller.on_playstation_changed(&|pressed| println!("ps {pressed}"));

// controller.on_gyro_x_changed(&|val| println!("gyro x: {val}"));
// controller.on_gyro_y_changed(&|val| println!("gyro y: {val}"));
// controller.on_gyro_z_changed(&|val| println!("gyro z: {val}"));

// controller.on_accel_x_changed(&|val| println!("accel x: {val}"));
// controller.on_accel_y_changed(&|val| println!("accel y: {val}"));
// controller.on_accel_z_changed(&|val| println!("accel z: {val}"));

controller.on_touchpad1_x_changed(&|val| println!("touchpad 1 x: {val}"));
controller.on_touchpad1_y_changed(&|val| println!("touchpad 1 y: {val}"));
controller.on_touchpad2_x_changed(&|val| println!("touchpad 2 x: {val}"));
controller.on_touchpad2_y_changed(&|val| println!("touchpad 2 y: {val}"));

controller.on_touchpad1_pressed(&|val| println!("touchpad 1 pressed: {val}"));
controller.on_touchpad2_pressed(&|val| println!("touchpad 2 pressed: {val}"));
let handle = controller.run();
Expand Down
2 changes: 1 addition & 1 deletion src/dualsense/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod stream;
pub use stream::*;
pub(crate) mod properties;
pub(crate) mod callback_helpers;
pub(crate) mod properties;
1 change: 1 addition & 0 deletions src/dualsense/properties/dpad.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Directional pad values
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DPad {
Up = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/dualsense/properties/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod offset;
pub(crate) mod dpad;
pub(crate) mod offset;
pub(crate) mod property;
pub(crate) mod symbols;
pub(crate) mod valuetype;
4 changes: 2 additions & 2 deletions src/dualsense/properties/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ impl Property {
| Property::AccelerationY
| Property::AccelerationZ => ValueType::U16((data[1] as u16) << 8 | data[0] as u16),

Property::TouchPadFinger1Active => ValueType::Bool(data[0] & 0x80 == 1),
Property::TouchPadFinger1Active => ValueType::Bool(data[0] & 0x80 == 0x80),
Property::TouchPad1Id => ValueType::U8(data[0] & 0x7F),
Property::TouchPad1X => ValueType::U16(((data[1] as u16 & 0x0F) << 8) | data[0] as u16),
Property::TouchPad1Y => {
ValueType::U16(((data[1] as u16) << 4) | (data[0] as u16 & 0xF0) >> 4)
}
Property::TouchPadFinger2Active => ValueType::Bool(data[0] & 0x80 == 1),
Property::TouchPadFinger2Active => ValueType::Bool(data[0] & 0x80 == 0x80),
Property::TouchPad2Id => ValueType::U8(data[0] & 0x7F),
Property::TouchPad2X => ValueType::U16((data[1] as u16 & 0x0F) << 8 | data[0] as u16),
Property::TouchPad2Y => {
Expand Down
1 change: 1 addition & 0 deletions src/dualsense/properties/symbols.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Symbols values
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Symbols {
Square = 1,
Expand Down
79 changes: 63 additions & 16 deletions src/dualsense/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const VENDOR_ID: u16 = 1356;
const PRODUCT_ID: u16 = 3302;
const PACKET_SIZE: usize = 64;

type CBFunction = Box<dyn Fn(ValueType) + Send>;
/// Main struct used for interacting with the controller
pub struct DualSense {
device: HidDevice,
callbacks: HashMap<Property, Vec<Box<dyn Fn(ValueType) + Send>>>,
callbacks: HashMap<Property, Vec<CBFunction>>,
cache: HashMap<Property, ValueType>,
}

Expand All @@ -31,7 +32,7 @@ impl DualSense {

/// Start listening to HID packets from the controller
pub fn run(mut self) -> JoinHandle<()> {
thread::spawn(move || loop {
thread::spawn(move || loop {
let mut buf = [0u8; PACKET_SIZE];
let bytes_read = self.device.read(&mut buf);
match bytes_read {
Expand All @@ -51,6 +52,11 @@ impl DualSense {
})
}

#[allow(dead_code)]
fn write(&mut self, _data: u8) {
// self.device.write(data);
}

/// Provide a callback to be called when the left stick's x coordinate changes
/// left: 0x00, right: 0xFF
pub fn on_left_pad_x_changed<F>(&mut self, cb: &'static F)
Expand Down Expand Up @@ -134,19 +140,19 @@ impl DualSense {
}

/// Provide a callback to be called when the options button is pressed
pub fn on_options_changed<F>(&mut self, cb: &'static F)
pub fn on_share_changed<F>(&mut self, cb: &'static F)
where
F: Fn(bool) + Send + Sync,
{
self.register_bool(Property::Options, cb);
self.register_bool(Property::Share, cb);
}

/// Provide a callback to be called when the options button is pressed
pub fn on_share_changed<F>(&mut self, cb: &'static F)
pub fn on_options_changed<F>(&mut self, cb: &'static F)
where
F: Fn(bool) + Send + Sync,
{
self.register_bool(Property::Share, cb);
self.register_bool(Property::Options, cb);
}

/// Provide a callback to be called when any dpad button is pressed
Expand All @@ -165,6 +171,30 @@ impl DualSense {
self.register_symbols(Property::Symbols, cb);
}

/// Provide a callback to be called when the mute button is pressed
pub fn on_mute_changed<F>(&mut self, cb: &'static F)
where
F: Fn(bool) + Send + Sync,
{
self.register_bool(Property::Mute, cb);
}

/// Provide a callback to be called when the touchpad is pressed
pub fn on_touchpad_changed<F>(&mut self, cb: &'static F)
where
F: Fn(bool) + Send + Sync,
{
self.register_bool(Property::TouchPad, cb);
}

/// Provide a callback to be called when the playstation button is pressed
pub fn on_playstation_pressed<F>(&mut self, cb: &'static F)
where
F: Fn(bool) + Send + Sync,
{
self.register_bool(Property::PlayStation, cb);
}

/// Provide a callback to be called when the gyroscope X axis is changed
pub fn on_gyro_x_changed<F>(&mut self, cb: &'static F)
where
Expand Down Expand Up @@ -212,7 +242,7 @@ impl DualSense {
{
self.register_u16(Property::AccelerationZ, cb);
}

/// Provide a callback to be called when the touchpad is touched
pub fn on_touchpad1_pressed<F>(&mut self, cb: &'static F)
where
Expand Down Expand Up @@ -251,7 +281,7 @@ impl DualSense {
{
self.register_u16(Property::TouchPad1X, cb);
}

/// Provide a callback to be called when the touchpad input from the first finger
/// on the Y axis is changed
pub fn on_touchpad1_y_changed<F>(&mut self, cb: &'static F)
Expand All @@ -260,7 +290,7 @@ impl DualSense {
{
self.register_u16(Property::TouchPad1Y, cb);
}

/// Provide a callback to be called when the touchpad input from the second finger
/// on the X axis is changed
pub fn on_touchpad2_x_changed<F>(&mut self, cb: &'static F)
Expand All @@ -269,7 +299,7 @@ impl DualSense {
{
self.register_u16(Property::TouchPad2X, cb);
}

/// Provide a callback to be called when the touchpad input from the second finger
/// on the Y axis is changed
pub fn on_touchpad2_y_changed<F>(&mut self, cb: &'static F)
Expand All @@ -285,7 +315,7 @@ impl DualSense {
{
self.callbacks
.entry(prop)
.or_insert_with(|| vec![])
.or_default()
.push(Box::new(move |x| cb(x.to_u8())));
}

Expand All @@ -295,7 +325,7 @@ impl DualSense {
{
self.callbacks
.entry(prop)
.or_insert_with(|| vec![])
.or_default()
.push(Box::new(move |x| cb(x.to_u16())));
}

Expand All @@ -305,7 +335,7 @@ impl DualSense {
{
self.callbacks
.entry(prop)
.or_insert_with(|| vec![])
.or_default()
.push(Box::new(move |x| cb(x.to_dpad())));
}

Expand All @@ -315,7 +345,7 @@ impl DualSense {
{
self.callbacks
.entry(prop)
.or_insert_with(|| vec![])
.or_default()
.push(Box::new(move |x| cb(x.to_symbol())));
}

Expand All @@ -325,7 +355,7 @@ impl DualSense {
{
self.callbacks
.entry(prop)
.or_insert_with(|| vec![])
.or_default()
.push(Box::new(move |x| cb(x.to_bool())));
}

Expand All @@ -350,6 +380,17 @@ impl DualSense {
})
}

#[allow(dead_code)]
fn debug_print_packet(data: &[u8; PACKET_SIZE]) {
data.chunks(8).for_each(|w| {
for b in w {
print!("{:#04x} ", b)
}
println!();
});
println!()
}

fn extract_bytes(prop: &Property, data: &[u8; 64]) -> ValueType {
if prop.offset().bits == (0..8) {
prop.convert(&data.as_slice()[prop.offset().bytes])
Expand All @@ -361,11 +402,17 @@ impl DualSense {
for i in prop.offset().bits {
let offset = i - prop.offset().bits.start;
let current_bit = (val & (1 << i)) >> i;
out = out | (current_bit << offset);
out |= current_bit << offset;
}
prop.convert(&[out])
} else {
todo!()
}
}
}

impl Default for DualSense {
fn default() -> Self {
DualSense::new()
}
}
Loading

0 comments on commit a2d4947

Please sign in to comment.