From bfc60b6b0d850e8812a3d641cf435d926f4ad1f0 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 20 Feb 2022 00:20:17 +0100 Subject: [PATCH] DMX: Better daemon --- MQTT | 4 ++-- dmx/daemon/Makefile | 2 +- dmx/daemon/burrow-dmxd.c | 51 ++++++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/MQTT b/MQTT index c2f2c0a..7f485bb 100644 --- a/MQTT +++ b/MQTT @@ -51,8 +51,8 @@ burrow/heating/water/active 0/1 burrow/heating/error [error code from message 0500:006b in decimal] burrow/heating/clock yyyy-mm-ddThh:mm -burrow/lights/catarium/top 0-255 [warm] 0-255 [cold] -burrow/lights/catarium/bottom 0-255 [warm] 0-255 [cold] +burrow/lights/catarium/top 0-1 [intensity] 0-1 [warm-cold] +burrow/lights/catarium/bottom 0-1 [intensity] 0-1 [warm-cold] bsb/stats/* bsb/frame hex dump of raw frames received diff --git a/dmx/daemon/Makefile b/dmx/daemon/Makefile index 9ac9ef9..cc6cdd4 100644 --- a/dmx/daemon/Makefile +++ b/dmx/daemon/Makefile @@ -7,7 +7,7 @@ MOSQUITTO_CFLAGS := $(shell $(PC) --cflags libmosquitto) MOSQUITTO_LIBS := $(shell $(PC) --libs libmosquitto) CFLAGS=-O2 -Wall -Wextra -Wno-sign-compare -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes $(UCW_CFLAGS) $(USB_CFLAGS) $(MOSQUITTO_CFLAGS) -LDLIBS=$(UCW_LIBS) $(USB_LIBS) $(MOSQUITTO_LIBS) -lpthread +LDLIBS=$(UCW_LIBS) $(USB_LIBS) $(MOSQUITTO_LIBS) -lpthread -lm all: burrow-dmxd diff --git a/dmx/daemon/burrow-dmxd.c b/dmx/daemon/burrow-dmxd.c index 5634537..6466482 100644 --- a/dmx/daemon/burrow-dmxd.c +++ b/dmx/daemon/burrow-dmxd.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -31,7 +32,8 @@ typedef unsigned int uint; static mtx_t light_mutex; static cnd_t light_cond; -static byte light_pwm[4]; +static double light_brightness[2]; +static double light_temperature[2]; static bool light_refresh; /*** MQTT ***/ @@ -86,23 +88,23 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c msg(L_DEBUG, "MQTT < %s %s", m->topic, val); int index; - if (!strcmp(m->topic, "burrow/lights/catarium/top")) + if (!strcmp(m->topic, "burrow/lights/catarium/bottom")) index = 0; - else if (!strcmp(m->topic, "burrow/lights/catarium/bottom")) + else if (!strcmp(m->topic, "burrow/lights/catarium/top")) index = 1; else return; - int warm, cold; - if (sscanf(val, "%d%d", &warm, &cold) != 2 || - !(warm >= 0 && warm < 256 && cold >= 0 && cold < 256)) { + double bright, temp; + if (sscanf(val, "%lf%lf", &bright, &temp) != 2 || + !(bright >= 0 && bright <= 1 && temp >= 0 && temp <= 1)) { msg(L_ERROR, "Invalid value of %s: %s", m->topic, val); return; } mtx_lock(&light_mutex); - light_pwm[2*index] = warm; - light_pwm[2*index + 1] = cold; + light_brightness[index] = bright; + light_temperature[index] = temp; light_refresh = 1; cnd_broadcast(&light_cond); mtx_unlock(&light_mutex); @@ -194,7 +196,30 @@ static void init_usb(void) open_device(); } -/*** Protocol ***/ +/*** DMX ***/ + +static byte dmx_packet[5]; + +static int dmx_build_packet(void) +{ + byte *pkt = dmx_packet; + pkt[0] = 0; + + for (int i=0; i < 2; i++) { + double r = 2; + double x = (exp(r*light_brightness[i]) - 1) / (exp(r) - 1); + double t = light_temperature[i]; + double w = 2*x*(1-t); + double c = 2*x*t; + pkt[2*i + 1] = CLAMP((int)(255*w), 0, 255); + pkt[2*i + 2] = CLAMP((int)(255*c), 0, 255); + } + + return sizeof(dmx_packet); +} + + +/*** Main ***/ static int use_daemon; static int use_debug; @@ -245,11 +270,7 @@ int main(int argc UNUSED, char **argv) while (!need_resend && !light_refresh) cnd_wait(&light_cond, &light_mutex); - byte packet[5]; - int len = sizeof(packet); - packet[0] = 0; - memcpy(packet + 1, light_pwm, 4); - + int len = dmx_build_packet(); light_refresh = 0; need_resend = 0; mtx_unlock(&light_mutex); @@ -257,7 +278,7 @@ int main(int argc UNUSED, char **argv) msg(L_DEBUG, "Sending DMX packet"); int err, transferred; - if (err = libusb_bulk_transfer(devh, 0x01, packet, len, &transferred, 1000)) + if (err = libusb_bulk_transfer(devh, 0x01, dmx_packet, len, &transferred, 1000)) usb_error("USB transfer failed: error %d", err); else if (transferred != len) usb_error("USB short transfer: %d out of %d bytes", transferred, len); -- 2.39.2