]> mj.ucw.cz Git - home-hw.git/commitdiff
SSR host: Cleanup, avoid non-changes
authorMartin Mares <mj@ucw.cz>
Fri, 17 Aug 2018 07:38:03 +0000 (09:38 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 17 Aug 2018 07:38:03 +0000 (09:38 +0200)
ssr/host/burrow-ssrd.c

index d999bc55d903828d739bc324747e34c5d49b89f8..1b3c4ffde3923ec5ee998fb13246ac5b213fc804 100644 (file)
@@ -25,7 +25,7 @@ static struct libusb_device_handle *devh;
 
 static struct mosquitto *mosq;
 
-static u32 ssr_state;
+static u32 ssr_state = ~0U;
 
 void open_device(void)
 {
@@ -84,12 +84,16 @@ static int transaction(uint req_len, uint resp_len)
        return received;
 }
 
-static void set_relays(void)
+static void set_relays(u32 mask, u32 data)
 {
-       msg(L_INFO, "Setting relays to %02x", ssr_state);
-       put_u32_be(req, 1);
-       put_u32_be(req+4, ssr_state);
-       transaction(8, 4);
+       u32 new = (ssr_state & mask) | data;
+       if (new != ssr_state) {
+               msg(L_INFO, "Setting relays to %02x", new);
+               put_u32_be(req, 1);
+               put_u32_be(req+4, new);
+               transaction(8, 4);
+               ssr_state = new;
+       }
 }
 
 static void mqtt_publish(const char *topic, const char *fmt, ...)
@@ -135,19 +139,20 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c
                        x = 0;
                }
                msg(L_INFO, "Setting fan level to %u", x);
-               ssr_state &= ~7U;
                switch (x) {
+                       case 0:
+                               set_relays(~7U, 0);
+                               break;
                        case 1:
-                               ssr_state |= 4;
+                               set_relays(~7U, 4);
                                break;
                        case 2:
-                               ssr_state |= 2;
+                               set_relays(~7U, 2);
                                break;
                        case 3:
-                               ssr_state |= 1;
+                               set_relays(~7U, 1);
                                break;
                }
-               set_relays();
        } else if (!strcmp(m->topic, "burrow/loft/circulation")) {
                uint x;
                const char *err;
@@ -156,10 +161,7 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c
                        x = 0;
                }
                msg(L_INFO, "Setting circulation to %u", x);
-               ssr_state &= ~8U;
-               if (x)
-                       ssr_state |= 8;
-               set_relays();
+               set_relays(~8U, (x ? 8 : 0));
        }
 }
 
@@ -197,7 +199,7 @@ int main(int argc UNUSED, char **argv)
        if (use_debug)
                libusb_set_debug(usb_ctxt, 3);
        open_device();
-       set_relays();
+       set_relays(0, 0);
 
        mosquitto_lib_init();
        mosq = mosquitto_new("ssrd", 1, NULL);
@@ -231,7 +233,7 @@ int main(int argc UNUSED, char **argv)
                        continue;
                }
 
-               next_run = now + 5;
+               next_run = now + 10;
 
                put_u32_be(req, 2);
                transaction(8, 8);