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 ) {
45 printf("Initializing joystick\n");
50 struct JS_DATA_TYPE js;
53 /* argument should be 0 or 1 */
54 if( joy_num != 1 && joy_num != 0 ) {
59 /* pick appropriate device file */
65 /* open device file */
66 joystick_fd = open(fname, O_RDONLY);
67 if (joystick_fd < 0) {
72 printf( "\nMove joystick around dead spot and press any joystick button.\n" );
73 status = read(joystick_fd, &js, JS_RETURN);
74 if (status != JS_RETURN) {
78 button = js.buttons & 1 || js.buttons & 2;
80 status = read(joystick_fd, &js, JS_RETURN);
81 if (status != JS_RETURN) {
85 button = js.buttons & 1 || js.buttons & 2;
86 if( js.x > joy_x_dead_max )
87 joy_x_dead_max = js.x;
88 if( js.x < joy_x_dead_min )
89 joy_x_dead_min = js.x;
90 if( js.y > joy_y_dead_max )
91 joy_y_dead_max = js.y;
92 if( js.y < joy_y_dead_min )
93 joy_y_dead_min = js.y;
95 // printf( "Xmin %d Xmax %d Ymin %d Ymax %d", joy_x_dead_min, joy_x_dead_max, \
96 // joy_y_dead_min, joy_y_dead_max );
98 status = read(joystick_fd, &js, JS_RETURN);
99 if (status != JS_RETURN) {
104 printf("\nJoystick calibration: X_dead_min = %d, X_dead_max = %d\n", joy_x_dead_min, joy_x_dead_max );
105 printf(" Y_dead_min = %d, Y_dead_max = %d\n", joy_y_dead_min, joy_y_dead_max );
109 printf( "\nCenter joystick and press any joystick button.\n" );
110 status = read(joystick_fd, &js, JS_RETURN);
111 if (status != JS_RETURN) {
115 button = js.buttons & 1 || js.buttons & 2;
116 while(button == 0 ) {
117 status = read(joystick_fd, &js, JS_RETURN);
118 if (status != JS_RETURN) {
122 button = js.buttons & 1 || js.buttons & 2;
124 status = read(joystick_fd, &js, JS_RETURN);
125 if (status != JS_RETURN) {
132 printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
136 printf( "\nMove joystick to upper left and press any joystick button.\n" );
137 status = read(joystick_fd, &js, JS_RETURN);
138 if (status != JS_RETURN) {
142 button = js.buttons & 1 || js.buttons & 2;
143 while(button == 0 ) {
144 status = read(joystick_fd, &js, JS_RETURN);
145 if (status != JS_RETURN) {
149 button = js.buttons & 1 || js.buttons & 2;
151 status = read(joystick_fd, &js, JS_RETURN);
152 if (status != JS_RETURN) {
158 printf("Joystick calibration: X_min = %d, Y_min = %d\n", joy_x_min, joy_y_min );
162 printf( "\nMove joystick to lower right and press any joystick button.\n" );
163 status = read(joystick_fd, &js, JS_RETURN);
164 if (status != JS_RETURN) {
168 button = js.buttons & 1 || js.buttons & 2;
169 while(button == 0 ) {
170 status = read(joystick_fd, &js, JS_RETURN);
171 if (status != JS_RETURN) {
175 button = js.buttons & 1 || js.buttons & 2;
177 status = read(joystick_fd, &js, JS_RETURN);
178 if (status != JS_RETURN) {
185 printf("Joystick calibration: X_max = %d, Y_max = %d\n", joy_x_max, joy_y_max );
187 // joy_x_ctr = (joy_x_max-joy_x_min)/2;
188 // joy_y_ctr = (joy_y_max-joy_y_min)/2;
189 // printf("Joystick calibration: X_ctr = %d, Y_ctr = %d\n", joy_x_ctr, joy_y_ctr );
191 return( joystick_fd );
195 /* void fgJoystickCalibrate( int joy_fd )
200 int fgJoystickRead( double *joy_x, double *joy_y, int *joy_b1, int *joy_b2 )
203 struct JS_DATA_TYPE js;
206 status = read(joystick_fd, &js, JS_RETURN);
207 if (status != JS_RETURN) {
212 /* printf("\n button 0: %s button 1: %s X position: %4d Y position: %4d\n",
213 (js.buttons & 1) ? "on " : "off",
214 (js.buttons & 2) ? "on " : "off",
218 if( js.x >= joy_x_dead_min && js.x <= joy_x_dead_max )
221 *joy_x = (double)js.x/(double)(joy_x_max-joy_x_min);
224 if( js.y >= joy_y_dead_min && js.y <= joy_y_dead_max )
227 *joy_y = (double)js.y/(double)(joy_y_max-joy_y_min);
230 *joy_b1 = js.buttons & 1;
231 *joy_b2 = js.buttons & 2;
239 /* Revision 1.2 1997/12/30 20:47:40 curt
240 /* Integrated new event manager with subsystem initializations.
242 * Revision 1.1 1997/08/29 18:06:54 curt