From b17f3b1b289afc352fd91fe4408c7ff1c0d47c24 Mon Sep 17 00:00:00 2001 From: curt Date: Sat, 24 Oct 1998 22:28:13 +0000 Subject: [PATCH] Renamed joystick.[ch] to joystick.[ch]xx Added js.hxx which is Steve's joystick interface class. --- Joystick/Makefile.am | 4 +- Joystick/{joystick.c => joystick.cxx} | 8 +- Joystick/{joystick.h => joystick.hxx} | 8 +- Joystick/js.cxx | 54 ++++++++ Joystick/js.hxx | 184 ++++++++++++++++++++++++++ 5 files changed, 253 insertions(+), 5 deletions(-) rename Joystick/{joystick.c => joystick.cxx} (96%) rename Joystick/{joystick.h => joystick.hxx} (87%) create mode 100644 Joystick/js.cxx create mode 100644 Joystick/js.hxx diff --git a/Joystick/Makefile.am b/Joystick/Makefile.am index 92442a608..6e2d09c7b 100644 --- a/Joystick/Makefile.am +++ b/Joystick/Makefile.am @@ -1,5 +1,7 @@ +EXTRA_DIST = js.cxx + noinst_LIBRARIES = libJoystick.a -libJoystick_a_SOURCES = joystick.c joystick.h +libJoystick_a_SOURCES = joystick.cxx joystick.hxx js.hxx INCLUDES += -I$(top_builddir) -I$(top_builddir)/Lib -I$(top_builddir)/Simulator diff --git a/Joystick/joystick.c b/Joystick/joystick.cxx similarity index 96% rename from Joystick/joystick.c rename to Joystick/joystick.cxx index f5da076a0..7e2cc2f73 100644 --- a/Joystick/joystick.c +++ b/Joystick/joystick.cxx @@ -256,9 +256,13 @@ int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ) /* $Log$ -/* Revision 1.7 1998/04/25 22:06:29 curt -/* Edited cvs log messages in source files ... bad bad bad! +/* Revision 1.1 1998/10/24 22:28:16 curt +/* Renamed joystick.[ch] to joystick.[ch]xx +/* Added js.hxx which is Steve's joystick interface class. /* + * Revision 1.7 1998/04/25 22:06:29 curt + * Edited cvs log messages in source files ... bad bad bad! + * * Revision 1.6 1998/04/18 04:14:05 curt * Moved fg_debug.c to it's own library. * diff --git a/Joystick/joystick.h b/Joystick/joystick.hxx similarity index 87% rename from Joystick/joystick.h rename to Joystick/joystick.hxx index 276afb939..d26428356 100644 --- a/Joystick/joystick.h +++ b/Joystick/joystick.hxx @@ -46,9 +46,13 @@ int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ); /* $Log$ -/* Revision 1.3 1998/04/22 13:26:21 curt -/* C++ - ifing the code a bit. +/* Revision 1.1 1998/10/24 22:28:18 curt +/* Renamed joystick.[ch] to joystick.[ch]xx +/* Added js.hxx which is Steve's joystick interface class. /* + * Revision 1.3 1998/04/22 13:26:21 curt + * C++ - ifing the code a bit. + * * Revision 1.2 1998/01/22 02:59:36 curt * Changed #ifdef FILE_H to #ifdef _FILE_H * diff --git a/Joystick/js.cxx b/Joystick/js.cxx new file mode 100644 index 000000000..d81ec696d --- /dev/null +++ b/Joystick/js.cxx @@ -0,0 +1,54 @@ + +#include "js.h" + +jsJoystick js0 ( 0 ) ; +jsJoystick js1 ( 1 ) ; + +int main () +{ + printf ( "Joystick test program.\n" ) ; + printf ( "~~~~~~~~~~~~~~~~~~~~~~\n" ) ; + + if ( js0 . notWorking () ) printf ( "Joystick 0 not detected\n" ) ; + if ( js1 . notWorking () ) printf ( "Joystick 1 not detected\n" ) ; + if ( js0 . notWorking () && js1 . notWorking () ) exit ( 1 ) ; + + js0 . setDeadBand ( 0, 0.1 ) ; + js0 . setDeadBand ( 1, 0.1 ) ; + js1 . setDeadBand ( 0, 0.1 ) ; + js1 . setDeadBand ( 1, 0.1 ) ; + + float *ax0 = new float [ js0.getNumAxes () ] ; + float *ax1 = new float [ js1.getNumAxes () ] ; + + while (1) + { + int b ; + + if ( ! js0 . notWorking () ) + { + js0 . read ( &b, ax0 ) ; + + printf ( "JS0: b0:%s b1:%s X:%1.3f Y:%1.3f ", + ( b & 1 ) ? "on " : "off", ( b & 2 ) ? "on " : "off", ax0[0], ax0[1] ) ; + } + + if ( ! js1 . notWorking () ) + { + js1 . read ( &b, ax1 ) ; + + printf ( "JS1: b0:%s b1:%s X:%1.3f Y:%1.3f ", + ( b & 1 ) ? "on " : "off", ( b & 2 ) ? "on " : "off", ax1[0], ax1[1] ) ; + } + + printf ( "\r" ) ; + fflush ( stdout ) ; + + /* give other processes a chance */ + + usleep ( 1 ) ; + } + + exit ( 0 ) ; +} + diff --git a/Joystick/js.hxx b/Joystick/js.hxx new file mode 100644 index 000000000..ea51c0ce9 --- /dev/null +++ b/Joystick/js.hxx @@ -0,0 +1,184 @@ +#ifndef __INCLUDED_JS_H__ +#define __INCLUDED_JS_H__ 1 + +#ifdef __linux__ +# include +# include +# include +# include +#endif + +#define JS_TRUE 1 +#define JS_FALSE 0 + +/* + This is all set up for the older Linux and BSD drivers + which restrict you to two axes. +*/ + +#define _JS_MAX_AXES 2 + +class jsJoystick +{ + JS_DATA_TYPE js ; + char fname [ 128 ] ; + int error ; + int fd ; + + int num_axes ; + + float dead_band [ _JS_MAX_AXES ] ; + float center [ _JS_MAX_AXES ] ; + float max [ _JS_MAX_AXES ] ; + float min [ _JS_MAX_AXES ] ; + + void open () + { + num_axes = _JS_MAX_AXES ; + + fd = ::open ( fname, O_RDONLY ) ; + + error = ( fd < 0 ) ; + + if ( error ) + return ; + + int counter = 0 ; + + /* + The Linux driver seems to return 512 for all axes + when no stick is present - but there is a chance + that could happen by accident - so it's gotta happen + on both axes for at least 100 attempts. + */ + + do + { + rawRead ( NULL, center ) ; + counter++ ; + } while ( counter < 100 && center[0] == 512.0f && center[1] == 512.0f ) ; + + if ( counter >= 100 ) + error = JS_TRUE ; + + for ( int i = 0 ; i < _JS_MAX_AXES ; i++ ) + { + max [ i ] = center [ i ] * 2.0f ; + min [ i ] = 0.0f ; + dead_band [ i ] = 0.0f ; + } + } + + void close () + { + if ( ! error ) + ::close ( fd ) ; + } + + float fudge_axis ( float value, int axis ) + { + if ( value < center[axis] ) + { + float xx = ( value - center[ axis ] ) / + ( center [ axis ] - min [ axis ] ) ; + + xx = ( xx > -dead_band [ axis ] ) ? 0.0f : + ( ( xx + dead_band [ axis ] ) / ( 1.0f - dead_band [ axis ] ) ) ; + + return ( xx < -1.0f ) ? -1.0f : xx ; + } + else + { + float xx = ( value - center [ axis ] ) / + ( max [ axis ] - center [ axis ] ) ; + + xx = ( xx < dead_band [ axis ] ) ? 0.0f : + ( ( xx - dead_band [ axis ] ) / ( 1.0f - dead_band [ axis ] ) ) ; + + return ( xx > 1.0f ) ? 1.0f : xx ; + } + } + +public: + + jsJoystick ( int id = 0 ) + { + sprintf ( fname, "/dev/js%d", id ) ; + open () ; + } + + ~jsJoystick () + { + close () ; + } + + int getNumAxes () { return num_axes ; } + int notWorking () { return error ; } + void setError () { error = JS_TRUE ; } + + float getDeadBand ( int axis ) { return dead_band [ axis ] ; } + void setDeadBand ( int axis, float db ) { dead_band [ axis ] = db ; } + + void setMinRange ( float *axes ) { memcpy ( min , axes, num_axes * sizeof(float) ) ; } + void setMaxRange ( float *axes ) { memcpy ( max , axes, num_axes * sizeof(float) ) ; } + void setCenter ( float *axes ) { memcpy ( center, axes, num_axes * sizeof(float) ) ; } + + void getMinRange ( float *axes ) { memcpy ( axes, min , num_axes * sizeof(float) ) ; } + void getMaxRange ( float *axes ) { memcpy ( axes, max , num_axes * sizeof(float) ) ; } + void getCenter ( float *axes ) { memcpy ( axes, center, num_axes * sizeof(float) ) ; } + + void read ( int *buttons, float *axes ) + { + if ( error ) + { + if ( buttons ) + *buttons = 0 ; + + if ( axes ) + for ( int i = 0 ; i < _JS_MAX_AXES ; i++ ) + axes[i] = 0.0f ; + } + + float raw_axes [ _JS_MAX_AXES ] ; + + rawRead ( buttons, raw_axes ) ; + + if ( axes ) + for ( int i = 0 ; i < _JS_MAX_AXES ; i++ ) + axes[i] = ( i < num_axes ) ? fudge_axis ( raw_axes[i], i ) : 0.0f ; + } + + void rawRead ( int *buttons, float *axes ) + { + if ( error ) + { + if ( buttons ) *buttons = 0 ; + if ( axes ) + for ( int i = 0 ; i < _JS_MAX_AXES ; i++ ) + axes[i] = 1500.0f ; + + return ; + } + + int status = ::read ( fd, &js, JS_RETURN ) ; + + if ( status != JS_RETURN ) + { + perror ( fname ) ; + setError () ; + return ; + } + + if ( buttons ) + *buttons = js.buttons ; + + if ( axes ) + { + axes[0] = (float) js.x ; + axes[1] = (float) js.y ; + } + } +} ; + +#endif +