xbee: support rc_proto and joystick
[protos/xbee.git] / main.c
diff --git a/main.c b/main.c
index 52c0d80..94c0f34 100644 (file)
--- a/main.c
+++ b/main.c
@@ -41,6 +41,7 @@
 #include <time.h>
 #include <netinet/in.h>
 #include <sys/queue.h>
+#include <linux/joystick.h>
 
 #include <getopt.h>
 
@@ -57,6 +58,7 @@
 #include "xbee_buf.h"
 #include "xbee_proto.h"
 #include "xbee.h"
+#include "joystick.h"
 #include "main.h"
 
 #define TIMEOUT_US 1000000
@@ -75,6 +77,7 @@ struct xbee_dev *xbee_dev;
 
 /* events */
 static struct event stdin_read_event, xbee_read_event;
+static struct event joystick_read_event;
 
 static struct cmdline *xbee_cl;
 
@@ -85,6 +88,8 @@ int xbee_raw = 0;
 int xbee_hexdump = 0;
 int xbee_debug = 0;
 FILE *xbee_logfile;
+static char *joystick_devname = NULL;
+struct joystick_info joyinfo;
 
 void xbeeapp_log(int always_on_stdout, const char *fmt, ...)
 {
@@ -696,7 +701,7 @@ parse_args(int argc, char **argv)
 
        argvopt = argv;
 
-       while ((opt = getopt_long(argc, argvopt, "hd:s:",
+       while ((opt = getopt_long(argc, argvopt, "hd:s:j:",
                                  lgopts, &option_index)) != EOF) {
 
                switch (opt) {
@@ -718,6 +723,10 @@ parse_args(int argc, char **argv)
                        xbee_raw = 1;
                        break;
 
+               case 'j':
+                       joystick_devname = optarg;
+                       break;
+
                /* long options */
                case 0:
                        /* if (!strcmp(lgopts[option_index].name, "option")) */
@@ -764,6 +773,17 @@ int main(int argc, char **argv)
        if (err < 0)
                return -1;
 
+       /* init joystick */
+       if (joystick_devname != NULL) {
+               if (joystick_init(joystick_devname, &joyinfo) < 0) {
+                       fprintf(stderr, "error in joystick init\n");
+                       return -1;
+               }
+               event_set(&joystick_read_event, joyinfo.fd, EV_READ | EV_PERSIST,
+                         joystick_input, &joyinfo);
+               event_add(&joystick_read_event, NULL);
+       }
+
        /* open xbee device */
        xbee_dev = xbee_open(xbee_devname, xbee_baud);
        if (xbee_dev == NULL)