~alexandre-hardy/linuxjoymap/linuxjoymap

« back to all changes in this revision

Viewing changes to tools/jsset16.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
                min[i]=max[i]=0;
 
69
        }
 
70
        ioctl(fd, JSIOCSCORR, cor);
 
71
        while (read(fd, &ev, sizeof(ev))==sizeof(ev)) {
 
72
                if (ev.type&JS_EVENT_BUTTON)
 
73
                        bval[ev.number]=ev.value;
 
74
                if (ev.type&JS_EVENT_AXIS) {
 
75
                        aval[ev.number]=ev.value;
 
76
                        if (ev.value<min[ev.number])
 
77
                                min[ev.number]=ev.value;        
 
78
                        if (ev.value>max[ev.number])
 
79
                                max[ev.number]=ev.value;        
 
80
                }
 
81
        }
 
82
        usleep(100000);
 
83
        count=0;
 
84
        while (count++<100) {
 
85
        while (read(fd, &ev, sizeof(ev))==sizeof(ev)) {
 
86
                        if (ev.type&JS_EVENT_BUTTON)
 
87
                                bval[ev.number]=ev.value;
 
88
                        if (ev.type&JS_EVENT_AXIS) {
 
89
                                aval[ev.number]=ev.value;
 
90
                                if (ev.value<min[ev.number])
 
91
                                        min[ev.number]=ev.value;        
 
92
                                if (ev.value>max[ev.number])
 
93
                                        max[ev.number]=ev.value;        
 
94
                        }
 
95
                }
 
96
        
 
97
                usleep(10000);
 
98
        }
 
99
 
 
100
        for (i=0; i<axes; i++) {
 
101
                cor[i].type=JS_CORR_BROKEN;
 
102
                cor[i].coef[0]=128;
 
103
                cor[i].coef[1]=128;
 
104
                cor[i].coef[2]=32768*16384/128;
 
105
                cor[i].coef[3]=32768*16384/128;
 
106
                if ((min[i]==max[i])&&(max[i]==0)) {
 
107
                        //assume a switching hat
 
108
                        cor[i].coef[0]=0;
 
109
                        cor[i].coef[1]=0;
 
110
                        cor[i].coef[2]=32768*16384/1;
 
111
                        cor[i].coef[3]=32768*16384/1;
 
112
                }
 
113
        }
 
114
        ioctl(fd, JSIOCSCORR, cor);
 
115
        return 0;
 
116
}