116 lines
3.4 KiB
JavaScript
116 lines
3.4 KiB
JavaScript
const dgram = require('dgram');
|
|
|
|
const {
|
|
clamp,
|
|
createFrame,
|
|
frameToPayload,
|
|
hexToRgb,
|
|
samplePalette,
|
|
toIndex,
|
|
} = require('./shared-frame-utils');
|
|
|
|
const DEFAULT_PORT = 4210;
|
|
const DEFAULT_WIDTH = 16;
|
|
const DEFAULT_HEIGHT = 16;
|
|
const DEFAULT_INTERVAL_MS = 65;
|
|
const BAND_COUNT = 5;
|
|
const WAVE_SPEED = 0.35;
|
|
const HORIZONTAL_SWAY = 0.45;
|
|
|
|
const paletteStops = [
|
|
{ stop: 0.0, color: hexToRgb('01010a') },
|
|
{ stop: 0.2, color: hexToRgb('041332') },
|
|
{ stop: 0.4, color: hexToRgb('0c3857') },
|
|
{ stop: 0.65, color: hexToRgb('1aa07a') },
|
|
{ stop: 0.85, color: hexToRgb('68d284') },
|
|
{ stop: 1.0, color: hexToRgb('f4f5c6') },
|
|
];
|
|
|
|
const host = process.argv[2];
|
|
const port = parseInt(process.argv[3] || String(DEFAULT_PORT), 10);
|
|
const width = parseInt(process.argv[4] || String(DEFAULT_WIDTH), 10);
|
|
const height = parseInt(process.argv[5] || String(DEFAULT_HEIGHT), 10);
|
|
const intervalMs = parseInt(process.argv[6] || String(DEFAULT_INTERVAL_MS), 10);
|
|
|
|
if (!host) {
|
|
console.error('Usage: node aurora-curtains.js <device-ip> [port] [width] [height] [interval-ms]');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (Number.isNaN(port) || Number.isNaN(width) || Number.isNaN(height) || Number.isNaN(intervalMs)) {
|
|
console.error('Invalid numeric argument. Expected integers for port, width, height, and interval-ms.');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
console.error('Matrix dimensions must be positive integers.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const socket = dgram.createSocket('udp4');
|
|
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
|
|
const frame = createFrame(width, height);
|
|
const bands = createBands(BAND_COUNT, width);
|
|
let timeSeconds = 0;
|
|
const frameTimeSeconds = intervalMs / 1000;
|
|
|
|
if (isBroadcast) {
|
|
socket.bind(() => {
|
|
socket.setBroadcast(true);
|
|
});
|
|
}
|
|
|
|
socket.on('error', (error) => {
|
|
console.error('Socket error:', error.message);
|
|
});
|
|
|
|
function createBands(count, matrixWidth) {
|
|
const generatedBands = [];
|
|
for (let index = 0; index < count; ++index) {
|
|
generatedBands.push({
|
|
center: Math.random() * (matrixWidth - 1),
|
|
phase: Math.random() * Math.PI * 2,
|
|
width: 1.2 + Math.random() * 1.8,
|
|
});
|
|
}
|
|
return generatedBands;
|
|
}
|
|
|
|
function generateFrame() {
|
|
timeSeconds += frameTimeSeconds;
|
|
|
|
for (let row = 0; row < height; ++row) {
|
|
const verticalRatio = row / Math.max(1, height - 1);
|
|
for (let col = 0; col < width; ++col) {
|
|
let intensity = 0;
|
|
|
|
bands.forEach((band, index) => {
|
|
const sway = Math.sin(timeSeconds * WAVE_SPEED + band.phase + verticalRatio * Math.PI * 2) * HORIZONTAL_SWAY;
|
|
const center = band.center + sway * (index % 2 === 0 ? 1 : -1);
|
|
const distance = Math.abs(col - center);
|
|
const blurred = Math.exp(-(distance * distance) / (2 * band.width * band.width));
|
|
intensity += blurred * (0.8 + Math.sin(timeSeconds * 0.4 + index) * 0.2);
|
|
});
|
|
|
|
const normalized = clamp(intensity / bands.length, 0, 1);
|
|
const gradientBlend = clamp((normalized * 0.7 + verticalRatio * 0.3), 0, 1);
|
|
frame[toIndex(col, row, width)] = samplePalette(paletteStops, gradientBlend);
|
|
}
|
|
}
|
|
|
|
return frameToPayload(frame);
|
|
}
|
|
|
|
function sendFrame() {
|
|
const payload = generateFrame();
|
|
const message = Buffer.from(payload, 'utf8');
|
|
socket.send(message, port, host);
|
|
}
|
|
|
|
setInterval(sendFrame, intervalMs);
|
|
|
|
console.log(
|
|
`Streaming aurora curtains to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms)`
|
|
);
|
|
|