From 36f712dc98dc8cc52e4c0093744e70671e3fa878 Mon Sep 17 00:00:00 2001 From: tth Date: Fri, 30 Oct 2020 22:21:20 +0100 Subject: [PATCH] asyncburp: boilerplate --- .gitignore | 1 + specific/Makefile | 16 +++++++- specific/asyncburp.c | 96 +++++++++++++++++++++++++++++++++++++++++++ specific/burpmsg.c | 95 ++++++++++++++++++++++++++++++++++++++++++ specific/burpmsg.h | 30 ++++++++++++++ specific/laserblast.c | 7 ++-- 6 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 specific/asyncburp.c create mode 100644 specific/burpmsg.c create mode 100644 specific/burpmsg.h diff --git a/.gitignore b/.gitignore index 13c19c7..38a3630 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,6 @@ tools/udp-dumper tools/*.o specific/joy2laser +specific/asyncburp specific/*.o diff --git a/specific/Makefile b/specific/Makefile index 01d215d..c43b291 100644 --- a/specific/Makefile +++ b/specific/Makefile @@ -2,8 +2,9 @@ OPTS = -Wall -g -DDEBUG_LEVEL=0 +all: joy2laser asyncburp -all: joy2laser +# -------------------------------------------- laserblast.o: laserblast.c Makefile gcc -c ${OPTS} $< @@ -11,4 +12,17 @@ laserblast.o: laserblast.c Makefile joy2laser: joy2laser.c Makefile laserblast.o gcc ${OPTS} $< laserblast.o ../functions/libpocosc.a -llo -o $@ +# -------------------------------------------- + +asyncburp.o: asyncburp.c burpmsg.h Makefile + gcc -c ${OPTS} $< + +burpmsg.o: burpmsg.c burpmsg.h Makefile + gcc -c ${OPTS} $< + +asyncburp: asyncburp.o burpmsg.o + gcc ${OPTS} $< burpmsg.o -llo -o $@ + +# -------------------------------------------- + diff --git a/specific/asyncburp.c b/specific/asyncburp.c new file mode 100644 index 0000000..ac31dcc --- /dev/null +++ b/specific/asyncburp.c @@ -0,0 +1,96 @@ +/* + * Joystick to Laser + * async version, with continuous refresh + * post Sonoptic 2020 + */ + +#include +#include +#include + +#include /* OSC library */ + +#include "laserblast.h" + +#include "burpmsg.h" + +/* ------------------------------------------------------------------- */ + +#define LOCAL_PORT "9000" +#define REMOTE_HOST "localhost" /* just loling */ +#define REMOTE_PORT "9999" + +/* ------------------------------------------------------------------- */ + +int verbosity; /* global variable */ + +/* ------------------------------------------------------------------- */ +static int bloub(void) +{ +BurpMsg message; + + +message.magic = BURP_MAGIC; + +display_burp_msg(&message, NULL); + +return 0; +} +/* ------------------------------------------------------------------- */ +static int help(int krkrkr) +{ +puts("HELP ME !"); +puts("\t-p NNN\t\tlocal listening port"); +puts("\t-R a.b.c.d\tremote host"); +puts("\t-P NNN\t\tremote port"); +puts("\t-L N\t\tlaser number"); +puts("\t-S N\t\tscene number"); +puts("\t-v\t\tincrease verbosity"); +return 1; +} +/* ------------------------------------------------------------------- */ + +int main(int argc, char *argv[]) +{ +char *local_port = LOCAL_PORT; +char *remote_host = REMOTE_HOST; +char *remote_port = REMOTE_PORT; + +int lasernumber, scenenumber; + +int opt; + +fprintf(stderr, "%s compiled %s at %s\n",argv[0], __DATE__, __TIME__); + +/* set some default values */ + +lasernumber = scenenumber = 0; + +/* parsing command line options */ +while ((opt = getopt(argc, argv, "hp:vL:S:R:P:")) != -1) { + switch (opt) { + case 'h': if (help(0)) exit(1); break; + case 'p': local_port = optarg; break; + case 'R': remote_host = optarg; break; + case 'P': remote_port = optarg; break; + case 'L': lasernumber = atoi(optarg); break; + case 'S': scenenumber = atoi(optarg); break; + case 'v': verbosity++; break; + } + } + +if (verbosity) { + fprintf(stderr, "-------------: %s\n", argv[0]); + fprintf(stderr, "pid : %d\n", getpid()); + fprintf(stderr, "local port : %s\n", local_port); + fprintf(stderr, "remote : %s %s\n", remote_host, remote_port); + fprintf(stderr, "scn/laser : %d %d\n", scenenumber, lasernumber); + } + + bloub(); + +return 2; +} +/* ------------------------------------------------------------------- */ + + diff --git a/specific/burpmsg.c b/specific/burpmsg.c new file mode 100644 index 0000000..d82c921 --- /dev/null +++ b/specific/burpmsg.c @@ -0,0 +1,95 @@ +/* + * BURPMSG.C + */ + +#include +#include +#include +#include + + +#include "burpmsg.h" + +/* ------------------------------------------------------------------- */ +int display_burp_msg(BurpMsg *header, void *data) +{ +char *txt_type; + +if (BURP_MAGIC != header->magic) { + fprintf(stderr, "burpmsg at %p : invalid magic %08X\n", + header, header->magic); + return -1; + } + +txt_type = "wtf?"; /* nice default value */ + +switch(header->msgtype) { + + case BURP_RESET: + txt_type = "reset"; + break; + case BURP_SETSIZE: + txt_type = "setsize"; + break; + case BURP_LASER: + txt_type = "laser"; + break; + case BURP_SCENE: + txt_type = "scene"; + break; + + default: + fprintf(stderr, "burpmsg at %p : invalid msgtype %d\n", + header, header->msgtype); + return -1; + } + +fprintf(stderr, "%4d %12s %9d %3d\n", + header->msgtype, txt_type, header->serial, header->szdata); + +return 0; +} +/* ------------------------------------------------------------------- */ +void la_grande_boucle( int fromfifo ) +{ +struct timeval tv; +fd_set fds; +int resval, foo; +BurpMsg message; + +for(;;) { /* start of infinite loop */ + + FD_ZERO(&fds); + FD_SET(fromfifo, &fds); + tv.tv_sec = 1; + tv.tv_usec = 0; + + resval = select(1, &fds, NULL, NULL, &tv); + + if (-1 == resval) { + perror("select()"); + /* ABORTING HERE ? */ + continue; + } + + if (resval) { + /* message available */ + foo = read(fromfifo, &message, sizeof(BurpMsg)); + if (sizeof(BurpMsg) != foo) { + fprintf(stderr, "err: %d read, %lu expected\n", + foo, sizeof(BurpMsg)); + } + display_burp_msg(&message, NULL); + } + else { + /* got a time out */ + fprintf(stderr, "TIMEOUT\n"); + } + + } + +/* NOT REACHED */ + +} +/* ------------------------------------------------------------------- */ + diff --git a/specific/burpmsg.h b/specific/burpmsg.h new file mode 100644 index 0000000..7e6fb30 --- /dev/null +++ b/specific/burpmsg.h @@ -0,0 +1,30 @@ +/* + * BURPMSG.H + */ + + +/* ------------------------------------------------------------------- */ + +typedef struct { + uint32_t magic; + uint32_t serial; + uint16_t msgtype; + uint16_t szdata; + } BurpMsg; + +#define BURP_MAGIC 0x98769876 + +/* message type */ +#define BURP_RESET 1 +#define BURP_SETSIZE 2 +#define BURP_LASER 3 +#define BURP_SCENE 4 +#define BURP_COLOR 5 + +/* ------------------------------------------------------------------- */ + +int display_burp_msg(BurpMsg *header, void *data); + +/* ------------------------------------------------------------------- */ + + diff --git a/specific/laserblast.c b/specific/laserblast.c index 6775d33..4e2e1c3 100644 --- a/specific/laserblast.c +++ b/specific/laserblast.c @@ -24,6 +24,9 @@ static int x_scale, y_scale; extern int verbosity; /* ------------------------------------------------------------------- */ +/* + * - prepare OSC transmission + */ int blast_init(char *host, char *port, int scene, int laser) { #if DEBUG_LEVEL @@ -31,7 +34,6 @@ fprintf(stderr, ">>> %s ( '%s:%p' %d %d )\n", __func__, host, port, scene, laser); #endif -/* prepare OSC transmission */ lo_addr = lo_address_new(host, port); if (verbosity) { fprintf(stderr, "%s is sending to %s:%s\n", __FILE__, host, port); @@ -118,6 +120,3 @@ return 0; /* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */ - - -