~alexandre-hardy/linuxjoymap/linuxjoymap

« back to all changes in this revision

Viewing changes to tools/jscal.c

  • Committer: Alexandre Hardy
  • Date: 2009-07-17 09:20:57 UTC
  • Revision ID: ah@zenwalk-20090717092057-oxa4o16isawqa7ue
Initial creation of project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <unistd.h>
 
2
#include <fcntl.h>
 
3
#include <sys/ioctl.h>
 
4
#include <linux/joystick.h>
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
 
 
8
void print_data(int aval[], int bval[], int axes, int btns) {
 
9
        int i;
 
10
        for (i=0; i<btns; i++) {
 
11
                printf("%2d |", bval[i]);
 
12
        }
 
13
        for (i=0; i<axes; i++) {
 
14
                printf(" %5d |", aval[i]);
 
15
        }
 
16
        printf("    \r");
 
17
        fflush(stdout);
 
18
}
 
19
 
 
20
void print_data_hdr(int aval[], int bval[], int axes, int btns) {
 
21
        int i;
 
22
        printf("js0: axes=%d, buttons=%d\n", axes, btns);
 
23
        for (i=0; i<btns; i++) {
 
24
                printf("b%02d|", i);
 
25
        }
 
26
        for (i=0; i<axes; i++) {
 
27
                printf("  a%02d  |", i, aval[i]);
 
28
        }
 
29
        printf("\n");
 
30
        for (i=0; i<btns; i++) {
 
31
                printf("____");
 
32
        }
 
33
        for (i=0; i<axes; i++) {
 
34
                printf("________");
 
35
        }
 
36
        printf("\n");
 
37
}
 
38
 
 
39
int main(int argc, char *argv[]) {
 
40
        int fd,i;
 
41
        int aval[256];
 
42
        int bval[256];
 
43
        int min[256];
 
44
        int max[256];
 
45
        int dmin[256];
 
46
        int dmax[256];
 
47
        int count=0;
 
48
        int nobutton=0;
 
49
        unsigned char axes, btns;
 
50
        struct js_event ev;
 
51
        struct js_corr cor[64];
 
52
        if (argc>=2)
 
53
                fd=open(argv[1], O_RDONLY|O_NONBLOCK);
 
54
        else
 
55
                fd=open("/dev/input/js0", O_RDONLY|O_NONBLOCK);
 
56
        if (fd<0) {     
 
57
                perror("Failed to open device");
 
58
                return 1;
 
59
        }
 
60
        ioctl(fd, JSIOCGAXES, &axes);
 
61
        ioctl(fd, JSIOCGBUTTONS, &btns);
 
62
        for (i=0; i<64; i++) {
 
63
                cor[i].type=JS_CORR_NONE;
 
64
                cor[i].coef[0]=128;
 
65
                cor[i].coef[1]=128;
 
66
                cor[i].coef[2]=65536*64;
 
67
                cor[i].coef[3]=65536*64;
 
68
        }
 
69
        ioctl(fd, JSIOCSCORR, cor);
 
70
 
 
71
        printf("Leave all axes centred, press a button when ready\n");
 
72
        print_data_hdr(aval, bval, axes, btns);
 
73
 
 
74
        for (i=0; i<axes; i++) aval[i]=128;
 
75
        for (i=0; i<axes; i++) bval[i]=0;
 
76
        //get initial values;
 
77
        nobutton=0;
 
78
        while (nobutton==0) {
 
79
                while (read(fd, &ev, sizeof(ev))==sizeof(ev)) {
 
80
                        if (ev.type&JS_EVENT_BUTTON)
 
81
                                bval[ev.number]=ev.value;
 
82
                        if (ev.type&JS_EVENT_AXIS)
 
83
                                aval[ev.number]=ev.value;
 
84
                }
 
85
                print_data(aval, bval, axes, btns);
 
86
                nobutton=0;
 
87
                for (i=0; i<btns; i++) nobutton+=bval[i];
 
88
        }
 
89
        //set initial deadzone
 
90
        for (i=0; i<axes; i++) dmin[i]=dmax[i]=aval[i];
 
91
        nobutton=0;
 
92
        
 
93
        //wait five seconds for scan of deadzone
 
94
        count=0;
 
95
        while (count<500) {
 
96
                while (read(fd, &ev, sizeof(ev))==sizeof(ev)) {
 
97
                        if (ev.type&JS_EVENT_BUTTON)
 
98
                                bval[ev.number]=ev.value;
 
99
                        if (ev.type&JS_EVENT_AXIS) {
 
100
                                aval[ev.number]=ev.value;
 
101
                                if (aval[ev.number]<dmin[ev.number])
 
102
                                        dmin[ev.number]=aval[ev.number];
 
103
                                if (aval[ev.number]>dmax[ev.number])
 
104
                                        dmax[ev.number]=aval[ev.number];
 
105
                        }
 
106
                }
 
107
                print_data(aval, bval, axes, btns);
 
108
                usleep(1000);
 
109
                count++;
 
110
        }
 
111
 
 
112
 
 
113
        for (i=0; i<axes; i++) min[i]=max[i]=aval[i];
 
114
        printf("\n\n");
 
115
        printf("Move axes through complete range of motion.\n");
 
116
        printf("Press a button when complete\n");
 
117
        print_data_hdr(aval, bval, axes, btns);
 
118
        while (nobutton==0) {
 
119
                while (read(fd, &ev, sizeof(ev))==sizeof(ev)) {
 
120
                        if (ev.type&JS_EVENT_BUTTON)
 
121
                                bval[ev.number]=ev.value;
 
122
                        if (ev.type&JS_EVENT_AXIS) {
 
123
                                aval[ev.number]=ev.value;
 
124
                                if (aval[ev.number]<min[ev.number])
 
125
                                        min[ev.number]=aval[ev.number];
 
126
                                if (aval[ev.number]>max[ev.number])
 
127
                                        max[ev.number]=aval[ev.number];
 
128
                        }
 
129
                }
 
130
                print_data(aval, bval, axes, btns);
 
131
                nobutton=0;
 
132
                for (i=0; i<btns; i++) nobutton+=bval[i];
 
133
        }
 
134
        printf("\n");
 
135
        for (i=0; i<axes; i++) {
 
136
                cor[i].type=JS_CORR_BROKEN;
 
137
                printf("%d: %d %d %d %d\n", i, dmin[i], dmax[i], min[i], max[i]);
 
138
                if (max[i]==dmax[i]) dmax[i]=max[i]-1;
 
139
                if (min[i]==dmin[i]) dmin[i]=min[i]+1;
 
140
                cor[i].coef[0]=dmin[i];
 
141
                cor[i].coef[1]=dmax[i];
 
142
                cor[i].coef[2]=32768*16384/(dmin[i]-min[i]);
 
143
                cor[i].coef[3]=32768*16384/(max[i]-dmax[i]);
 
144
        }
 
145
        ioctl(fd, JSIOCSCORR, cor);
 
146
        return 0;
 
147
}