2001-06-04 21:26:53 +00:00
|
|
|
// jsinput.cxx -- wait for and identify input from joystick
|
|
|
|
//
|
|
|
|
// Written by Tony Peden, started May 2001
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001 Tony Peden (apeden@earthlink.net)
|
|
|
|
//
|
|
|
|
// 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
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2001-06-04 21:26:53 +00:00
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
2008-07-25 18:38:29 +00:00
|
|
|
#include <iostream>
|
2006-07-04 23:29:05 +00:00
|
|
|
|
2008-07-27 16:25:13 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::cin;
|
|
|
|
using std::endl;
|
2006-07-04 23:29:05 +00:00
|
|
|
|
2001-12-10 23:00:27 +00:00
|
|
|
#include "jsinput.h"
|
2001-06-04 21:26:53 +00:00
|
|
|
|
|
|
|
jsInput::jsInput(jsSuper *j) {
|
2006-07-04 23:29:05 +00:00
|
|
|
jss=j;
|
|
|
|
pretty_display=true;
|
|
|
|
joystick=axis=button=-1;
|
|
|
|
axis_threshold=0.2;
|
2001-06-04 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsInput::~jsInput(void) {}
|
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
int jsInput::getInput() {
|
|
|
|
|
|
|
|
bool gotit=false;
|
|
|
|
|
|
|
|
float delta;
|
|
|
|
int i, current_button = 0, button_bits = 0;
|
2001-06-04 21:26:53 +00:00
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
joystick=axis=button=-1;
|
|
|
|
axis_positive=false;
|
2001-06-04 21:26:53 +00:00
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
if(pretty_display) {
|
|
|
|
printf ( "+----------------------------------------------\n" ) ;
|
|
|
|
printf ( "| Btns " ) ;
|
2001-06-04 21:26:53 +00:00
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
|
|
|
|
printf ( "Ax:%3d ", i ) ;
|
|
|
|
|
|
|
|
for ( ; i < 8 ; i++ )
|
2001-06-04 21:26:53 +00:00
|
|
|
printf ( " " ) ;
|
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
printf ( "|\n" ) ;
|
|
|
|
|
|
|
|
printf ( "+----------------------------------------------\n" ) ;
|
|
|
|
}
|
2001-06-04 21:26:53 +00:00
|
|
|
|
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
jss->firstJoystick();
|
|
|
|
do {
|
2001-06-04 21:26:53 +00:00
|
|
|
jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
|
2006-07-04 23:29:05 +00:00
|
|
|
axes_iv[jss->getCurrentJoystickId()] ) ;
|
|
|
|
} while( jss->nextJoystick() );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(!gotit) {
|
2001-06-04 21:26:53 +00:00
|
|
|
jss->firstJoystick();
|
|
|
|
do {
|
|
|
|
|
2006-07-04 23:29:05 +00:00
|
|
|
jss->getJoystick()->read ( ¤t_button, axes ) ;
|
|
|
|
|
|
|
|
if(pretty_display) printf ( "| %04x ", current_button ) ;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
|
|
|
|
|
|
|
|
delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
|
|
|
|
if(pretty_display) printf ( "%+.3f ", delta ) ;
|
|
|
|
if(!gotit) {
|
|
|
|
if( fabs(delta) > axis_threshold ) {
|
|
|
|
gotit=true;
|
|
|
|
joystick=jss->getCurrentJoystickId();
|
|
|
|
axis=i;
|
|
|
|
axis_positive=(delta>0);
|
|
|
|
} else if( current_button != 0 ) {
|
|
|
|
gotit=true;
|
|
|
|
joystick=jss->getCurrentJoystickId();
|
|
|
|
button_bits=current_button;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pretty_display) {
|
|
|
|
for ( ; i < 8 ; i++ )
|
|
|
|
printf ( " . " ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} while( jss->nextJoystick() && !gotit);
|
2001-06-04 21:26:53 +00:00
|
|
|
if(pretty_display) {
|
2006-07-04 23:29:05 +00:00
|
|
|
printf ( "|\r" ) ;
|
|
|
|
fflush ( stdout ) ;
|
2001-06-04 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ulMilliSecondSleep(1);
|
2006-07-04 23:29:05 +00:00
|
|
|
}
|
|
|
|
if(button_bits != 0) {
|
2005-07-31 14:05:55 +00:00
|
|
|
for(int i=0;i<=31;i++) {
|
2006-07-04 23:29:05 +00:00
|
|
|
if( ( button_bits & (1 << i) ) > 0 ) {
|
|
|
|
button=i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsInput::findDeadBand() {
|
|
|
|
|
|
|
|
float delta;
|
|
|
|
int i;
|
|
|
|
float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
|
|
|
|
|
|
|
|
jss->firstJoystick();
|
|
|
|
do {
|
|
|
|
jss->getJoystick()->read ( NULL,
|
|
|
|
axes_iv[jss->getCurrentJoystickId()] ) ;
|
|
|
|
for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
|
|
|
|
dead_band[jss->getCurrentJoystickId()][i] = 0;
|
|
|
|
}
|
|
|
|
} while( jss->nextJoystick() );
|
|
|
|
|
|
|
|
ulClock clock;
|
|
|
|
cout << 10;
|
|
|
|
cout.flush();
|
|
|
|
|
|
|
|
for (int j = 9; j >= 0; j--) {
|
|
|
|
double start_time = clock.getAbsTime();
|
|
|
|
do {
|
|
|
|
jss->firstJoystick();
|
|
|
|
do {
|
|
|
|
|
|
|
|
jss->getJoystick()->read ( NULL, axes ) ;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
|
|
|
|
|
|
|
|
delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
|
|
|
|
if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
|
|
|
|
dead_band[jss->getCurrentJoystickId()][i] = delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while( jss->nextJoystick());
|
|
|
|
|
|
|
|
ulMilliSecondSleep(1);
|
|
|
|
clock.update();
|
|
|
|
} while (clock.getAbsTime() - start_time < 1.0);
|
|
|
|
|
|
|
|
cout << " - " << j;
|
|
|
|
cout.flush();
|
|
|
|
}
|
|
|
|
cout << endl << endl;
|
|
|
|
|
|
|
|
jss->firstJoystick();
|
|
|
|
do {
|
|
|
|
for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
|
|
|
|
jss->getJoystick()->setDeadBand(i, dead_band[jss->getCurrentJoystickId()][i]);
|
|
|
|
printf("Joystick %i, axis %i: %f\n", jss->getCurrentJoystickId(), i, dead_band[jss->getCurrentJoystickId()][i]);
|
|
|
|
}
|
|
|
|
} while( jss->nextJoystick() );
|
2001-06-04 21:26:53 +00:00
|
|
|
}
|