use std::thread;
use std::time::Duration;
use vigem_rust::{Client, Ds4Button, Ds4Dpad, Ds4Report};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::connect()?;
println!("Connected to ViGEm bus");
let ds4 = client.new_ds4_target().plugin()?;
println!("Plugged in virtual DualShock 4 controller");
ds4.wait_for_ready()?;
println!("Controller is ready. You can test it at https://hardwaretester.com/gamepad");
let notifications = ds4.register_notification()?;
thread::spawn(move || {
println!("Notification Thread Started. Waiting for feedback from the host...");
while let Ok(Ok(notification)) = notifications.recv() {
println!("Notification Thread Received feedback:");
println!(
" - Rumble: Large Motor = {}, Small Motor = {}",
notification.large_motor, notification.small_motor
);
println!(
" - Lightbar Color: R={}, G={}, B={}",
notification.lightbar.red, notification.lightbar.green, notification.lightbar.blue
);
}
});
let mut report = Ds4Report::default();
report.set_dpad(Ds4Dpad::East);
report.buttons |= Ds4Button::CROSS.bits();
report.trigger_r = 255;
let mut angle: f64 = 0.0;
loop {
let (sin, cos) = angle.sin_cos();
report.thumb_rx = (128.0 + sin * 127.0) as u8;
report.thumb_ry = (128.0 + cos * 127.0) as u8;
ds4.update(&report)?;
thread::sleep(Duration::from_millis(16));
angle += 0.05;
}
}