use std::thread;
use std::time::Duration;
use vigem_rust::{Client, X360Button, X360Report};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::connect()?;
println!("Connected to ViGEm bus");
let x360 = client.new_x360_target().plugin()?;
println!("Plugged in virtual Xbox 360 controller");
x360.wait_for_ready()?;
println!("Controller is ready. You can test it at https://hardwaretester.com/gamepad");
let notifications = x360.register_notification()?;
thread::spawn(move || {
println!("Notification Thread Started. Waiting for feedback...");
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!(" - LED Number/Player Index: {}", notification.led_number);
}
});
let mut report = X360Report::default();
let mut angle: f64 = 0.0;
let mut step = 0;
loop {
angle += 0.1;
let (sin, cos) = angle.sin_cos();
report.thumb_lx = (sin * 32767.0) as i16;
report.thumb_ly = (cos * 32767.0) as i16;
if step % 2 == 0 {
report.buttons = X360Button::A;
} else {
report.buttons = X360Button::B;
}
x360.update(&report)?;
thread::sleep(Duration::from_millis(16));
step += 1;
}
}