1 #ifndef __INCLUDED_JS_H__
2 #define __INCLUDED_JS_H__ 1
8 # include <linux/joystick.h>
15 This is all set up for the older Linux and BSD drivers
16 which restrict you to two axes.
19 #define _JS_MAX_AXES 2
30 float dead_band [ _JS_MAX_AXES ] ;
31 float center [ _JS_MAX_AXES ] ;
32 float max [ _JS_MAX_AXES ] ;
33 float min [ _JS_MAX_AXES ] ;
37 num_axes = _JS_MAX_AXES ;
39 fd = ::open ( fname, O_RDONLY ) ;
49 The Linux driver seems to return 512 for all axes
50 when no stick is present - but there is a chance
51 that could happen by accident - so it's gotta happen
52 on both axes for at least 100 attempts.
57 rawRead ( NULL, center ) ;
59 } while ( counter < 100 && center[0] == 512.0f && center[1] == 512.0f ) ;
64 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
66 max [ i ] = center [ i ] * 2.0f ;
68 dead_band [ i ] = 0.0f ;
78 float fudge_axis ( float value, int axis )
80 if ( value < center[axis] )
82 float xx = ( value - center[ axis ] ) /
83 ( center [ axis ] - min [ axis ] ) ;
85 xx = ( xx > -dead_band [ axis ] ) ? 0.0f :
86 ( ( xx + dead_band [ axis ] ) / ( 1.0f - dead_band [ axis ] ) ) ;
88 return ( xx < -1.0f ) ? -1.0f : xx ;
92 float xx = ( value - center [ axis ] ) /
93 ( max [ axis ] - center [ axis ] ) ;
95 xx = ( xx < dead_band [ axis ] ) ? 0.0f :
96 ( ( xx - dead_band [ axis ] ) / ( 1.0f - dead_band [ axis ] ) ) ;
98 return ( xx > 1.0f ) ? 1.0f : xx ;
104 jsJoystick ( int id = 0 )
106 sprintf ( fname, "/dev/js%d", id ) ;
115 int getNumAxes () { return num_axes ; }
116 int notWorking () { return error ; }
117 void setError () { error = JS_TRUE ; }
119 float getDeadBand ( int axis ) { return dead_band [ axis ] ; }
120 void setDeadBand ( int axis, float db ) { dead_band [ axis ] = db ; }
122 void setMinRange ( float *axes ) { memcpy ( min , axes, num_axes * sizeof(float) ) ; }
123 void setMaxRange ( float *axes ) { memcpy ( max , axes, num_axes * sizeof(float) ) ; }
124 void setCenter ( float *axes ) { memcpy ( center, axes, num_axes * sizeof(float) ) ; }
126 void getMinRange ( float *axes ) { memcpy ( axes, min , num_axes * sizeof(float) ) ; }
127 void getMaxRange ( float *axes ) { memcpy ( axes, max , num_axes * sizeof(float) ) ; }
128 void getCenter ( float *axes ) { memcpy ( axes, center, num_axes * sizeof(float) ) ; }
130 void read ( int *buttons, float *axes )
138 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
142 float raw_axes [ _JS_MAX_AXES ] ;
144 rawRead ( buttons, raw_axes ) ;
147 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
148 axes[i] = ( i < num_axes ) ? fudge_axis ( raw_axes[i], i ) : 0.0f ;
151 void rawRead ( int *buttons, float *axes )
155 if ( buttons ) *buttons = 0 ;
157 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
163 int status = ::read ( fd, &js, JS_RETURN ) ;
165 if ( status != JS_RETURN )
173 *buttons = js.buttons ;
177 axes[0] = (float) js.x ;
178 axes[1] = (float) js.y ;