Modify `game-controller` example to include `set_rumble`
@@ -36,7 +36,7 @@ fn main() {
}
}
- let controller =
+ let mut controller =
match controller {
Some(c) => c,
None => panic!("Couldn't open any controller"),
@@ -44,10 +44,29 @@ fn main() {
println!("Controller mapping: {}", controller.mapping());
+ let (mut lo_freq, mut hi_freq) = (0, 0);
+
for event in sdl_context.event_pump().unwrap().wait_iter() {
use sdl2::event::Event;
+ use sdl2::controller::Axis;
match event {
+ Event::ControllerAxisMotion{ axis: Axis::TriggerLeft, value: val, .. } => {
+ // Trigger axes go from 0 to 32767, so this should be okay
+ lo_freq = (val as u16) * 2;
+ match controller.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::ControllerAxisMotion{ axis: Axis::TriggerRight, value: val, .. } => {
+ // Trigger axes go from 0 to 32767, so this should be okay
+ hi_freq = (val as u16) * 2;
+ match controller.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::ControllerAxisMotion{ axis, value: val, .. } => {
// Axis motion is an absolute value in the range
// [-32768, 32767]. Let's simulate a very rough dead
