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>
32 jsInput::jsInput(jsSuper *j) {
35 joystick=axis=button=-1;
39 jsInput::~jsInput(void) {}
41 int jsInput::getInput() {
46 int i, current_button = 0, button_bits = 0;
48 joystick=axis=button=-1;
52 printf ( "+----------------------------------------------\n" ) ;
53 printf ( "| Btns " ) ;
55 for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
56 printf ( "Ax:%3d ", i ) ;
63 printf ( "+----------------------------------------------\n" ) ;
69 jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
70 axes_iv[jss->getCurrentJoystickId()] ) ;
71 } while( jss->nextJoystick() );
79 jss->getJoystick()->read ( ¤t_button, axes ) ;
81 if(pretty_display) printf ( "| %04x ", current_button ) ;
83 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
85 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
86 if(pretty_display) printf ( "%+.3f ", delta ) ;
88 if( fabs(delta) > axis_threshold ) {
90 joystick=jss->getCurrentJoystickId();
92 axis_positive=(delta>0);
93 } else if( current_button != 0 ) {
95 joystick=jss->getCurrentJoystickId();
96 button_bits=current_button;
102 for ( ; i < 8 ; i++ )
107 } while( jss->nextJoystick() && !gotit);
113 ulMilliSecondSleep(1);
115 if(button_bits != 0) {
116 for(int i=0;i<=31;i++) {
117 if( ( button_bits & (1 << i) ) > 0 ) {
127 void jsInput::findDeadBand() {
131 float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
133 jss->firstJoystick();
135 jss->getJoystick()->read ( NULL,
136 axes_iv[jss->getCurrentJoystickId()] ) ;
137 for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
138 dead_band[jss->getCurrentJoystickId()][i] = 0;
140 } while( jss->nextJoystick() );
146 for (int j = 9; j >= 0; j--) {
147 double start_time = clock.getAbsTime();
149 jss->firstJoystick();
152 jss->getJoystick()->read ( NULL, axes ) ;
154 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
156 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
157 if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
158 dead_band[jss->getCurrentJoystickId()][i] = delta;
161 } while( jss->nextJoystick());
163 ulMilliSecondSleep(1);
165 } while (clock.getAbsTime() - start_time < 1.0);
170 cout << endl << endl;
172 jss->firstJoystick();
174 for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
175 jss->getJoystick()->setDeadBand(i, dead_band[jss->getCurrentJoystickId()][i]);
176 printf("Joystick %i, axis %i: %f\n", jss->getCurrentJoystickId(), i, dead_band[jss->getCurrentJoystickId()][i]);
178 } while( jss->nextJoystick() );