controller.c
867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <em.h>
#define PORT 2
int
main(int argc, char *argv[])
{
int cur_stat, always;
GamePad cur_pad, old_pad;
int port;
if (argc == 2) {
port = atoi(argv[1]);
} else {
port = PORT;
}
if (emInitController(port) < 0) {
fprintf(stderr, "Cannot open port %d\n", port);
perror("Error");
exit (1);
}
always = 0;
printf("Controller initialized on port /dev/ttyd%d\n", port);
while (1) {
cur_stat = emGetController(port, &cur_pad);
if (always || cur_pad.button != old_pad.button ||
cur_pad.stick_x != old_pad.stick_x ||
cur_pad.stick_y != old_pad.stick_y) {
printf("controller: %2d 0x%04x %3d %3d\n", cur_stat,
cur_pad.button, cur_pad.stick_x, cur_pad.stick_y);
old_pad = cur_pad;
}
}
}