clock.c
C source for A clock on a round ESP32 display, 1678 lines. Raw file
#include "clock.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_task_wdt.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hud.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
extern const uint8_t wood_rgb_start[] asm("_binary_wood_rgb_start");
static const char *TAG = "wood_clock";
static const size_t k_fb = (size_t)HUD_W * HUD_H * 3;
static uint8_t *s_cache;
static uint8_t *s_static[CLOCK_FACES];
static uint8_t *s_static_slab;
static int s_all_static;
static int s_prerender;
static int s_face;
static int s_cache_ok;
static hud_tiles_t s_occ;
static uint8_t s_ready;
typedef struct {
int h24, min, wday, mday, mon, year;
} slow_t;
static slow_t s_slow;
static int s_slow_ok;
static int s_led_prev[14];
static int s_led_ok;
static int s_cas_prev[7];
static int s_cas_ok;
typedef struct {
int h24, h12, min, sec, wday, mday, mon, year;
float secf, minf, hourf;
int blink;
} now_t;
static int64_t s_blob_t0;
static int s_blob_pending;
static int s_blob_prev[4];
static const char *const k_names[CLOCK_FACES] = {
"WOOD", "CASIO", "NEON",
"BAUHAUS", "LED", "ORBIT", "VECTOR",
};
static const char *const k_days[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
static const char *const k_mon[] = {
"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
};
static const uint8_t k_seg[10] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,
};
static int clampi(int v, int lo, int hi)
{
if (v < lo) {
return lo;
}
if (v > hi) {
return hi;
}
return v;
}
static float cover01(float v)
{
if (v <= 0.0f) {
return 0.0f;
}
if (v >= 1.0f) {
return 1.0f;
}
return v;
}
static unsigned hash2(int x, int y)
{
unsigned h = (unsigned)(x * 374761393u + y * 668265263u);
h = (h ^ (h >> 13)) * 1274126177u;
return h;
}
static now_t read_now(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm local;
localtime_r(&tv.tv_sec, &local);
now_t n;
n.h24 = local.tm_hour;
n.h12 = local.tm_hour % 12;
n.min = local.tm_min;
n.sec = local.tm_sec;
n.wday = local.tm_wday;
n.mday = local.tm_mday;
n.mon = local.tm_mon;
n.year = local.tm_year + 1900;
n.secf = (float)local.tm_sec + (float)tv.tv_usec / 1000000.0f;
n.minf = (float)local.tm_min + n.secf / 60.0f;
n.hourf = (float)n.h12 + n.minf / 60.0f;
n.blink = tv.tv_usec < 500000;
return n;
}
static void tapered_at(uint8_t *rgb, int cx, int cy, float ang, float r0, float r1,
float w0, float w1, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
float ca = cosf(ang);
float sa = sinf(ang);
hud_line_r2(rgb,
(float)cx + ca * r0, (float)cy + sa * r0, w0,
(float)cx + ca * r1, (float)cy + sa * r1, w1,
r, g, b, a);
}
static void tapered(uint8_t *rgb, float ang, float r0, float r1, float w0, float w1,
uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
tapered_at(rgb, HUD_CX, HUD_CY, ang, r0, r1, w0, w1, r, g, b, a);
}
static void prerender_yield(int y)
{
if (s_prerender && (y & 31) == 0) {
esp_task_wdt_reset();
vTaskDelay(1);
}
}
static void chrome_annulus(uint8_t *rgb, int cx, int cy, float ri, float ro)
{
int rmax = (int)(ro + 2.0f);
for (int y = -rmax; y <= rmax; y++) {
prerender_yield(y);
for (int x = -rmax; x <= rmax; x++) {
float d = sqrtf((float)(x * x + y * y));
float cov = cover01(d - (ri - 0.5f)) * cover01((ro + 0.5f) - d);
if (cov <= 0.0f) {
continue;
}
float ang = atan2f((float)y, (float)x);
float light = 0.40f + 0.50f * cosf(ang + 0.90f);
float spec = light > 0.80f ? (light - 0.80f) * 6.0f : 0.0f;
if (spec > 1.0f) {
spec = 1.0f;
}
float edge = 0.0f;
if (d < ri + 2.2f) {
edge += (ri + 2.2f - d) * 0.18f;
}
if (d > ro - 2.2f) {
edge += (d - (ro - 2.2f)) * 0.22f;
}
int v = (int)(78.0f + light * 120.0f + spec * 90.0f + edge * 50.0f);
hud_pixel(rgb, cx + x, cy + y,
(uint8_t)clampi(v, 0, 255),
(uint8_t)clampi(v + 6, 0, 255),
(uint8_t)clampi(v + 12, 0, 255),
(uint8_t)(cov * 255.0f + 0.5f));
}
}
}
static void wood_bg(uint8_t *rgb)
{
const uint8_t *src = wood_rgb_start;
const float den = (float)(HUD_W - 1);
for (int y = 0; y < HUD_H; y++) {
prerender_yield(y);
for (int x = 0; x < HUD_W; x++) {
uint8_t *p = rgb + (y * HUD_W + x) * 3;
if (!hud_in_disk(x, y)) {
p[0] = p[1] = p[2] = 0;
continue;
}
float fx = (float)x * 359.0f / den;
float fy = (float)y * 359.0f / den;
int x0 = (int)fx;
int y0 = (int)fy;
int x1 = x0 + 1 < 360 ? x0 + 1 : 359;
int y1 = y0 + 1 < 360 ? y0 + 1 : 359;
float tx = fx - (float)x0;
float ty = fy - (float)y0;
const uint8_t *s00 = src + (y0 * HUD_SRC + x0) * 3;
const uint8_t *s10 = src + (y0 * HUD_SRC + x1) * 3;
const uint8_t *s01 = src + (y1 * HUD_SRC + x0) * 3;
const uint8_t *s11 = src + (y1 * HUD_SRC + x1) * 3;
int r = (int)((s00[0] * (1.0f - tx) + s10[0] * tx) * (1.0f - ty)
+ (s01[0] * (1.0f - tx) + s11[0] * tx) * ty);
int g = (int)((s00[1] * (1.0f - tx) + s10[1] * tx) * (1.0f - ty)
+ (s01[1] * (1.0f - tx) + s11[1] * tx) * ty);
int b = (int)((s00[2] * (1.0f - tx) + s10[2] * tx) * (1.0f - ty)
+ (s01[2] * (1.0f - tx) + s11[2] * tx) * ty);
float nx = (float)(x - HUD_CX) / (float)HUD_CX;
float ny = (float)(y - HUD_CY) / (float)HUD_CY;
float gloss = 0.45f - (nx + 0.28f) * (nx + 0.28f) - (ny + 0.42f) * (ny + 0.42f) * 0.85f;
if (gloss > 0.0f) {
int add = (int)(gloss * 22.0f);
r = clampi(r + add, 0, 255);
g = clampi(g + add * 3 / 4, 0, 255);
b = clampi(b + add / 2, 0, 255);
}
p[0] = (uint8_t)r;
p[1] = (uint8_t)g;
p[2] = (uint8_t)b;
}
}
}
static void shade_wood(uint8_t *rgb, int px, int py, float t, float light)
{
if (!hud_in_disk(px, py)) {
return;
}
t = cover01(t);
float shade = (1.0f - t) * (0.40f + light * 0.22f) + t * (0.92f + light * 0.16f);
uint8_t *p = rgb + (py * HUD_W + px) * 3;
p[0] = (uint8_t)clampi((int)(p[0] * shade + light * 22.0f * (1.0f - t)), 0, 255);
p[1] = (uint8_t)clampi((int)(p[1] * shade + light * 14.0f * (1.0f - t)), 0, 255);
p[2] = (uint8_t)clampi((int)(p[2] * shade + light * 8.0f * (1.0f - t)), 0, 255);
}
static void wood_bevel_ring(uint8_t *rgb, int cx, int cy, float r0, float bevel)
{
int rmax = (int)(r0 + bevel + 2.0f);
for (int y = -rmax; y <= rmax; y++) {
prerender_yield(y);
for (int x = -rmax; x <= rmax; x++) {
float d = sqrtf((float)(x * x + y * y));
if (d < r0 || d > r0 + bevel) {
continue;
}
float t = (d - r0) / bevel;
float ang = atan2f((float)y, (float)x);
shade_wood(rgb, cx + x, cy + y, t, 0.38f + 0.62f * cosf(ang + 0.90f));
}
}
}
static void screw(uint8_t *rgb, int x, int y)
{
chrome_annulus(rgb, x, y, 3.0f, 9.0f);
hud_fill_circle(rgb, x, y, 3, 50, 52, 58, 255);
hud_line(rgb, x - 4, y - 1, x + 4, y + 1, 30, 32, 36, 255);
}
static const uint8_t *static_src(void);
static void digit7(uint8_t *rgb, int x, int y, int w, int h, int t, int n,
uint8_t on_r, uint8_t on_g, uint8_t on_b,
uint8_t off_r, uint8_t off_g, uint8_t off_b)
{
/* n < 0 is a dash: only the middle bar, unlit segs still drawn. */
uint8_t m = (n < 0) ? 0x40u : k_seg[(n > 9) ? 0 : n];
int mid = y + h / 2 - t / 2;
int segs[7][4] = {
{ x + t, y, w - 2 * t, t },
{ x + w - t, y + t, t, h / 2 - t },
{ x + w - t, mid + t, t, h / 2 - t },
{ x + t, y + h - t, w - 2 * t, t },
{ x, mid + t, t, h / 2 - t },
{ x, y + t, t, h / 2 - t },
{ x + t, mid, w - 2 * t, t },
};
for (int i = 0; i < 7; i++) {
int on = (m >> i) & 1;
hud_fill_rect(rgb, segs[i][0], segs[i][1], segs[i][2], segs[i][3],
on ? on_r : off_r, on ? on_g : off_g, on ? on_b : off_b, 255);
}
}
static int blob_center_y(void)
{
if (s_blob_t0 == 0) {
return -1;
}
int64_t dt = esp_timer_get_time() - s_blob_t0;
const int64_t up_us = 420000;
const int64_t hold_us = 700000;
const int64_t down_us = 420000;
const int y_rest = 398;
const int y_off = 520;
if (dt < 0) {
return -1;
}
if (dt < up_us) {
float t = (float)dt / (float)up_us;
float u = 1.0f - t;
float e = 1.0f - u * u * u;
return (int)lroundf((float)y_off + (float)(y_rest - y_off) * e);
}
if (dt < up_us + hold_us) {
return y_rest;
}
if (dt < up_us + hold_us + down_us) {
float t = (float)(dt - up_us - hold_us) / (float)down_us;
float e = t * t * t;
return (int)lroundf((float)y_rest + (float)(y_off - y_rest) * e);
}
s_blob_t0 = 0;
return -1;
}
static void blob_geom(int cy, int *x0, int *y0, int *x1, int *y1, int *pad_x, int *pad_y, int *scale)
{
char buf[24];
snprintf(buf, sizeof buf, "%d of %d", s_face + 1, CLOCK_FACES);
*scale = 4;
*pad_x = 24;
*pad_y = 16;
int tw = hud_text_width(*scale, buf);
int th = 7 * *scale;
int w = tw + *pad_x * 2;
int h = th + *pad_y * 2;
*x0 = HUD_CX - w / 2;
*y0 = cy - h / 2;
*x1 = *x0 + w;
*y1 = *y0 + h;
}
static void draw_face_blob(uint8_t *rgb, hud_tiles_t dirty)
{
int cy = blob_center_y();
int x0, y0, x1, y1, pad_x, pad_y, scale;
if (s_blob_prev[2] > s_blob_prev[0]) {
hud_tiles_add_rect(dirty, s_blob_prev[0] - 8, s_blob_prev[1] - 8,
s_blob_prev[2] + 8, s_blob_prev[3] + 8);
}
if (cy < 0) {
s_blob_prev[0] = s_blob_prev[1] = s_blob_prev[2] = s_blob_prev[3] = 0;
return;
}
blob_geom(cy, &x0, &y0, &x1, &y1, &pad_x, &pad_y, &scale);
char buf[24];
snprintf(buf, sizeof buf, "%d of %d", s_face + 1, CLOCK_FACES);
int w = x1 - x0;
int h = y1 - y0;
int rad = h / 2;
hud_fill_rect(rgb, x0 + rad, y0, w - 2 * rad, h, 240, 240, 236, 255);
hud_fill_circle(rgb, x0 + rad, y0 + rad, rad, 240, 240, 236, 255);
hud_fill_circle(rgb, x0 + w - rad, y0 + rad, rad, 240, 240, 236, 255);
hud_text(rgb, x0 + pad_x, y0 + pad_y, scale, buf, 8, 8, 8, 255);
hud_tiles_add_rect(dirty, x0 - 8, y0 - 8, x1 + 8, y1 + 8);
s_blob_prev[0] = x0;
s_blob_prev[1] = y0;
s_blob_prev[2] = x1;
s_blob_prev[3] = y1;
}
static void analog_hm(uint8_t *rgb, const now_t *n, float hl, float ml,
uint8_t hr, uint8_t hg, uint8_t hb)
{
float ha = -(float)M_PI / 2.0f + n->hourf * (float)M_PI / 6.0f;
float ma = -(float)M_PI / 2.0f + n->minf * (float)M_PI / 30.0f;
tapered(rgb, ha, 0.0f, hl, 6.4f, 2.2f, hr, hg, hb, 255);
tapered(rgb, ma, 0.0f, ml, 4.4f, 1.6f, hr, hg, hb, 255);
}
static void analog_s(uint8_t *rgb, const now_t *n, float sl,
uint8_t sr, uint8_t sg, uint8_t sb)
{
if (sl <= 1.0f) {
return;
}
float sa = -(float)M_PI / 2.0f + n->secf * (float)M_PI / 30.0f;
tapered(rgb, sa, -28.0f, sl, 1.3f, 0.9f, sr, sg, sb, 255);
}
/* ---------- 0 wood ---------- */
static void static_wood(uint8_t *rgb)
{
wood_bg(rgb);
wood_bevel_ring(rgb, HUD_CX, HUD_CY, 196.0f, 12.0f);
chrome_annulus(rgb, HUD_CX, HUD_CY, 208.0f, 230.0f);
chrome_annulus(rgb, HUD_CX, HUD_CY, 198.0f, 204.0f);
static const char *const lab[] = {
"12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
};
for (int i = 0; i < 60; i++) {
float ang = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 30.0f;
if ((i % 5) == 0) {
tapered(rgb, ang, 168.0f, 190.0f, 2.6f, 2.2f, 214, 176, 96, 255);
} else {
tapered(rgb, ang, 180.0f, 190.0f, 1.1f, 1.0f, 196, 168, 112, 220);
}
}
for (int h = 0; h < 12; h++) {
int cardinal = (h % 3) == 0;
int scale = cardinal ? 3 : 2;
float ang = -(float)M_PI / 2.0f + (float)h * (float)M_PI / 6.0f;
float rad = cardinal ? 142.0f : 146.0f;
int tw = hud_text_width(scale, lab[h]);
int x = HUD_CX + (int)lroundf(cosf(ang) * rad) - tw / 2;
int y = HUD_CY + (int)lroundf(sinf(ang) * rad) - (7 * scale) / 2;
hud_text_outline(rgb, x, y, scale, lab[h], 236, 224, 198);
}
for (int i = 0; i < 4; i++) {
float a = (float)M_PI / 4.0f + (float)i * (float)M_PI / 2.0f;
screw(rgb, HUD_CX + (int)lroundf(cosf(a) * 219.0f),
HUD_CY + (int)lroundf(sinf(a) * 219.0f));
}
chrome_annulus(rgb, HUD_CX, HUD_CY, 6.0f, 13.0f);
hud_fill_circle(rgb, HUD_CX, HUD_CY, 6, 36, 32, 30, 255);
hud_fill_circle(rgb, HUD_CX, HUD_CY, 3, 176, 40, 32, 255);
}
static void slow_wood(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
}
static void fast_wood(uint8_t *rgb, const now_t *n)
{
analog_hm(rgb, n, 106.0f, 156.0f, 255, 255, 255);
analog_s(rgb, n, 170.0f, 176, 40, 32);
}
/* ---------- 1 casio ---------- */
static void static_casio(uint8_t *rgb)
{
hud_clear(rgb, 78, 92, 52);
hud_ring(rgb, HUD_CX, HUD_CY, 226.0f, 8.0f, 40, 48, 28, 180);
}
static void casio_geom(int *x0, int *y0, int *colon_x, int *total)
{
const int dw = 70;
const int gap = 8;
const int colon = 12;
const int side = 14;
const int pair = dw + gap + dw;
*total = pair + side + colon + side + pair;
*x0 = (HUD_W - *total) / 2;
*y0 = 124;
*colon_x = *x0 + pair + side;
}
static void cas_vals(const now_t *n, int v[7])
{
v[0] = n->h24 / 10;
v[1] = n->h24 % 10;
v[2] = n->min / 10;
v[3] = n->min % 10;
v[4] = n->wday;
v[5] = n->mday;
v[6] = n->mon;
}
static void cas_time_box(int i, int *x, int *y, int *w, int *h)
{
const int dw = 70;
const int dh = 132;
const int gap = 8;
const int colon = 12;
const int side = 14;
int x0, y0, colon_x, total;
casio_geom(&x0, &y0, &colon_x, &total);
(void)total;
*y = y0;
*w = dw;
*h = dh;
if (i == 0) {
*x = x0;
} else if (i == 1) {
*x = x0 + dw + gap;
} else if (i == 2) {
*x = x0 + dw + gap + dw + side + colon + side;
} else {
*x = x0 + dw + gap + dw + side + colon + side + dw + gap;
}
}
static void cas_date_layout(int xs[3], int ws[3], int *y, int *th)
{
const int dscale = 6;
const int dh = 132;
int x0, y0, colon_x, total;
casio_geom(&x0, &y0, &colon_x, &total);
ws[0] = hud_text_width(dscale, "WED");
ws[1] = hud_text_width(dscale, "00");
ws[2] = hud_text_width(dscale, "JAN");
int gap_d = (total - ws[0] - ws[1] - ws[2]) / 2;
if (gap_d < 4) {
gap_d = 4;
}
xs[0] = x0;
xs[1] = xs[0] + ws[0] + gap_d;
xs[2] = xs[1] + ws[1] + gap_d;
*y = y0 + dh + 22;
*th = 7 * dscale;
}
static void copy_rect(uint8_t *dst, const uint8_t *src, int x0, int y0, int x1, int y1)
{
if (!dst || !src) {
return;
}
x0 = clampi(x0, 0, HUD_W);
y0 = clampi(y0, 0, HUD_H);
x1 = clampi(x1, 0, HUD_W);
y1 = clampi(y1, 0, HUD_H);
if (x1 <= x0 || y1 <= y0) {
return;
}
size_t row = (size_t)(x1 - x0) * 3;
for (int y = y0; y < y1; y++) {
int off = (y * HUD_W + x0) * 3;
memcpy(dst + off, src + off, row);
}
}
static void patch_casio(uint8_t *rgb, const now_t *n, hud_tiles_t dirty, int force)
{
const int dw = 70;
const int dh = 132;
const int t = 14;
const uint8_t on_r = 22, on_g = 28, on_b = 16;
const uint8_t off_r = 68, off_g = 80, off_b = 44;
int v[7];
cas_vals(n, v);
for (int i = 0; i < 4; i++) {
if (!force && s_cas_ok && v[i] == s_cas_prev[i]) {
continue;
}
int x, y, w, h;
cas_time_box(i, &x, &y, &w, &h);
digit7(rgb, x, y, dw, dh, t, v[i], on_r, on_g, on_b, off_r, off_g, off_b);
if (dirty) {
hud_tiles_add_rect(dirty, x, y, x + w, y + h);
}
s_cas_prev[i] = v[i];
}
int xs[3], ws[3], dy, th;
cas_date_layout(xs, ws, &dy, &th);
char day[8];
snprintf(day, sizeof day, "%02d", n->mday);
const char *fields[3] = { k_days[n->wday], day, k_mon[n->mon] };
for (int i = 0; i < 3; i++) {
if (!force && s_cas_ok && v[4 + i] == s_cas_prev[4 + i]) {
continue;
}
if (!force) {
copy_rect(rgb, static_src(), xs[i], dy, xs[i] + ws[i], dy + th);
}
hud_text(rgb, xs[i], dy, 6, fields[i], 22, 28, 16, 255);
if (dirty) {
hud_tiles_add_rect(dirty, xs[i], dy, xs[i] + ws[i], dy + th);
}
s_cas_prev[4 + i] = v[4 + i];
}
s_cas_ok = 1;
}
static void slow_casio(uint8_t *rgb, const now_t *n)
{
patch_casio(rgb, n, NULL, 1);
}
static void fast_casio(uint8_t *rgb, const now_t *n)
{
int x0, y0, colon_x, total;
casio_geom(&x0, &y0, &colon_x, &total);
(void)x0;
(void)total;
const uint8_t on_r = 22, on_g = 28, on_b = 16;
const uint8_t off_r = 68, off_g = 80, off_b = 44;
uint8_t cr = n->blink ? on_r : off_r;
uint8_t cg = n->blink ? on_g : off_g;
uint8_t cb = n->blink ? on_b : off_b;
hud_fill_rect(rgb, colon_x, y0 + 36, 12, 16, cr, cg, cb, 255);
hud_fill_rect(rgb, colon_x, y0 + 80, 12, 16, cr, cg, cb, 255);
}
#if 0 /* SKELETON Unitas 6498-1 — restore as face 2 and bump CLOCK_FACES */
/* ---------- 2 skeleton : Unitas / ETA 6498-1 from the dial ---------- */
/* 16.5''' / 36.6 mm, 18,000 vph (2.5 Hz), small seconds at 6.
* Barrel left, centre wheel at the axis, third at 4–5, fourth at 6,
* escape at 8, pallet, annular balance at 12. Bridges 105/110/125/121
* hold the train. https://monochrome-watches.com/history-eta-unitas-6497-and-6498-hand-wound-lepine-savonette-movements-technical-guide/
*/
#define SK_CX 233
#define SK_CY 233
#define SK_BX 152
#define SK_BY 205
#define SK_TX 258
#define SK_TY 288
#define SK_FX 233
#define SK_FY 338
#define SK_EX 198
#define SK_EY 312
#define SK_PX 188
#define SK_PY 230
#define SK_AX 233
#define SK_AY 122
#define SK_NX 304
#define SK_NY 248
static float sk_wrap(float a)
{
const float t = 6.28318530718f;
a = fmodf(a, t);
if (a < 0.0f) {
a += t;
}
return a;
}
static void sk_put(uint8_t *rgb, int x, int y, uint8_t r, uint8_t g, uint8_t b, float cov)
{
if (cov <= 0.0f) {
return;
}
hud_pixel(rgb, x, y, r, g, b, (uint8_t)(cover01(cov) * 255.0f + 0.5f));
}
static void sk_jewel(uint8_t *rgb, int cx, int cy, int rad)
{
hud_fill_circle(rgb, cx, cy, rad + 2, 186, 176, 150, 255);
hud_fill_circle(rgb, cx, cy, rad, 150, 18, 42, 255);
hud_fill_circle(rgb, cx, cy, rad > 2 ? rad - 2 : 1, 196, 48, 70, 255);
hud_fill_circle(rgb, cx - rad / 3, cy - rad / 3, rad > 3 ? 2 : 1, 240, 190, 200, 220);
}
static void sk_screw(uint8_t *rgb, int cx, int cy, int rad)
{
hud_fill_circle(rgb, cx, cy, rad + 1, 90, 96, 104, 255);
hud_fill_circle(rgb, cx, cy, rad, 24, 52, 118, 255);
hud_fill_circle(rgb, cx - 1, cy - 1, rad > 2 ? rad - 2 : 1, 48, 88, 160, 180);
hud_line(rgb, cx - rad + 1, cy, cx + rad - 1, cy, 12, 24, 60, 255);
}
static void sk_wheel(uint8_t *rgb, int cx, int cy, float r_hub, float r_arm,
float r_rim, float r_out, int teeth, int spokes, float rot,
uint8_t wr, uint8_t wg, uint8_t wb)
{
int rmax = (int)(r_out + 2.0f);
float depth = r_out - r_rim;
float span = (spokes > 0) ? (6.28318530718f / (float)spokes) : 1.0f;
float tspan = (teeth > 0) ? (6.28318530718f / (float)teeth) : 1.0f;
for (int y = -rmax; y <= rmax; y++) {
for (int x = -rmax; x <= rmax; x++) {
float d = sqrtf((float)(x * x + y * y));
if (d > r_out + 1.3f) {
continue;
}
float ang = atan2f((float)y, (float)x) - rot;
float cov = 0.0f;
if (d <= r_hub + 0.6f) {
cov = cover01(r_hub + 0.6f - d);
} else if (d < r_arm) {
if (spokes <= 0) {
cov = 1.0f;
} else {
float u = sk_wrap(ang + span * 0.5f);
u = fabsf(fmodf(u, span) - span * 0.5f);
float half = 2.1f - 0.4f * ((d - r_hub) / (r_arm - r_hub + 0.01f));
cov = cover01(half + 0.5f - u * d);
}
} else if (d <= r_out + 0.6f) {
float rim = cover01(d - (r_arm - 0.4f)) * cover01(r_rim + 0.6f - d);
float u = sk_wrap(ang);
u = fmodf(u, tspan) / tspan;
float tooth = cover01(0.46f - fabsf(u - 0.5f) * 2.0f);
float outer = r_rim + tooth * depth;
float tc = cover01(outer + 0.5f - d) * cover01(d - (r_rim - 0.4f));
cov = rim > tc ? rim : tc;
}
if (cov <= 0.0f) {
continue;
}
float light = 0.52f + 0.40f * cosf(atan2f((float)y, (float)x) + 0.95f);
sk_put(rgb, cx + x, cy + y,
(uint8_t)clampi((int)(wr * light), 0, 255),
(uint8_t)clampi((int)(wg * light), 0, 255),
(uint8_t)clampi((int)(wb * light), 0, 255), cov);
}
}
}
static void sk_pinion(uint8_t *rgb, int cx, int cy, float ro, int leaves, float rot)
{
sk_wheel(rgb, cx, cy, ro * 0.28f, ro * 0.40f, ro * 0.70f, ro, leaves, 0, rot,
176, 182, 190);
hud_fill_circle(rgb, cx, cy, 2, 40, 36, 32, 255);
}
static void sk_escape(uint8_t *rgb, int cx, int cy, float ro, float rot)
{
int rmax = (int)(ro + 2.0f);
float r_rim = ro * 0.62f;
const int teeth = 15;
float tspan = 6.28318530718f / (float)teeth;
for (int y = -rmax; y <= rmax; y++) {
for (int x = -rmax; x <= rmax; x++) {
float d = sqrtf((float)(x * x + y * y));
if (d > ro + 1.3f) {
continue;
}
float ang = atan2f((float)y, (float)x) - rot;
float cov;
if (d < r_rim) {
float u = sk_wrap(ang + 0.628f);
u = fabsf(fmodf(u, 1.256637f) - 0.628f);
cov = (d < 4.0f) ? 1.0f : cover01(1.7f - u * d);
} else {
float u = fmodf(sk_wrap(ang), tspan) / tspan;
float club = 0.18f + 0.22f * cover01((d - r_rim) / (ro - r_rim));
cov = cover01(club - fabsf(u - 0.50f)) * cover01(ro + 0.5f - d);
}
if (cov <= 0.0f) {
continue;
}
float light = 0.55f + 0.38f * cosf(atan2f((float)y, (float)x) + 0.8f);
sk_put(rgb, cx + x, cy + y,
(uint8_t)clampi((int)(200.0f * light), 0, 255),
(uint8_t)clampi((int)(206.0f * light), 0, 255),
(uint8_t)clampi((int)(214.0f * light), 0, 255), cov);
}
}
hud_fill_circle(rgb, cx, cy, 3, 36, 34, 32, 255);
}
static void sk_spring(uint8_t *rgb, int cx, int cy, float r0, float r1,
float turns, float rot, float thick,
uint8_t r, uint8_t g, uint8_t b)
{
int steps = (int)(turns * 48.0f);
if (steps < 40) {
steps = 40;
}
float x1 = (float)cx + r0 * cosf(rot);
float y1 = (float)cy + r0 * sinf(rot);
for (int i = 1; i <= steps; i++) {
float t = (float)i / (float)steps;
float a = rot + t * turns * 6.28318530718f;
float rad = r0 + t * (r1 - r0);
float x2 = (float)cx + rad * cosf(a);
float y2 = (float)cy + rad * sinf(a);
hud_line_r(rgb, x1, y1, x2, y2, thick, r, g, b, 255);
x1 = x2;
y1 = y2;
}
}
static void sk_balance(uint8_t *rgb, float rot)
{
int cx = SK_AX;
int cy = SK_AY;
float ro = 52.0f;
float ri = 43.0f;
int rmax = 55;
for (int y = -rmax; y <= rmax; y++) {
for (int x = -rmax; x <= rmax; x++) {
float d = sqrtf((float)(x * x + y * y));
float ang = atan2f((float)y, (float)x) - rot;
float cov = cover01(d - (ri - 0.6f)) * cover01((ro + 0.6f) - d);
if (d < ri) {
float u = sk_wrap(ang);
float spoke = 1e9f;
for (int s = 0; s < 3; s++) {
float sa = sk_wrap(u - (float)s * 2.094395f);
if (sa > 3.14159265f) {
sa = 6.2831853f - sa;
}
float p = sa * d;
if (p < spoke) {
spoke = p;
}
}
cov = cover01(1.8f - spoke) * cover01(d - 5.0f);
}
if (cov <= 0.0f) {
continue;
}
float light = 0.50f + 0.42f * cosf(atan2f((float)y, (float)x) + 0.7f);
sk_put(rgb, cx + x, cy + y,
(uint8_t)clampi((int)(214.0f * light), 0, 255),
(uint8_t)clampi((int)(180.0f * light), 0, 255),
(uint8_t)clampi((int)(98.0f * light), 0, 255), cov);
}
}
hud_fill_circle(rgb, cx, cy, 6, 48, 46, 42, 255);
chrome_annulus(rgb, cx, cy, 2.0f, 4.5f);
float rx = (float)cx + 7.0f * cosf(rot + 1.2f);
float ry = (float)cy + 7.0f * sinf(rot + 1.2f);
hud_fill_circle(rgb, (int)lroundf(rx), (int)lroundf(ry), 2, 180, 30, 50, 255);
sk_spring(rgb, cx, cy, 5.0f, 28.0f, 7.2f, rot * 0.12f, 0.7f, 110, 128, 150);
}
static void sk_pallet(uint8_t *rgb, float ang)
{
float px = (float)SK_PX;
float py = (float)SK_PY;
float dxe = (float)SK_EX - px;
float dye = (float)SK_EY - py;
float le = sqrtf(dxe * dxe + dye * dye);
if (le < 1.0f) {
le = 1.0f;
}
float dxa = (float)SK_AX - px;
float dya = (float)SK_AY - py;
float la = sqrtf(dxa * dxa + dya * dya);
if (la < 1.0f) {
la = 1.0f;
}
float ca = cosf(ang);
float sa = sinf(ang);
float ex = px + dxe * (le - 14.0f) / le + ca * 6.0f;
float ey = py + dye * (le - 14.0f) / le + sa * 6.0f;
float fx = px + dxe * (le - 14.0f) / le - ca * 6.0f;
float fy = py + dye * (le - 14.0f) / le - sa * 6.0f;
float hx = px + dxa * (la - 40.0f) / la;
float hy = py + dya * (la - 40.0f) / la;
hud_line_r(rgb, px, py, ex, ey, 2.0f, 150, 154, 160, 255);
hud_line_r(rgb, px, py, fx, fy, 2.0f, 150, 154, 160, 255);
hud_line_r(rgb, px, py, hx, hy, 1.7f, 150, 154, 160, 255);
hud_fill_circle(rgb, (int)lroundf(ex), (int)lroundf(ey), 3, 176, 28, 48, 255);
hud_fill_circle(rgb, (int)lroundf(fx), (int)lroundf(fy), 3, 176, 28, 48, 255);
hud_fill_circle(rgb, SK_PX, SK_PY, 3, 40, 38, 36, 255);
sk_jewel(rgb, SK_PX, SK_PY, 3);
}
static void sk_bridge(uint8_t *rgb, int x0, int y0, int x1, int y1, float rad)
{
int minx = (x0 < x1 ? x0 : x1) - (int)rad - 2;
int maxx = (x0 > x1 ? x0 : x1) + (int)rad + 2;
int miny = (y0 < y1 ? y0 : y1) - (int)rad - 2;
int maxy = (y0 > y1 ? y0 : y1) + (int)rad + 2;
float dx = (float)(x1 - x0);
float dy = (float)(y1 - y0);
float len2 = dx * dx + dy * dy;
if (len2 < 1.0f) {
len2 = 1.0f;
}
for (int y = miny; y <= maxy; y++) {
for (int x = minx; x <= maxx; x++) {
if (!hud_in_disk(x, y)) {
continue;
}
float t = ((float)(x - x0) * dx + (float)(y - y0) * dy) / len2;
t = cover01(t);
float px = (float)x0 + t * dx - (float)x;
float py = (float)y0 + t * dy - (float)y;
float d = sqrtf(px * px + py * py);
float cov = cover01(rad + 0.6f - d);
if (cov <= 0.0f) {
continue;
}
float stripe = 0.50f + 0.12f * sinf(((float)x * 0.38f + (float)y * 0.92f));
float bevel = 1.0f + 0.35f * cover01(d - (rad - 2.4f));
int v = (int)((168.0f + stripe * 40.0f) * bevel);
sk_put(rgb, x, y,
(uint8_t)clampi(v, 0, 255),
(uint8_t)clampi(v + 4, 0, 255),
(uint8_t)clampi(v + 10, 0, 255), cov);
}
}
}
static void sk_perlage(uint8_t *rgb)
{
const float cell = 13.0f;
for (int y = 0; y < HUD_H; y++) {
for (int x = 0; x < HUD_W; x++) {
if (!hud_in_disk(x, y)) {
uint8_t *p = rgb + (y * HUD_W + x) * 3;
p[0] = p[1] = p[2] = 0;
continue;
}
float dx = (float)(x - SK_CX);
float dy = (float)(y - SK_CY);
float rd = sqrtf(dx * dx + dy * dy);
if (rd > 188.0f) {
continue;
}
int gx = (int)floorf((float)x / cell);
int gy = (int)floorf((float)y / cell);
float best = 99.0f;
for (int oy = -1; oy <= 1; oy++) {
for (int ox = -1; ox <= 1; ox++) {
float cx = ((float)(gx + ox) + ((gy + oy) & 1 ? 0.5f : 0.0f)) * cell;
float cy = (float)(gy + oy) * cell;
float ddx = (float)x - cx;
float ddy = (float)y - cy;
float d = sqrtf(ddx * ddx + ddy * ddy);
if (d < best) {
best = d;
}
}
}
float pearl = cover01(1.0f - best / 7.2f);
int v = 30 + (int)(pearl * 26.0f) + (best < 1.4f ? 14 : 0);
float edge = cover01((188.0f - rd) * 0.08f);
v = (int)(v * (0.55f + 0.45f * edge));
uint8_t *p = rgb + (y * HUD_W + x) * 3;
p[0] = (uint8_t)clampi(v, 0, 255);
p[1] = (uint8_t)clampi(v + 3, 0, 255);
p[2] = (uint8_t)clampi(v + 8, 0, 255);
}
}
}
static void sk_chapter(uint8_t *rgb)
{
chrome_annulus(rgb, SK_CX, SK_CY, 214.0f, 230.0f);
chrome_annulus(rgb, SK_CX, SK_CY, 198.0f, 206.0f);
for (int i = 0; i < 60; i++) {
float a = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 30.0f;
if ((i % 5) == 0) {
tapered(rgb, a, 182.0f, 196.0f, 1.8f, 1.5f, 210, 200, 176, 255);
} else {
tapered(rgb, a, 190.0f, 196.0f, 0.7f, 0.6f, 150, 144, 128, 220);
}
}
}
static void sk_crystal(uint8_t *rgb)
{
for (int y = 0; y < HUD_H; y++) {
for (int x = 0; x < HUD_W; x++) {
if (!hud_in_disk(x, y)) {
continue;
}
float nx = (float)(x - SK_CX) / (float)HUD_R;
float ny = (float)(y - SK_CY) / (float)HUD_R;
float d2 = nx * nx + ny * ny;
float spec = 0.55f - (nx + 0.35f) * (nx + 0.35f) * 1.6f
- (ny + 0.48f) * (ny + 0.48f) * 1.1f;
if (spec < 0.0f) {
spec = 0.0f;
}
float edge = cover01(d2 * 1.15f - 0.72f) * 0.16f;
uint8_t *p = rgb + (y * HUD_W + x) * 3;
int add = (int)(spec * 22.0f);
p[0] = (uint8_t)clampi(p[0] + add - (int)(edge * p[0]), 0, 255);
p[1] = (uint8_t)clampi(p[1] + add - (int)(edge * p[1]), 0, 255);
p[2] = (uint8_t)clampi(p[2] + add + (int)(spec * 6.0f) - (int)(edge * p[2]), 0, 255);
}
}
}
static void static_skeleton(uint8_t *rgb)
{
sk_perlage(rgb);
sk_bridge(rgb, 118, 155, 190, 250, 26.0f);
sk_bridge(rgb, 210, 210, 275, 305, 16.0f);
sk_bridge(rgb, 233, 250, 233, 360, 15.0f);
sk_bridge(rgb, 170, 200, 210, 250, 11.0f);
sk_bridge(rgb, 190, 88, 280, 155, 20.0f);
sk_jewel(rgb, SK_BX, SK_BY, 5);
sk_jewel(rgb, SK_CX, SK_CY, 4);
sk_jewel(rgb, SK_TX, SK_TY, 4);
sk_jewel(rgb, SK_FX, SK_FY, 4);
sk_jewel(rgb, SK_EX, SK_EY, 4);
sk_jewel(rgb, SK_PX, SK_PY, 3);
sk_jewel(rgb, SK_AX, SK_AY, 5);
sk_screw(rgb, 128, 162, 4);
sk_screw(rgb, 150, 232, 4);
sk_screw(rgb, 184, 248, 4);
sk_screw(rgb, 220, 218, 3);
sk_screw(rgb, 272, 300, 4);
sk_screw(rgb, 233, 358, 4);
sk_screw(rgb, 176, 214, 3);
sk_screw(rgb, 204, 248, 3);
sk_screw(rgb, 198, 96, 4);
sk_screw(rgb, 272, 148, 4);
hud_fill_circle(rgb, SK_AX - 34, SK_AY + 6, 7, 40, 42, 48, 255);
hud_arc(rgb, SK_AX - 34, SK_AY + 6, 10.0f, 1.6f, 0.8f, 3.4f, 200, 196, 180, 255);
hud_fill_circle(rgb, SK_AX - 40, SK_AY + 2, 2, 24, 52, 110, 255);
hud_fill_circle(rgb, SK_AX - 28, SK_AY + 12, 2, 24, 52, 110, 255);
for (int i = 0; i < 12; i++) {
float a = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 6.0f;
int x0 = SK_FX + (int)lroundf(cosf(a) * 22.0f);
int y0 = SK_FY + (int)lroundf(sinf(a) * 22.0f);
int x1 = SK_FX + (int)lroundf(cosf(a) * 26.0f);
int y1 = SK_FY + (int)lroundf(sinf(a) * 26.0f);
hud_line(rgb, x0, y0, x1, y1, 180, 170, 140, 200);
}
sk_chapter(rgb);
sk_crystal(rgb);
}
static void slow_skeleton(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
}
static void fast_skeleton(uint8_t *rgb, const now_t *n)
{
float t = (float)n->h12 * 3600.0f + (float)n->min * 60.0f + n->secf;
const float twopi = 6.28318530718f;
float a_barrel = -t * twopi / (8.0f * 3600.0f);
float a_centre = t * twopi / 3600.0f;
float a_third = -t * twopi / 3600.0f * 8.0f;
float a_fourth = t * twopi / 60.0f;
float a_escape = -t * twopi * 10.0f / 60.0f;
float a_minw = -t * twopi / 3600.0f * 3.0f;
float a_hourw = t * twopi / (12.0f * 3600.0f);
float a_bal = 4.60f * sinf(twopi * 2.5f * n->secf);
float a_pal = (sinf(twopi * 2.5f * n->secf) >= 0.0f) ? 0.16f : -0.16f;
sk_wheel(rgb, SK_BX, SK_BY, 18.0f, 26.0f, 58.0f, 66.0f, 84, 0, a_barrel,
186, 142, 58);
sk_spring(rgb, SK_BX, SK_BY, 12.0f, 52.0f, 5.6f, a_barrel * 0.15f, 1.2f,
168, 150, 96);
hud_fill_circle(rgb, SK_BX, SK_BY, 8, 48, 44, 38, 255);
sk_jewel(rgb, SK_BX, SK_BY, 4);
sk_wheel(rgb, SK_CX, SK_CY, 8.0f, 20.0f, 40.0f, 48.0f, 64, 5, a_centre,
198, 156, 72);
sk_pinion(rgb, SK_CX, SK_CY, 14.0f, 12, a_centre);
sk_wheel(rgb, SK_TX, SK_TY, 6.0f, 16.0f, 30.0f, 36.0f, 75, 5, a_third,
198, 156, 72);
sk_pinion(rgb, SK_TX, SK_TY, 11.0f, 10, a_third);
sk_wheel(rgb, SK_FX, SK_FY, 6.0f, 14.0f, 24.0f, 30.0f, 60, 5, a_fourth,
198, 156, 72);
sk_pinion(rgb, SK_FX, SK_FY, 10.0f, 10, a_fourth);
sk_wheel(rgb, SK_NX, SK_NY, 5.0f, 10.0f, 18.0f, 22.0f, 36, 5, a_minw,
190, 150, 70);
sk_wheel(rgb, SK_CX, SK_CY, 4.0f, 8.0f, 15.0f, 18.0f, 40, 5, a_hourw,
176, 136, 64);
sk_escape(rgb, SK_EX, SK_EY, 16.0f, a_escape);
sk_pinion(rgb, SK_EX, SK_EY, 8.0f, 8, a_escape);
sk_pallet(rgb, a_pal);
sk_balance(rgb, a_bal);
sk_jewel(rgb, SK_CX, SK_CY, 3);
sk_jewel(rgb, SK_TX, SK_TY, 3);
sk_jewel(rgb, SK_FX, SK_FY, 3);
sk_jewel(rgb, SK_EX, SK_EY, 3);
sk_jewel(rgb, SK_AX, SK_AY, 4);
hud_fill_circle(rgb, SK_AX - 34, SK_AY + 6, 6, 36, 38, 44, 230);
hud_arc(rgb, SK_AX - 34, SK_AY + 6, 9.0f, 1.5f, 0.8f, 3.4f, 204, 200, 184, 255);
hud_fill_circle(rgb, SK_AX - 40, SK_AY + 2, 2, 24, 52, 110, 255);
hud_fill_circle(rgb, SK_AX - 28, SK_AY + 12, 2, 24, 52, 110, 255);
analog_hm(rgb, n, 92.0f, 128.0f, 220, 224, 230);
float sa = -(float)M_PI / 2.0f + n->secf * (float)M_PI / 30.0f;
tapered_at(rgb, SK_FX, SK_FY, sa, -4.0f, 22.0f, 1.1f, 0.7f, 36, 72, 160, 255);
hud_fill_circle(rgb, SK_FX, SK_FY, 3, 200, 204, 210, 255);
hud_fill_circle(rgb, SK_CX, SK_CY, 5, 200, 204, 210, 255);
hud_fill_circle(rgb, SK_CX, SK_CY, 2, 36, 72, 160, 255);
}
#endif
/* ---------- 2 neon ---------- */
static void static_neon(uint8_t *rgb)
{
hud_clear(rgb, 4, 6, 12);
hud_ring(rgb, HUD_CX, HUD_CY, 222.0f, 3.0f, 0, 220, 255, 180);
hud_ring(rgb, HUD_CX, HUD_CY, 188.0f, 1.4f, 255, 40, 160, 120);
for (int i = 0; i < 60; i++) {
float a = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 30.0f;
if ((i % 5) == 0) {
tapered(rgb, a, 196.0f, 214.0f, 1.8f, 1.4f, 0, 255, 230, 255);
} else {
tapered(rgb, a, 206.0f, 214.0f, 0.8f, 0.7f, 0, 140, 160, 180);
}
}
hud_fill_circle(rgb, HUD_CX, HUD_CY, 7, 0, 255, 220, 255);
}
static float s_neon_a1 = -100.0f;
static void slow_neon(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
s_neon_a1 = -100.0f;
}
static void fast_neon(uint8_t *rgb, const now_t *n)
{
float sa = -(float)M_PI / 2.0f + n->secf * (float)M_PI / 30.0f;
float a0 = s_neon_a1;
if (a0 < -10.0f || sa + 0.02f < a0) {
a0 = -(float)M_PI / 2.0f;
} else {
a0 -= 0.03f;
}
if (sa > a0) {
hud_arc(rgb, HUD_CX, HUD_CY, 176.0f, 5.0f, a0, sa, 255, 40, 180, 200);
if (s_cache) {
hud_arc(s_cache, HUD_CX, HUD_CY, 176.0f, 5.0f, a0, sa, 255, 40, 180, 200);
}
s_neon_a1 = sa;
}
analog_hm(rgb, n, 100.0f, 150.0f, 180, 255, 255);
analog_s(rgb, n, 166.0f, 255, 60, 200);
}
/* ---------- 4 bauhaus ---------- */
static void static_bauhaus(uint8_t *rgb)
{
hud_clear(rgb, 236, 230, 218);
hud_ring(rgb, HUD_CX, HUD_CY, 226.0f, 8.0f, 20, 20, 22, 255);
for (int i = 0; i < 60; i++) {
float a = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 30.0f;
if (i == 0) {
tapered(rgb, a, 160.0f, 206.0f, 6.4f, 6.4f, 16, 16, 18, 255);
} else if ((i % 5) == 0) {
tapered(rgb, a, 168.0f, 206.0f, 3.2f, 3.2f, 16, 16, 18, 255);
} else {
tapered(rgb, a, 196.0f, 206.0f, 1.2f, 1.2f, 16, 16, 18, 220);
}
}
hud_fill_circle(rgb, HUD_CX, HUD_CY, 8, 16, 16, 18, 255);
hud_fill_circle(rgb, HUD_CX, HUD_CY, 3, 200, 32, 28, 255);
}
static void slow_bauhaus(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
}
static void fast_bauhaus(uint8_t *rgb, const now_t *n)
{
analog_hm(rgb, n, 112.0f, 158.0f, 16, 16, 18);
analog_s(rgb, n, 176.0f, 200, 32, 28);
}
/* ---------- 5 led ---------- */
static void static_led(uint8_t *rgb)
{
const int r_max2 = HUD_CX * HUD_CX;
const int er = 80, eg = 12, eb = 10;
for (int y = 0; y < HUD_H; y++) {
int dy = y - HUD_CY;
for (int x = 0; x < HUD_W; x++) {
uint8_t *p = rgb + (y * HUD_W + x) * 3;
if (!hud_in_disk(x, y)) {
p[0] = p[1] = p[2] = 0;
continue;
}
int dx = x - HUD_CX;
int d2 = dx * dx + dy * dy;
int t = d2 >= r_max2 ? 256 : (d2 * 256) / r_max2;
p[0] = (uint8_t)((er * t) >> 8);
p[1] = (uint8_t)((eg * t) >> 8);
p[2] = (uint8_t)((eb * t) >> 8);
}
}
}
static void led_vals(const now_t *n, int v[14])
{
int year = n->year;
int mon = n->mon + 1;
int day = n->mday;
v[0] = n->h24 / 10;
v[1] = n->h24 % 10;
v[2] = n->min / 10;
v[3] = n->min % 10;
v[4] = year / 1000;
v[5] = (year / 100) % 10;
v[6] = (year / 10) % 10;
v[7] = year % 10;
v[8] = -1;
v[9] = mon / 10;
v[10] = mon % 10;
v[11] = -1;
v[12] = day / 10;
v[13] = day % 10;
}
static void led_box(int i, int *x, int *y, int *w, int *h, int *t)
{
if (i < 4) {
static const int xs[4] = { 52, 132, 264, 344 };
*x = xs[i];
*y = 184;
*w = 70;
*h = 100;
*t = 12;
return;
}
const int dw = 20;
const int dg = 2;
*w = dw;
*h = 28;
*t = 4;
*y = 314;
*x = (HUD_W - (10 * dw + 9 * dg)) / 2 + (i - 4) * (dw + dg);
}
static void patch_led(uint8_t *rgb, const now_t *n, hud_tiles_t dirty, int force)
{
const uint8_t on_r = 255, on_g = 32, on_b = 24;
const uint8_t off_r = 48, off_g = 8, off_b = 6;
int v[14];
led_vals(n, v);
for (int i = 0; i < 14; i++) {
if (!force && s_led_ok && v[i] == s_led_prev[i]) {
continue;
}
int x, y, w, h, t;
led_box(i, &x, &y, &w, &h, &t);
digit7(rgb, x, y, w, h, t, v[i], on_r, on_g, on_b, off_r, off_g, off_b);
if (dirty) {
hud_tiles_add_rect(dirty, x, y, x + w, y + h);
}
s_led_prev[i] = v[i];
}
s_led_ok = 1;
}
static void slow_led(uint8_t *rgb, const now_t *n)
{
patch_led(rgb, n, NULL, 1);
}
static void fast_led(uint8_t *rgb, const now_t *n)
{
const uint8_t on_r = 255, on_g = 32, on_b = 24;
const uint8_t off_r = 48, off_g = 8, off_b = 6;
uint8_t cr = n->blink ? on_r : off_r;
uint8_t cg = n->blink ? on_g : off_g;
uint8_t cb = n->blink ? on_b : off_b;
hud_fill_circle(rgb, HUD_CX, 214, 6, cr, cg, cb, 255);
hud_fill_circle(rgb, HUD_CX, 252, 6, cr, cg, cb, 255);
}
/* ---------- 8 orbit ---------- */
static void static_orbit(uint8_t *rgb)
{
for (int y = 0; y < HUD_H; y++) {
for (int x = 0; x < HUD_W; x++) {
uint8_t *p = rgb + (y * HUD_W + x) * 3;
if (!hud_in_disk(x, y)) {
p[0] = p[1] = p[2] = 0;
continue;
}
unsigned h = hash2(x, y);
if ((h & 0x1FF) < 4) {
int v = 140 + (int)(h & 63);
p[0] = p[1] = p[2] = (uint8_t)v;
} else {
p[0] = 4;
p[1] = 6;
p[2] = 14;
}
}
}
hud_ring(rgb, HUD_CX, HUD_CY, 70.0f, 1.2f, 80, 90, 140, 140);
hud_ring(rgb, HUD_CX, HUD_CY, 120.0f, 1.2f, 80, 90, 140, 140);
hud_ring(rgb, HUD_CX, HUD_CY, 170.0f, 1.2f, 80, 90, 140, 140);
hud_fill_circle(rgb, HUD_CX, HUD_CY, 18, 255, 200, 60, 255);
hud_fill_circle(rgb, HUD_CX - 4, HUD_CY - 4, 6, 255, 240, 180, 180);
}
static void slow_orbit(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
}
static void fast_orbit(uint8_t *rgb, const now_t *n)
{
float ha = -(float)M_PI / 2.0f + n->hourf * (float)M_PI / 6.0f;
float ma = -(float)M_PI / 2.0f + n->minf * (float)M_PI / 30.0f;
float sa = -(float)M_PI / 2.0f + n->secf * (float)M_PI / 30.0f;
int hx = HUD_CX + (int)lroundf(cosf(ha) * 170.0f);
int hy = HUD_CY + (int)lroundf(sinf(ha) * 170.0f);
int mx = HUD_CX + (int)lroundf(cosf(ma) * 120.0f);
int my = HUD_CY + (int)lroundf(sinf(ma) * 120.0f);
int sx = HUD_CX + (int)lroundf(cosf(sa) * 70.0f);
int sy = HUD_CY + (int)lroundf(sinf(sa) * 70.0f);
hud_fill_circle(rgb, hx, hy, 14, 80, 160, 255, 255);
hud_fill_circle(rgb, hx - 3, hy - 2, 4, 160, 200, 255, 180);
hud_fill_circle(rgb, mx, my, 10, 90, 200, 120, 255);
hud_fill_circle(rgb, sx, sy, 6, 200, 200, 210, 255);
}
/* ---------- 9 vector / crt ---------- */
static void static_crt(uint8_t *rgb)
{
for (int y = 0; y < HUD_H; y++) {
for (int x = 0; x < HUD_W; x++) {
uint8_t *p = rgb + (y * HUD_W + x) * 3;
if (!hud_in_disk(x, y)) {
p[0] = p[1] = p[2] = 0;
continue;
}
int v = (y & 1) ? 4 : 10;
p[0] = 0;
p[1] = (uint8_t)v;
p[2] = 2;
}
}
hud_circle(rgb, HUD_CX, HUD_CY, 210, 0, 80, 40, 180);
for (int i = 0; i < 12; i++) {
float a = -(float)M_PI / 2.0f + (float)i * (float)M_PI / 6.0f;
if (i == 0) {
tapered(rgb, a, 180.0f, 206.0f, 4.2f, 3.6f, 0, 220, 90, 200);
} else {
tapered(rgb, a, 188.0f, 206.0f, 1.4f, 1.2f, 0, 220, 90, 200);
}
}
hud_fill_circle(rgb, HUD_CX, HUD_CY, 4, 80, 255, 120, 255);
}
static void slow_crt(uint8_t *rgb, const now_t *n)
{
(void)rgb;
(void)n;
}
static void fast_crt(uint8_t *rgb, const now_t *n)
{
float ha = -(float)M_PI / 2.0f + n->hourf * (float)M_PI / 6.0f;
float ma = -(float)M_PI / 2.0f + n->minf * (float)M_PI / 30.0f;
float sa = -(float)M_PI / 2.0f + n->secf * (float)M_PI / 30.0f;
tapered(rgb, ha, 0.0f, 100.0f, 4.0f, 2.0f, 0, 80, 30, 160);
tapered(rgb, ma, 0.0f, 150.0f, 3.0f, 1.4f, 0, 80, 30, 140);
tapered(rgb, ha, 0.0f, 98.0f, 1.8f, 1.0f, 40, 255, 110, 255);
tapered(rgb, ma, 0.0f, 148.0f, 1.4f, 0.8f, 40, 255, 110, 255);
tapered(rgb, sa, 0.0f, 168.0f, 2.2f, 1.2f, 0, 80, 30, 120);
tapered(rgb, sa, 0.0f, 166.0f, 1.0f, 0.7f, 180, 255, 160, 255);
}
/* ---------- dispatch ---------- */
static void draw_static_face(uint8_t *rgb, int face)
{
esp_task_wdt_reset();
switch (face) {
case 1: static_casio(rgb); break;
case 2: static_neon(rgb); break;
case 3: static_bauhaus(rgb); break;
case 4: static_led(rgb); break;
case 5: static_orbit(rgb); break;
case 6: static_crt(rgb); break;
default: static_wood(rgb); break;
}
}
static const uint8_t *static_src(void)
{
if (s_all_static) {
return s_static[s_face];
}
return s_static[0];
}
static void paint_slow(uint8_t *rgb, const now_t *n)
{
now_t snapped = *n;
snapped.secf = 0.0f;
snapped.minf = (float)n->min;
snapped.hourf = (float)n->h12 + snapped.minf / 60.0f;
switch (s_face) {
case 1: slow_casio(rgb, n); break;
case 2: slow_neon(rgb, &snapped); break;
case 3: slow_bauhaus(rgb, &snapped); break;
case 4: slow_led(rgb, n); break;
case 5: slow_orbit(rgb, &snapped); break;
case 6: slow_crt(rgb, &snapped); break;
default: slow_wood(rgb, &snapped); break;
}
}
static void paint_fast(uint8_t *rgb, const now_t *n)
{
switch (s_face) {
case 1: fast_casio(rgb, n); break;
case 2: fast_neon(rgb, n); break;
case 3: fast_bauhaus(rgb, n); break;
case 4: fast_led(rgb, n); break;
case 5: fast_orbit(rgb, n); break;
case 6: fast_crt(rgb, n); break;
default: fast_wood(rgb, n); break;
}
}
static int slow_changed(const now_t *n)
{
return !s_slow_ok
|| n->h24 != s_slow.h24
|| n->min != s_slow.min
|| n->wday != s_slow.wday
|| n->mday != s_slow.mday
|| n->mon != s_slow.mon
|| n->year != s_slow.year;
}
static void slow_commit(const now_t *n)
{
s_slow.h24 = n->h24;
s_slow.min = n->min;
s_slow.wday = n->wday;
s_slow.mday = n->mday;
s_slow.mon = n->mon;
s_slow.year = n->year;
s_slow_ok = 1;
}
static void copy_static_to(uint8_t *dst)
{
const uint8_t *src = static_src();
if (src) {
memcpy(dst, src, k_fb);
} else {
draw_static_face(dst, s_face);
}
}
static void bake_slow(const now_t *n)
{
if (!s_cache) {
return;
}
copy_static_to(s_cache);
paint_slow(s_cache, n);
s_cache_ok = 1;
slow_commit(n);
}
static void rebuild_cache(void)
{
if (!s_all_static && s_static[0]) {
s_prerender = 1;
draw_static_face(s_static[0], s_face);
s_prerender = 0;
}
if (s_cache) {
copy_static_to(s_cache);
s_cache_ok = 1;
} else {
s_cache_ok = 0;
}
s_slow_ok = 0;
s_led_ok = 0;
s_cas_ok = 0;
s_ready = 0;
}
static uint8_t *alloc_fb(void)
{
return (uint8_t *)heap_caps_malloc(k_fb, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
}
static void free_static_faces(void)
{
if (s_static_slab) {
heap_caps_free(s_static_slab);
s_static_slab = NULL;
} else {
for (int i = 0; i < CLOCK_FACES; i++) {
if (s_static[i]) {
heap_caps_free(s_static[i]);
}
}
}
for (int i = 0; i < CLOCK_FACES; i++) {
s_static[i] = NULL;
}
s_all_static = 0;
}
static int alloc_all_statics(void)
{
uint8_t *slab = (uint8_t *)heap_caps_malloc(k_fb * (size_t)CLOCK_FACES,
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (slab) {
s_static_slab = slab;
for (int i = 0; i < CLOCK_FACES; i++) {
s_static[i] = slab + (size_t)i * k_fb;
}
s_all_static = 1;
return 1;
}
for (int i = 0; i < CLOCK_FACES; i++) {
s_static[i] = alloc_fb();
if (!s_static[i]) {
free_static_faces();
return 0;
}
}
s_all_static = 1;
return 1;
}
static void prerender_statics(void)
{
s_prerender = 1;
if (s_all_static) {
for (int i = 0; i < CLOCK_FACES; i++) {
draw_static_face(s_static[i], i);
vTaskDelay(1);
}
} else if (s_static[0]) {
draw_static_face(s_static[0], s_face);
}
s_prerender = 0;
}
static void seed_time_if_needed(void)
{
time_t now = time(NULL);
if (now >= 1700000000) {
return;
}
struct tm t;
memset(&t, 0, sizeof t);
char mon[4];
int day = 0, year = 0, h = 0, m = 0, s = 0;
static const char k_months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
if (sscanf(__DATE__, "%3s %d %d", mon, &day, &year) != 3) {
return;
}
if (sscanf(__TIME__, "%d:%d:%d", &h, &m, &s) != 3) {
return;
}
const char *p = strstr(k_months, mon);
t.tm_mon = p ? (int)(p - k_months) / 3 : 0;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_hour = h;
t.tm_min = m;
t.tm_sec = s;
t.tm_isdst = -1;
time_t compiled = mktime(&t);
if (compiled > 0) {
struct timeval tv = { .tv_sec = compiled, .tv_usec = 0 };
settimeofday(&tv, NULL);
}
}
int clock_init(void)
{
setenv("TZ", "GMT-1", 1);
tzset();
seed_time_if_needed();
s_face = 0;
size_t free0 = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
size_t big0 = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
if (!alloc_all_statics()) {
s_static[0] = alloc_fb();
}
prerender_statics();
s_cache = alloc_fb();
rebuild_cache();
size_t free1 = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
ESP_LOGI(TAG, "static bg %s cache=%s fb=%u spiram %u->%u largest was %u",
s_all_static ? "all prerendered" : (s_static[0] ? "current" : "none"),
s_cache ? "ok" : "none",
(unsigned)k_fb,
(unsigned)free0, (unsigned)free1, (unsigned)big0);
return s_cache ? 0 : -1;
}
void clock_next_face(void)
{
s_face = (s_face + 1) % CLOCK_FACES;
rebuild_cache();
s_blob_t0 = 0;
s_blob_pending = 1;
s_blob_prev[0] = s_blob_prev[1] = s_blob_prev[2] = s_blob_prev[3] = 0;
}
const char *clock_face_name(void)
{
return k_names[s_face];
}
int clock_face_index(void)
{
return s_face;
}
const uint8_t *clock_background(void)
{
return s_cache;
}
void clock_frame(uint8_t *rgb, hud_tiles_t dirty, int *full)
{
hud_tiles_t now;
now_t n = read_now();
if (!s_ready || !s_cache) {
if (s_cache) {
bake_slow(&n);
memcpy(rgb, s_cache, k_fb);
} else {
copy_static_to(rgb);
paint_slow(rgb, &n);
}
hud_tiles_begin(now);
paint_fast(rgb, &n);
hud_tiles_end();
hud_tiles_all(dirty);
memcpy(s_occ, now, sizeof now);
s_ready = s_cache ? 1 : 0;
*full = 1;
return;
}
if (slow_changed(&n)) {
hud_tiles_t changed;
memset(changed, 0, sizeof changed);
if ((s_face == 1 || s_face == 4) && s_slow_ok) {
if (s_face == 1) {
patch_casio(s_cache, &n, changed, 0);
} else {
patch_led(s_cache, &n, changed, 0);
}
hud_restore_tiles(rgb, s_cache, changed);
slow_commit(&n);
} else {
bake_slow(&n);
hud_tiles_diff(changed, rgb, s_cache);
hud_restore_tiles(rgb, s_cache, changed);
}
if (s_blob_pending) {
s_blob_t0 = esp_timer_get_time();
s_blob_pending = 0;
}
hud_tiles_begin(now);
paint_fast(rgb, &n);
draw_face_blob(rgb, now);
hud_tiles_end();
hud_tiles_or(dirty, s_occ, now);
hud_tiles_or(dirty, dirty, changed);
memcpy(s_occ, now, sizeof now);
*full = 0;
return;
}
if (s_blob_pending) {
s_blob_t0 = esp_timer_get_time();
s_blob_pending = 0;
}
hud_tiles_begin(now);
paint_fast(rgb, &n);
draw_face_blob(rgb, now);
hud_tiles_end();
hud_tiles_or(dirty, s_occ, now);
memcpy(s_occ, now, sizeof now);
*full = 0;
}