~ubuntu-branches/ubuntu/quantal/joystick/quantal

« back to all changes in this revision

Viewing changes to .pc/jitter-tolerance.patch/utils/jscal.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Kitt
  • Date: 2010-05-16 16:11:59 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100516161159-tf754mo83ojh1yqj
Tags: 20051019-11
evtest: flush standard output, thanks Florian Fainelli! Closes:
#581740.

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@ucw.cz>, or by paper mail:
 
30
 * Vojtech Pavlik, Simunkova 1594, 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
#include <stdlib.h>
 
43
 
 
44
#include <asm/param.h>
 
45
#include <linux/joystick.h>
 
46
 
 
47
#define PIT_HZ 1193180L
 
48
 
 
49
#define NUM_POS 3
 
50
#define MAX_AXES 16
 
51
#define MAX_CORR 1
 
52
 
 
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};
 
56
 
 
57
struct correction_data {
 
58
        int cmin[NUM_POS];
 
59
        int cmax[NUM_POS];
 
60
};
 
61
 
 
62
int fd;
 
63
struct js_corr corr[MAX_AXES];
 
64
char axes, buttons, fuzz;
 
65
int version;
 
66
struct correction_data corda[MAX_AXES];
 
67
 
 
68
struct js_info {
 
69
        int buttons;
 
70
        int axis[MAX_AXES];
 
71
        } js;
 
72
 
 
73
void print_position(int i, int a)
 
74
{
 
75
        printf("Axis %d: %8d\r", i, a);
 
76
        fflush(stdout);
 
77
 
 
78
}
 
79
 
 
80
int get_time(void)
 
81
{
 
82
        struct timeval tv;
 
83
 
 
84
        gettimeofday(&tv, NULL);
 
85
 
 
86
        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
87
}
 
88
 
 
89
void wait_for_event(int d, struct js_info *s)
 
90
{
 
91
        struct js_event ev;
 
92
        struct timeval tv;
 
93
        char buf;
 
94
        fd_set set;
 
95
 
 
96
        tv.tv_sec = 0;
 
97
        tv.tv_usec = 100000;
 
98
 
 
99
        FD_ZERO(&set);
 
100
        FD_SET(0, &set);
 
101
        FD_SET(d, &set);
 
102
 
 
103
        if (select(d+1, &set, NULL, NULL, &tv)) {
 
104
 
 
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) {
 
108
                                case JS_EVENT_AXIS:
 
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);
 
112
                        }
 
113
                }
 
114
 
 
115
                if (FD_ISSET(0, &set)) {
 
116
                        read(0, &buf, 1);
 
117
                        s->buttons |= (1 << 31);
 
118
                }
 
119
 
 
120
        } else {
 
121
                s->buttons &= ~(1 << 31);
 
122
        }
 
123
}
 
124
 
 
125
void putcs(char *s)
 
126
{
 
127
        int i;
 
128
        putchar('\r');
 
129
        for (i = 0; i < 78; i++) putchar(' ');
 
130
        putchar('\r');
 
131
        fputs(s, stdout);
 
132
        fflush(stdout);
 
133
}
 
134
 
 
135
int solve_broken(int *results, struct correction_data inputs)
 
136
{
 
137
        double a, b, c, d;
 
138
 
 
139
        a = inputs.cmin[1];
 
140
        b = inputs.cmax[1];
 
141
        c = 32767.0 / (inputs.cmin[1] - inputs.cmax[0]);
 
142
        d = 32767.0 / (inputs.cmin[2] - inputs.cmax[1]);
 
143
 
 
144
        results[0] = rint(a);
 
145
        results[1] = rint(b);
 
146
        results[2] = rint(c*16384.0);
 
147
        results[3] = rint(d*16384.0);
 
148
 
 
149
        return 1;
 
150
}
 
151
 
 
152
void help(void)
 
153
{
 
154
        putchar('\n');
 
155
        puts("Usage: jscal <device>");
 
156
        putchar('\n');
 
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");
 
166
        putchar('\n');
 
167
}
 
168
 
 
169
void print_info()
 
170
{
 
171
        int i,j;
 
172
 
 
173
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
174
                perror("jscal: error getting axes");
 
175
                exit(1);
 
176
        }
 
177
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
178
                perror("jscal: error getting buttons");
 
179
                exit(1);
 
180
        }
 
181
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
182
                perror("jscal: error getting correction");
 
183
                exit(1);
 
184
        }
 
185
 
 
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(',');
 
195
                        }
 
196
                putchar('\n');
 
197
                }
 
198
        }
 
199
        putchar('\n');
 
200
}
 
201
 
 
202
void calibrate()
 
203
{
 
204
        int i, j, t, b;
 
205
 
 
206
        for (i=0; i<MAX_AXES; i++) {
 
207
                corr[i].type = JS_CORR_NONE;
 
208
                corr[i].prec = 0;
 
209
        }
 
210
 
 
211
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
212
                perror("jscal: error setting correction");
 
213
                exit(1);
 
214
        }
 
215
 
 
216
        {
 
217
 
 
218
                int i;
 
219
                int amax[MAX_AXES], amin[MAX_AXES];
 
220
 
 
221
                puts("Calibrating precision: wait and don't touch the joystick.");
 
222
 
 
223
                wait_for_event(fd, &js);
 
224
                t = get_time();
 
225
                while (get_time() < t+50) wait_for_event(fd, &js);
 
226
 
 
227
                wait_for_event(fd, &js);
 
228
                t = get_time();
 
229
                for(i=0; i < axes; i++)
 
230
                        amin[i] = amax[i] = js.axis[i];
 
231
 
 
232
                do {
 
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]);
 
238
                        }
 
239
                        printf("\r");
 
240
                        fflush(stdout);
 
241
                } while (get_time() < t+2000);
 
242
 
 
243
                printf("Done. Precision is:                                             \n");
 
244
 
 
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);
 
248
                }
 
249
 
 
250
                puts("");
 
251
 
 
252
        }
 
253
 
 
254
 
 
255
        b = js.buttons;
 
256
 
 
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]);
 
261
 
 
262
                while (!(b ^ js.buttons)) {
 
263
                        print_position(j, js.axis[j]);
 
264
                        wait_for_event(fd, &js);
 
265
                }
 
266
 
 
267
                putcs("Hold ... ");
 
268
 
 
269
                corda[j].cmin[i] = js.axis[j];
 
270
                corda[j].cmax[i] = js.axis[j];
 
271
 
 
272
                t = get_time();
 
273
 
 
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);
 
278
                }
 
279
                puts("OK.");
 
280
        }
 
281
 
 
282
        puts("");
 
283
 
 
284
        for (j = 0; j < axes; j++) {
 
285
                solve_broken(corr[j].coef, corda[j]);
 
286
                corr[j].type = JS_CORR_BROKEN;
 
287
        }
 
288
 
 
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(',');
 
298
                        }
 
299
                putchar('\n');
 
300
                }
 
301
        }
 
302
 
 
303
        putchar('\n');
 
304
 
 
305
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
306
                perror("jscal: error setting correction");
 
307
                exit(1);
 
308
        }
 
309
}
 
310
 
 
311
void print_version()
 
312
{
 
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);
 
317
}
 
318
 
 
319
void print_settings(char *devicename)
 
320
{
 
321
        int i,j;
 
322
 
 
323
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
324
                perror("jscal: error getting axes");
 
325
                exit(1);
 
326
        }
 
327
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
328
                perror("jscal: error getting buttons");
 
329
                exit(1);
 
330
        }
 
331
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
332
                perror("jscal: error getting correction");
 
333
                exit(1);
 
334
        }
 
335
 
 
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]);
 
341
        }
 
342
        printf(" %s\n",devicename);
 
343
}
 
344
 
 
345
void set_correction(char *p)
 
346
{
 
347
        int i,j;
 
348
        int t = 0;
 
349
 
 
350
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
351
                perror("jscal: error getting axes");
 
352
                exit(1);
 
353
        }
 
354
 
 
355
        if (axes > MAX_AXES) axes = MAX_AXES;
 
356
 
 
357
        if (!p) {
 
358
                fprintf(stderr, "jscal: missing number of axes\n");
 
359
                exit(1);
 
360
        }
 
361
        sscanf(p, "%d", &t);
 
362
        p = strstr(p, ",");
 
363
 
 
364
        if (t != axes) {
 
365
                fprintf(stderr, "jscal: joystick has different number of axes (%d) than specified in command line (%d)\n", 
 
366
                        axes, t);
 
367
                exit(1);
 
368
        }
 
369
 
 
370
 
 
371
        for (i = 0; i < axes; i++) {
 
372
 
 
373
                if (!p) {
 
374
                        fprintf(stderr, "jscal: missing correction type for axis %d\n", i);
 
375
                        exit(1);
 
376
                }
 
377
                sscanf(++p, "%d", &t);
 
378
                p = strstr(p, ",");
 
379
 
 
380
 
 
381
                if (t > MAX_CORR) {
 
382
                        fprintf(stderr, "jscal: unknown correction type for axis %d\n", i);
 
383
                        exit(1);
 
384
                }
 
385
                corr[i].type = t;
 
386
 
 
387
                if (!p) {
 
388
                        fprintf(stderr, "jscal: missing precision for axis %d\n", i);
 
389
                        exit(1);
 
390
                }
 
391
                sscanf(++p, "%d", &t);
 
392
                p = strstr(p, ",");
 
393
 
 
394
                corr[i].prec = t;
 
395
 
 
396
                for(j = 0; j < corr_coef_num[corr[i].type]; j++) {
 
397
                        if (!p) {
 
398
                                fprintf(stderr, "jscal: missing coefficient %d for axis %d\n", j, i);
 
399
                                exit(1);
 
400
                        }
 
401
                        sscanf(++p, "%d", (int*) &corr[i].coef[j]);
 
402
                        p = strstr(p, ",");
 
403
                }
 
404
        }
 
405
 
 
406
        if (p) {
 
407
                fprintf(stderr, "jscal: too many values\n");
 
408
                exit(1);
 
409
        }
 
410
        
 
411
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
412
                perror("jscal: error setting correction");
 
413
                exit(1);
 
414
        }
 
415
}
 
416
 
 
417
void test_center()
 
418
{
 
419
        int i;
 
420
        struct js_event ev;
 
421
 
 
422
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
423
                perror("jscal: error getting axes");
 
424
                exit(1);
 
425
        }
 
426
 
 
427
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
428
                perror("jscal: error getting buttons");
 
429
                exit(1);
 
430
        }
 
431
 
 
432
        if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
 
433
                perror("jscal: cannot set nonblocking mode");
 
434
                exit(1);
 
435
        }
 
436
 
 
437
        while (read(fd, &ev, sizeof(struct js_event)) == sizeof(struct js_event)) {
 
438
                switch (ev.type & ~JS_EVENT_INIT) {
 
439
                        case JS_EVENT_AXIS:
 
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);
 
443
                }
 
444
        }
 
445
 
 
446
        for (i = 0; i < axes; i++) if (js.axis[i]) {
 
447
                fprintf(stderr, "jscal: axes not calibrated\n");
 
448
                exit(2);
 
449
        }
 
450
        if (js.buttons) {
 
451
                fprintf(stderr, "jscal: buttons pressed\n");
 
452
                exit(3);
 
453
        }
 
454
}
 
455
 
 
456
int action = 0;
 
457
 
 
458
int main(int argc, char **argv)
 
459
{
 
460
        int option_index = 0;
 
461
        char *parameter = NULL;
 
462
        int t;
 
463
 
 
464
        static struct option long_options[] =
 
465
        {
 
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'}
 
472
        };
 
473
 
 
474
        if (argc == 1) {
 
475
                help();
 
476
                exit(1);
 
477
        }
 
478
 
 
479
        do {
 
480
                t = getopt_long(argc, argv, "chps:vVt", long_options, &option_index);
 
481
                switch (t) {
 
482
                        case 'p':
 
483
                        case 's':
 
484
                        case 'c':
 
485
                        case 't':
 
486
                        case 'V':
 
487
                                if (action) {
 
488
                                        fprintf(stderr, "jscal: more than one action specified\n");
 
489
                                        exit(1);
 
490
                                } else {
 
491
                                        action = t;
 
492
                                        if ((parameter=optarg)) strcpy(parameter,optarg);
 
493
                                }
 
494
                                break;
 
495
                        case 'h':
 
496
                                help();
 
497
                                exit(0);
 
498
                        case 0:
 
499
                        case EOF:
 
500
                                break;
 
501
                        case ':':
 
502
                                fprintf(stderr, "jscal: missing parameter\n");
 
503
                                exit(1);
 
504
                        case '?':
 
505
                                fprintf(stderr, "jscal: unknown option\n");
 
506
                                exit(1);
 
507
                        default:
 
508
                                fprintf(stderr, "jscal: option parsing error\n");
 
509
                }
 
510
        } while (t != EOF);
 
511
 
 
512
        if (argc != optind + 1) {
 
513
                fprintf(stderr, "jscal: missing devicename\n");
 
514
                exit(1);
 
515
        }
 
516
 
 
517
        if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
 
518
                perror("jscal: can't open joystick device");
 
519
                exit(1);
 
520
        }
 
521
 
 
522
        if (ioctl(fd, JSIOCGVERSION, &version)) {
 
523
                perror("jscal: error getting version");
 
524
                exit(1);
 
525
        }
 
526
        if (version != JS_VERSION) {
 
527
                fprintf(stderr, "jscal: wrong version\n");
 
528
                print_version();
 
529
                exit(1);
 
530
        }
 
531
 
 
532
        switch (action) {
 
533
                case 0:
 
534
                        print_info();
 
535
                        break;
 
536
                case 'c':
 
537
                        print_info();
 
538
                        calibrate();
 
539
                        break;
 
540
                case 'p':
 
541
                        print_settings(argv[argc -1]);
 
542
                        break;
 
543
                case 's':
 
544
                        set_correction(parameter);
 
545
                        break;
 
546
                case 't':
 
547
                        test_center();
 
548
                        break;
 
549
                case 'V':
 
550
                        print_version();
 
551
                        break;
 
552
                default:
 
553
                        fprintf(stderr, "jscal: this cannot happen\n");
 
554
                        exit(1);
 
555
        }
 
556
 
 
557
        close(fd);
 
558
        return 0;
 
559
}