4
* Copyright (c) 1997-199 Vojtech Pavlik
10
* This is a joystick calibration program for Linux joystick driver.
14
* This program is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2 of the License, or
17
* (at your option) any later version.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
* Should you need to contact me, the author, you can do so either by
29
* e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
30
* Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
33
#include <sys/ioctl.h>
44
#include <asm/param.h>
45
#include <linux/joystick.h>
47
#define PIT_HZ 1193180L
53
const char *pos_name[] = {"minimum", "center", "maximum"};
54
const char *corr_name[] = {"none (raw)", "broken line"};
55
const char corr_coef_num[] = {0,4};
57
struct correction_data {
63
struct js_corr corr[MAX_AXES];
64
char axes, buttons, fuzz;
66
struct correction_data corda[MAX_AXES];
73
void print_position(int i, int a)
75
printf("Axis %d: %8d\r", i, a);
84
gettimeofday(&tv, NULL);
86
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
89
void wait_for_event(int d, struct js_info *s)
103
if (select(d+1, &set, NULL, NULL, &tv)) {
105
if (FD_ISSET(d, &set)) {
106
if (read(d, &ev, sizeof(struct js_event)) == sizeof(struct js_event))
107
switch (ev.type & ~JS_EVENT_INIT) {
109
s->axis[ev.number] = ev.value; break;
110
case JS_EVENT_BUTTON:
111
s->buttons = (s->buttons & ~(1 << ev.number)) | (ev.value << ev.number);
115
if (FD_ISSET(0, &set)) {
117
s->buttons |= (1 << 31);
121
s->buttons &= ~(1 << 31);
129
for (i = 0; i < 78; i++) putchar(' ');
135
int solve_broken(int *results, struct correction_data inputs)
141
c = 32767.0 / (inputs.cmin[1] - inputs.cmax[0]);
142
d = 32767.0 / (inputs.cmin[2] - inputs.cmax[1]);
144
results[0] = rint(a);
145
results[1] = rint(b);
146
results[2] = rint(c*16384.0);
147
results[3] = rint(d*16384.0);
155
puts("Usage: jscal <device>");
157
puts(" -c --calibrate Calibrate the joystick");
158
puts(" -h --help Display this help");
159
puts(" -s <x,y,z...> --set-correction Sets correction to specified values");
160
puts(" -t --test-center Tests if joystick is corectly calibrated");
161
puts(" returns 0 on success, see the jscal");
162
puts(" manpage for error values");
163
puts(" -V --version Prints the version numbers");
164
puts(" -p --print-correction Prints the current settings as a jscal");
165
puts(" command line");
173
if (ioctl(fd, JSIOCGAXES, &axes)) {
174
perror("jscal: error getting axes");
177
if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
178
perror("jscal: error getting buttons");
181
if (ioctl(fd, JSIOCGCORR, &corr)) {
182
perror("jscal: error getting correction");
186
printf("Joystick has %d axes and %d buttons.\n", axes, buttons);
187
for (i = 0; i < axes; i++) {
188
printf("Correction for axis %d is %s, precision is %d.\n",
189
i, corr_name[(int)corr[i].type], corr[i].prec);
190
if (corr_coef_num[(int)corr[i].type]) {
191
printf("Coeficients are:");
192
for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
193
printf(" %d", corr[i].coef[j]);
194
if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
206
for (i=0; i<MAX_AXES; i++) {
207
corr[i].type = JS_CORR_NONE;
211
if (ioctl(fd, JSIOCSCORR, &corr)) {
212
perror("jscal: error setting correction");
219
int amax[MAX_AXES], amin[MAX_AXES];
221
puts("Calibrating precision: wait and don't touch the joystick.");
223
wait_for_event(fd, &js);
225
while (get_time() < t+50) wait_for_event(fd, &js);
227
wait_for_event(fd, &js);
229
for(i=0; i < axes; i++)
230
amin[i] = amax[i] = js.axis[i];
233
wait_for_event(fd, &js);
234
for(i=0; i < axes; i++) {
235
if (amin[i] > js.axis[i]) amin[i] = js.axis[i];
236
if (amax[i] < js.axis[i]) amax[i] = js.axis[i];
237
printf("Axis %d:%5d,%5d ", i, amin[i], amax[i]);
241
} while (get_time() < t+2000);
243
printf("Done. Precision is: \n");
245
for (i=0; i < axes; i++) {
246
corr[i].prec = amax[i] - amin[i];
247
printf("Axis: %d: %5d\n", i, corr[i].prec);
257
for (j = 0; j < axes; j++)
258
for (i = 0; i < NUM_POS; i++) {
259
while(b ^ js.buttons) wait_for_event(fd, &js);
260
printf("Move axis %d to %s position and push any button.\n", j, pos_name[i]);
262
while (!(b ^ js.buttons)) {
263
print_position(j, js.axis[j]);
264
wait_for_event(fd, &js);
269
corda[j].cmin[i] = js.axis[j];
270
corda[j].cmax[i] = js.axis[j];
274
while (get_time() < t + 2000 && (b ^ js.buttons)) {
275
if (js.axis[j] < corda[j].cmin[i]) corda[j].cmin[i] = js.axis[j];
276
if (js.axis[j] > corda[j].cmax[i]) corda[j].cmax[i] = js.axis[j];
277
wait_for_event(fd, &js);
284
for (j = 0; j < axes; j++) {
285
solve_broken(corr[j].coef, corda[j]);
286
corr[j].type = JS_CORR_BROKEN;
289
puts("Setting correction to:");
290
for (i = 0; i < axes; i++) {
291
printf("Correction for axis %d: %s, precision: %d.\n",
292
i, corr_name[(int)corr[i].type], corr[i].prec);
293
if (corr_coef_num[(int)corr[i].type]) {
294
printf("Coeficients:");
295
for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
296
printf(" %d", corr[i].coef[j]);
297
if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
305
if (ioctl(fd, JSIOCSCORR, &corr)) {
306
perror("jscal: error setting correction");
313
printf("JsCal was compiled for driver version: %d.%d.%d\n", JS_VERSION >> 16,
314
(JS_VERSION >> 8) & 0xff, JS_VERSION & 0xff);
315
printf("Current running driver version: %d.%d.%d\n", version >> 16,
316
(version >> 8) & 0xff, version & 0xff);
319
void print_settings(char *devicename)
323
if (ioctl(fd, JSIOCGAXES, &axes)) {
324
perror("jscal: error getting axes");
327
if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
328
perror("jscal: error getting buttons");
331
if (ioctl(fd, JSIOCGCORR, &corr)) {
332
perror("jscal: error getting correction");
336
printf("jscal -s %d", axes);
337
for (i = 0; i < axes; i++) {
338
printf( ",%d,%d", corr[i].type, corr[i].prec);
339
for (j = 0; j < corr_coef_num[(int)corr[i].type]; j++)
340
printf(",%d", corr[i].coef[j]);
342
printf(" %s\n",devicename);
345
void set_correction(char *p)
350
if (ioctl(fd, JSIOCGAXES, &axes)) {
351
perror("jscal: error getting axes");
355
if (axes > MAX_AXES) axes = MAX_AXES;
358
fprintf(stderr, "jscal: missing number of axes\n");
365
fprintf(stderr, "jscal: joystick has different number of axes (%d) than specified in command line (%d)\n",
371
for (i = 0; i < axes; i++) {
374
fprintf(stderr, "jscal: missing correction type for axis %d\n", i);
377
sscanf(++p, "%d", &t);
382
fprintf(stderr, "jscal: unknown correction type for axis %d\n", i);
388
fprintf(stderr, "jscal: missing precision for axis %d\n", i);
391
sscanf(++p, "%d", &t);
396
for(j = 0; j < corr_coef_num[corr[i].type]; j++) {
398
fprintf(stderr, "jscal: missing coefficient %d for axis %d\n", j, i);
401
sscanf(++p, "%d", (int*) &corr[i].coef[j]);
407
fprintf(stderr, "jscal: too many values\n");
411
if (ioctl(fd, JSIOCSCORR, &corr)) {
412
perror("jscal: error setting correction");
422
if (ioctl(fd, JSIOCGAXES, &axes)) {
423
perror("jscal: error getting axes");
427
if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
428
perror("jscal: error getting buttons");
432
if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
433
perror("jscal: cannot set nonblocking mode");
437
while (read(fd, &ev, sizeof(struct js_event)) == sizeof(struct js_event)) {
438
switch (ev.type & ~JS_EVENT_INIT) {
440
js.axis[ev.number] = ev.value; break;
441
case JS_EVENT_BUTTON:
442
js.buttons = (js.buttons & ~(1 << ev.number)) | (ev.value << ev.number);
446
for (i = 0; i < axes; i++) if (js.axis[i]) {
447
fprintf(stderr, "jscal: axes not calibrated\n");
451
fprintf(stderr, "jscal: buttons pressed\n");
458
int main(int argc, char **argv)
460
int option_index = 0;
461
char *parameter = NULL;
464
static struct option long_options[] =
466
{"calibrate", no_argument, NULL, 'c'},
467
{"help", no_argument, NULL, 'h'},
468
{"set-correction", required_argument, NULL, 's'},
469
{"test-center", no_argument, NULL, 't'},
470
{"version", no_argument, NULL, 'V'},
471
{"print-correction", no_argument, NULL, 'p'}
480
t = getopt_long(argc, argv, "chps:vVt", long_options, &option_index);
488
fprintf(stderr, "jscal: more than one action specified\n");
492
if ((parameter=optarg)) strcpy(parameter,optarg);
502
fprintf(stderr, "jscal: missing parameter\n");
505
fprintf(stderr, "jscal: unknown option\n");
508
fprintf(stderr, "jscal: option parsing error\n");
512
if (argc != optind + 1) {
513
fprintf(stderr, "jscal: missing devicename\n");
517
if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
518
perror("jscal: can't open joystick device");
522
if (ioctl(fd, JSIOCGVERSION, &version)) {
523
perror("jscal: error getting version");
526
if (version != JS_VERSION) {
527
fprintf(stderr, "jscal: wrong version\n");
541
print_settings(argv[argc -1]);
544
set_correction(parameter);
553
fprintf(stderr, "jscal: this cannot happen\n");