Files
esp32vga/vga_test/main/content.c
T
jimmyandClaude Opus 5.5 c307f08de8 Add ESP32-S3 VGA firmware with web interface
800x600 RGB332 VGA output for the XIAO ESP32S3 Plus, double buffered in
PSRAM. Includes content modes (test pattern, plasma, stars, clock,
Mandelbrot, message, images, Halloween, Haunting), a flash image gallery,
WiFi AP+STA web UI and OTA updates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 22:05:32 +13:00

1191 lines
40 KiB
C

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/param.h>
#include "esp_heap_caps.h"
#include "esp_random.h"
#include "esp_timer.h"
#include "content.h"
#include "display.h"
#define WHITE rgb332(7, 7, 3)
#define BLACK 0
static const struct {
const char *key, *name;
} content_names[CONTENT_COUNT] = {
[CONTENT_TEST] = {"test", "Test pattern"},
[CONTENT_PLASMA] = {"plasma", "Plasma"},
[CONTENT_STARS] = {"stars", "Starfield"},
[CONTENT_CLOCK] = {"clock", "Clock"},
[CONTENT_MANDEL] = {"mandel", "Mandelbrot"},
[CONTENT_MESSAGE] = {"message", "Message"},
[CONTENT_IMAGE] = {"image", "Image"},
[CONTENT_HALLOWEEN] = {"halloween", "Halloween scene"},
[CONTENT_HAUNT] = {"haunt", "Haunting (projection)"},
};
const char *content_key(content_id_t c)
{
return c < CONTENT_COUNT ? content_names[c].key : "?";
}
const char *content_name(content_id_t c)
{
return c < CONTENT_COUNT ? content_names[c].name : "?";
}
int content_from_key(const char *key)
{
for (int i = 0; i < CONTENT_COUNT; i++) {
if (strcmp(key, content_names[i].key) == 0) {
return i;
}
}
return -1;
}
static void text_centred(const font_t *f, int scale, int y, const char *s, uint8_t fg, int bg)
{
gfx_text(canvas, f, scale, (W - gfx_text_width(f, scale, s)) / 2, y, s, fg, bg);
}
// Hue 0-255 around the colour wheel, full saturation
static uint8_t hue332(int hue)
{
int h = (hue & 255) * 6;
int seg = h >> 8, f = h & 255;
int r, g, b;
switch (seg) {
case 0: r = 255; g = f; b = 0; break;
case 1: r = 255 - f; g = 255; b = 0; break;
case 2: r = 0; g = 255; b = f; break;
case 3: r = 0; g = 255 - f; b = 255; break;
case 4: r = f; g = 0; b = 255; break;
default: r = 255; g = 0; b = 255 - f; break;
}
return rgb888_to_332(r, g, b);
}
// ---------------------------------------------------------------------------
// Test pattern with network info and bouncing squares
#define NUM_SPRITES 6
typedef struct {
int x, y, size, dx, dy;
uint8_t colour;
} sprite_t;
typedef struct {
int x, y, size;
} rect_t;
static sprite_t sprites[NUM_SPRITES];
static rect_t sprites_drawn[2][NUM_SPRITES];
static void test_enter(const show_t *s)
{
memset(canvas, 0, W * H);
const uint8_t bars[8] = {
rgb332(7, 7, 3), rgb332(7, 7, 0), rgb332(0, 7, 3), rgb332(0, 7, 0),
rgb332(7, 0, 3), rgb332(7, 0, 0), rgb332(0, 0, 3), rgb332(0, 0, 0),
};
const int bw = W / 8, rh = H / 12;
for (int i = 0; i < 8; i++) {
gfx_fill(canvas, i * bw, 0, bw, 5 * rh, bars[i]);
}
// Ramps: red, green (8 steps), blue (4 steps), grey (8 steps)
const int y0 = 5 * rh + rh / 4;
for (int i = 0; i < 8; i++) {
gfx_fill(canvas, i * bw, y0, bw, rh, rgb332(i, 0, 0));
gfx_fill(canvas, i * bw, y0 + rh, bw, rh, rgb332(0, i, 0));
gfx_fill(canvas, i * bw, y0 + 3 * rh, bw, rh, rgb332(i, i, i >> 1));
}
for (int i = 0; i < 4; i++) {
gfx_fill(canvas, i * 2 * bw, y0 + 2 * rh, 2 * bw, rh, rgb332(0, 0, i));
}
// How to reach the web page
net_info_t n;
net_get_info(&n);
char line[200];
int y = y0 + 4 * rh + (H - (y0 + 4 * rh) - 64) / 2;
uint8_t dim = rgb332(5, 5, 2), hi = rgb332(7, 7, 0);
snprintf(line, sizeof(line), "WiFi \"%s\" password \"%s\" then open http://%s", n.ap_ssid, n.ap_pass, n.ap_ip);
text_centred(&font16, 1, y, line, dim, -1);
if (n.sta_connected) {
snprintf(line, sizeof(line), "On \"%s\": http://%s", n.sta_ssid, n.sta_ip);
text_centred(&font16, 1, y + 24, line, hi, -1);
} else if (n.sta_ssid[0]) {
snprintf(line, sizeof(line), "Connecting to \"%s\"...", n.sta_ssid);
text_centred(&font16, 1, y + 24, line, dim, -1);
} else {
text_centred(&font16, 1, y + 24, "Add your home WiFi in the web page's Settings", dim, -1);
}
snprintf(line, sizeof(line), "%dx%d @ %.1f Hz", W, H, REFRESH_HZ);
text_centred(&font16, 1, y + 48, line, rgb332(3, 3, 1), -1);
// White outline to check the picture is centred and nothing is cut off
gfx_fill(canvas, 0, 0, W - 1, 1, WHITE);
gfx_fill(canvas, 0, H - 1, W - 1, 1, WHITE);
gfx_fill(canvas, 0, 0, 1, H, WHITE);
gfx_fill(canvas, W - 2, 0, 1, H, WHITE);
// Last column stays black so the colour lines idle at black during blanking
gfx_fill(canvas, W - 1, 0, 1, H, BLACK);
const sprite_t init[NUM_SPRITES] = {
{ 20, 20, 60, 3, 2, rgb332(7, 7, 3)},
{300, 100, 40, -4, 3, rgb332(7, 0, 0)},
{500, 300, 50, 5, -2, rgb332(0, 7, 0)},
{100, 350, 30, -3, -4, rgb332(0, 0, 3)},
{400, 50, 70, 2, 5, rgb332(7, 4, 0)},
{200, 200, 45, -5, 2, rgb332(4, 0, 3)},
};
memcpy(sprites, init, sizeof(sprites));
memset(sprites_drawn, 0, sizeof(sprites_drawn));
}
static bool test_draw(uint8_t *fb, int buf)
{
// Each buffer remembers where the squares were last drawn in it, so only
// those areas need erasing
for (int i = 0; i < NUM_SPRITES; i++) {
rect_t *r = &sprites_drawn[buf][i];
gfx_restore(fb, r->x, r->y, r->size, r->size);
}
for (int i = 0; i < NUM_SPRITES; i++) {
sprite_t *s = &sprites[i];
s->x += s->dx;
s->y += s->dy;
// Keep inside the white outline
if (s->x < 1 || s->x + s->size > W - 2) {
s->dx = -s->dx;
s->x += 2 * s->dx;
}
if (s->y < 1 || s->y + s->size > H - 1) {
s->dy = -s->dy;
s->y += 2 * s->dy;
}
gfx_fill(fb, s->x, s->y, s->size, s->size, s->colour);
sprites_drawn[buf][i] = (rect_t){s->x, s->y, s->size};
}
return true;
}
// ---------------------------------------------------------------------------
// Plasma, computed in 4x4 blocks at 30 fps
#define PB 4
static int8_t sin_tab[256];
static uint8_t plasma_pal[256];
static uint8_t *plasma_radial; // per block, distance from centre
static uint32_t plasma_t;
static void plasma_enter(const show_t *s)
{
for (int i = 0; i < 256; i++) {
sin_tab[i] = (int8_t)(127 * sinf(i * 2 * M_PI / 256));
plasma_pal[i] = hue332(i);
}
if (!plasma_radial) {
plasma_radial = heap_caps_malloc((W / PB) * (H / PB), MALLOC_CAP_SPIRAM);
for (int y = 0; y < H / PB; y++) {
for (int x = 0; x < W / PB; x++) {
float dx = x - W / PB / 2, dy = y - H / PB / 2;
plasma_radial[y * (W / PB) + x] = (uint8_t)(sqrtf(dx * dx + dy * dy) * 2.5f);
}
}
}
memset(canvas, 0, W * H);
}
static bool plasma_draw(uint8_t *fb, int buf)
{
static uint8_t line[W];
if (plasma_t++ & 1) {
return false;
}
int t = plasma_t >> 1;
for (int by = 0; by < H / PB; by++) {
int sy = sin_tab[(by * 3 + t * 2) & 255];
const uint8_t *rad = plasma_radial + by * (W / PB);
for (int bx = 0; bx < W / PB; bx++) {
int v = sin_tab[(bx * 2 + t) & 255] + sy
+ sin_tab[((bx + by) * 2 + t * 3) & 255]
+ sin_tab[(rad[bx] - t * 4) & 255];
memset(line + bx * PB, plasma_pal[((v >> 2) + t) & 255], PB);
}
for (int r = 0; r < PB; r++) {
memcpy(fb + (by * PB + r) * W, line, W);
}
}
return true;
}
// ---------------------------------------------------------------------------
// Starfield
#define NUM_STARS 600
typedef struct {
float x, y, z;
} star_t;
typedef struct {
int16_t x, y;
uint8_t size;
} star_drawn_t;
static star_t *stars;
static star_drawn_t *stars_drawn[2];
static void star_reset(star_t *s, bool anywhere)
{
s->x = (esp_random() % 20001) / 10000.0f - 1.0f;
s->y = (esp_random() % 20001) / 10000.0f - 1.0f;
s->z = anywhere ? (esp_random() % 1000) / 1000.0f + 0.05f : 1.0f;
}
static void stars_enter(const show_t *s)
{
if (!stars) {
stars = heap_caps_malloc(NUM_STARS * sizeof(star_t), MALLOC_CAP_INTERNAL);
stars_drawn[0] = heap_caps_malloc(NUM_STARS * sizeof(star_drawn_t), MALLOC_CAP_INTERNAL);
stars_drawn[1] = heap_caps_malloc(NUM_STARS * sizeof(star_drawn_t), MALLOC_CAP_INTERNAL);
}
for (int i = 0; i < NUM_STARS; i++) {
star_reset(&stars[i], true);
}
memset(stars_drawn[0], 0, NUM_STARS * sizeof(star_drawn_t));
memset(stars_drawn[1], 0, NUM_STARS * sizeof(star_drawn_t));
memset(canvas, 0, W * H);
}
static bool stars_draw(uint8_t *fb, int buf)
{
star_drawn_t *drawn = stars_drawn[buf];
for (int i = 0; i < NUM_STARS; i++) {
gfx_fill(fb, drawn[i].x, drawn[i].y, drawn[i].size, drawn[i].size, BLACK);
}
for (int i = 0; i < NUM_STARS; i++) {
star_t *s = &stars[i];
s->z -= 0.006f;
int sx = W / 2 + (int)(s->x / s->z * (W / 2));
int sy = H / 2 + (int)(s->y / s->z * (W / 2));
if (s->z <= 0.02f || sx < 0 || sx >= W || sy < 0 || sy >= H) {
star_reset(s, false);
drawn[i].size = 0;
continue;
}
int size = s->z < 0.2f ? 4 : s->z < 0.4f ? 3 : s->z < 0.65f ? 2 : 1;
int level = 7 - (int)(s->z * 6);
uint8_t c = rgb332(level, level, level >> 1 | 1);
gfx_fill(fb, sx, sy, size, size, c);
drawn[i] = (star_drawn_t){sx, sy, size};
}
return true;
}
// ---------------------------------------------------------------------------
// Clock
#define CLOCK_BG rgb332(0, 0, 1)
static time_t clock_last;
// Font sizes from largest to smallest; at equal size the less-scaled
// (smoother) one comes first
typedef struct {
const font_t *f;
int scale;
} text_size_t;
static const text_size_t text_sizes[] = {
{&font96, 3}, {&font96, 2}, {&font48, 3}, {&font96, 1}, {&font48, 2},
{&font48, 1}, {&font16, 2}, {&font16, 1},
};
#define NUM_TEXT_SIZES (sizeof(text_sizes) / sizeof(text_sizes[0]))
// Largest size for a single line of `chars` characters within max_w x max_h
static text_size_t fit_line(int chars, int max_w, int max_h)
{
for (int i = 0; i < NUM_TEXT_SIZES; i++) {
const text_size_t *t = &text_sizes[i];
if (chars * t->f->w * t->scale <= max_w && t->f->h * t->scale <= max_h) {
return *t;
}
}
return text_sizes[NUM_TEXT_SIZES - 1];
}
static int line_height(text_size_t t)
{
return t.f->h * t.scale;
}
static void clock_enter(const show_t *s)
{
memset(canvas, CLOCK_BG, W * H);
clock_last = 0;
}
static void clock_tick(void)
{
time_t now = time(NULL);
if (now == clock_last) {
return;
}
clock_last = now;
struct tm tm;
localtime_r(&now, &tm);
char line1[32], line2[64];
const char *hint = NULL;
if (tm.tm_year + 1900 < 2024) {
strcpy(line1, "Time not set");
strcpy(line2, "Open the web page to sync");
hint = "or add your home WiFi in Settings";
} else {
strftime(line1, sizeof(line1), "%H:%M:%S", &tm);
strftime(line2, sizeof(line2), "%a %e %b", &tm);
}
// As big as fits: the time across the full width, the date below
const int margin = 16, gap = H / 24;
text_size_t big = fit_line(strlen(line1), W - 2 * margin, H * 55 / 100);
text_size_t small = fit_line(strlen(line2), W - 2 * margin, H * 25 / 100);
text_size_t tiny = fit_line(hint ? strlen(hint) : 1, W - 2 * margin, H / 10);
int total = line_height(big) + gap + line_height(small) + (hint ? gap + line_height(tiny) : 0);
int y = (H - total) / 2;
memset(canvas, CLOCK_BG, W * H);
text_centred(big.f, big.scale, y, line1, hint ? rgb332(7, 4, 0) : rgb332(0, 7, 3), -1);
y += line_height(big) + gap;
text_centred(small.f, small.scale, y, line2, rgb332(4, 6, 3), -1);
if (hint) {
text_centred(tiny.f, tiny.scale, y + line_height(small) + gap, hint, rgb332(4, 6, 3), -1);
}
canvas_dirty_all();
}
// ---------------------------------------------------------------------------
// Mandelbrot, drawn progressively. Each selection shows the next view.
typedef struct {
float cx, cy, width;
int max_iter;
} mandel_view_t;
static const mandel_view_t mandel_views[] = {
{-0.6f, 0.0f, 3.4f, 64},
{-0.7453f, 0.1127f, 0.0095f, 200}, // seahorse valley
{0.2850f, 0.0110f, 0.0400f, 160}, // elephant valley
{-0.1592f, -1.0317f, 0.0200f, 160},
{-1.7690f, 0.0040f, 0.0450f, 160}, // mini-brot on the spike
};
#define NUM_VIEWS (sizeof(mandel_views) / sizeof(mandel_views[0]))
static int mandel_view = -1;
static int mandel_row;
static uint8_t mandel_pal[257];
static void mandel_enter(const show_t *s)
{
mandel_view = (mandel_view + 1) % NUM_VIEWS;
mandel_row = 0;
for (int i = 0; i < 256; i++) {
mandel_pal[i] = hue332(i * 3 + 140);
}
mandel_pal[256] = BLACK;
memset(canvas, 0, W * H);
}
static void mandel_tick(void)
{
if (mandel_row >= H) {
return;
}
const mandel_view_t *v = &mandel_views[mandel_view];
const float step = v->width / W;
const float x0 = v->cx - v->width / 2, y0 = v->cy - step * H / 2;
int64_t until = esp_timer_get_time() + 8000;
int first = mandel_row;
while (mandel_row < H && esp_timer_get_time() < until) {
uint8_t *row = canvas + mandel_row * W;
float ci = y0 + mandel_row * step;
for (int x = 0; x < W; x++) {
float cr = x0 + x * step, zr = 0, zi = 0, zr2 = 0, zi2 = 0;
int n = 0;
while (n < v->max_iter && zr2 + zi2 < 4.0f) {
zi = 2 * zr * zi + ci;
zr = zr2 - zi2 + cr;
zr2 = zr * zr;
zi2 = zi * zi;
n++;
}
row[x] = n == v->max_iter ? mandel_pal[256] : mandel_pal[(n * 256 / v->max_iter + n) & 255];
}
mandel_row++;
}
canvas_dirty(0, first, W, mandel_row - first);
}
// ---------------------------------------------------------------------------
// Message
#define MSG_MAX_LINES 64
// Word-wrap text into lines of at most `cols` characters. Returns the number
// of lines, or -1 if it needs more than max_lines or has to split a word.
static int wrap_text(const char *p, int cols, int max_lines, char lines[][MSG_MAX + 1], bool allow_split)
{
int n = 0;
while (*p) {
if (n >= MIN(max_lines, MSG_MAX_LINES)) {
return -1;
}
int len = 0, brk = -1;
while (p[len] && p[len] != '\n' && len < cols) {
if (p[len] == ' ') {
brk = len;
}
len++;
}
if (p[len] && p[len] != '\n' && p[len] != ' ') {
if (brk > 0) {
len = brk; // break at the last space
} else if (!allow_split) {
return -1; // a word is longer than the line
}
}
memcpy(lines[n], p, len);
lines[n++][len] = 0;
p += len;
if (*p == '\n' || *p == ' ') {
p++;
}
}
return n;
}
static void message_enter(const show_t *s)
{
const message_t *m = &s->msg;
static const text_size_t fixed[4] = {{&font16, 1}, {&font16, 2}, {&font48, 1}, {&font48, 2}};
static char lines[MSG_MAX_LINES][MSG_MAX + 1];
const int max_w = W - 32, max_h = H - 16;
memset(canvas, m->bg, W * H);
text_size_t t = fixed[MIN(m->size, 3)];
int n = -1;
if (m->size >= MSG_SIZE_FILL) {
// Largest size the whole message fits at without splitting words
for (int i = 0; i < NUM_TEXT_SIZES && n < 0; i++) {
t = text_sizes[i];
n = wrap_text(m->text, max_w / (t.f->w * t.scale), max_h / line_height(t), lines, false);
}
}
if (n < 0) {
n = wrap_text(m->text, max_w / (t.f->w * t.scale), max_h / line_height(t), lines, true);
if (n < 0) {
n = MIN(max_h / line_height(t), MSG_MAX_LINES); // show what fits
}
}
int y = (H - n * line_height(t)) / 2;
for (int i = 0; i < n; i++, y += line_height(t)) {
text_centred(t.f, t.scale, y, lines[i], m->fg, -1);
}
}
// ---------------------------------------------------------------------------
// Image (from the gallery, or the live upload)
static void image_enter(const show_t *s)
{
bool ok;
if (s->slot == SLOT_LIVE) {
live_image_lock();
ok = live_image != NULL;
if (ok) {
memcpy(canvas, live_image, W * H);
}
live_image_unlock();
} else {
ok = gallery_read(s->slot, canvas);
}
if (!ok) {
char buf[48];
memset(canvas, rgb332(1, 1, 1), W * H);
snprintf(buf, sizeof(buf), s->slot == SLOT_LIVE ? "No image uploaded" : "Gallery slot %d is empty", s->slot + 1);
text_centred(&font48, 1, H / 2 - 24, buf, WHITE, -1);
}
}
// ---------------------------------------------------------------------------
// Shapes, for scenes drawn into the canvas
static void fill_ellipse(uint8_t *s, int cx, int cy, int rx, int ry, uint8_t c)
{
for (int dy = -ry; dy <= ry; dy++) {
int dx = (int)(rx * sqrtf(1.0f - (float)dy * dy / ((float)ry * ry)));
gfx_fill(s, cx - dx, cy + dy, 2 * dx + 1, 1, c);
}
}
static void fill_tri(uint8_t *s, int x0, int y0, int x1, int y1, int x2, int y2, uint8_t c)
{
const int xs[3] = {x0, x1, x2}, ys[3] = {y0, y1, y2};
int top = MIN(y0, MIN(y1, y2)), bottom = MAX(y0, MAX(y1, y2));
for (int y = top; y <= bottom; y++) {
int lo = W, hi = -1;
for (int e = 0; e < 3; e++) {
int ax = xs[e], ay = ys[e], bx = xs[(e + 1) % 3], by = ys[(e + 1) % 3];
if ((y < ay && y < by) || (y > ay && y > by)) {
continue;
}
int x = ay == by ? MIN(ax, bx) : ax + (bx - ax) * (y - ay) / (by - ay);
int x2 = ay == by ? MAX(ax, bx) : x;
lo = MIN(lo, x);
hi = MAX(hi, x2);
}
if (hi >= lo) {
gfx_fill(s, lo, y, hi - lo + 1, 1, c);
}
}
}
static void thick_line(uint8_t *s, int x0, int y0, int x1, int y1, int width, uint8_t c)
{
int steps = MAX(abs(x1 - x0), abs(y1 - y0)) + 1;
for (int i = 0; i < steps; i++) {
int x = x0 + (x1 - x0) * i / steps, y = y0 + (y1 - y0) * i / steps;
gfx_fill(s, x - width / 2, y - width / 2, width, width, c);
}
}
// Quantise an 8-bit colour to RGB332 with 4x4 ordered dithering, so smooth
// gradients don't band
static uint8_t dither332(int x, int y, float r, float g, float b)
{
static const uint8_t bayer[4][4] = {{0, 8, 2, 10}, {12, 4, 14, 6}, {3, 11, 1, 9}, {15, 7, 13, 5}};
float t = (bayer[y & 3][x & 3] + 0.5f) / 16;
int ri = (int)(r * 7 / 255 + t), gi = (int)(g * 7 / 255 + t), bi = (int)(b * 3 / 255 + t);
return rgb332(MIN(ri, 7), MIN(gi, 7), MIN(bi, 3));
}
// Pixel art: '.' is transparent, other characters index `colours` by
// position in `keys`
typedef struct {
const char *const *rows;
int w, h;
} art_t;
static void draw_art(uint8_t *s, const art_t *a, int x, int y, int scale, const char *keys, const uint8_t *colours)
{
for (int r = 0; r < a->h; r++) {
for (int col = 0; col < a->w; col++) {
const char *k = strchr(keys, a->rows[r][col]);
if (a->rows[r][col] != '.' && k) {
gfx_fill(s, x + col * scale, y + r * scale, scale, scale, colours[k - keys]);
}
}
}
}
// ---------------------------------------------------------------------------
// Halloween: night sky, moon, graveyard, flickering jack-o'-lanterns,
// flapping bats and bobbing ghosts
static const char *const bat_up_rows[] = {
"##...........##",
".##...#.#...##.",
"..###.###.###..",
"...#########...",
"....###e###....",
".....#...#.....",
};
static const char *const bat_down_rows[] = {
"......#.#......",
"......###......",
"...####e####...",
".####.###.####.",
"##....###....##",
"#......#......#",
};
static const char *const ghost_rows[] = {
".....######.....",
"...##########...",
"..############..",
".##############.",
".###ee####ee###.",
".###ee####ee###.",
"################",
"################",
"######mmmm######",
"#####mmmmmm#####",
"######mmmm######",
"################",
"################",
"################",
"################",
"#.###..##..###.#",
"...#....#....#..",
};
static const art_t bat_up = {bat_up_rows, 15, 6};
static const art_t bat_down = {bat_down_rows, 15, 6};
static const art_t ghost_art = {ghost_rows, 16, 17};
#define NUM_BATS 6
#define NUM_GHOSTS 2
#define NUM_SPOOKS (NUM_BATS + NUM_GHOSTS)
#define ORANGE rgb332(7, 3, 0)
#define ORANGE_MID rgb332(6, 3, 0)
#define ORANGE_DARK rgb332(5, 2, 0)
#define GROUND rgb332(0, 1, 0)
typedef struct {
int x, y, rx, ry;
} pumpkin_t;
typedef struct {
float x, y, speed, phase;
} flyer_t;
static pumpkin_t pumpkins[2];
static flyer_t bats[NUM_BATS], ghosts[NUM_GHOSTS];
typedef struct {
int x0, y0, x1, y1;
} spook_box_t;
static spook_box_t spooks_drawn[2][NUM_SPOOKS];
static uint32_t hw_frame;
static int hw_day = -1;
static int title_y, count_y;
static uint32_t rng_state;
static uint32_t rng(void)
{
rng_state = rng_state * 1664525 + 1013904223;
return rng_state >> 8;
}
static int ground_y(int x)
{
return (int)(H * 0.80f + 18 * sinf(x * 0.008f) + 10 * sinf(x * 0.021f + 1));
}
static void tree_branch(float x, float y, float angle, float len, float width, int depth)
{
float x2 = x + cosf(angle) * len, y2 = y - sinf(angle) * len;
thick_line(canvas, x, y, x2, y2, MAX((int)width, 1), BLACK);
if (depth == 0) {
return;
}
float spread = 0.35f + (rng() % 100) / 300.0f;
tree_branch(x2, y2, angle + spread, len * 0.72f, width * 0.68f, depth - 1);
tree_branch(x2, y2, angle - spread * 0.9f, len * 0.68f, width * 0.68f, depth - 1);
if (depth > 3 && rng() % 2) {
tree_branch(x2, y2, angle + (rng() % 100 - 50) / 200.0f, len * 0.5f, width * 0.5f, depth - 2);
}
}
static void pumpkin_face(const pumpkin_t *p, uint8_t glow)
{
const int x = p->x, y = p->y, rx = p->rx, ry = p->ry;
// Eyes and nose
fill_tri(canvas, x - rx * 45 / 100, y - ry * 8 / 100, x - rx * 12 / 100, y - ry * 8 / 100, x - rx * 30 / 100, y - ry * 45 / 100, glow);
fill_tri(canvas, x + rx * 12 / 100, y - ry * 8 / 100, x + rx * 45 / 100, y - ry * 8 / 100, x + rx * 30 / 100, y - ry * 45 / 100, glow);
fill_tri(canvas, x - rx * 9 / 100, y + ry * 14 / 100, x + rx * 9 / 100, y + ry * 14 / 100, x, y - ry * 4 / 100, glow);
// Grin: the lower half of an ellipse, with teeth
const int top = y + ry * 25 / 100, depth = ry * 38 / 100, half = rx * 62 / 100;
for (int dy = 0; dy <= depth; dy++) {
int dx = (int)(half * sqrtf(1.0f - (float)dy * dy / ((float)depth * depth)));
gfx_fill(canvas, x - dx, top + dy, 2 * dx + 1, 1, glow);
}
const int tw = rx * 13 / 100, th = ry * 12 / 100;
gfx_fill(canvas, x - rx * 30 / 100, top, tw, th, ORANGE);
gfx_fill(canvas, x + rx * 17 / 100, top, tw, th, ORANGE);
gfx_fill(canvas, x - tw / 2, top + depth - th + 1, tw, th, ORANGE);
}
static void pumpkin_draw(const pumpkin_t *p)
{
const int x = p->x, y = p->y, rx = p->rx, ry = p->ry;
thick_line(canvas, x, y - ry + 4, x + rx / 8, y - ry - ry * 35 / 100, MAX(rx / 7, 3), rgb332(2, 2, 0));
fill_ellipse(canvas, x, y, rx, ry, ORANGE_DARK);
fill_ellipse(canvas, x - rx * 45 / 100, y, rx * 52 / 100, ry * 96 / 100, ORANGE_MID);
fill_ellipse(canvas, x + rx * 45 / 100, y, rx * 52 / 100, ry * 96 / 100, ORANGE_MID);
fill_ellipse(canvas, x, y, rx * 50 / 100, ry, ORANGE);
pumpkin_face(p, rgb332(7, 7, 0));
}
static void pumpkin_bounds(const pumpkin_t *p, int *x, int *y, int *w, int *h)
{
*x = p->x - p->rx * 62 / 100;
*y = p->y - p->ry * 45 / 100;
*w = p->rx * 124 / 100 + 1;
*h = p->ry * 108 / 100 + 1;
}
static void halloween_title(uint8_t colour)
{
const char *t = "HAPPY HALLOWEEN";
int x = (W - gfx_text_width(&font48, 1, t)) / 2;
// Dark outline so it reads against the sky
for (int dy = -2; dy <= 2; dy += 2) {
for (int dx = -2; dx <= 2; dx += 2) {
if (dx || dy) {
gfx_text(canvas, &font48, 1, x + dx, title_y + dy, t, BLACK, -1);
}
}
}
gfx_text(canvas, &font48, 1, x, title_y, t, colour, -1);
canvas_dirty(x - 2, title_y - 2, gfx_text_width(&font48, 1, t) + 4, font48.h + 4);
}
// Sky gradient with moon glow, for rows y0..y1 and columns x0..x1
static void halloween_sky(int x0, int y0, int x1, int y1)
{
const int mx = W * 82 / 100, my = H * 22 / 100, mr = H * 12 / 100;
for (int y = MAX(y0, 0); y < MIN(y1, H); y++) {
float t = (float)y / H;
// Kept near the DAC's blue levels (0, 85, 170): dithering between
// distant blue steps otherwise makes the mid-sky look brown
float r = 4 + 72 * t, g = 2 + 16 * t, b = 75 + 75 * t;
for (int x = MAX(x0, 0); x < MIN(x1, W); x++) {
// Moon glow
float dx = x - mx, dy = y - my, d = sqrtf(dx * dx + dy * dy) / mr;
float glow = d > 1 && d < 2.6f ? (2.6f - d) / 1.6f : 0;
glow *= glow * 0.8f;
canvas[y * W + x] = dither332(x, y, r + (200 - r) * glow, g + (150 - g) * glow, b + (110 - b) * glow);
}
}
}
static void halloween_countdown(void)
{
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
hw_day = tm.tm_yday;
// Only redraw the sky under the text: the moon is on the same rows
const int h = font16.h * 2, w = 300, x0 = (W - w) / 2;
halloween_sky(x0, count_y, x0 + w, count_y + h);
if (tm.tm_year + 1900 >= 2024) {
char buf[40];
struct tm hw = {.tm_year = tm.tm_year, .tm_mon = 9, .tm_mday = 31, .tm_hour = 12};
if (tm.tm_mon > 9) {
hw.tm_year++; // already past this year's
}
struct tm today = {.tm_year = tm.tm_year, .tm_mon = tm.tm_mon, .tm_mday = tm.tm_mday, .tm_hour = 12};
int days = (int)((mktime(&hw) - mktime(&today) + 43200) / 86400);
if (days == 0) {
strcpy(buf, "Trick or treat!");
} else {
snprintf(buf, sizeof(buf), days == 1 ? "1 night to go..." : "%d nights to go...", days);
}
text_centred(&font16, 2, count_y, buf, rgb332(4, 7, 0), -1);
}
canvas_dirty(x0, count_y, w, h);
}
static void halloween_enter(const show_t *s)
{
rng_state = 31; // same scene every time
title_y = H * 5 / 100;
count_y = title_y + font48.h + 8;
halloween_sky(0, 0, W, H);
// Stars, kept away from the moon
const int mx = W * 82 / 100, my = H * 22 / 100, mr = H * 12 / 100;
for (int i = 0; i < 140; i++) {
int x = rng() % W, y = rng() % (H * 6 / 10);
if ((x - mx) * (x - mx) + (y - my) * (y - my) < 9 * mr * mr) {
continue;
}
canvas[y * W + x] = i % 5 ? rgb332(5, 5, 2) : WHITE;
}
// Moon with craters
fill_ellipse(canvas, mx, my, mr, mr, rgb332(7, 7, 2));
fill_ellipse(canvas, mx - mr / 3, my - mr / 4, mr / 5, mr / 5, rgb332(6, 6, 1));
fill_ellipse(canvas, mx + mr / 3, my + mr / 5, mr / 4, mr / 4, rgb332(6, 6, 1));
fill_ellipse(canvas, mx - mr / 6, my + mr / 2, mr / 8, mr / 8, rgb332(6, 6, 1));
// Hill
for (int x = 0; x < W; x++) {
int gy = ground_y(x);
gfx_fill(canvas, x, gy, 1, H - gy, GROUND);
}
// Bare tree on the left
const int tx = W * 14 / 100;
tree_branch(tx, ground_y(tx) + 4, M_PI / 2 + 0.05f, H * 0.17f, W / 50.0f, 7);
// Tombstones
const int stones[][2] = {{W * 45 / 100, 36}, {W * 74 / 100, 30}, {W * 90 / 100, 40}};
for (int i = 0; i < 3; i++) {
int x = stones[i][0], w = stones[i][1], h = w * 13 / 10, gy = ground_y(x) + 10;
fill_ellipse(canvas, x, gy - h, w / 2, w / 2, rgb332(3, 3, 1));
gfx_fill(canvas, x - w / 2, gy - h, w + 1, h, rgb332(3, 3, 1));
gfx_text(canvas, &font16, 1, x - 12, gy - h - 4, "RIP", rgb332(1, 1, 0), -1);
}
pumpkins[0] = (pumpkin_t){W * 30 / 100, H * 85 / 100, W * 10 / 100, H * 10 / 100};
pumpkins[1] = (pumpkin_t){W * 60 / 100, H * 88 / 100, W * 6 / 100, H * 6 / 100};
pumpkin_draw(&pumpkins[0]);
pumpkin_draw(&pumpkins[1]);
halloween_title(ORANGE);
halloween_countdown();
for (int i = 0; i < NUM_BATS; i++) {
bats[i] = (flyer_t){(float)(rng() % (W * 3 / 2)), 40 + rng() % (H / 2), 1.5f + (rng() % 100) / 60.0f, (rng() % 628) / 100.0f};
}
for (int i = 0; i < NUM_GHOSTS; i++) {
ghosts[i] = (flyer_t){W * (25 + 45 * i) / 100.0f, H * 62 / 100.0f, i ? -0.6f : 0.5f, i * 2.0f};
}
memset(spooks_drawn, 0, sizeof(spooks_drawn));
hw_frame = 0;
}
static void halloween_tick(void)
{
hw_frame++;
// Candle flicker
if (hw_frame % 4 == 0) {
const uint8_t glows[] = {rgb332(7, 7, 0), rgb332(7, 6, 0), rgb332(7, 5, 0), rgb332(7, 7, 1), rgb332(7, 6, 0)};
for (int i = 0; i < 2; i++) {
if (rng() % 3 == 0) {
pumpkin_face(&pumpkins[i], glows[rng() % sizeof(glows)]);
int x, y, w, h;
pumpkin_bounds(&pumpkins[i], &x, &y, &w, &h);
canvas_dirty(x, y, w, h);
}
}
}
// Now and then the title sputters like a failing light
static int sputter;
if (sputter == 0 && rng() % 400 == 0) {
sputter = 18;
}
if (sputter > 0) {
sputter--;
halloween_title(sputter == 0 || sputter % 6 > 2 ? ORANGE : rgb332(2, 1, 0));
}
if (hw_frame % 600 == 0) {
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
if (tm.tm_yday != hw_day) {
halloween_countdown();
}
}
}
static bool halloween_draw(uint8_t *fb, int buf)
{
spook_box_t *drawn = spooks_drawn[buf];
for (int i = 0; i < NUM_SPOOKS; i++) {
gfx_restore(fb, drawn[i].x0, drawn[i].y0, drawn[i].x1 - drawn[i].x0, drawn[i].y1 - drawn[i].y0);
}
const uint8_t bat_colours[] = {rgb332(1, 0, 1), rgb332(7, 1, 0)};
for (int i = 0; i < NUM_BATS; i++) {
flyer_t *b = &bats[i];
b->x -= b->speed;
b->phase += 0.07f;
if (b->x < -50) {
b->x = W + rng() % 200;
b->y = 40 + rng() % (H / 2);
}
const int scale = 4;
int x = (int)b->x, y = (int)(b->y + 22 * sinf(b->phase));
bool up = ((hw_frame / 5) + i) & 1;
draw_art(fb, up ? &bat_up : &bat_down, x, y, scale, "#e", bat_colours);
drawn[i] = (spook_box_t){x, y, x + 15 * scale, y + 6 * scale};
}
const uint8_t ghost_colours[] = {rgb332(7, 7, 3), rgb332(0, 0, 1), rgb332(0, 0, 1)};
for (int i = 0; i < NUM_GHOSTS; i++) {
flyer_t *g = &ghosts[i];
g->x += g->speed;
g->phase += 0.04f;
const int scale = 4, gw = 16 * scale, gh = 17 * scale;
if (g->x < 20 || g->x + gw > W - 20) {
g->speed = -g->speed;
}
int x = (int)g->x, y = (int)(g->y + 14 * sinf(g->phase));
draw_art(fb, &ghost_art, x, y, scale, "#em", ghost_colours);
drawn[NUM_BATS + i] = (spook_box_t){x, y, x + gw, y + gh};
}
return true;
}
// ---------------------------------------------------------------------------
// Haunting, for projecting onto a scrim: black projects as nothing, so the
// background stays black and only the spooks are lit.
#define HAUNT_GHOSTS 3
#define HAUNT_EYES 4
#define HAUNT_BATS 4
#define HAUNT_SPRITES (HAUNT_GHOSTS + HAUNT_EYES + HAUNT_BATS + 1)
typedef enum { SPOOK_HIDDEN, SPOOK_IN, SPOOK_SHOWN, SPOOK_OUT } spook_state_t;
typedef struct {
float x, y, vx, phase;
int scale;
spook_state_t state;
int timer; // frames left in this state
} haunt_ghost_t;
typedef struct {
int x, y, size;
uint8_t colour;
spook_state_t state;
int timer, blink;
} haunt_eyes_t;
static haunt_ghost_t hghosts[HAUNT_GHOSTS];
static haunt_eyes_t heyes[HAUNT_EYES];
static flyer_t hbats[HAUNT_BATS];
static pumpkin_t hpumpkins[2];
static spook_box_t haunt_drawn[2][HAUNT_SPRITES];
static int scare_timer, scare_frame;
static uint32_t haunt_frame;
#define SECONDS(s) ((int)((s) * 60))
#define FADE_FRAMES SECONDS(2.5)
static int rand_range(int lo, int hi)
{
return lo + (int)(rng() % (uint32_t)(hi - lo + 1));
}
static void ghost_respawn(haunt_ghost_t *g)
{
g->scale = rand_range(H / 75, H / 46); // 8-13 at 600 lines
const int gw = 16 * g->scale, gh = 17 * g->scale;
g->x = rand_range(0, W - gw);
g->y = rand_range(10, H * 55 / 100 - gh / 2);
g->vx = (rng() % 2 ? 1 : -1) * (0.3f + (rng() % 100) / 150.0f);
g->phase = (rng() % 628) / 100.0f;
g->state = SPOOK_HIDDEN;
g->timer = rand_range(SECONDS(1), SECONDS(6));
}
static void eyes_respawn(haunt_eyes_t *e)
{
const uint8_t colours[] = {rgb332(7, 7, 0), rgb332(7, 1, 0), rgb332(3, 7, 0), rgb332(7, 4, 0)};
e->size = rand_range(H / 30, H / 13);
e->x = rand_range(e->size * 3, W - e->size * 3);
e->y = rand_range(e->size, H * 70 / 100);
e->colour = colours[rng() % 4];
e->state = SPOOK_HIDDEN;
e->timer = rand_range(SECONDS(1), SECONDS(8));
e->blink = 0;
}
static void haunt_enter(const show_t *s)
{
rng_state = esp_random();
memset(canvas, BLACK, W * H);
hpumpkins[0] = (pumpkin_t){W * 16 / 100, H * 82 / 100, W * 15 / 100, H * 15 / 100};
hpumpkins[1] = (pumpkin_t){W * 85 / 100, H * 85 / 100, W * 12 / 100, H * 12 / 100};
pumpkin_draw(&hpumpkins[0]);
pumpkin_draw(&hpumpkins[1]);
for (int i = 0; i < HAUNT_GHOSTS; i++) {
ghost_respawn(&hghosts[i]);
}
hghosts[0].timer = SECONDS(0.5); // something appears straight away
for (int i = 0; i < HAUNT_EYES; i++) {
eyes_respawn(&heyes[i]);
}
for (int i = 0; i < HAUNT_BATS; i++) {
hbats[i] = (flyer_t){(float)(rng() % (W * 3 / 2)), 30 + rng() % (H / 2), 1.5f + (rng() % 100) / 60.0f, (rng() % 628) / 100.0f};
}
memset(haunt_drawn, 0, sizeof(haunt_drawn));
scare_timer = rand_range(SECONDS(25), SECONDS(50));
scare_frame = -1;
haunt_frame = 0;
}
static void haunt_tick(void)
{
haunt_frame++;
if (haunt_frame % 4 == 0) {
const uint8_t glows[] = {rgb332(7, 7, 0), rgb332(7, 6, 0), rgb332(7, 5, 0), rgb332(7, 7, 1), rgb332(7, 6, 0)};
for (int i = 0; i < 2; i++) {
if (rng() % 3 == 0) {
pumpkin_face(&hpumpkins[i], glows[rng() % sizeof(glows)]);
int x, y, w, h;
pumpkin_bounds(&hpumpkins[i], &x, &y, &w, &h);
canvas_dirty(x, y, w, h);
}
}
}
}
// Advance a fade-in / show / fade-out / hidden cycle. Returns brightness 0..1.
static float spook_step(spook_state_t *state, int *timer, int show_min, int show_max, int hide_min, int hide_max, bool *respawn)
{
*respawn = false;
if (--*timer <= 0) {
switch (*state) {
case SPOOK_HIDDEN: *state = SPOOK_IN; *timer = FADE_FRAMES; break;
case SPOOK_IN: *state = SPOOK_SHOWN; *timer = rand_range(show_min, show_max); break;
case SPOOK_SHOWN: *state = SPOOK_OUT; *timer = FADE_FRAMES; break;
case SPOOK_OUT: *state = SPOOK_HIDDEN; *timer = rand_range(hide_min, hide_max); *respawn = true; break;
}
}
switch (*state) {
case SPOOK_IN: return 1.0f - (float)*timer / FADE_FRAMES;
case SPOOK_SHOWN: return 1.0f;
case SPOOK_OUT: return (float)*timer / FADE_FRAMES;
default: return 0;
}
}
static bool haunt_draw(uint8_t *fb, int buf)
{
spook_box_t *drawn = haunt_drawn[buf];
for (int i = 0; i < HAUNT_SPRITES; i++) {
gfx_restore(fb, drawn[i].x0, drawn[i].y0, drawn[i].x1 - drawn[i].x0, drawn[i].y1 - drawn[i].y0);
drawn[i] = (spook_box_t){0};
}
int n = 0;
// Eyes in the dark: open, blink now and then, close, move somewhere else
for (int i = 0; i < HAUNT_EYES; i++, n++) {
haunt_eyes_t *e = &heyes[i];
bool respawn;
// The "fade" here is how open the eyes are
float open = spook_step(&e->state, &e->timer, SECONDS(3), SECONDS(8), SECONDS(2), SECONDS(10), &respawn);
if (respawn) {
eyes_respawn(e);
continue;
}
if (e->state == SPOOK_SHOWN && e->blink == 0 && rng() % 200 == 0) {
e->blink = 12;
}
if (e->blink > 0) {
e->blink--;
open *= abs(e->blink - 6) / 6.0f;
}
// Eyes open much faster than ghosts fade
open = MIN(1.0f, open * 4);
int ry = (int)(e->size * 0.6f * open);
if (ry < 1) {
continue;
}
const int gap = e->size * 3 / 2;
for (int side = -1; side <= 1; side += 2) {
int cx = e->x + side * gap;
fill_ellipse(fb, cx, e->y, e->size, ry, e->colour);
fill_ellipse(fb, cx, e->y, MAX(e->size / 5, 1), ry, BLACK); // cat's-eye pupil
}
drawn[n] = (spook_box_t){e->x - gap - e->size, e->y - ry, e->x + gap + e->size + 1, e->y + ry + 1};
}
// Ghosts fading in and out
for (int i = 0; i < HAUNT_GHOSTS; i++, n++) {
haunt_ghost_t *g = &hghosts[i];
bool respawn;
float bright = spook_step(&g->state, &g->timer, SECONDS(5), SECONDS(12), SECONDS(1), SECONDS(6), &respawn);
if (respawn) {
ghost_respawn(g);
continue;
}
if (bright <= 0) {
continue;
}
const int gw = 16 * g->scale, gh = 17 * g->scale;
g->x += g->vx;
g->phase += 0.03f;
if (g->x < 0 || g->x + gw > W) {
g->vx = -g->vx;
}
int x = (int)g->x, y = (int)(g->y + 18 * sinf(g->phase));
int lvl = (int)(bright * 7 + 0.5f);
if (lvl == 0) {
continue;
}
// Black eyes and mouth: the fabric shows through them
const uint8_t colours[] = {rgb332(lvl, lvl, (lvl * 3 + 3) / 7), BLACK, BLACK};
draw_art(fb, &ghost_art, x, y, g->scale, "#em", colours);
drawn[n] = (spook_box_t){x, y, x + gw, y + gh};
}
// Bats
const uint8_t bat_colours[] = {rgb332(3, 0, 2), rgb332(7, 0, 0)};
for (int i = 0; i < HAUNT_BATS; i++, n++) {
flyer_t *b = &hbats[i];
b->x -= b->speed;
b->phase += 0.07f;
const int scale = H / 110;
if (b->x < -15 * scale) {
b->x = W + rng() % 300;
b->y = 30 + rng() % (H / 2);
}
int x = (int)b->x, y = (int)(b->y + 22 * sinf(b->phase));
bool up = ((haunt_frame / 5) + i) & 1;
draw_art(fb, up ? &bat_up : &bat_down, x, y, scale, "#e", bat_colours);
drawn[n] = (spook_box_t){x, y, x + 15 * scale, y + 6 * scale};
}
// Now and then a ghost rushes at the audience
if (scare_frame < 0 && --scare_timer <= 0) {
scare_frame = 0;
}
if (scare_frame >= 0) {
const int grow = 24, hold = 30;
const int full = H / 17; // fills the screen height
int scale = scare_frame < grow ? 2 + scare_frame * (full - 2) / grow : full;
int gw = 16 * scale, gh = 17 * scale;
int x = (W - gw) / 2, y = (H - gh) / 2;
const uint8_t colours[] = {WHITE, BLACK, BLACK};
draw_art(fb, &ghost_art, x, y, scale, "#em", colours);
drawn[n] = (spook_box_t){x, y, x + gw, y + gh};
if (++scare_frame > grow + hold) {
scare_frame = -1;
scare_timer = rand_range(SECONDS(45), SECONDS(90));
}
}
return true;
}
// ---------------------------------------------------------------------------
static const content_ops_t ops[CONTENT_COUNT] = {
[CONTENT_TEST] = {test_enter, NULL, test_draw},
[CONTENT_PLASMA] = {plasma_enter, NULL, plasma_draw},
[CONTENT_STARS] = {stars_enter, NULL, stars_draw},
[CONTENT_CLOCK] = {clock_enter, clock_tick, NULL},
[CONTENT_MANDEL] = {mandel_enter, mandel_tick, NULL},
[CONTENT_MESSAGE] = {message_enter, NULL, NULL},
[CONTENT_IMAGE] = {image_enter, NULL, NULL},
[CONTENT_HALLOWEEN] = {halloween_enter, halloween_tick, halloween_draw},
[CONTENT_HAUNT] = {haunt_enter, haunt_tick, haunt_draw},
};
const content_ops_t *content_ops(content_id_t c)
{
return &ops[c < CONTENT_COUNT ? c : CONTENT_TEST];
}