Preview open-mic colour on the rig and save it back to the pad.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c9df537567
commit
10c39015b7
8 files changed
+805
No files matched your search
@@ -0,0 +1,110 @@
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const pads = $("pads");
|
||||
const sliders = [...document.querySelectorAll("input[type=range]")];
|
||||
const ROWS = [[13, 14, 15, 16], [9, 10, 11, 12], [5, 6, 7, 8], [1, 2, 3, 4]];
|
||||
let dragging = false;
|
||||
let seenDev = null;
|
||||
let devDown = false;
|
||||
|
||||
for (const row of ROWS) {
|
||||
for (const pad of row) {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.dataset.pad = String(pad);
|
||||
button.textContent = String(pad);
|
||||
pads.appendChild(button);
|
||||
}
|
||||
}
|
||||
|
||||
function hex(part) {
|
||||
return part.toString(16).padStart(2, "0");
|
||||
}
|
||||
|
||||
function ink(r, g, b) {
|
||||
return r * 0.3 + g * 0.6 + b * 0.1 > 140 ? "#1a1206" : "#fff";
|
||||
}
|
||||
|
||||
function paint(data) {
|
||||
$("meta").textContent = `${data.artnet} · ${data.midi}`;
|
||||
$("status").textContent = data.status;
|
||||
const byPad = new Map(data.pads.map((item) => [item.pad, item]));
|
||||
for (const button of pads.querySelectorAll("button")) {
|
||||
const item = byPad.get(Number(button.dataset.pad));
|
||||
button.style.background = `#${hex(item.r)}${hex(item.g)}${hex(item.b)}`;
|
||||
button.style.color = ink(item.r, item.g, item.b);
|
||||
button.classList.toggle("selected", data.editing && item.pad === data.selected);
|
||||
}
|
||||
if (!dragging && data.selected) {
|
||||
const item = byPad.get(data.selected);
|
||||
sliders[0].value = item.r;
|
||||
sliders[1].value = item.g;
|
||||
sliders[2].value = item.b;
|
||||
sliders[3].value = item.w;
|
||||
}
|
||||
for (const slider of sliders) {
|
||||
slider.parentElement.querySelector("output").textContent = slider.value;
|
||||
}
|
||||
}
|
||||
|
||||
async function post(url, body) {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || "request failed");
|
||||
paint(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
pads.addEventListener("click", (ev) => {
|
||||
const button = ev.target.closest("button");
|
||||
if (!button) return;
|
||||
post("/api/pad", {pad: Number(button.dataset.pad)}).catch((err) => {
|
||||
$("status").textContent = err.message;
|
||||
});
|
||||
});
|
||||
|
||||
for (const slider of sliders) {
|
||||
slider.addEventListener("pointerdown", () => {
|
||||
dragging = true;
|
||||
});
|
||||
slider.addEventListener("input", () => {
|
||||
slider.parentElement.querySelector("output").textContent = slider.value;
|
||||
post("/api/part", {part: Number(slider.dataset.part), level: Number(slider.value)}).catch((err) => {
|
||||
$("status").textContent = err.message;
|
||||
});
|
||||
});
|
||||
}
|
||||
window.addEventListener("pointerup", () => {
|
||||
dragging = false;
|
||||
});
|
||||
|
||||
async function poll() {
|
||||
try {
|
||||
const data = await fetch("/api/state", {cache: "no-store"}).then((res) => res.json());
|
||||
paint(data);
|
||||
} catch {
|
||||
$("meta").textContent = "Reconnecting…";
|
||||
}
|
||||
setTimeout(poll, 200);
|
||||
}
|
||||
|
||||
async function watchDev() {
|
||||
try {
|
||||
const dev = await fetch("/api/dev", {cache: "no-store"}).then((res) => res.json());
|
||||
if (devDown || (seenDev && (dev.boot !== seenDev.boot || dev.static !== seenDev.static))) {
|
||||
location.reload();
|
||||
return;
|
||||
}
|
||||
seenDev = dev;
|
||||
devDown = false;
|
||||
} catch {
|
||||
devDown = seenDev !== null;
|
||||
}
|
||||
setTimeout(watchDev, 400);
|
||||
}
|
||||
|
||||
poll();
|
||||
watchDev();
|
||||
Reference in new issue
Block a user