Добавлен модуль ядра и приложение для работы с трехосевыми магнитометрами.
[lede-packages.git] / 3-axis / src / 3-axis.c
1 #include <stdio.h>
2 #include <time.h>
3 #include <sys/times.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <stdint.h>
8 #include <assert.h>
9 #include <linux/input.h>
10
11 int read_all(int fd, char *buf, int count)
12 {
13     int n_read = 0;
14     while (n_read != count) {
15         int result = read(fd, buf + n_read, count - n_read);
16         if (result < 0)
17             return result;
18         else if (result == 0)
19             return n_read;
20         n_read += result;
21     }
22     return n_read;
23 }
24
25 int main(int argc, char *argv[])
26 {
27     int fd;
28     struct input_event ev;
29     __s32 x_value,
30         y_value,
31         z_value;
32     __u8 x_got=0,
33         y_got=0,
34         z_got=0;    
35     int timeout=10;
36     int start;
37
38     assert(16 == sizeof(struct input_event));
39
40     if (argc != 2) {
41         fprintf(stderr, "missing /dev/input/XXX\n");
42         return 1;
43     }
44
45     start=time(0);
46
47     if ((fd = open(argv[1], O_RDONLY)) == -1) {
48         perror("open");
49         return 1;
50     }
51
52     while (! (x_got && y_got && z_got)) {
53
54         int ret = read(fd, (char *) &ev, sizeof(struct input_event));
55         if (ret != sizeof(struct input_event)) {
56             fprintf(stderr, "ret == %d\n", ret);
57             perror("read");
58             return 1;
59         }
60         if (ev.type==3 && ev.code==16) {
61          x_value=ev.value;
62          x_got=1;
63         }
64         if (ev.type==3 && ev.code==17) {
65           y_value=ev.value;
66           y_got=1;
67         }
68         if (ev.type==3 && ev.code==10) {
69           z_value=ev.value;
70           z_got=1;
71         }
72         if (time(0) > start+timeout) break;
73     }
74
75     close(fd);
76
77     if (x_got && y_got && z_got) {
78       printf("%d %d %d\n",x_value,y_value,z_value);
79     } else {
80       fprintf(stderr, "data not aquired until timeout");
81       perror("timeout");
82       return 1;
83     }
84
85     return 0;
86 }