#[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: u8Left thumbstick X-axis (0-255). 128 is center.
thumb_ly: u8Left thumbstick Y-axis (0-255). 128 is center.
thumb_rx: u8Right thumbstick X-axis (0-255). 128 is center.
thumb_ry: u8Right thumbstick Y-axis (0-255). 128 is center.
A bitmask of the main digital buttons and D-Pad state. See Ds4Button and set_dpad.
special: u8A bitmask of the special buttons. See Ds4SpecialButton.
trigger_l: u8Left trigger value (0-255).
trigger_r: u8Right trigger value (0-255).
Implementations§
Source§impl Ds4Report
impl Ds4Report
Sourcepub fn set_dpad(&mut self, dpad: Ds4Dpad)
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§
impl Copy for Ds4Report
Auto Trait Implementations§
impl Freeze for Ds4Report
impl RefUnwindSafe for Ds4Report
impl Send for Ds4Report
impl Sync for Ds4Report
impl Unpin for Ds4Report
impl UnwindSafe for Ds4Report
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more