From 03bdd5ab88a174bf651af8b421a1bc2965c6a0c0 Mon Sep 17 00:00:00 2001 From: tth Date: Fri, 2 Aug 2019 18:04:22 +0200 Subject: [PATCH] adding 'joystick->osc' soft --- .gitignore | 1 + Makefile | 3 + doc/gadgets-osc.tex | 12 ++++ osc-joy.c | 143 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 osc-joy.c diff --git a/.gitignore b/.gitignore index 8bec7c0..1744ede 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ MANIFEST tarball osc2cursor +osc-joy doc/*.toc doc/*.log diff --git a/Makefile b/Makefile index bde1dcd..9ec3ca2 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,7 @@ OPTS = -Wall -g -DDEBUG_LEVEL=1 osc2cursor: osc2cursor.c ${DEPS} functions/libpocosc.a gcc ${OPTS} $< functions/libpocosc.a -llo -lcurses -o $@ +osc-joy: osc-joy.c ${DEPS} functions/libpocosc.a + gcc ${OPTS} $< functions/libpocosc.a -llo -o $@ + # ---------------------------------------------------- diff --git a/doc/gadgets-osc.tex b/doc/gadgets-osc.tex index 3df3ba0..0e08d2c 100644 --- a/doc/gadgets-osc.tex +++ b/doc/gadgets-osc.tex @@ -31,6 +31,18 @@ De quoi parle-t-on exactement ? % ------------------------------------------------------------------- +\subsection{osc-joy} + +\begin{verbatim} + * joystick -> osc Aug 2 2019 * + -r remote host (localhost) + -p remote UDP port (9000) + -j joystick device (/dev/input/js0) + -v increase verbosity + -o offset added to button number + -I change text id ("suck my stick") +\end{verbatim} + \pagebreak \tableofcontents \printindex diff --git a/osc-joy.c b/osc-joy.c new file mode 100644 index 0000000..b67d10b --- /dev/null +++ b/osc-joy.c @@ -0,0 +1,143 @@ +/* + * Pour compiler : + * gcc -Wall osc-joy.c -llo -o osc-joy + * + * Options : + * -j joydevice default /dev/input/js0 + * -h destination (or IP addr) default localhost + * -p destport default is 9000 + * -o nnn offset for button number + */ + + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "functions/senders.h" + +/* default values, can be changed on command line */ + +#define REMOTE_HOST "localhost" /* just loling */ +#define REMOTE_PORT "9000" +#define JOY_DEVICE "/dev/input/js0" +#define MY_TEXT_ID "suck my stick" + +/* ----------------------------------------------------------------- */ +int verbosity = 0; +int button_offset = 0; + +char *my_id = MY_TEXT_ID; + +/* ----------------------------------------------------------------- */ +static void help(int k) +{ +puts("\t * joystick -> osc "__DATE__" *"); +puts("\t-r\tremote host ("REMOTE_HOST")"); +puts("\t-p\tremote UDP port ("REMOTE_PORT")"); +puts("\t-j\tjoystick device ("JOY_DEVICE")"); +puts("\t-v\tincrease verbosity"); +puts("\t-o\toffset added to button number"); +puts("\t-I\tchange text id (\""MY_TEXT_ID"\")"); +exit(0); +} +/* ----------------------------------------------------------------- */ +int main(int argc, char *argv[]) +{ +int foo, joy_fd; +struct js_event js; +int x_pos, y_pos, flag; +char joy_name[128]; +int opt; +char *remote_host = REMOTE_HOST; +char *remote_port = REMOTE_PORT; +char *joy_device = JOY_DEVICE; + +/* parsing command line options */ +while ((opt = getopt(argc, argv, "hp:r:vj:o:I:")) != -1) { + switch (opt) { + case 'h': help(0); break; + case 'r': remote_host = optarg; break; + case 'p': remote_port = optarg; break; + case 'j': joy_device = optarg; break; + case 'v': verbosity++; break; + case 'o': button_offset = atoi(optarg); + break; + case 'I': my_id = optarg; break; + default: exit(1); break; + } + } + +if (verbosity) { + fprintf(stderr, "%s is sending to %s:%s\n", argv[0], + remote_host, remote_port); + fprintf(stderr, " the stick '%s'is on %s\n", my_id, joy_device); + } + +lo_address t = lo_address_new(remote_host, remote_port); + +if( ( joy_fd = open(joy_device , O_RDONLY)) == -1 ) { + fprintf(stderr, "Couldn't open %s\n", JOY_DEVICE); + exit(1); + } + +if (verbosity) { + if (ioctl(joy_fd, JSIOCGNAME(sizeof(joy_name)), joy_name) < 0) + strncpy(joy_name, "Unknown", sizeof(joy_name)); + fprintf(stderr, "Name: %s\n", joy_name); + } + +send_data_id(t, my_id); + +x_pos = y_pos = 0; +for (;;) { + foo = read(joy_fd, &js, sizeof(struct js_event)); + if (8 != foo) { + fprintf(stderr, "err reading joy\n"); + exit(1); + } + + /* calibration datas ignored */ + if (js.type > 128) continue; + + if (verbosity>1) { + fprintf(stderr, "%10u %2d %2d %7d\n", + js.time, js.type, js.number, js.value); + } + + if (2==js.type) { /* oscillating stick */ + flag = 0; + switch (js.number) + { + case 0: + x_pos = js.value; + flag = 1; + break; + case 1: + y_pos = js.value; + flag = 1; + break; + } + + if (flag) { + foo = send_data_xy(t, x_pos, y_pos); + flag = 0; + } + } + + if (1==js.type) { /* it's a button */ + foo = send_data_button(t, js.number+button_offset, + js.value); + } + + } + +return 0; +} +/* ----------------------------------------------------------------- */