~ubuntu-branches/ubuntu/lucid/joystick/lucid

« back to all changes in this revision

Viewing changes to utils/jscal.c

  • Committer: Bazaar Package Importer
  • Author(s): Edward Betts
  • Date: 2001-12-26 13:32:55 UTC
  • Revision ID: james.westby@ubuntu.com-20011226133255-hlp8fay1eq671xwn
Tags: upstream-20010903
ImportĀ upstreamĀ versionĀ 20010903

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * jscal.c  Version 1.2
 
3
 *
 
4
 * Copyright (c) 1997-199 Vojtech Pavlik
 
5
 *
 
6
 * Sponsored by SuSE
 
7
 */
 
8
 
 
9
/*
 
10
 * This is a joystick calibration program for Linux joystick driver.
 
11
 */
 
12
 
 
13
/*
 
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.
 
18
 *
 
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.
 
23
 *
 
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
 
27
 *
 
28
 * Should you need to contact me, the author, you can do so either by
 
29
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
 
30
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
 
31
 */
 
32
 
 
33
#include <sys/ioctl.h>
 
34
#include <sys/time.h>
 
35
#include <fcntl.h>
 
36
#include <unistd.h>
 
37
#include <stdio.h>
 
38
#include <math.h>
 
39
#include <getopt.h>
 
40
#include <string.h>
 
41
#include <fcntl.h>
 
42
 
 
43
#include <asm/param.h>
 
44
#include <linux/joystick.h>
 
45
 
 
46
#define PIT_HZ 1193180L
 
47
 
 
48
#define NUM_POS 3
 
49
#define MAX_AXES 16
 
50
#define MAX_CORR 1
 
51
 
 
52
const char *pos_name[] = {"minimum", "center", "maximum"};
 
53
const char *corr_name[] = {"none (raw)", "broken line"};
 
54
const char corr_coef_num[] = {0,4};
 
55
 
 
56
struct correction_data {
 
57
        int cmin[NUM_POS];
 
58
        int cmax[NUM_POS];
 
59
};
 
60
 
 
61
int fd;
 
62
struct js_corr corr[MAX_AXES];
 
63
char axes, buttons, fuzz;
 
64
int version;
 
65
struct correction_data corda[MAX_AXES];
 
66
 
 
67
struct js_info {
 
68
        int buttons;
 
69
        int axis[MAX_AXES];
 
70
        } js;
 
71
 
 
72
void print_position(int i, int a)
 
73
{
 
74
        printf("Axis %d: %8d\r", i, a);
 
75
        fflush(stdout);
 
76
 
 
77
}
 
78
 
 
79
int get_time(void)
 
80
{
 
81
        struct timeval tv;
 
82
 
 
83
        gettimeofday(&tv, NULL);
 
84
 
 
85
        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
86
}
 
87
 
 
88
void wait_for_event(int d, struct js_info *s)
 
89
{
 
90
        struct js_event ev;
 
91
        struct timeval tv;
 
92
        char buf;
 
93
        fd_set set;
 
94
 
 
95
        tv.tv_sec = 0;
 
96
        tv.tv_usec = 100000;
 
97
 
 
98
        FD_ZERO(&set);
 
99
        FD_SET(0, &set);
 
100
        FD_SET(d, &set);
 
101
 
 
102
        if (select(d+1, &set, NULL, NULL, &tv)) {
 
103
 
 
104
                if (FD_ISSET(d, &set)) {
 
105
                        if (read(d, &ev, sizeof(struct js_event)) == sizeof(struct js_event))
 
106
                        switch (ev.type & ~JS_EVENT_INIT) {
 
107
                                case JS_EVENT_AXIS:
 
108
                                        s->axis[ev.number] = ev.value; break;
 
109
                                case JS_EVENT_BUTTON:
 
110
                                        s->buttons = (s->buttons & ~(1 << ev.number)) | (ev.value << ev.number);
 
111
                        }
 
112
                }
 
113
 
 
114
                if (FD_ISSET(0, &set)) {
 
115
                        read(0, &buf, 1);
 
116
                        s->buttons |= (1 << 31);
 
117
                }
 
118
 
 
119
        } else {
 
120
                s->buttons &= ~(1 << 31);
 
121
        }
 
122
}
 
123
 
 
124
void putcs(char *s)
 
125
{
 
126
        int i;
 
127
        putchar('\r');
 
128
        for (i = 0; i < 78; i++) putchar(' ');
 
129
        putchar('\r');
 
130
        fputs(s, stdout);
 
131
        fflush(stdout);
 
132
}
 
133
 
 
134
int solve_broken(int *results, struct correction_data inputs)
 
135
{
 
136
        double a, b, c, d;
 
137
 
 
138
        a = inputs.cmin[1];
 
139
        b = inputs.cmax[1];
 
140
        c = 32767.0 / (inputs.cmin[1] - inputs.cmax[0]);
 
141
        d = 32767.0 / (inputs.cmin[2] - inputs.cmax[1]);
 
142
 
 
143
        results[0] = rint(a);
 
144
        results[1] = rint(b);
 
145
        results[2] = rint(c*16384.0);
 
146
        results[3] = rint(d*16384.0);
 
147
 
 
148
        return 1;
 
149
}
 
150
 
 
151
void help(void)
 
152
{
 
153
        putchar('\n');
 
154
        puts("Usage: jscal <device>");
 
155
        putchar('\n');
 
156
        puts("  -c             --calibrate         Calibrate the joystick");
 
157
        puts("  -h             --help              Display this help");
 
158
        puts("  -s <x,y,z...>  --set-correction    Sets correction to specified values");
 
159
        puts("  -t             --test-center       Tests if joystick is corectly calibrated");
 
160
        puts("                                       returns 0 on success, see the jscal");
 
161
        puts("                                       manpage for error values");
 
162
        puts("  -V             --version           Prints the version numbers");
 
163
        puts("  -p             --print-correction  Prints the current settings as a jscal");
 
164
        puts("                                       command line");
 
165
        putchar('\n');
 
166
}
 
167
 
 
168
void print_info()
 
169
{
 
170
        int i,j;
 
171
 
 
172
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
173
                perror("jscal: error getting axes");
 
174
                exit(1);
 
175
        }
 
176
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
177
                perror("jscal: error getting buttons");
 
178
                exit(1);
 
179
        }
 
180
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
181
                perror("jscal: error getting correction");
 
182
                exit(1);
 
183
        }
 
184
 
 
185
        printf("Joystick has %d axes and %d buttons.\n", axes, buttons);
 
186
        for (i = 0; i < axes; i++) {
 
187
                printf("Correction for axis %d is %s, precision is %d.\n",
 
188
                        i, corr_name[(int)corr[i].type], corr[i].prec);
 
189
                if (corr_coef_num[(int)corr[i].type]) {
 
190
                        printf("Coeficients are:");
 
191
                        for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
 
192
                                printf(" %d", corr[i].coef[j]);
 
193
                                if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
 
194
                        }
 
195
                putchar('\n');
 
196
                }
 
197
        }
 
198
        putchar('\n');
 
199
}
 
200
 
 
201
void calibrate()
 
202
{
 
203
        int i, j, t, b;
 
204
 
 
205
        for (i=0; i<MAX_AXES; i++) {
 
206
                corr[i].type = JS_CORR_NONE;
 
207
                corr[i].prec = 0;
 
208
        }
 
209
 
 
210
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
211
                perror("jscal: error setting correction");
 
212
                exit(1);
 
213
        }
 
214
 
 
215
        {
 
216
 
 
217
                int i;
 
218
                int amax[MAX_AXES], amin[MAX_AXES];
 
219
 
 
220
                puts("Calibrating precision: wait and don't touch the joystick.");
 
221
 
 
222
                wait_for_event(fd, &js);
 
223
                t = get_time();
 
224
                while (get_time() < t+50) wait_for_event(fd, &js);
 
225
 
 
226
                wait_for_event(fd, &js);
 
227
                t = get_time();
 
228
                for(i=0; i < axes; i++)
 
229
                        amin[i] = amax[i] = js.axis[i];
 
230
 
 
231
                do {
 
232
                        wait_for_event(fd, &js);
 
233
                        for(i=0; i < axes; i++) {
 
234
                                if (amin[i] > js.axis[i]) amin[i] = js.axis[i];
 
235
                                if (amax[i] < js.axis[i]) amax[i] = js.axis[i];
 
236
                                printf("Axis %d:%5d,%5d ", i, amin[i], amax[i]);
 
237
                        }
 
238
                        printf("\r");
 
239
                        fflush(stdout);
 
240
                } while (get_time() < t+2000);
 
241
 
 
242
                printf("Done. Precision is:                                             \n");
 
243
 
 
244
                for (i=0; i < axes; i++) {
 
245
                        corr[i].prec = amax[i] - amin[i];
 
246
                        printf("Axis: %d: %5d\n", i, corr[i].prec);
 
247
                }
 
248
 
 
249
                puts("");
 
250
 
 
251
        }
 
252
 
 
253
 
 
254
        b = js.buttons;
 
255
 
 
256
        for (j = 0; j < axes; j++)
 
257
        for (i = 0; i < NUM_POS; i++) {
 
258
                while(b ^ js.buttons) wait_for_event(fd, &js);
 
259
                printf("Move axis %d to %s position and push any button.\n", j,  pos_name[i]);
 
260
 
 
261
                while (!(b ^ js.buttons)) {
 
262
                        print_position(j, js.axis[j]);
 
263
                        wait_for_event(fd, &js);
 
264
                }
 
265
 
 
266
                putcs("Hold ... ");
 
267
 
 
268
                corda[j].cmin[i] = js.axis[j];
 
269
                corda[j].cmax[i] = js.axis[j];
 
270
 
 
271
                t = get_time();
 
272
 
 
273
                while (get_time() < t + 2000 && (b ^ js.buttons)) {
 
274
                        if (js.axis[j] < corda[j].cmin[i]) corda[j].cmin[i] = js.axis[j];
 
275
                        if (js.axis[j] > corda[j].cmax[i]) corda[j].cmax[i] = js.axis[j];
 
276
                        wait_for_event(fd, &js);
 
277
                }
 
278
                puts("OK.");
 
279
        }
 
280
 
 
281
        puts("");
 
282
 
 
283
        for (j = 0; j < axes; j++) {
 
284
                solve_broken(corr[j].coef, corda[j]);
 
285
                corr[j].type = JS_CORR_BROKEN;
 
286
        }
 
287
 
 
288
        puts("Setting correction to:");
 
289
        for (i = 0; i < axes; i++) {
 
290
                printf("Correction for axis %d: %s, precision: %d.\n",
 
291
                        i, corr_name[(int)corr[i].type], corr[i].prec);
 
292
                if (corr_coef_num[(int)corr[i].type]) {
 
293
                        printf("Coeficients:");
 
294
                        for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
 
295
                                printf(" %d", corr[i].coef[j]);
 
296
                                if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
 
297
                        }
 
298
                putchar('\n');
 
299
                }
 
300
        }
 
301
 
 
302
        putchar('\n');
 
303
 
 
304
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
305
                perror("jscal: error setting correction");
 
306
                exit(1);
 
307
        }
 
308
}
 
309
 
 
310
void print_version()
 
311
{
 
312
        printf("JsCal was compiled for driver version: %d.%d.%d\n", JS_VERSION >> 16,
 
313
                (JS_VERSION >> 8) & 0xff, JS_VERSION & 0xff);
 
314
        printf("Current running driver version: %d.%d.%d\n", version >> 16,
 
315
                (version >> 8) & 0xff, version & 0xff);
 
316
}
 
317
 
 
318
void print_settings(char *devicename)
 
319
{
 
320
        int i,j;
 
321
 
 
322
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
323
                perror("jscal: error getting axes");
 
324
                exit(1);
 
325
        }
 
326
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
327
                perror("jscal: error getting buttons");
 
328
                exit(1);
 
329
        }
 
330
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
331
                perror("jscal: error getting correction");
 
332
                exit(1);
 
333
        }
 
334
 
 
335
        printf("jscal -s %d", axes);
 
336
        for (i = 0; i < axes; i++) {
 
337
                printf( ",%d,%d", corr[i].type, corr[i].prec);
 
338
                for (j = 0; j < corr_coef_num[(int)corr[i].type]; j++)
 
339
                        printf(",%d", corr[i].coef[j]);
 
340
        }
 
341
        printf(" %s\n",devicename);
 
342
}
 
343
 
 
344
void set_correction(char *p)
 
345
{
 
346
        int i,j;
 
347
        int t = 0;
 
348
 
 
349
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
350
                perror("jscal: error getting axes");
 
351
                exit(1);
 
352
        }
 
353
 
 
354
        if (axes > MAX_AXES) axes = MAX_AXES;
 
355
 
 
356
        if (!p) {
 
357
                fprintf(stderr, "jscal: missing number of axes\n");
 
358
                exit(1);
 
359
        }
 
360
        sscanf(p, "%d", &t);
 
361
        p = strstr(p, ",");
 
362
 
 
363
        if (t != axes) {
 
364
                fprintf(stderr, "jscal: joystick has different number of axes (%d) than specified in command line (%d)\n", 
 
365
                        axes, t);
 
366
                exit(1);
 
367
        }
 
368
 
 
369
 
 
370
        for (i = 0; i < axes; i++) {
 
371
 
 
372
                if (!p) {
 
373
                        fprintf(stderr, "jscal: missing correction type for axis %d\n", i);
 
374
                        exit(1);
 
375
                }
 
376
                sscanf(++p, "%d", &t);
 
377
                p = strstr(p, ",");
 
378
 
 
379
 
 
380
                if (t > MAX_CORR) {
 
381
                        fprintf(stderr, "jscal: unknown correction type for axis %d\n", i);
 
382
                        exit(1);
 
383
                }
 
384
                corr[i].type = t;
 
385
 
 
386
                if (!p) {
 
387
                        fprintf(stderr, "jscal: missing precision for axis %d\n", i);
 
388
                        exit(1);
 
389
                }
 
390
                sscanf(++p, "%d", &t);
 
391
                p = strstr(p, ",");
 
392
 
 
393
                corr[i].prec = t;
 
394
 
 
395
                for(j = 0; j < corr_coef_num[corr[i].type]; j++) {
 
396
                        if (!p) {
 
397
                                fprintf(stderr, "jscal: missing coefficient %d for axis %d\n", j, i);
 
398
                                exit(1);
 
399
                        }
 
400
                        sscanf(++p, "%d", (int*) &corr[i].coef[j]);
 
401
                        p = strstr(p, ",");
 
402
                }
 
403
        }
 
404
 
 
405
        if (p) {
 
406
                fprintf(stderr, "jscal: too many values\n");
 
407
                exit(1);
 
408
        }
 
409
        
 
410
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
411
                perror("jscal: error setting correction");
 
412
                exit(1);
 
413
        }
 
414
}
 
415
 
 
416
void test_center()
 
417
{
 
418
        int i;
 
419
        struct js_event ev;
 
420
 
 
421
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
422
                perror("jscal: error getting axes");
 
423
                exit(1);
 
424
        }
 
425
 
 
426
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
427
                perror("jscal: error getting buttons");
 
428
                exit(1);
 
429
        }
 
430
 
 
431
        if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
 
432
                perror("jscal: cannot set nonblocking mode");
 
433
                exit(1);
 
434
        }
 
435
 
 
436
        while (read(fd, &ev, sizeof(struct js_event)) == sizeof(struct js_event)) {
 
437
                switch (ev.type & ~JS_EVENT_INIT) {
 
438
                        case JS_EVENT_AXIS:
 
439
                                js.axis[ev.number] = ev.value; break;
 
440
                        case JS_EVENT_BUTTON:
 
441
                                js.buttons = (js.buttons & ~(1 << ev.number)) | (ev.value << ev.number);
 
442
                }
 
443
        }
 
444
 
 
445
        for (i = 0; i < axes; i++) if (js.axis[i]) {
 
446
                fprintf(stderr, "jscal: axes not calibrated\n");
 
447
                exit(2);
 
448
        }
 
449
        if (js.buttons) {
 
450
                fprintf(stderr, "jscal: buttons pressed\n");
 
451
                exit(3);
 
452
        }
 
453
}
 
454
 
 
455
int action = 0;
 
456
 
 
457
int main(int argc, char **argv)
 
458
{
 
459
        int option_index = 0;
 
460
        char *parameter = NULL;
 
461
        int t;
 
462
 
 
463
        static struct option long_options[] =
 
464
        {
 
465
                {"calibrate", no_argument, NULL, 'c'},
 
466
                {"help", no_argument, NULL, 'h'},
 
467
                {"set-correction", required_argument, NULL, 's'},
 
468
                {"test-center", no_argument, NULL, 't'},
 
469
                {"version", no_argument, NULL, 'V'},
 
470
                {"print-correction", no_argument, NULL, 'p'}
 
471
        };
 
472
 
 
473
        if (argc == 1) {
 
474
                help();
 
475
                exit(1);
 
476
        }
 
477
 
 
478
        do {
 
479
                t = getopt_long(argc, argv, "chps:vVt", long_options, &option_index);
 
480
                switch (t) {
 
481
                        case 'p':
 
482
                        case 's':
 
483
                        case 'c':
 
484
                        case 't':
 
485
                        case 'V':
 
486
                                if (action) {
 
487
                                        fprintf(stderr, "jscal: more than one action specified\n");
 
488
                                        exit(1);
 
489
                                } else {
 
490
                                        action = t;
 
491
                                        if ((parameter=optarg)) strcpy(parameter,optarg);
 
492
                                }
 
493
                                break;
 
494
                        case 'h':
 
495
                                help();
 
496
                                exit(0);
 
497
                        case 0:
 
498
                        case EOF:
 
499
                                break;
 
500
                        case ':':
 
501
                                fprintf(stderr, "jscal: missing parameter\n");
 
502
                                exit(1);
 
503
                        case '?':
 
504
                                fprintf(stderr, "jscal: unknown option\n");
 
505
                                exit(1);
 
506
                        default:
 
507
                                fprintf(stderr, "jscal: option parsing error\n");
 
508
                }
 
509
        } while (t != EOF);
 
510
 
 
511
        if (argc != optind + 1) {
 
512
                fprintf(stderr, "jscal: missing devicename\n");
 
513
                exit(1);
 
514
        }
 
515
 
 
516
        if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
 
517
                perror("jscal: can't open joystick device");
 
518
                exit(1);
 
519
        }
 
520
 
 
521
        if (ioctl(fd, JSIOCGVERSION, &version)) {
 
522
                perror("jscal: error getting version");
 
523
                exit(1);
 
524
        }
 
525
        if (version != JS_VERSION) {
 
526
                fprintf(stderr, "jscal: wrong version\n");
 
527
                print_version();
 
528
                exit(1);
 
529
        }
 
530
 
 
531
        switch (action) {
 
532
                case 0:
 
533
                        print_info();
 
534
                        break;
 
535
                case 'c':
 
536
                        print_info();
 
537
                        calibrate();
 
538
                        break;
 
539
                case 'p':
 
540
                        print_settings(argv[argc -1]);
 
541
                        break;
 
542
                case 's':
 
543
                        set_correction(parameter);
 
544
                        break;
 
545
                case 't':
 
546
                        test_center();
 
547
                        break;
 
548
                case 'V':
 
549
                        print_version();
 
550
                        break;
 
551
                default:
 
552
                        fprintf(stderr, "jscal: this cannot happen\n");
 
553
                        exit(1);
 
554
        }
 
555
 
 
556
        close(fd);
 
557
        return 0;
 
558
}