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>
387 lines
16 KiB
HTML
387 lines
16 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>ESP32 VGA</title>
|
||
<style>
|
||
:root {
|
||
--bg: #f4f5f7; --card: #fff; --text: #1d2026; --muted: #667085; --line: #e1e4ea;
|
||
--accent: #2563eb; --accent-text: #fff; --danger: #c0352b; --input: #fff;
|
||
}
|
||
@media (prefers-color-scheme: dark) {
|
||
:root {
|
||
--bg: #111318; --card: #1b1e25; --text: #e6e8ec; --muted: #9aa1ad; --line: #2c313b;
|
||
--accent: #5b8cff; --accent-text: #0b1020; --danger: #ff6b5e; --input: #14171d;
|
||
}
|
||
}
|
||
* { box-sizing: border-box; }
|
||
body { margin: 0; background: var(--bg); color: var(--text);
|
||
font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
|
||
header { padding: 18px 16px 6px; max-width: 760px; margin: 0 auto; }
|
||
h1 { font-size: 22px; margin: 0; }
|
||
#status { color: var(--muted); font-size: 13px; margin-top: 2px; }
|
||
main { max-width: 760px; margin: 0 auto; padding: 8px 16px 40px; display: grid; gap: 14px; }
|
||
section { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
|
||
h2 { font-size: 16px; margin: 0 0 12px; }
|
||
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
|
||
input[type=text], input[type=password], textarea, select {
|
||
width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
|
||
background: var(--input); color: var(--text); font: inherit; }
|
||
textarea { min-height: 80px; resize: vertical; }
|
||
input[type=color] { width: 56px; height: 36px; padding: 2px; border: 1px solid var(--line);
|
||
border-radius: 8px; background: var(--input); }
|
||
input[type=file] { font: inherit; color: var(--muted); max-width: 100%; }
|
||
button { font: inherit; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
|
||
background: var(--input); color: var(--text); cursor: pointer; }
|
||
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
|
||
button.danger { color: var(--danger); }
|
||
button:disabled { opacity: .5; cursor: default; }
|
||
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
|
||
.row > .grow { flex: 1 1 160px; }
|
||
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
|
||
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
|
||
.tiles button { padding: 14px 8px; }
|
||
.tiles button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
|
||
canvas#preview, canvas.shot { width: 100%; height: auto; border-radius: 8px; border: 1px solid var(--line);
|
||
background: #000; image-rendering: pixelated; margin-top: 12px; display: none; }
|
||
.check { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 14px; margin: 0; }
|
||
.gallery { display: grid; gap: 6px; }
|
||
.slot { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
|
||
.slot:first-child { border-top: 0; }
|
||
.slot .n { width: 26px; color: var(--muted); font-variant-numeric: tabular-nums; }
|
||
.slot .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||
.slot .name.empty { color: var(--muted); }
|
||
.slot.on .name { font-weight: 600; color: var(--accent); }
|
||
.note { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
|
||
#toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text);
|
||
color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px; opacity: 0;
|
||
transition: opacity .2s; pointer-events: none; max-width: calc(100% - 32px); }
|
||
#toast.show { opacity: 1; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header>
|
||
<h1>ESP32 VGA</h1>
|
||
<div id="status">Connecting…</div>
|
||
</header>
|
||
<main>
|
||
<section>
|
||
<h2>Show</h2>
|
||
<div class="tiles" id="tiles"></div>
|
||
<div class="actions"><button id="shot">Preview screen</button></div>
|
||
<canvas id="screen" class="shot"></canvas>
|
||
</section>
|
||
|
||
<section>
|
||
<h2>Message</h2>
|
||
<textarea id="msgText" maxlength="256"></textarea>
|
||
<div class="row">
|
||
<div><label for="msgFg">Text</label><input type="color" id="msgFg"></div>
|
||
<div><label for="msgBg">Background</label><input type="color" id="msgBg"></div>
|
||
<div class="grow"><label for="msgSize">Size</label>
|
||
<select id="msgSize">
|
||
<option value="0">Small</option><option value="1">Medium</option>
|
||
<option value="2">Large</option><option value="3">Huge</option>
|
||
<option value="4">Fill screen</option>
|
||
</select></div>
|
||
</div>
|
||
<div class="actions"><button class="primary" id="msgShow">Show message</button></div>
|
||
</section>
|
||
|
||
<section>
|
||
<h2>Image</h2>
|
||
<input type="file" id="imgFile" accept="image/*">
|
||
<div class="row">
|
||
<div class="grow"><label for="imgFit">Fit</label>
|
||
<select id="imgFit">
|
||
<option value="contain">Fit inside (black bars)</option>
|
||
<option value="cover">Fill screen (crop)</option>
|
||
<option value="stretch">Stretch</option>
|
||
</select></div>
|
||
<div><label> </label><label class="check"><input type="checkbox" id="imgDither" checked> Dither</label></div>
|
||
</div>
|
||
<canvas id="preview"></canvas>
|
||
<div class="row">
|
||
<div class="grow"><label for="imgName">Name</label><input type="text" id="imgName" maxlength="31"></div>
|
||
<div class="grow"><label for="imgSlot">Gallery slot</label><select id="imgSlot"></select></div>
|
||
</div>
|
||
<div class="actions">
|
||
<button class="primary" id="imgShow" disabled>Show now</button>
|
||
<button id="imgSave" disabled>Save to gallery</button>
|
||
</div>
|
||
<p class="note" id="imgNote">The browser converts the picture to the display's 256 colours before sending it.</p>
|
||
</section>
|
||
|
||
<section>
|
||
<h2>Gallery</h2>
|
||
<div class="gallery" id="gallery"></div>
|
||
</section>
|
||
|
||
<section>
|
||
<h2>Settings</h2>
|
||
<label for="ssid">Home WiFi name</label>
|
||
<input type="text" id="ssid" autocomplete="off">
|
||
<label for="pass">Password</label>
|
||
<input type="password" id="pass" autocomplete="off">
|
||
<div class="actions"><button id="wifiSave">Save WiFi</button></div>
|
||
<p class="note" id="wifiNote"></p>
|
||
|
||
<label for="tz">Time zone</label>
|
||
<div class="row">
|
||
<div class="grow"><select id="tzPreset">
|
||
<option value="NZST-12NZDT,M9.5.0,M4.1.0/3">New Zealand</option>
|
||
<option value="AEST-10AEDT,M10.1.0,M4.1.0/3">Sydney / Melbourne</option>
|
||
<option value="AEST-10">Brisbane</option>
|
||
<option value="AWST-8">Perth</option>
|
||
<option value="GMT0BST,M3.5.0/1,M10.5.0">UK</option>
|
||
<option value="CET-1CEST,M3.5.0,M10.5.0/3">Central Europe</option>
|
||
<option value="EST5EDT,M3.2.0,M11.1.0">US Eastern</option>
|
||
<option value="PST8PDT,M3.2.0,M11.1.0">US Pacific</option>
|
||
<option value="UTC0">UTC</option>
|
||
<option value="">Custom…</option>
|
||
</select></div>
|
||
<div class="grow"><input type="text" id="tz" placeholder="POSIX TZ string"></div>
|
||
</div>
|
||
<div class="actions"><button id="timeSave">Save time zone and sync clock</button></div>
|
||
|
||
<label for="fwFile">Firmware update (.bin)</label>
|
||
<input type="file" id="fwFile" accept=".bin">
|
||
<div class="actions"><button id="fwUpload" disabled>Upload and restart</button></div>
|
||
<p class="note" id="fwNote"></p>
|
||
</section>
|
||
</main>
|
||
<div id="toast"></div>
|
||
|
||
<script>
|
||
const $ = id => document.getElementById(id);
|
||
let state = null, converted = null, sourceImage = null;
|
||
|
||
function toast(msg) {
|
||
const t = $('toast');
|
||
t.textContent = msg;
|
||
t.classList.add('show');
|
||
clearTimeout(toast.timer);
|
||
toast.timer = setTimeout(() => t.classList.remove('show'), 2500);
|
||
}
|
||
|
||
async function api(path, opts = {}) {
|
||
const r = await fetch(path, opts);
|
||
if (!r.ok) throw new Error(await r.text() || r.statusText);
|
||
return r.headers.get('content-type')?.includes('json') ? r.json() : r.text();
|
||
}
|
||
|
||
const post = (path, body) => api(path, {
|
||
method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body)
|
||
});
|
||
|
||
async function run(btn, fn, done) {
|
||
if (btn) btn.disabled = true;
|
||
try { await fn(); if (done) toast(done); await refresh(); }
|
||
catch (e) { toast('Error: ' + e.message); }
|
||
finally { if (btn) btn.disabled = false; }
|
||
}
|
||
|
||
async function refresh() {
|
||
state = await api('/api/state');
|
||
render();
|
||
}
|
||
|
||
function render() {
|
||
const s = state, n = s.net;
|
||
let net = `${s.w}×${s.h} @ ${s.hz.toFixed(1)} Hz`;
|
||
net += n.sta_connected ? ` · on ${n.sta_ssid} at ${n.sta_ip}` : n.sta_ssid ? ` · connecting to ${n.sta_ssid}…` : '';
|
||
$('status').textContent = net;
|
||
|
||
const tiles = $('tiles');
|
||
tiles.replaceChildren(...s.contents.filter(c => c.key !== 'image').map(c => {
|
||
const b = document.createElement('button');
|
||
b.textContent = c.name;
|
||
b.className = c.key === s.content ? 'on' : '';
|
||
b.onclick = () => run(null, () => post('/api/show', {content: c.key}));
|
||
return b;
|
||
}));
|
||
|
||
if (!render.msgLoaded) {
|
||
$('msgText').value = s.msg.text;
|
||
$('msgFg').value = s.msg.fg;
|
||
$('msgBg').value = s.msg.bg;
|
||
$('msgSize').value = s.msg.size;
|
||
$('ssid').value = n.sta_ssid;
|
||
setTz(s.tz);
|
||
render.msgLoaded = true;
|
||
}
|
||
|
||
const sel = $('imgSlot'), prev = sel.value;
|
||
sel.replaceChildren(...s.slots.map((name, i) => {
|
||
const o = document.createElement('option');
|
||
o.value = i;
|
||
o.textContent = `${i + 1}: ${name ?? '(empty)'}`;
|
||
return o;
|
||
}));
|
||
sel.value = prev !== '' ? prev : Math.max(0, s.slots.indexOf(null));
|
||
|
||
const g = $('gallery');
|
||
g.replaceChildren(...s.slots.map((name, i) => {
|
||
const row = document.createElement('div');
|
||
const showing = s.content === 'image' && s.slot === i;
|
||
row.className = 'slot' + (showing ? ' on' : '');
|
||
row.innerHTML = `<span class="n">${i + 1}</span><span class="name"></span>`;
|
||
const label = row.querySelector('.name');
|
||
label.textContent = name ?? 'empty';
|
||
if (name === null) label.classList.add('empty');
|
||
else {
|
||
const show = document.createElement('button');
|
||
show.textContent = 'Show';
|
||
show.onclick = () => run(show, () => post('/api/show', {content: 'image', slot: i}));
|
||
const del = document.createElement('button');
|
||
del.textContent = 'Delete';
|
||
del.className = 'danger';
|
||
del.onclick = () => confirm(`Delete "${name}"?`) &&
|
||
run(del, () => api('/api/image?slot=' + i, {method: 'DELETE'}), 'Deleted');
|
||
row.append(show, del);
|
||
}
|
||
return row;
|
||
}));
|
||
|
||
$('wifiNote').textContent = n.sta_connected
|
||
? `Connected. You can also reach this page at http://${n.sta_ip}`
|
||
: `The board's own network "${n.ap_ssid}" (http://${n.ap_ip}) always stays on.`;
|
||
$('fwNote').textContent = `Running ${s.version} from ${s.partition}, built ${s.built}.`;
|
||
}
|
||
|
||
// Screenshot of what the VGA output is showing
|
||
$('shot').onclick = e => run(e.target, async () => {
|
||
const r = await fetch('/api/screenshot');
|
||
if (!r.ok) throw new Error(r.statusText);
|
||
const px = new Uint8Array(await r.arrayBuffer()), W = state.w, H = state.h;
|
||
const c = $('screen');
|
||
c.width = W; c.height = H;
|
||
c.style.display = 'block';
|
||
const ctx = c.getContext('2d'), id = ctx.createImageData(W, H);
|
||
for (let i = 0; i < W * H; i++) {
|
||
const v = px[i];
|
||
id.data[i * 4] = (v >> 5) * 255 / 7;
|
||
id.data[i * 4 + 1] = ((v >> 2) & 7) * 255 / 7;
|
||
id.data[i * 4 + 2] = (v & 3) * 255 / 3;
|
||
id.data[i * 4 + 3] = 255;
|
||
}
|
||
ctx.putImageData(id, 0, 0);
|
||
});
|
||
|
||
// Message
|
||
$('msgShow').onclick = e => run(e.target, () => post('/api/message', {
|
||
text: $('msgText').value, fg: $('msgFg').value, bg: $('msgBg').value, size: +$('msgSize').value
|
||
}));
|
||
|
||
// Image conversion: scale to the display, then quantise to RGB332
|
||
// (8 red, 8 green, 4 blue levels), optionally with Floyd-Steinberg dithering.
|
||
function convert() {
|
||
if (!sourceImage) return;
|
||
const W = state.w, H = state.h, img = sourceImage;
|
||
const c = $('preview');
|
||
c.width = W; c.height = H;
|
||
c.style.display = 'block';
|
||
const ctx = c.getContext('2d', {willReadFrequently: true});
|
||
ctx.fillStyle = '#000';
|
||
ctx.fillRect(0, 0, W, H);
|
||
const fit = $('imgFit').value, iw = img.naturalWidth, ih = img.naturalHeight;
|
||
let dw = W, dh = H;
|
||
if (fit !== 'stretch') {
|
||
const k = (fit === 'cover' ? Math.max : Math.min)(W / iw, H / ih);
|
||
dw = iw * k; dh = ih * k;
|
||
}
|
||
ctx.imageSmoothingQuality = 'high';
|
||
ctx.drawImage(img, (W - dw) / 2, (H - dh) / 2, dw, dh);
|
||
|
||
const id = ctx.getImageData(0, 0, W, H), px = id.data;
|
||
const buf = new Float32Array(W * H * 3);
|
||
for (let i = 0, j = 0; i < px.length; i += 4, j += 3) {
|
||
buf[j] = px[i]; buf[j + 1] = px[i + 1]; buf[j + 2] = px[i + 2];
|
||
}
|
||
const levels = [7, 7, 3], dither = $('imgDither').checked;
|
||
const out = new Uint8Array(W * H);
|
||
for (let y = 0; y < H; y++) {
|
||
for (let x = 0; x < W; x++) {
|
||
const p = y * W + x, q = [0, 0, 0];
|
||
for (let ch = 0; ch < 3; ch++) {
|
||
const old = Math.min(255, Math.max(0, buf[p * 3 + ch]));
|
||
const L = levels[ch], v = Math.round(old * L / 255), val = v * 255 / L;
|
||
q[ch] = v;
|
||
px[p * 4 + ch] = val;
|
||
if (dither) {
|
||
const err = old - val;
|
||
if (x + 1 < W) buf[(p + 1) * 3 + ch] += err * 7 / 16;
|
||
if (y + 1 < H) {
|
||
if (x > 0) buf[(p + W - 1) * 3 + ch] += err * 3 / 16;
|
||
buf[(p + W) * 3 + ch] += err * 5 / 16;
|
||
if (x + 1 < W) buf[(p + W + 1) * 3 + ch] += err / 16;
|
||
}
|
||
}
|
||
}
|
||
out[p] = (q[0] << 5) | (q[1] << 2) | q[2];
|
||
}
|
||
}
|
||
ctx.putImageData(id, 0, 0);
|
||
converted = out;
|
||
$('imgShow').disabled = $('imgSave').disabled = false;
|
||
}
|
||
|
||
$('imgFile').onchange = e => {
|
||
const f = e.target.files[0];
|
||
if (!f) return;
|
||
const img = new Image();
|
||
img.onload = () => { sourceImage = img; convert(); URL.revokeObjectURL(img.src); };
|
||
img.onerror = () => toast('Could not read that image');
|
||
img.src = URL.createObjectURL(f);
|
||
$('imgName').value = f.name.replace(/\.[^.]+$/, '').slice(0, 31);
|
||
};
|
||
$('imgFit').onchange = $('imgDither').onchange = convert;
|
||
|
||
function upload(btn, slot) {
|
||
const name = encodeURIComponent($('imgName').value || 'Image');
|
||
return run(btn, () => api(`/api/image?slot=${slot}&name=${name}`, {
|
||
method: 'POST', headers: {'Content-Type': 'application/octet-stream'}, body: converted
|
||
}), slot < 0 ? 'Showing' : 'Saved to slot ' + (slot + 1));
|
||
}
|
||
$('imgShow').onclick = e => upload(e.target, -1);
|
||
$('imgSave').onclick = e => {
|
||
const slot = +$('imgSlot').value;
|
||
if (state.slots[slot] !== null && !confirm(`Replace "${state.slots[slot]}" in slot ${slot + 1}?`)) return;
|
||
upload(e.target, slot);
|
||
};
|
||
|
||
// Settings
|
||
$('wifiSave').onclick = e => run(e.target, () => post('/api/wifi', {ssid: $('ssid').value, pass: $('pass').value}),
|
||
$('ssid').value ? 'Saved, connecting…' : 'Home WiFi removed');
|
||
|
||
function setTz(tz) {
|
||
const preset = [...$('tzPreset').options].find(o => o.value === tz);
|
||
$('tzPreset').value = preset ? tz : '';
|
||
$('tz').value = tz;
|
||
}
|
||
$('tzPreset').onchange = () => { if ($('tzPreset').value) $('tz').value = $('tzPreset').value; };
|
||
$('tz').oninput = () => setTz($('tz').value);
|
||
const syncTime = tz => post('/api/time', {epoch: Math.round(Date.now() / 1000), ...(tz ? {tz} : {})});
|
||
$('timeSave').onclick = e => run(e.target, () => syncTime($('tz').value), 'Clock updated');
|
||
|
||
$('fwFile').onchange = e => $('fwUpload').disabled = !e.target.files[0];
|
||
$('fwUpload').onclick = e => {
|
||
const f = $('fwFile').files[0];
|
||
if (!f || !confirm(`Install ${f.name}? The board will restart.`)) return;
|
||
$('fwNote').textContent = 'Uploading…';
|
||
run(e.target, async () => {
|
||
await api('/api/ota', {method: 'POST', body: f});
|
||
$('fwNote').textContent = 'Installed. Restarting…';
|
||
setTimeout(() => location.reload(), 8000);
|
||
});
|
||
};
|
||
|
||
// Give the board the time straight away, so the clock works without internet
|
||
syncTime().catch(() => {});
|
||
refresh().catch(e => $('status').textContent = 'Error: ' + e.message);
|
||
setInterval(() => document.hidden || refresh().catch(() => {}), 5000);
|
||
</script>
|
||
</body>
|
||
</html>
|