@@ -28,12 +28,15 @@ fn main() {
28
28
}
29
29
30
30
// Print the joystick's power level, if a joystick was found.
31
- match joystick {
31
+ let mut joystick = match joystick {
32
32
Some ( j) => {
33
33
println ! ( "\" {}\" power level: {:?}" , j. name( ) , j. power_level( ) . unwrap( ) ) ;
34
+ j
34
35
} ,
35
36
None => panic ! ( "Couldn't open any joystick" ) ,
36
- }
37
+ } ;
38
+
39
+ let ( mut lo_freq, mut hi_freq) = ( 0 , 0 ) ;
37
40
38
41
for event in sdl_context. event_pump ( ) . unwrap ( ) . wait_iter ( ) {
39
42
use sdl2:: event:: Event ;
@@ -48,10 +51,34 @@ fn main() {
48
51
println ! ( "Axis {} moved to {}" , axis_idx, val) ;
49
52
}
50
53
}
51
- Event :: JoyButtonDown { button_idx, .. } =>
52
- println ! ( "Button {} down" , button_idx) ,
53
- Event :: JoyButtonUp { button_idx, .. } =>
54
- println ! ( "Button {} up" , button_idx) ,
54
+ Event :: JoyButtonDown { button_idx, .. } => {
55
+ println ! ( "Button {} down" , button_idx) ;
56
+ if button_idx == 0 {
57
+ lo_freq = 65535 ;
58
+ } else if button_idx == 1 {
59
+ hi_freq = 65535 ;
60
+ }
61
+ if button_idx < 2 {
62
+ match joystick. set_rumble ( lo_freq, hi_freq, 15000 ) {
63
+ Ok ( ( ) ) => println ! ( "Set rumble to ({}, {})" , lo_freq, hi_freq) ,
64
+ Err ( e) => println ! ( "Error setting rumble to ({}, {}): {:?}" , lo_freq, hi_freq, e) ,
65
+ }
66
+ }
67
+ }
68
+ Event :: JoyButtonUp { button_idx, .. } => {
69
+ println ! ( "Button {} up" , button_idx) ;
70
+ if button_idx == 0 {
71
+ lo_freq = 0 ;
72
+ } else if button_idx == 1 {
73
+ hi_freq = 0 ;
74
+ }
75
+ if button_idx < 2 {
76
+ match joystick. set_rumble ( lo_freq, hi_freq, 15000 ) {
77
+ Ok ( ( ) ) => println ! ( "Set rumble to ({}, {})" , lo_freq, hi_freq) ,
78
+ Err ( e) => println ! ( "Error setting rumble to ({}, {}): {:?}" , lo_freq, hi_freq, e) ,
79
+ }
80
+ }
81
+ }
55
82
Event :: JoyHatMotion { hat_idx, state, .. } =>
56
83
println ! ( "Hat {} moved to {:?}" , hat_idx, state) ,
57
84
Event :: Quit { ..} => break ,
0 commit comments