]> mj.ucw.cz Git - home-hw.git/commitdiff
DMX: Better daemon
authorMartin Mares <mj@ucw.cz>
Sat, 19 Feb 2022 23:20:17 +0000 (00:20 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 19 Feb 2022 23:20:17 +0000 (00:20 +0100)
MQTT
dmx/daemon/Makefile
dmx/daemon/burrow-dmxd.c

diff --git a/MQTT b/MQTT
index c2f2c0add02d89921afb3dcf52c44a7d63fbda03..7f485bb759476a92d5fcb0919915b48eec8e2dda 100644 (file)
--- 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
index 9ac9ef94befd640d2274e542d6138f7e9e8ee067..cc6cdd44909c4ee5a92206ce75adf98e4ba63045 100644 (file)
@@ -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
 
index 5634537cb96f62ed76c4443c6dba29fbc1d80781..6466482b38a33ef7a7c95caf1713f50656af21a5 100644 (file)
@@ -10,6 +10,7 @@
 #include <ucw/string.h>
 #include <ucw/unaligned.h>
 
+#include <math.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -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);