From 16fea8acf4fae8f7239f810e392e924725dbfdf5 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 17 Aug 2018 09:38:03 +0200 Subject: [PATCH] SSR host: Cleanup, avoid non-changes --- ssr/host/burrow-ssrd.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/ssr/host/burrow-ssrd.c b/ssr/host/burrow-ssrd.c index d999bc5..1b3c4ff 100644 --- a/ssr/host/burrow-ssrd.c +++ b/ssr/host/burrow-ssrd.c @@ -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); -- 2.39.2