controller.c 867 Bytes
#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;
	}
    }
}