From: Martin Mareš Date: Sat, 17 Jan 2026 17:50:11 +0000 (+0100) Subject: X-mas: Binary clock X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=7b01d72f790149a27989fd9775bad56796a6e516;p=home-hw.git X-mas: Binary clock --- diff --git a/xmas-lights/daemon/xmas-lights.c b/xmas-lights/daemon/xmas-lights.c index c8d5aa4..e9f2222 100644 --- a/xmas-lights/daemon/xmas-lights.c +++ b/xmas-lights/daemon/xmas-lights.c @@ -264,6 +264,41 @@ static void clock_year(uint t) } } +static void clock_blank(void) +{ + memset(lights, 0, sizeof(lights)); +} + +static void clock_time(uint h, uint m) +{ + memset(lights, 0, sizeof(lights)); + + uint x = (h << 6) | m; + const uint NBITS = 11; + const uint BITSIZE = 5; + const uint BITGAP = 6; + const uint BIGGAP_AT = 6; + const uint BIGGAP_ENLARGE = 10; + const uint BITOFF = (NPIX_NUM_LEDS - NBITS * BITSIZE - (NBITS - 1) * BITGAP - BIGGAP_ENLARGE) / 2; + + for (uint i=0; i < NBITS; i++) { + uint j = BITOFF + (BITSIZE + BITGAP) * i; + if (i >= BIGGAP_AT) + j += BIGGAP_ENLARGE; + for (uint k = 0; k < BITSIZE; k++) { + if (x & (1 << i)) { + lights[j+k][0] = 0; + lights[j+k][1] = 0; + lights[j+k][2] = 255; + } else { + lights[j+k][0] = 64; + lights[j+k][1] = 64; + lights[j+k][2] = 64; + } + } + } +} + static void clock_tick(void) { struct timeval tv; @@ -272,14 +307,23 @@ static void clock_tick(void) sleep(1); return; } - uint t = (uint) tv.tv_sec + 1; + time_t t = tv.tv_sec + 1; usleep(1000000 - tv.tv_usec); - uint midnight = 1767222000; +#if 0 + // Countdown to New Year + time_t midnight = 1767222000; if (t < midnight) - clock_countdown(midnight - t); + clock_countdown((uint)(midnight - t)); else clock_year(2026); +#else + struct tm *tm = localtime(&t); + if (tm->tm_hour >= 9 && tm->tm_hour < 16) + clock_blank(); + else + clock_time(tm->tm_hour, tm->tm_min); +#endif } /*** Main ***/