1
0
Fork 0

Renamed joystick.[ch] to joystick.[ch]xx

Added js.hxx which is Steve's joystick interface class.
This commit is contained in:
curt 1998-10-24 22:28:13 +00:00
parent 995729b5a1
commit b17f3b1b28
5 changed files with 253 additions and 5 deletions

View file

@ -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

View file

@ -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.
*

View file

@ -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
*

54
Joystick/js.cxx Normal file
View file

@ -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 ) ;
}

184
Joystick/js.hxx Normal file
View file

@ -0,0 +1,184 @@
#ifndef __INCLUDED_JS_H__
#define __INCLUDED_JS_H__ 1
#ifdef __linux__
# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
# include <linux/joystick.h>
#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