From ceed7a736b55517ae19184d5b22d715b03bc581f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Mare=C5=A1?= Date: Wed, 31 Dec 2025 19:55:23 +0100 Subject: [PATCH] X-mas: Countdown --- xmas-lights/daemon/Makefile | 16 ++-- .../daemon/{led-strip.c => xmas-lights.c} | 86 ++++++++++++++++++- xmas-lights/firmware/main.c | 4 +- 3 files changed, 92 insertions(+), 14 deletions(-) rename xmas-lights/daemon/{led-strip.c => xmas-lights.c} (79%) 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/xmas-lights.c similarity index 79% rename from xmas-lights/daemon/led-strip.c rename to xmas-lights/daemon/xmas-lights.c index 73df90d..c8d5aa4 100644 --- a/xmas-lights/daemon/led-strip.c +++ b/xmas-lights/daemon/xmas-lights.c @@ -36,6 +36,8 @@ static bool light_refresh; /*** MQTT ***/ +#if 0 + static struct mosquitto *mosq; static bool mqtt_connected; @@ -147,6 +149,8 @@ static void mqtt_init(void) die("Mosquitto: Cannot start service thread"); } +#endif + /*** USB ***/ static struct libusb_context *usb_ctxt; @@ -208,6 +212,76 @@ static void init_usb(void) open_device(); } +/*** Clock ***/ + +static void clock_countdown(uint t) +{ + memset(lights, 1, sizeof(lights)); + + const uint NBITS = 17; + const uint BITSIZE = 3; + const uint BITGAP = 4; + const uint BITOFF = (NPIX_NUM_LEDS - NBITS * BITSIZE - (NBITS - 1) * BITGAP) / 2; + + for (uint i=0; i < NBITS; i++) { + uint j = BITOFF + (BITSIZE + BITGAP) * i; + for (uint k = 0; k < BITSIZE; k++) { + if (t & (1 << i)) { + lights[j+k][0] = 0; + lights[j+k][1] = 255; + lights[j+k][2] = 0; + } else { + lights[j+k][0] = 255; + lights[j+k][1] = 0; + lights[j+k][2] = 0; + } + } + } +} + +static void clock_year(uint t) +{ + memset(lights, 1, sizeof(lights)); + + const uint NBITS = 11; + const uint BITSIZE = 5; + const uint BITGAP = 6; + const uint BITOFF = (NPIX_NUM_LEDS - NBITS * BITSIZE - (NBITS - 1) * BITGAP) / 2; + + for (uint i=0; i < NBITS; i++) { + uint j = BITOFF + (BITSIZE + BITGAP) * i; + for (uint k = 0; k < BITSIZE; k++) { + if (t & (1 << i)) { + lights[j+k][0] = 0; + lights[j+k][1] = 255; + lights[j+k][2] = 0; + } else { + lights[j+k][0] = 255; + lights[j+k][1] = 0; + lights[j+k][2] = 0; + } + } + } +} + +static void clock_tick(void) +{ + struct timeval tv; + if (gettimeofday(&tv, NULL) < 0) { + msg(L_ERROR, "gettimeofday: %m"); + sleep(1); + return; + } + uint t = (uint) tv.tv_sec + 1; + usleep(1000000 - tv.tv_usec); + + uint midnight = 1767222000; + if (t < midnight) + clock_countdown(midnight - t); + else + clock_year(2026); +} + /*** Main ***/ int main(int argc UNUSED, char **argv) @@ -218,7 +292,7 @@ int main(int argc UNUSED, char **argv) mtx_init(&light_mutex, mtx_plain); cnd_init(&light_cond); - mqtt_init(); + // mqtt_init(); init_usb(); bool need_resend = true; @@ -232,12 +306,16 @@ int main(int argc UNUSED, char **argv) 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"); @@ -250,9 +328,9 @@ int main(int argc UNUSED, char **argv) 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; -- 2.47.3