]> git.mxchange.org Git - flightgear.git/blob - Joystick/joystick.c
6de1d98b6d2620d661ea5a95daaf3ec6cf67ddcc
[flightgear.git] / Joystick / joystick.c
1 /**************************************************************************
2  * joystick.h -- joystick support
3  *
4  * Written by Michele America, started September 1997.
5  *
6  * Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_JOYSTICK
28
29 #include <linux/joystick.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <unistd.h>
33
34 #endif
35
36 static joy_x_min=0, joy_x_ctr=0, joy_x_max=0;
37 static joy_y_min=0, joy_y_ctr=0, joy_y_max=0;
38 static joy_x_dead_min=1000, joy_x_dead_max=-1000;
39 static joy_y_dead_min=1000, joy_y_dead_max=-1000;
40
41
42 static int joystick_fd;
43
44 int fgJoystickInit( int joy_num )
45 {
46 #ifdef HAVE_JOYSTICK
47         int status;
48         char *fname;
49         struct JS_DATA_TYPE js;
50         int button;
51
52         /* argument should be 0 or 1 */
53         if( joy_num != 1 && joy_num != 0 ) {
54                 perror( "js" );
55                 return( 1 );
56         }
57
58         /* pick appropriate device file */
59         if (joy_num == 0)
60                 fname = "/dev/js0";
61         if (joy_num == 1)
62                 fname = "/dev/js1";
63
64         /* open device file */
65         joystick_fd = open(fname, O_RDONLY);
66         if (joystick_fd < 0) {
67                 perror ("js");
68                 return( 1 );
69         }
70         
71         printf( "\nMove joystick around dead spot and press any joystick button.\n" );
72         status = read(joystick_fd, &js, JS_RETURN);
73         if (status != JS_RETURN) {
74                 perror("js");
75                 return( 1 );
76         }
77         button = js.buttons & 1 || js.buttons & 2;
78         while(button == 0 ) {
79                 status = read(joystick_fd, &js, JS_RETURN);
80                 if (status != JS_RETURN) {
81                         perror("js");
82                         return( 1 );
83                 }
84                 button = js.buttons & 1 || js.buttons & 2;
85                 if( js.x > joy_x_dead_max )
86                         joy_x_dead_max = js.x;
87                 if( js.x < joy_x_dead_min )
88                         joy_x_dead_min = js.x;
89                 if( js.y > joy_y_dead_max )
90                         joy_y_dead_max = js.y;
91                 if( js.y < joy_y_dead_min )
92                         joy_y_dead_min = js.y;
93                 
94                 // printf( "Xmin %d Xmax %d Ymin %d Ymax %d", joy_x_dead_min, joy_x_dead_max, \
95                 //              joy_y_dead_min, joy_y_dead_max );
96         }
97         status = read(joystick_fd, &js, JS_RETURN);
98         if (status != JS_RETURN) {
99                 perror("js");
100                 return( 1 );
101         }
102         
103         printf("\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n", joy_x_dead_min, joy_x_dead_max );
104         printf("                      Y_dead_min = %d, Y_dead_max = %d\n", joy_y_dead_min, joy_y_dead_max );
105         
106         sleep( 1 );
107         
108         printf( "\nCenter joystick and press any joystick button.\n" );
109         status = read(joystick_fd, &js, JS_RETURN);
110         if (status != JS_RETURN) {
111                 perror("js");
112                 return( 1 );
113         }
114         button = js.buttons & 1 || js.buttons & 2;
115         while(button == 0 ) {
116                 status = read(joystick_fd, &js, JS_RETURN);
117                 if (status != JS_RETURN) {
118                         perror("js");
119                         return( 1 );
120                 }
121                 button = js.buttons & 1 || js.buttons & 2;
122         }
123         status = read(joystick_fd, &js, JS_RETURN);
124         if (status != JS_RETURN) {
125                 perror("js");
126                 return( 1 );
127         }
128         joy_x_ctr = js.x;
129         joy_y_ctr = js.y;
130         
131         printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
132         
133         sleep( 1 );
134         
135         printf( "\nMove joystick to upper left and press any joystick button.\n" );
136         status = read(joystick_fd, &js, JS_RETURN);
137         if (status != JS_RETURN) {
138                 perror("js");
139                 return( 1 );
140         }
141         button = js.buttons & 1 || js.buttons & 2;
142         while(button == 0 ) {
143                 status = read(joystick_fd, &js, JS_RETURN);
144                 if (status != JS_RETURN) {
145                         perror("js");
146                         return( 1 );
147                 }
148                 button = js.buttons & 1 || js.buttons & 2;
149         }
150         status = read(joystick_fd, &js, JS_RETURN);
151         if (status != JS_RETURN) {
152                 perror("js");
153                 return( 1 );
154         }
155         joy_x_min = js.x;
156         joy_y_min = js.y;
157         printf("Joystick calibration: X_min = %d, Y_min = %d\n", joy_x_min, joy_y_min );
158         
159         sleep( 1 );
160         
161         printf( "\nMove joystick to lower right and press any joystick button.\n" );
162         status = read(joystick_fd, &js, JS_RETURN);
163         if (status != JS_RETURN) {
164                 perror("js");
165                 return( 1 );
166         }
167         button = js.buttons & 1 || js.buttons & 2;
168         while(button == 0 ) {
169                 status = read(joystick_fd, &js, JS_RETURN);
170                 if (status != JS_RETURN) {
171                         perror("js");
172                         return( 1 );
173                 }
174                 button = js.buttons & 1 || js.buttons & 2;
175         }
176         status = read(joystick_fd, &js, JS_RETURN);
177         if (status != JS_RETURN) {
178                 perror("js");
179                 return( 1 );
180         }
181         joy_x_max = js.x;
182         joy_y_max = js.y;
183         
184         printf("Joystick calibration: X_max = %d, Y_max = %d\n", joy_x_max, joy_y_max );
185         
186         // joy_x_ctr = (joy_x_max-joy_x_min)/2;
187         // joy_y_ctr = (joy_y_max-joy_y_min)/2;
188         // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
189         
190         return( joystick_fd );
191 #endif
192 }
193
194 /* void fgJoystickCalibrate( int joy_fd )
195 {
196
197 } */
198
199 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
200 {
201 #ifdef HAVE_JOYSTICK
202         struct JS_DATA_TYPE js;
203         int status;
204         
205         status = read(joystick_fd, &js, JS_RETURN);
206         if (status != JS_RETURN) {
207                 perror("js");
208                 return( 1 );
209         }
210
211         /* printf("\n button 0: %s  button 1: %s  X position: %4d  Y position: %4d\n",
212                  (js.buttons & 1) ? "on " : "off",
213                  (js.buttons & 2) ? "on " : "off",
214                  js.x,
215                  js.y ); */
216                  
217         if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
218                 *joy_x = 0.5;
219         else
220                 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
221         *joy_x = *joy_x*2-1;
222         
223         if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
224                 *joy_y = 0.5;
225         else
226                 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
227         *joy_y = *joy_y*2-1;
228         
229         *joy_b1 = js.buttons & 1;
230         *joy_b2 = js.buttons & 2;
231
232         return( 0 );
233 #endif
234 }
235
236
237 /* $Log$
238 /* Revision 1.1  1997/08/29 18:06:54  curt
239 /* Initial revision.
240 /*
241  */