feat: udp stream

This commit is contained in:
2025-10-01 21:47:27 +02:00
parent 99e7ed4809
commit f3d99b174f
13 changed files with 517 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
const dgram = require('dgram');
const host = process.argv[2];
const port = parseInt(process.argv[3] || '4210', 10);
const pixels = parseInt(process.argv[4] || '64', 10);
const intervalMs = parseInt(process.argv[5] || '30', 10);
if (!host) {
console.error('Usage: node bouncing-ball.js <device-ip> [port] [pixels] [interval-ms]');
process.exit(1);
}
const socket = dgram.createSocket('udp4');
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
let position = Math.random() * (pixels - 1);
let velocity = randomVelocity();
function randomVelocity() {
const min = 0.15;
const max = 0.4;
const sign = Math.random() < 0.5 ? -1 : 1;
return (min + Math.random() * (max - min)) * sign;
}
function rebound(sign) {
velocity = randomVelocity() * sign;
}
function mix(a, b, t) {
return a + (b - a) * t;
}
function generateFrame() {
const dt = intervalMs / 1000;
position += velocity * dt * 60; // scale velocity to 60 FPS reference
if (position < 0) {
position = -position;
rebound(1);
} else if (position > pixels - 1) {
position = (pixels - 1) - (position - (pixels - 1));
rebound(-1);
}
const activeIndex = Math.max(0, Math.min(pixels - 1, Math.round(position)));
let payload = 'RAW:';
for (let i = 0; i < pixels; i++) {
if (i === activeIndex) {
payload += 'ff8000';
continue;
}
const distance = Math.abs(i - position);
const intensity = Math.max(0, 1 - distance);
const green = Math.round(mix(20, 200, intensity)).toString(16).padStart(2, '0');
const blue = Math.round(mix(40, 255, intensity)).toString(16).padStart(2, '0');
payload += '00' + green + blue;
}
return payload;
}
function sendFrame() {
const payload = generateFrame();
const message = Buffer.from(payload, 'utf8');
socket.send(message, port, host);
}
setInterval(sendFrame, intervalMs);
if (isBroadcast) {
socket.bind(() => {
socket.setBroadcast(true);
});
}
console.log(`Streaming bouncing ball pattern to ${host}:${port} with ${pixels} pixels (interval=${intervalMs}ms)`);

View File

@@ -0,0 +1,55 @@
const dgram = require('dgram');
const host = process.argv[2];
const port = parseInt(process.argv[3] || '4210', 10);
const pixels = parseInt(process.argv[4] || '64', 10);
const speed = parseFloat(process.argv[5] || '0.5'); // cycles per second
if (!host) {
console.error('Usage: node fade-green-blue.js <device-ip> [port] [pixels] [speed-hz]');
process.exit(1);
}
const socket = dgram.createSocket('udp4');
const intervalMs = 50;
let tick = 0;
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
function generateFrame() {
const timeSeconds = (tick * intervalMs) / 1000;
const phase = timeSeconds * speed * Math.PI * 2;
const blend = (Math.sin(phase) + 1) * 0.5; // 0..1
const green = Math.round(255 * (1 - blend));
const blue = Math.round(255 * blend);
let payload = 'RAW:';
const gHex = green.toString(16).padStart(2, '0');
const bHex = blue.toString(16).padStart(2, '0');
for (let i = 0; i < pixels; i++) {
payload += '00';
payload += gHex;
payload += bHex;
}
return payload;
}
function sendFrame() {
const payload = generateFrame();
const message = Buffer.from(payload, 'utf8');
socket.send(message, port, host);
tick += 1;
}
setInterval(sendFrame, intervalMs);
if (isBroadcast) {
socket.bind(() => {
socket.setBroadcast(true);
});
}
console.log(`Streaming green/blue fade to ${host}:${port} with ${pixels} pixels (speed=${speed}Hz)`);

View File

@@ -0,0 +1,59 @@
const dgram = require('dgram');
const host = process.argv[2];
const port = parseInt(process.argv[3] || '4210', 10);
const pixels = parseInt(process.argv[4] || '64', 10);
const intervalMs = parseInt(process.argv[5] || '30', 10);
if (!host) {
console.error('Usage: node rainbow.js <device-ip> [port] [pixels] [interval-ms]');
process.exit(1);
}
const socket = dgram.createSocket('udp4');
let offset = 0;
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
function wheel(pos) {
pos = 255 - pos;
if (pos < 85) {
return [255 - pos * 3, 0, pos * 3];
}
if (pos < 170) {
pos -= 85;
return [0, pos * 3, 255 - pos * 3];
}
pos -= 170;
return [pos * 3, 255 - pos * 3, 0];
}
function generateFrame() {
let payload = 'RAW:';
for (let i = 0; i < pixels; i++) {
const colorIndex = (i * 256 / pixels + offset) & 255;
const [r, g, b] = wheel(colorIndex);
payload += r.toString(16).padStart(2, '0');
payload += g.toString(16).padStart(2, '0');
payload += b.toString(16).padStart(2, '0');
}
offset = (offset + 1) & 255;
return payload;
}
function sendFrame() {
const payload = generateFrame();
const message = Buffer.from(payload, 'utf8');
socket.send(message, port, host);
}
setInterval(sendFrame, intervalMs);
if (isBroadcast) {
socket.bind(() => {
socket.setBroadcast(true);
});
}
console.log(`Streaming rainbow pattern to ${host}:${port} with ${pixels} pixels (interval=${intervalMs}ms)`);