From 1052134571f0ccf32091d7ca4889a67e9b4399e5 Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Fri, 14 Oct 2016 01:46:47 +0200 Subject: Add osc support --- cethcan/Makefile | 5 +++-- cethcan/cethcan.h | 7 +++++++ cethcan/light.c | 25 +++++++++++++++++++++++++ cethcan/main.c | 2 ++ cethcan/osc.c | 29 +++++++++++++++++++++++++++++ cethcan/ttydmx.c | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 cethcan/osc.c diff --git a/cethcan/Makefile b/cethcan/Makefile index 933dd77..b2bc6b6 100644 --- a/cethcan/Makefile +++ b/cethcan/Makefile @@ -3,10 +3,11 @@ love: cethcan PKGS="libevent jansson" L_CFLAGS=-g -O0 -pthread -Wall -Wextra -Wshadow -pedantic -Wno-unused-parameter -Wno-format -std=gnu11 `pkg-config --cflags $(PKGS)` $(CFLAGS) -L_LDFLAGS=-g -pthread `pkg-config --libs $(PKGS)` -lcrypto $(LDFLAGS) +L_LDFLAGS=-g -pthread `pkg-config --libs $(PKGS)` -lcrypto -lcosc $(LDFLAGS) cethcan: main.o can.o ether.o light.o beanctr.o \ - http.o socketcan.o jsonrpc.o rpc.o espnet.o ttydmx.o + http.o socketcan.o jsonrpc.o rpc.o espnet.o ttydmx.o \ + osc.o gcc $(L_LDFLAGS) -o $@ $^ clean: diff --git a/cethcan/cethcan.h b/cethcan/cethcan.h index b272766..f07ea9a 100644 --- a/cethcan/cethcan.h +++ b/cethcan/cethcan.h @@ -25,6 +25,10 @@ #include #include +#include +#include +#include + #include "protocol.h" extern int verbosity; @@ -113,4 +117,7 @@ extern int espnet_init_conf(json_t *config); extern int ttydmx_init_conf(json_t *config); extern void http_init(void); +extern struct osc_server *osc_server; +extern void osc_init(void); + #endif /* _CETHCAN_H */ diff --git a/cethcan/light.c b/cethcan/light.c index e3b20a9..0adf28a 100644 --- a/cethcan/light.c +++ b/cethcan/light.c @@ -48,6 +48,26 @@ int light_set(struct light *l, unsigned value) return 0; } +static void light_osc_set(void *arg, struct osc_element *e) +{ + struct light *l = arg; + unsigned value; + + if (!e) + return; + if (e->type == OSC_INT32) + value = ((struct osc_int32*)e)->value; + else if (e->type == OSC_FLOAT32) + value = 255 * ((struct osc_float32*)e)->value; + else + return; + + if (value > 255) + value = 255; + + light_set(l, value); +} + unsigned light_getset(struct light *l) { if (l->aggregate) { @@ -197,6 +217,11 @@ int light_init_conf(json_t *config) l->u = can_register_alloc(l, light_can_handler, "light[%s]", l->name); l->u->json = light_json_handler; + char buf[1024]; + snprintf(buf, sizeof(buf), "/lights/%s/value", l->name); + lprintf("Adding light osc endpoint %s", buf); + osc_server_add_method(osc_server, buf, light_osc_set, l); + *plights = l; plights = &l->next; return 0; diff --git a/cethcan/main.c b/cethcan/main.c index 8d38757..b9c6daf 100644 --- a/cethcan/main.c +++ b/cethcan/main.c @@ -44,6 +44,8 @@ int main(int argc, char **argv) ev_base = event_base_new(); + osc_init(); + can_init(); json_t *ethercfg = json_object_get(config, "ethernet"); diff --git a/cethcan/osc.c b/cethcan/osc.c new file mode 100644 index 0000000..cd957f0 --- /dev/null +++ b/cethcan/osc.c @@ -0,0 +1,29 @@ +#include "cethcan.h" + +struct osc_server *osc_server = NULL; +static struct event *osc_server_event = NULL; + +static void osc_sock_handler(int sock, short event, void *arg) +{ + if (osc_server_run(osc_server)) { + event_del(osc_server_event); + osc_server_event = NULL; + } +} + +void osc_init(void) +{ + if (osc_server) + return; + + osc_server = osc_server_new(NULL, "4223", NULL); + if (!osc_server) + return; + + if (osc_server_set_blocking(osc_server, false)) + return; + + osc_server_event = event_new(ev_base, osc_server_fd(osc_server), + EV_READ | EV_PERSIST, osc_sock_handler, NULL); + event_add(osc_server_event, NULL); +} diff --git a/cethcan/ttydmx.c b/cethcan/ttydmx.c index 05469c1..45d8b5b 100644 --- a/cethcan/ttydmx.c +++ b/cethcan/ttydmx.c @@ -76,6 +76,33 @@ int ttydmx_set(struct ttydmx_device *dev, unsigned r, unsigned g, unsigned b) return 0; } +static void ttydmx_osc_set(void *arg, struct osc_element *e) +{ + struct ttydmx_device *dev = arg; + unsigned r,g,b; + + if (!e || !e->next || !e->next->next) + return; + + if (e->type == OSC_INT32 + && e->next->type == OSC_INT32 + && e->next->next->type == OSC_INT32) { + r = ((struct osc_int32*)e)->value; + g = ((struct osc_int32*)e->next)->value; + b = ((struct osc_int32*)e->next->next)->value; + } else if (e->type == OSC_FLOAT32 + && e->next->type == OSC_FLOAT32 + && e->next->next->type == OSC_FLOAT32) { + r = 255 * ((struct osc_float32*)e)->value; + g = 255 * ((struct osc_float32*)e->next)->value; + b = 255 * ((struct osc_float32*)e->next->next)->value; + } else { + return; + } + + ttydmx_set(dev, r, g, b); +} + void ttydmx_get(struct ttydmx_device *dev, unsigned *r, unsigned *g, unsigned *b) { @@ -137,6 +164,12 @@ static struct ttydmx_device *ttydmx_add_dev(struct ttydmx_sink *sink, if (d->baseaddr + 4 > sink->maxaddr) sink->maxaddr = d->baseaddr + 4; + + char buf[1024]; + snprintf(buf, sizeof(buf), "/dmx/%s/%s/value", sink->ttydev, d->name); + lprintf("Adding dmx osc endpoint %s", buf); + osc_server_add_method(osc_server, buf, ttydmx_osc_set, d); + return d; } @@ -255,6 +288,7 @@ int ttydmx_init_conf(json_t *config) free(sink); return 1; } + *devp = dev; devp = &dev->next; } -- cgit v1.2.1