1
0
Fork 0

Completely rewritten to use Steve Baker's joystick interface class.

This commit is contained in:
curt 1998-10-25 10:56:25 +00:00
parent 58b4e200ec
commit 372eeb7aa7
2 changed files with 175 additions and 321 deletions

View file

@ -1,289 +1,144 @@
/************************************************************************** // joystick.cxx -- joystick support
* joystick.h -- joystick support //
* // Written by Curtis Olson, started October 1998.
* Written by Michele America, started September 1997. //
* // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
* Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt //
* // This program is free software; you can redistribute it and/or
* This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as
* modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the
* published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version.
* License, or (at your option) any later version. //
* // This program is distributed in the hope that it will be useful, but
* This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of
* WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details.
* General Public License for more details. //
* // You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software
* along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
* // $Id$
* $Id$ // (Log is kept at end of this file)
* (Log is kept at end of this file)
**************************************************************************/
#ifdef HAVE_JOYSTICK #include <Aircraft/aircraft.hxx>
#include <linux/joystick.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <Debug/fg_debug.h> #include <Debug/fg_debug.h>
#include <Joystick/js.hxx>
static joy_x_min=0, joy_x_ctr=0, joy_x_max=0; #include "joystick.hxx"
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; // joystick classes
static jsJoystick *js0;
static jsJoystick *js1;
int fgJoystickInit( int joy_num ) { // these will hold the values of the axes
static float *js_ax0, *js_ax1;
// Initialize the joystick(s)
int fgJoystickInit( void ) {
fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n"); fgPrintf( FG_INPUT, FG_INFO, "Initializing joystick\n");
#ifdef HAVE_JOYSTICK js0 = new jsJoystick ( 0 );
int status; js1 = new jsJoystick ( 1 );
char *fname;
struct JS_DATA_TYPE js;
int button;
/* argument should be 0 or 1 */ if ( js0->notWorking () ) {
if( joy_num != 1 && joy_num != 0 ) { // not working
perror( "js" ); } else {
return( 1 ); // allocate storage for axes values
} js_ax0 = new float [ js0->getNumAxes() ];
/* pick appropriate device file */ // configure
if (joy_num == 0) js0->setDeadBand( 0, 0.1 );
fname = "/dev/js0"; js0->setDeadBand( 1, 0.1 );
if (joy_num == 1)
fname = "/dev/js1";
/* open device file */ fgPrintf ( FG_INPUT, FG_INFO,
joystick_fd = open(fname, O_RDONLY); " Joystick 0 detected with %d axes\n",
if (joystick_fd < 0) { js0->getNumAxes() );
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 ) if ( js1->notWorking () ) {
{ // not working
} else {
// allocate storage for axes values
js_ax1 = new float [ js1->getNumAxes() ];
} */ // configure
js1->setDeadBand( 0, 0.1 );
js1->setDeadBand( 1, 0.1 );
int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 ) fgPrintf ( FG_INPUT, FG_INFO,
{ " Joystick 1 detected with %d axes\n",
#ifdef HAVE_JOYSTICK js1->getNumAxes() );
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", if ( js0->notWorking() && js1->notWorking() ) {
(js.buttons & 1) ? "on " : "off", fgPrintf ( FG_INPUT, FG_INFO, " No joysticks detected\n" );
(js.buttons & 2) ? "on " : "off", return 0;
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 1;
return( 0 );
} }
/* $Log$ // update the control parameters based on joystick intput
/* Revision 1.1 1998/10/24 22:28:16 curt int fgJoystickRead( void ) {
/* Renamed joystick.[ch] to joystick.[ch]xx fgCONTROLS *c;
/* Added js.hxx which is Steve's joystick interface class. int b;
/*
* Revision 1.7 1998/04/25 22:06:29 curt c = current_aircraft.controls;
* Edited cvs log messages in source files ... bad bad bad!
* if ( ! js0->notWorking() ) {
* Revision 1.6 1998/04/18 04:14:05 curt js0->read( &b, js_ax0 ) ;
* Moved fg_debug.c to it's own library. fgAileronSet( js_ax0[0] );
* fgElevSet( -js_ax0[1] );
* Revision 1.5 1998/02/12 21:59:44 curt }
* Incorporated code changes contributed by Charlie Hotchkiss
* <chotchkiss@namg.us.anritsu.com> if ( ! js1->notWorking() ) {
* js1->read( &b, js_ax1 ) ;
* Revision 1.4 1998/02/03 23:20:20 curt fgRudderSet( js_ax1[0] );
* Lots of little tweaks to fix various consistency problems discovered by fgThrottleSet(FG_Throttle_All, -js_ax1[1] * 1.05 );
* 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. return 1;
* }
* Revision 1.3 1998/01/27 00:47:54 curt
* Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
* system and commandline/config file processing code. // $Log$
* // Revision 1.2 1998/10/25 10:56:25 curt
* Revision 1.2 1997/12/30 20:47:40 curt // Completely rewritten to use Steve Baker's joystick interface class.
* Integrated new event manager with subsystem initializations. //
* // Revision 1.1 1998/10/24 22:28:16 curt
* Revision 1.1 1997/08/29 18:06:54 curt // Renamed joystick.[ch] to joystick.[ch]xx
* Initial revision. // 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
// <chotchkiss@namg.us.anritsu.com>
//
// 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 <pbleisch@acm.org> 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.
//

View file

@ -1,62 +1,61 @@
/************************************************************************** // joystick.cxx -- joystick support
* joystick.h -- joystick support //
* // Written by Curtis Olson, started October 1998.
* Written by Michele America, started September 1997. //
* // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
* Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt //
* // This program is free software; you can redistribute it and/or
* This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as
* modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the
* published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version.
* License, or (at your option) any later version. //
* // This program is distributed in the hope that it will be useful, but
* This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of
* WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details.
* General Public License for more details. //
* // You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software
* along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
* // $Id$
* $Id$ // (Log is kept at end of this file)
* (Log is kept at end of this file)
**************************************************************************/
#ifndef _JOYSTICK_H #ifndef _JOYSTICK_HXX
#define _JOYSTICK_H #define _JOYSTICK_HXX
#ifdef __cplusplus #ifndef __cplusplus
extern "C" { # error This library requires 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
#endif /* _JOYSTICK_H */ // Initialize the joystick(s)
int fgJoystickInit( void );
// update the control parameters based on joystick intput
int fgJoystickRead( void );
/* $Log$ #endif // _JOYSTICK_HXX
/* 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. // $Log$
/* // Revision 1.2 1998/10/25 10:56:27 curt
* Revision 1.3 1998/04/22 13:26:21 curt // Completely rewritten to use Steve Baker's joystick interface class.
* C++ - ifing the code a bit. //
* // Revision 1.1 1998/10/24 22:28:18 curt
* Revision 1.2 1998/01/22 02:59:36 curt // Renamed joystick.[ch] to joystick.[ch]xx
* Changed #ifdef FILE_H to #ifdef _FILE_H // Added js.hxx which is Steve's joystick interface class.
* //
* Revision 1.1 1997/08/29 18:06:55 curt // Revision 1.3 1998/04/22 13:26:21 curt
* Initial revision. // 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.
//