Modify `joystick` example to include `set_rumble`
@@ -28,12 +28,15 @@ fn main() {
}
// Print the joystick's power level, if a joystick was found.
- match joystick {
+ let mut joystick = match joystick {
Some(j) => {
println!("\"{}\" power level: {:?}", j.name(), j.power_level().unwrap());
+ j
},
None => panic!("Couldn't open any joystick"),
- }
+ };
+
+ let (mut lo_freq, mut hi_freq) = (0, 0);
for event in sdl_context.event_pump().unwrap().wait_iter() {
use sdl2::event::Event;
@@ -48,10 +51,34 @@ fn main() {
println!("Axis {} moved to {}", axis_idx, val);
}
}
- Event::JoyButtonDown{ button_idx, .. } =>
- println!("Button {} down", button_idx),
- Event::JoyButtonUp{ button_idx, .. } =>
- println!("Button {} up", button_idx),
+ Event::JoyButtonDown{ button_idx, .. } => {
+ println!("Button {} down", button_idx);
+ if button_idx == 0 {
+ lo_freq = 65535;
+ } else if button_idx == 1 {
+ hi_freq = 65535;
+ }
+ if button_idx < 2 {
+ match joystick.set_rumble(lo_freq, hi_freq, 15000) {
+ Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
+ Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
+ }
+ }
+ }
+ Event::JoyButtonUp{ button_idx, .. } => {
+ println!("Button {} up", button_idx);
+ if button_idx == 0 {
+ lo_freq = 0;
+ } else if button_idx == 1 {
+ hi_freq = 0;
+ }
+ if button_idx < 2 {
+ match joystick.set_rumble(lo_freq, hi_freq, 15000) {
+ Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
+ Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
+ }
+ }
+ }
Event::JoyHatMotion{ hat_idx, state, .. } =>
println!("Hat {} moved to {:?}", hat_idx, state),
Event::Quit{..} => break,
