11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using System . Text ;
54using System . Threading ;
6- using System . Threading . Tasks ;
75using SlimDX ;
86using SlimDX . DirectInput ;
97
@@ -15,34 +13,28 @@ public class JoystickQueryThread : IDisposable
1513 private const int CENTER_VALUE = 32767 ;
1614 private const float ANGLE_RATIO = 0.0054933317056795f ;
1715 private const float VELOCITY_RATIO = 0.0030518509475997f ;
16+ private const float ROTATION_RATIO = 0.0030518509475997f ;
1817
1918 private DirectInput directInputHandle ;
2019 private Joystick joystick ;
2120 private long angle ;
2221 private long velocity ;
23- private bool [ ] dPad ;
22+ private long rotation ;
2423
2524 public JoystickQueryThread ( )
2625 {
2726 angle = 0 ;
2827 velocity = 0 ;
29-
30-
28+ rotation = 0 ;
3129 }
3230
33- /* REMOVE AFTER TESTING
34- public Joystick Joystick
35- {
36- get { return joystick; }
37- }
38- */
3931 public bool InitializeJoystick ( )
4032 {
4133 directInputHandle = new DirectInput ( ) ;
4234 IList < DeviceInstance > devicelist = directInputHandle . GetDevices ( ) ;
4335
4436 DeviceInstance joystickinstance = null ;
45- for ( int i = 0 ; i < devicelist . Count ; i ++ ) //Traverse through all the lists and find the joystick to enumerate
37+ for ( int i = 0 ; i < devicelist . Count ; i ++ ) // Traverse through all the lists and find the joystick to enumerate
4638 {
4739 DeviceInstance dinstance = devicelist . ElementAt ( i ) ;
4840 if ( dinstance . Type == DeviceType . Joystick || dinstance . Type == DeviceType . Gamepad )
@@ -57,8 +49,8 @@ public bool InitializeJoystick()
5749 return false ;
5850 }
5951
60- joystick = new Joystick ( directInputHandle , joystickinstance . ProductGuid ) ; //Create a joystick object to interface with
61- Result acquire = joystick . Acquire ( ) ; //Pull all data from it
52+ joystick = new Joystick ( directInputHandle , joystickinstance . ProductGuid ) ; // Create a joystick object to interface with
53+ Result acquire = joystick . Acquire ( ) ; // Pull all data from it
6254
6355 if ( acquire . IsFailure )
6456 {
@@ -75,14 +67,16 @@ public void QueryJoystick()
7567 long yVelocity ;
7668 long angleValue ;
7769 long velocityValue ;
70+ long zRotation ;
71+ long rotationValue ;
7872
7973 while ( true )
8074 {
8175 if ( joystick == null )
8276 {
8377 if ( ! InitializeJoystick ( ) )
8478 {
85- System . Threading . Thread . Sleep ( 1000 ) ;
79+ Thread . Sleep ( 1000 ) ;
8680 continue ;
8781 }
8882 }
@@ -97,8 +91,10 @@ public void QueryJoystick()
9791 yVelocity = joystickstate . Y ;
9892 angleValue = xAngle - CENTER_VALUE ;
9993 velocityValue = CENTER_VALUE - yVelocity ;
94+ zRotation = joystickstate . RotationZ ;
95+ rotationValue = CENTER_VALUE - zRotation ;
10096
101-
97+ // account for dead zone: angle
10298 if ( Math . Abs ( angleValue ) < 4000 )
10399 {
104100 Angle = 0 ;
@@ -108,6 +104,7 @@ public void QueryJoystick()
108104 Angle = ( long ) ( angleValue * ANGLE_RATIO ) ;
109105 }
110106
107+ // account for dead zone: velocity
111108 if ( Math . Abs ( velocityValue ) < 3000 )
112109 {
113110 Velocity = 0 ;
@@ -116,6 +113,16 @@ public void QueryJoystick()
116113 {
117114 Velocity = ( long ) ( velocityValue * VELOCITY_RATIO ) ;
118115 }
116+
117+ // account for dead zone: rotation
118+ if ( Math . Abs ( rotationValue ) < 3000 )
119+ {
120+ Rotation = 0 ;
121+ }
122+ else
123+ {
124+ Rotation = ( long ) ( rotationValue * ROTATION_RATIO ) ;
125+ }
119126 }
120127 }
121128
@@ -125,7 +132,6 @@ public long Angle
125132 {
126133 return Interlocked . Read ( ref angle ) ;
127134 }
128-
129135 set
130136 {
131137 Interlocked . Exchange ( ref angle , value ) ;
@@ -144,6 +150,18 @@ public long Velocity
144150 }
145151 }
146152
153+ public long Rotation
154+ {
155+ get
156+ {
157+ return Interlocked . Read ( ref rotation ) ;
158+ }
159+ set
160+ {
161+ Interlocked . Exchange ( ref rotation , value ) ;
162+ }
163+ }
164+
147165 public void Dispose ( )
148166 {
149167 directInputHandle . Dispose ( ) ;
0 commit comments