Ds4Report

Struct Ds4Report 

Source
#[repr(C)]
pub struct Ds4Report { pub thumb_lx: u8, pub thumb_ly: u8, pub thumb_rx: u8, pub thumb_ry: u8, pub buttons: u16, pub special: u8, pub trigger_l: u8, pub trigger_r: u8, }
Expand description

Represents the standard input state of a virtual DualShock 4 controller.

An instance of this struct is sent to the bus via TargetHandle::update to update the controller’s state.

§Examples

use vigem_rust::controller::ds4::{Ds4Report, Ds4Button, Ds4Dpad};

let mut report = Ds4Report::default();

// Set analog stick positions (128 is center)
report.thumb_lx = 200; // Move right
report.thumb_ly = 50;  // Move up

// Press the Cross button
report.buttons = Ds4Button::CROSS.bits();

// Set the D-Pad to South
report.set_dpad(Ds4Dpad::South);

// Pull the right trigger
report.trigger_r = 255;

Fields§

§thumb_lx: u8

Left thumbstick X-axis (0-255). 128 is center.

§thumb_ly: u8

Left thumbstick Y-axis (0-255). 128 is center.

§thumb_rx: u8

Right thumbstick X-axis (0-255). 128 is center.

§thumb_ry: u8

Right thumbstick Y-axis (0-255). 128 is center.

§buttons: u16

A bitmask of the main digital buttons and D-Pad state. See Ds4Button and set_dpad.

§special: u8

A bitmask of the special buttons. See Ds4SpecialButton.

§trigger_l: u8

Left trigger value (0-255).

§trigger_r: u8

Right trigger value (0-255).

Implementations§

Source§

impl Ds4Report

Source

pub fn set_dpad(&mut self, dpad: Ds4Dpad)

Sets the D-Pad state on the report.

This helper correctly manipulates the lower 4 bits of the buttons field to set the D-Pad state, leaving the other button flags untouched.

§Examples
use vigem_rust::controller::ds4::{Ds4Report, Ds4Button, Ds4Dpad};

let mut report = Ds4Report::default();
report.buttons = Ds4Button::SQUARE.bits();
report.set_dpad(Ds4Dpad::East);

// The Square button is still set
assert!(report.buttons & Ds4Button::SQUARE.bits() != 0);
// The D-Pad bits are correctly set
assert_eq!(report.buttons & 0x000F, Ds4Dpad::East as u16);
Examples found in repository?
examples/ds4.rs (line 42)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // Connect to the ViGEm bus
7    // This can fail if the ViGEm bus driver is not installed.
8    let client = Client::connect()?;
9    println!("Connected to ViGEm bus");
10
11    // Create and plugin the virtual controller
12    let ds4 = client.new_ds4_target().plugin()?;
13    println!("Plugged in virtual DualShock 4 controller");
14
15    // Wait for the controller to be ready
16    ds4.wait_for_ready()?;
17    println!("Controller is ready. You can test it at https://hardwaretester.com/gamepad");
18
19    // Set up a notification listener in a separate thread
20    // This allows us to react to feedback from the system, like rumble or LED changes.
21    let notifications = ds4.register_notification()?;
22    thread::spawn(move || {
23        println!("Notification Thread Started. Waiting for feedback from the host...");
24        while let Ok(Ok(notification)) = notifications.recv() {
25            println!("Notification Thread Received feedback:");
26            println!(
27                "  - Rumble: Large Motor = {}, Small Motor = {}",
28                notification.large_motor, notification.small_motor
29            );
30            println!(
31                "  - Lightbar Color: R={}, G={}, B={}",
32                notification.lightbar.red, notification.lightbar.green, notification.lightbar.blue
33            );
34        }
35    });
36
37    // Here, we'll send reports to the controller to simulate input.
38
39    let mut report = Ds4Report::default();
40
41    // Hold the right D-pad button
42    report.set_dpad(Ds4Dpad::East);
43    // Hold the Cross button
44    report.buttons |= Ds4Button::CROSS.bits();
45    // Fully press the right trigger
46    report.trigger_r = 255;
47
48    let mut angle: f64 = 0.0;
49
50    loop {
51        // Animate the right thumbstick in a circle
52        let (sin, cos) = angle.sin_cos();
53
54        // DS4 thumbsticks are 0-255, with 128 as the center.
55        report.thumb_rx = (128.0 + sin * 127.0) as u8;
56        report.thumb_ry = (128.0 + cos * 127.0) as u8;
57
58        // Send the updated report to the controller
59        ds4.update(&report)?;
60
61        thread::sleep(Duration::from_millis(16));
62        angle += 0.05;
63    }
64}

Trait Implementations§

Source§

impl Clone for Ds4Report

Source§

fn clone(&self) -> Ds4Report

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ds4Report

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Ds4Report

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Ds4Report

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.