-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamepad_device.rs
44 lines (40 loc) · 1.1 KB
/
gamepad_device.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use input_device::InputDevice;
use inputs::Inputs;
use joy;
pub struct GamepadDevice {
device: joy::Device,
}
impl GamepadDevice {
pub fn new(dev: &str) -> GamepadDevice {
let tmp = dev.to_string() + "\0";
let device = joy::Device::open(tmp.as_bytes()).unwrap();
GamepadDevice { device }
}
}
impl InputDevice for GamepadDevice {
fn poll(&mut self, inputs: &mut Inputs) {
for ev in &mut self.device {
use joy::Event::*;
match ev {
Axis(n, value) => {
match n {
0 => {
inputs.steering = (value as f32) / (0x7FFF as f32);
// println!("axis 0: {}\r", inputs.steering);
}
4 => {
inputs.throttle = ((value as i32 + 0x7FFF_i32) as f32) / (0xFFFF as f32);
// println!("axis 4: {}\r", inputs.throttle);
}
5 => {
inputs.throttle = -((value as i32 + 0x7FFF_i32) as f32) / (0xFFFF as f32);
// println!("axis 5: {}\r", inputs.throttle);
}
_ => {}
}
},
Button(_, _) => {}
}
}
}
}