]> mj.ucw.cz Git - home-hw.git/commitdiff
Rainbow: All settings are floats, added global brightness
authorMartin Mares <mj@ucw.cz>
Mon, 21 Feb 2022 21:42:42 +0000 (22:42 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 21 Feb 2022 21:42:42 +0000 (22:42 +0100)
MQTT
rainbow/daemon/burrow-rainbowd.c

diff --git a/MQTT b/MQTT
index c770e8c664116bc71713ecb46a917ffb5859da42..6219bc57c558adcc60c57c2d29eb2b9bd43f0992 100644 (file)
--- a/MQTT
+++ b/MQTT
@@ -53,7 +53,8 @@ burrow/heating/clock          yyyy-mm-ddThh:mm
 
 burrow/lights/catarium/top     0-1 [intensity] 0-1 [warm-cold]
 burrow/lights/catarium/bottom  0-1 [intensity] 0-1 [warm-cold]
-burrow/lights/rainbow/<led>    0-255 [R] 0-255 [G] 0-255 [B]
+burrow/lights/rainbow/<led>    0-1 [R] 0-1 [G] 0-1 [B]
+burrow/lights/rainbow/brightness       0-1
 
 bsb/stats/*
 bsb/frame                      hex dump of raw frames received
index 6ec45464ce195b0df91bdfc6569cd9e26fe49688..95cbfe1548f241a76fddccdfce789492fa4007fb 100644 (file)
@@ -36,10 +36,11 @@ static cnd_t led_cond;
 static bool led_refresh;
 
 struct led {
-    byte r, g, b;
+    double r, g, b;
 };
 
 static struct led leds[NPIX_NUM_LEDS];
+static double led_brightness = 1;
 
 /*** MQTT ***/
 
@@ -96,14 +97,29 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c
        if (!str_has_prefix(m->topic, px))
                return;
 
+       const char *top = m->topic + strlen(px);
+       if (!strcmp(top, "brightness")) {
+               double b = atof(val);
+               if (!(b >= 0 && b <= 1)) {
+                       msg(L_ERROR, "Invalid value of %s: %s", m->topic, val);
+                       return;
+               }
+               mtx_lock(&led_mutex);
+               led_brightness = b;
+               led_refresh = 1;
+               cnd_broadcast(&led_cond);
+               mtx_unlock(&led_mutex);
+               return;
+       }
+
        uint index;
        if (str_to_uint(&index, m->topic + strlen(px), NULL, 10 | STN_WHOLE) || index >= NPIX_NUM_LEDS) {
                msg(L_ERROR, "Unsupported topic: %s", m->topic);
                return;
        }
 
-       uint r, g, b;
-       if (sscanf(val, "%u%u%u", &r, &g, &b) != 3 || r >= 256 || g >= 256 || b >= 256) {
+       double r, g, b;
+       if (sscanf(val, "%lf%lf%lf", &r, &g, &b) != 3 || !(r >= 0 && r <= 1) || !(g >= 0 && g <= 1) || !(b >= 0 && b <= 1)) {
                msg(L_ERROR, "Invalid value of %s: %s", m->topic, val);
                return;
        }
@@ -212,9 +228,9 @@ static int npix_build_packet(void)
        byte *pkt = npix_packet;
 
        for (int i=0; i < NPIX_NUM_LEDS; i++) {
-               *pkt++ = leds[i].r;
-               *pkt++ = leds[i].g;
-               *pkt++ = leds[i].b;
+               *pkt++ = (int)(leds[i].r * led_brightness * 255);
+               *pkt++ = (int)(leds[i].g * led_brightness * 255);
+               *pkt++ = (int)(leds[i].b * led_brightness * 255);
        }
 
        return pkt - npix_packet;