1 // jsinput.cxx -- wait for and identify input from joystick
2 //
3 // Written by Tony Peden, started May 2001
4 //
5 // Copyright (C) 2001 Tony Peden (apeden@earthlink.net)
6 //
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.
11 //
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.
16 //
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.
20
21 #include <simgear/compiler.h>
22
23 #include <iostream>
24
25 using std::cout;
26 using std::cin;
27 using std::endl;
28
29 #include "jsinput.h"
30
31 #include <simgear/timing/timestamp.hxx>
32
jsInput(jsSuper * j)33 jsInput::jsInput(jsSuper *j) {
34 jss=j;
35 pretty_display=true;
36 joystick=axis=button=-1;
37 axis_threshold=0.2;
38 }
39
~jsInput(void)40 jsInput::~jsInput(void) {}
41
getInput()42 int jsInput::getInput() {
43
44 bool gotit=false;
45
46 float delta;
47 int i, current_button = 0, button_bits = 0;
48
49 joystick=axis=button=-1;
50 axis_positive=false;
51
52 if(pretty_display) {
53 printf ( "+----------------------------------------------\n" ) ;
54 printf ( "| Btns " ) ;
55
56 for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
57 printf ( "Ax:%3d ", i ) ;
58
59 for ( ; i < 8 ; i++ )
60 printf ( " " ) ;
61
62 printf ( "|\n" ) ;
63
64 printf ( "+----------------------------------------------\n" ) ;
65 }
66
67
68 jss->firstJoystick();
69 do {
70 jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
71 axes_iv[jss->getCurrentJoystickId()] ) ;
72 } while( jss->nextJoystick() );
73
74
75
76 while(!gotit) {
77 jss->firstJoystick();
78 do {
79
80 jss->getJoystick()->read ( ¤t_button, axes ) ;
81
82 if(pretty_display) printf ( "| %04x ", current_button ) ;
83
84 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
85
86 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
87 if(pretty_display) printf ( "%+.3f ", delta ) ;
88 if(!gotit) {
89 if( fabs(delta) > axis_threshold ) {
90 gotit=true;
91 joystick=jss->getCurrentJoystickId();
92 axis=i;
93 axis_positive=(delta>0);
94 } else if( current_button != 0 ) {
95 gotit=true;
96 joystick=jss->getCurrentJoystickId();
97 button_bits=current_button;
98 }
99 }
100 }
101
102 if(pretty_display) {
103 for ( ; i < 8 ; i++ )
104 printf ( " . " ) ;
105 }
106
107
108 } while( jss->nextJoystick() && !gotit);
109 if(pretty_display) {
110 printf ( "|\r" ) ;
111 fflush ( stdout ) ;
112 }
113
114 SGTimeStamp::sleepForMSec(1);
115 }
116 if(button_bits != 0) {
117 for(int i=0;i<=31;i++) {
118 if( ( button_bits & (1 << i) ) > 0 ) {
119 button=i;
120 break;
121 }
122 }
123 }
124
125 return 0;
126 }
127
findDeadBand()128 void jsInput::findDeadBand() {
129
130 float delta;
131 int i;
132 float dead_band[MAX_JOYSTICKS][_JS_MAX_AXES];
133
134 jss->firstJoystick();
135 do {
136 jss->getJoystick()->read ( NULL,
137 axes_iv[jss->getCurrentJoystickId()] ) ;
138 for ( i = 0; i < jss->getJoystick()->getNumAxes(); i++ ) {
139 dead_band[jss->getCurrentJoystickId()][i] = 0;
140 }
141 } while( jss->nextJoystick() );
142
143 SGTimeStamp clock = SGTimeStamp::now();
144 cout << 10;
145 cout.flush();
146
147 for (int j = 9; j >= 0; j--) {
148 clock.stamp();
149 do {
150 jss->firstJoystick();
151 do {
152
153 jss->getJoystick()->read ( NULL, axes ) ;
154
155 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
156
157 delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i];
158 if (fabs(delta) > dead_band[jss->getCurrentJoystickId()][i])
159 dead_band[jss->getCurrentJoystickId()][i] = delta;
160 }
161
162 } while( jss->nextJoystick());
163
164 SGTimeStamp::sleepForMSec(1);
165 } while (clock.elapsedMSec() < 1000);
166
167 cout << " - " << j;
168 cout.flush();
169 }
170 cout << endl << endl;
171
172 jss->firstJoystick();
173 do {
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]);
177 }
178 } while( jss->nextJoystick() );
179 }
180