From: Martin Mareš Date: Wed, 31 Dec 2025 18:55:23 +0000 (+0100) Subject: X-mas: Countdown X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=ceed7a736b55517ae19184d5b22d715b03bc581f;p=home-hw.git X-mas: Countdown --- diff --git a/xmas-lights/daemon/Makefile b/xmas-lights/daemon/Makefile index 5fffc46..ea3f973 100644 --- a/xmas-lights/daemon/Makefile +++ b/xmas-lights/daemon/Makefile @@ -9,20 +9,20 @@ 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) -g LDLIBS=$(UCW_LIBS) $(USB_LIBS) $(MOSQUITTO_LIBS) -lpthread -lm -all: led-strip +all: xmas-lights -led-strip: led-strip.o +xmas-lights: xmas-lights.o -led-strip.o: led-strip.c ../firmware/interface.h +xmas-lights.o: xmas-lights.c ../firmware/interface.h -install: led-strip - install led-strip /usr/local/sbin/ +install: xmas-lights + install xmas-lights /usr/local/sbin/ clean: - rm -f *.o led-strip + rm -f *.o xmas-lights deploy: - rsync -av --delete .. root@berry.lan:ksp/led-strip/ - ssh root@berry.lan 'cd ksp/led-strip/daemon/ && make install' + rsync -av --delete .. root@berry:xmas-lights/src + ssh root@berry 'cd xmas-lights/src/daemon/ && make clean && make install' .PHONY: all install clean upload diff --git a/xmas-lights/daemon/led-strip.c b/xmas-lights/daemon/led-strip.c deleted file mode 100644 index 73df90d..0000000 --- a/xmas-lights/daemon/led-strip.c +++ /dev/null @@ -1,269 +0,0 @@ -/* - * Daemon for X-mas Led Strip - * - * (c) 2025 Martin Mares - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -typedef unsigned char byte; -typedef uint32_t u32; -typedef unsigned int uint; - -#include "../firmware/interface.h" - -static mtx_t light_mutex; -static cnd_t light_cond; -static byte lights[NPIX_NUM_LEDS][3]; -static bool light_refresh; - -/*** MQTT ***/ - -static struct mosquitto *mosq; -static bool mqtt_connected; - -static void mqtt_publish(const char *topic, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - - if (mqtt_connected) { - char m[256]; - int l = vsnprintf(m, sizeof(m), fmt, args); - int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true); - if (err != MOSQ_ERR_SUCCESS) - msg(L_ERROR, "Mosquitto: Publish failed, error=%d", err); - } - - va_end(args); -} - -static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status) -{ - if (!status) { - msg(L_DEBUG, "MQTT: Connection established"); - mqtt_connected = true; - mqtt_publish("status/led-strip", "ok"); - if (mosquitto_subscribe(mosq, NULL, "ksp/led-strip", 1) != MOSQ_ERR_SUCCESS) - die("Mosquitto: subscribe failed"); - } else if (mqtt_connected) { - msg(L_DEBUG, "MQTT: Connection lost"); - mqtt_connected = false; - } -} - -static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message) -{ - msg(L_DEBUG, "MQTT(%d): %s", level, message); -} - -static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m) -{ - char val[4096]; - if (m->payloadlen >= sizeof(val) - 1) { - msg(L_ERROR, "Invalid value for topic %s", m->topic); - return; - } - memcpy(val, m->payload, m->payloadlen); - val[m->payloadlen] = 0; - msg(L_DEBUG, "MQTT < %s %s", m->topic, val); - - if (strcmp(m->topic, "ksp/led-strip")) - return; - - mtx_lock(&light_mutex); - uint i = 0; - char *x = val; - while (*x) { - if (*x == ' ') - x++; - else { - char *end; - unsigned long val = strtoul(x, &end, 16); - if (end == x) - break; - if (i >= NPIX_NUM_LEDS) - break; - lights[i][0] = (val >> 16) & 0xff; - lights[i][1] = (val >> 8) & 0xff; - lights[i][2] = (val >> 0) & 0xff; - i++; - x = end; - } - } - if (i == 0) { - for (uint i=0; ilevels &= ~(1U << L_DEBUG); - - mtx_init(&light_mutex, mtx_plain); - cnd_init(&light_cond); - - mqtt_init(); - init_usb(); - - bool need_resend = true; - for (;;) { - if (!devh) { - msg(L_INFO, "Waiting for device to appear..."); - while (!devh) { - sleep(5); - open_device(); - } - need_resend = true; - } - - mtx_lock(&light_mutex); - while (!need_resend && !light_refresh) - cnd_wait(&light_cond, &light_mutex); - light_refresh = 0; - need_resend = 0; - mtx_unlock(&light_mutex); - - msg(L_DEBUG, "Sending USB packet"); - - uint batch = 63/3; - for (uint i=0; i < NPIX_NUM_LEDS; i += batch) { - byte packet[64]; - uint n = NPIX_NUM_LEDS - i; - if (n > batch) - n = batch; - packet[0] = i; - mtx_lock(&light_mutex); - for (uint j=0; j + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +typedef unsigned char byte; +typedef uint32_t u32; +typedef unsigned int uint; + +#include "../firmware/interface.h" + +static mtx_t light_mutex; +static cnd_t light_cond; +static byte lights[NPIX_NUM_LEDS][3]; +static bool light_refresh; + +/*** MQTT ***/ + +#if 0 + +static struct mosquitto *mosq; +static bool mqtt_connected; + +static void mqtt_publish(const char *topic, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + if (mqtt_connected) { + char m[256]; + int l = vsnprintf(m, sizeof(m), fmt, args); + int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true); + if (err != MOSQ_ERR_SUCCESS) + msg(L_ERROR, "Mosquitto: Publish failed, error=%d", err); + } + + va_end(args); +} + +static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status) +{ + if (!status) { + msg(L_DEBUG, "MQTT: Connection established"); + mqtt_connected = true; + mqtt_publish("status/led-strip", "ok"); + if (mosquitto_subscribe(mosq, NULL, "ksp/led-strip", 1) != MOSQ_ERR_SUCCESS) + die("Mosquitto: subscribe failed"); + } else if (mqtt_connected) { + msg(L_DEBUG, "MQTT: Connection lost"); + mqtt_connected = false; + } +} + +static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message) +{ + msg(L_DEBUG, "MQTT(%d): %s", level, message); +} + +static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m) +{ + char val[4096]; + if (m->payloadlen >= sizeof(val) - 1) { + msg(L_ERROR, "Invalid value for topic %s", m->topic); + return; + } + memcpy(val, m->payload, m->payloadlen); + val[m->payloadlen] = 0; + msg(L_DEBUG, "MQTT < %s %s", m->topic, val); + + if (strcmp(m->topic, "ksp/led-strip")) + return; + + mtx_lock(&light_mutex); + uint i = 0; + char *x = val; + while (*x) { + if (*x == ' ') + x++; + else { + char *end; + unsigned long val = strtoul(x, &end, 16); + if (end == x) + break; + if (i >= NPIX_NUM_LEDS) + break; + lights[i][0] = (val >> 16) & 0xff; + lights[i][1] = (val >> 8) & 0xff; + lights[i][2] = (val >> 0) & 0xff; + i++; + x = end; + } + } + if (i == 0) { + for (uint i=0; ilevels &= ~(1U << L_DEBUG); + + mtx_init(&light_mutex, mtx_plain); + cnd_init(&light_cond); + + // mqtt_init(); + init_usb(); + + bool need_resend = true; + for (;;) { + if (!devh) { + msg(L_INFO, "Waiting for device to appear..."); + while (!devh) { + sleep(5); + open_device(); + } + need_resend = true; + } + +#if 0 + mtx_lock(&light_mutex); + while (!need_resend && !light_refresh) + cnd_wait(&light_cond, &light_mutex); + light_refresh = 0; + need_resend = 0; + mtx_unlock(&light_mutex); +#endif + + clock_tick(); + + msg(L_DEBUG, "Sending USB packet"); + + uint batch = 63/3; + for (uint i=0; i < NPIX_NUM_LEDS; i += batch) { + byte packet[64]; + uint n = NPIX_NUM_LEDS - i; + if (n > batch) + n = batch; + packet[0] = i; + mtx_lock(&light_mutex); + for (uint j=0; j= 100) { last_blink = ms_ticks; debug_led_toggle(); -#if 0 +#if 1 if (!got_first_message) { static uint i; neopixel_set(i, 0, 0, 7); @@ -468,7 +468,7 @@ int main(void) } #endif } - effect(); + // effect(); if (usb_event_pending) { usbd_poll(usbd_dev); usb_event_pending = 0;