1 // jsinput.cxx -- wait for and identify input from joystick
3 // Written by Tony Peden, started May 2001
5 // Copyright (C) 2001 Tony Peden (apeden@earthlink.net)
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <simgear/compiler.h>
31 jsInput::jsInput(jsSuper *j) {
34 joystick=axis=button=-1;
38 jsInput::~jsInput(void) {}
40 int jsInput::getInput() {
45 int i, current_button = 0, button_bits = 0;
47 joystick=axis=button=-1;
51 printf ( "+----------------------------------------------\n" ) ;
52 printf ( "| Btns " ) ;
54 for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
55 printf ( "Ax:%3d ", i ) ;
62 printf ( "+----------------------------------------------\n" ) ;
68 jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
69 axes_iv[jss->getCurrentJoystickId()] ) ;
70 } while( jss->nextJoystick() );
78 jss->getJoystick()->read ( ¤t_button, axes ) ;
80 if(pretty_display) printf ( "| %04x ", current_button ) ;
82 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
84 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
85 if(pretty_display) printf ( "%+.3f ", delta ) ;
87 if( fabs(delta) > axis_threshold ) {
89 joystick=jss->getCurrentJoystickId();
91 axis_positive=(delta>0);
92 } else if( current_button != 0 ) {
94 joystick=jss->getCurrentJoystickId();
95 button_bits=current_button;
101 for ( ; i < 8 ; i++ )
106 } while( jss->nextJoystick() && !gotit);
112 ulMilliSecondSleep(1);
114 if(button_bits != 0) {
115 for(int i=0;i<=31;i++) {
116 if( ( button_bits & (1 << i) ) > 0 ) {
126 void jsInput::findDeadBand() {
130 float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
132 jss->firstJoystick();
134 jss->getJoystick()->read ( NULL,
135 axes_iv[jss->getCurrentJoystickId()] ) ;
136 for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
137 dead_band[jss->getCurrentJoystickId()][i] = 0;
139 } while( jss->nextJoystick() );
145 for (int j = 9; j >= 0; j--) {
146 double start_time = clock.getAbsTime();
148 jss->firstJoystick();
151 jss->getJoystick()->read ( NULL, axes ) ;
153 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
155 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
156 if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
157 dead_band[jss->getCurrentJoystickId()][i] = delta;
160 } while( jss->nextJoystick());
162 ulMilliSecondSleep(1);
164 } while (clock.getAbsTime() - start_time < 1.0);
169 cout << endl << endl;
171 jss->firstJoystick();
173 for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
174 jss->getJoystick()->setDeadBand(i, dead_band[jss->getCurrentJoystickId()][i]);
175 printf("Joystick %i, axis %i: %f\n", jss->getCurrentJoystickId(), i, dead_band[jss->getCurrentJoystickId()][i]);
177 } while( jss->nextJoystick() );