1 /**************************************************************************
2 * joystick.h -- joystick support
4 * Written by Michele America, started September 1997.
6 * Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
29 #include <linux/joystick.h>
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;
42 static int joystick_fd;
44 int fgJoystickInit( int joy_num )
49 struct JS_DATA_TYPE js;
52 /* argument should be 0 or 1 */
53 if( joy_num != 1 && joy_num != 0 ) {
58 /* pick appropriate device file */
64 /* open device file */
65 joystick_fd = open(fname, O_RDONLY);
66 if (joystick_fd < 0) {
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) {
77 button = js.buttons & 1 || js.buttons & 2;
79 status = read(joystick_fd, &js, JS_RETURN);
80 if (status != JS_RETURN) {
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;
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 );
97 status = read(joystick_fd, &js, JS_RETURN);
98 if (status != JS_RETURN) {
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 );
108 printf( "\nCenter joystick and press any joystick button.\n" );
109 status = read(joystick_fd, &js, JS_RETURN);
110 if (status != JS_RETURN) {
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) {
121 button = js.buttons & 1 || js.buttons & 2;
123 status = read(joystick_fd, &js, JS_RETURN);
124 if (status != JS_RETURN) {
131 printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
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) {
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) {
148 button = js.buttons & 1 || js.buttons & 2;
150 status = read(joystick_fd, &js, JS_RETURN);
151 if (status != JS_RETURN) {
157 printf("Joystick calibration: X_min = %d, Y_min = %d\n", joy_x_min, joy_y_min );
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) {
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) {
174 button = js.buttons & 1 || js.buttons & 2;
176 status = read(joystick_fd, &js, JS_RETURN);
177 if (status != JS_RETURN) {
184 printf("Joystick calibration: X_max = %d, Y_max = %d\n", joy_x_max, joy_y_max );
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 );
190 return( joystick_fd );
194 /* void fgJoystickCalibrate( int joy_fd )
199 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
202 struct JS_DATA_TYPE js;
205 status = read(joystick_fd, &js, JS_RETURN);
206 if (status != JS_RETURN) {
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",
217 if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
220 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
223 if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
226 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
229 *joy_b1 = js.buttons & 1;
230 *joy_b2 = js.buttons & 2;
238 /* Revision 1.1 1997/08/29 18:06:54 curt