From: curt Date: Sat, 24 Oct 1998 22:28:13 +0000 (+0000) Subject: Renamed joystick.[ch] to joystick.[ch]xx X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b17f3b1b289afc352fd91fe4408c7ff1c0d47c24;p=flightgear.git Renamed joystick.[ch] to joystick.[ch]xx Added js.hxx which is Steve's joystick interface class. --- 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.c deleted file mode 100644 index f5da076a0..000000000 --- a/Joystick/joystick.c +++ /dev/null @@ -1,285 +0,0 @@ -/************************************************************************** - * joystick.h -- joystick support - * - * Written by Michele America, started September 1997. - * - * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id$ - * (Log is kept at end of this file) - **************************************************************************/ - - -#ifdef HAVE_JOYSTICK - -#include -#include -#include -#include - -#endif - -#include - -static joy_x_min=0, joy_x_ctr=0, joy_x_max=0; -static joy_y_min=0, joy_y_ctr=0, joy_y_max=0; -static joy_x_dead_min=1000, joy_x_dead_max=-1000; -static joy_y_dead_min=1000, joy_y_dead_max=-1000; - - -static int joystick_fd; - -int fgJoystickInit( int joy_num ) { - - fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n"); - -#ifdef HAVE_JOYSTICK - int status; - char *fname; - struct JS_DATA_TYPE js; - int button; - - /* argument should be 0 or 1 */ - if( joy_num != 1 && joy_num != 0 ) { - perror( "js" ); - return( 1 ); - } - - /* pick appropriate device file */ - if (joy_num == 0) - fname = "/dev/js0"; - if (joy_num == 1) - fname = "/dev/js1"; - - /* open device file */ - joystick_fd = open(fname, O_RDONLY); - if (joystick_fd < 0) { - perror ("js"); - return( 1 ); - } - - fgPrintf( FG_INPUT,FG_ALERT, - "\nMove joystick around dead spot and press any joystick button.\n" ); - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - while(button == 0 ) { - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - if( js.x > joy_x_dead_max ) - joy_x_dead_max = js.x; - if( js.x < joy_x_dead_min ) - joy_x_dead_min = js.x; - if( js.y > joy_y_dead_max ) - joy_y_dead_max = js.y; - if( js.y < joy_y_dead_min ) - joy_y_dead_min = js.y; - - /* printf( "Xmin %d Xmax %d Ymin %d Ymax %d", - joy_x_dead_min, joy_x_dead_max, - joy_y_dead_min, joy_y_dead_max ); */ - } - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - - fgPrintf( FG_INPUT, FG_DEBUG, - "\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n", - joy_x_dead_min, joy_x_dead_max ); - fgPrintf( FG_INPUT, FG_DEBUG, - " Y_dead_min = %d, Y_dead_max = %d\n", - joy_y_dead_min, joy_y_dead_max ); - - sleep( 1 ); - - fgPrintf( FG_INPUT, FG_DEBUG, - "\nCenter joystick and press any joystick button.\n" ); - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - while(button == 0 ) { - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - } - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - joy_x_ctr = js.x; - joy_y_ctr = js.y; - - fgPrintf( FG_INPUT, FG_DEBUG, - "Joystick calibration: X_ctr = %d, Y_ctr = %d\n", - joy_x_ctr, joy_y_ctr ); - - sleep( 1 ); - - fgPrintf( FG_INPUT, FG_DEBUG, - "\nMove joystick to upper left and press any joystick button.\n" ); - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - while(button == 0 ) { - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - } - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - joy_x_min = js.x; - joy_y_min = js.y; - fgPrintf( FG_INPUT, FG_DEBUG, - "Joystick calibration: X_min = %d, Y_min = %d\n", - joy_x_min, joy_y_min ); - - sleep( 1 ); - - fgPrintf( FG_INPUT, FG_DEBUG, - "\nMove joystick to lower right and press any joystick button.\n" ); - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - while(button == 0 ) { - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - button = js.buttons & 1 || js.buttons & 2; - } - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - joy_x_max = js.x; - joy_y_max = js.y; - - fgPrintf( FG_INPUT, FG_DEBUG, - "Joystick calibration: X_max = %d, Y_max = %d\n", - joy_x_max, joy_y_max ); - - // joy_x_ctr = (joy_x_max-joy_x_min)/2; - // joy_y_ctr = (joy_y_max-joy_y_min)/2; - // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr ); - - return( joystick_fd ); -#else - return( 1 ); -#endif -} - -/* void fgJoystickCalibrate( int joy_fd ) -{ - -} */ - -int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ) -{ -#ifdef HAVE_JOYSTICK - struct JS_DATA_TYPE js; - int status; - - status = read(joystick_fd, &js, JS_RETURN); - if (status != JS_RETURN) { - perror("js"); - return( 1 ); - } - - /* printf("\n button 0: %s button 1: %s X position: %4d Y position: %4d\n", - (js.buttons & 1) ? "on " : "off", - (js.buttons & 2) ? "on " : "off", - js.x, - js.y ); */ - - if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max ) - *joy_x = 0.5; - else - *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min); - *joy_x = *joy_x*2-1; - - if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max ) - *joy_y = 0.5; - else - *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min); - *joy_y = *joy_y*2-1; - - *joy_b1 = js.buttons & 1; - *joy_b2 = js.buttons & 2; - -#endif - return( 0 ); -} - - -/* $Log$ -/* 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. - * - * Revision 1.5 1998/02/12 21:59:44 curt - * Incorporated code changes contributed by Charlie Hotchkiss - * - * - * Revision 1.4 1998/02/03 23:20:20 curt - * Lots of little tweaks to fix various consistency problems discovered by - * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper - * passed arguments along to the real printf(). Also incorporated HUD changes - * by Michele America. - * - * Revision 1.3 1998/01/27 00:47:54 curt - * Incorporated Paul Bleisch's new debug message - * system and commandline/config file processing code. - * - * Revision 1.2 1997/12/30 20:47:40 curt - * Integrated new event manager with subsystem initializations. - * - * Revision 1.1 1997/08/29 18:06:54 curt - * Initial revision. - * - */ diff --git a/Joystick/joystick.cxx b/Joystick/joystick.cxx new file mode 100644 index 000000000..7e2cc2f73 --- /dev/null +++ b/Joystick/joystick.cxx @@ -0,0 +1,289 @@ +/************************************************************************** + * joystick.h -- joystick support + * + * Written by Michele America, started September 1997. + * + * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifdef HAVE_JOYSTICK + +#include +#include +#include +#include + +#endif + +#include + +static joy_x_min=0, joy_x_ctr=0, joy_x_max=0; +static joy_y_min=0, joy_y_ctr=0, joy_y_max=0; +static joy_x_dead_min=1000, joy_x_dead_max=-1000; +static joy_y_dead_min=1000, joy_y_dead_max=-1000; + + +static int joystick_fd; + +int fgJoystickInit( int joy_num ) { + + fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n"); + +#ifdef HAVE_JOYSTICK + int status; + char *fname; + struct JS_DATA_TYPE js; + int button; + + /* argument should be 0 or 1 */ + if( joy_num != 1 && joy_num != 0 ) { + perror( "js" ); + return( 1 ); + } + + /* pick appropriate device file */ + if (joy_num == 0) + fname = "/dev/js0"; + if (joy_num == 1) + fname = "/dev/js1"; + + /* open device file */ + joystick_fd = open(fname, O_RDONLY); + if (joystick_fd < 0) { + perror ("js"); + return( 1 ); + } + + fgPrintf( FG_INPUT,FG_ALERT, + "\nMove joystick around dead spot and press any joystick button.\n" ); + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + while(button == 0 ) { + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + if( js.x > joy_x_dead_max ) + joy_x_dead_max = js.x; + if( js.x < joy_x_dead_min ) + joy_x_dead_min = js.x; + if( js.y > joy_y_dead_max ) + joy_y_dead_max = js.y; + if( js.y < joy_y_dead_min ) + joy_y_dead_min = js.y; + + /* printf( "Xmin %d Xmax %d Ymin %d Ymax %d", + joy_x_dead_min, joy_x_dead_max, + joy_y_dead_min, joy_y_dead_max ); */ + } + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + + fgPrintf( FG_INPUT, FG_DEBUG, + "\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n", + joy_x_dead_min, joy_x_dead_max ); + fgPrintf( FG_INPUT, FG_DEBUG, + " Y_dead_min = %d, Y_dead_max = %d\n", + joy_y_dead_min, joy_y_dead_max ); + + sleep( 1 ); + + fgPrintf( FG_INPUT, FG_DEBUG, + "\nCenter joystick and press any joystick button.\n" ); + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + while(button == 0 ) { + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + } + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + joy_x_ctr = js.x; + joy_y_ctr = js.y; + + fgPrintf( FG_INPUT, FG_DEBUG, + "Joystick calibration: X_ctr = %d, Y_ctr = %d\n", + joy_x_ctr, joy_y_ctr ); + + sleep( 1 ); + + fgPrintf( FG_INPUT, FG_DEBUG, + "\nMove joystick to upper left and press any joystick button.\n" ); + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + while(button == 0 ) { + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + } + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + joy_x_min = js.x; + joy_y_min = js.y; + fgPrintf( FG_INPUT, FG_DEBUG, + "Joystick calibration: X_min = %d, Y_min = %d\n", + joy_x_min, joy_y_min ); + + sleep( 1 ); + + fgPrintf( FG_INPUT, FG_DEBUG, + "\nMove joystick to lower right and press any joystick button.\n" ); + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + while(button == 0 ) { + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + button = js.buttons & 1 || js.buttons & 2; + } + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + joy_x_max = js.x; + joy_y_max = js.y; + + fgPrintf( FG_INPUT, FG_DEBUG, + "Joystick calibration: X_max = %d, Y_max = %d\n", + joy_x_max, joy_y_max ); + + // joy_x_ctr = (joy_x_max-joy_x_min)/2; + // joy_y_ctr = (joy_y_max-joy_y_min)/2; + // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr ); + + return( joystick_fd ); +#else + return( 1 ); +#endif +} + +/* void fgJoystickCalibrate( int joy_fd ) +{ + +} */ + +int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ) +{ +#ifdef HAVE_JOYSTICK + struct JS_DATA_TYPE js; + int status; + + status = read(joystick_fd, &js, JS_RETURN); + if (status != JS_RETURN) { + perror("js"); + return( 1 ); + } + + /* printf("\n button 0: %s button 1: %s X position: %4d Y position: %4d\n", + (js.buttons & 1) ? "on " : "off", + (js.buttons & 2) ? "on " : "off", + js.x, + js.y ); */ + + if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max ) + *joy_x = 0.5; + else + *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min); + *joy_x = *joy_x*2-1; + + if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max ) + *joy_y = 0.5; + else + *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min); + *joy_y = *joy_y*2-1; + + *joy_b1 = js.buttons & 1; + *joy_b2 = js.buttons & 2; + +#endif + return( 0 ); +} + + +/* $Log$ +/* 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. + * + * Revision 1.5 1998/02/12 21:59:44 curt + * Incorporated code changes contributed by Charlie Hotchkiss + * + * + * Revision 1.4 1998/02/03 23:20:20 curt + * Lots of little tweaks to fix various consistency problems discovered by + * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper + * passed arguments along to the real printf(). Also incorporated HUD changes + * by Michele America. + * + * Revision 1.3 1998/01/27 00:47:54 curt + * Incorporated Paul Bleisch's new debug message + * system and commandline/config file processing code. + * + * Revision 1.2 1997/12/30 20:47:40 curt + * Integrated new event manager with subsystem initializations. + * + * Revision 1.1 1997/08/29 18:06:54 curt + * Initial revision. + * + */ diff --git a/Joystick/joystick.h b/Joystick/joystick.h deleted file mode 100644 index 276afb939..000000000 --- a/Joystick/joystick.h +++ /dev/null @@ -1,58 +0,0 @@ -/************************************************************************** - * joystick.h -- joystick support - * - * Written by Michele America, started September 1997. - * - * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id$ - * (Log is kept at end of this file) - **************************************************************************/ - - -#ifndef _JOYSTICK_H -#define _JOYSTICK_H - - -#ifdef __cplusplus -extern "C" { -#endif - - -int fgJoystickInit( int joy_num ); -int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ); - - -#ifdef __cplusplus -} -#endif - - -#endif /* _JOYSTICK_H */ - - -/* $Log$ -/* 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 - * - * Revision 1.1 1997/08/29 18:06:55 curt - * Initial revision. - * - */ diff --git a/Joystick/joystick.hxx b/Joystick/joystick.hxx new file mode 100644 index 000000000..d26428356 --- /dev/null +++ b/Joystick/joystick.hxx @@ -0,0 +1,62 @@ +/************************************************************************** + * joystick.h -- joystick support + * + * Written by Michele America, started September 1997. + * + * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef _JOYSTICK_H +#define _JOYSTICK_H + + +#ifdef __cplusplus +extern "C" { +#endif + + +int fgJoystickInit( int joy_num ); +int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* _JOYSTICK_H */ + + +/* $Log$ +/* 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 + * + * Revision 1.1 1997/08/29 18:06:55 curt + * Initial revision. + * + */ 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 +