96 lines
2.7 KiB
JavaScript
96 lines
2.7 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 = 50;
|
|
const SHIMMER = 0.08;
|
|
|
|
const paletteStops = [
|
|
{ stop: 0.0, color: hexToRgb('031521') },
|
|
{ stop: 0.35, color: hexToRgb('024f6d') },
|
|
{ stop: 0.65, color: hexToRgb('13a4a1') },
|
|
{ stop: 0.85, color: hexToRgb('67dcd0') },
|
|
{ stop: 1.0, color: hexToRgb('fcdba4') },
|
|
];
|
|
|
|
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 ocean-glimmer.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);
|
|
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 generateFrame() {
|
|
timeSeconds += frameTimeSeconds;
|
|
|
|
for (let row = 0; row < height; ++row) {
|
|
const v = row / Math.max(1, height - 1);
|
|
for (let col = 0; col < width; ++col) {
|
|
const u = col / Math.max(1, width - 1);
|
|
const base =
|
|
0.33 +
|
|
0.26 * Math.sin(u * Math.PI * 2 + timeSeconds * 1.2) +
|
|
0.26 * Math.sin(v * Math.PI * 2 - timeSeconds * 0.9) +
|
|
0.26 * Math.sin((u + v) * Math.PI * 2 + timeSeconds * 0.5);
|
|
const noise = (Math.random() - 0.5) * SHIMMER;
|
|
const value = clamp(base + noise, 0, 1);
|
|
frame[toIndex(col, row, width)] = samplePalette(paletteStops, value);
|
|
}
|
|
}
|
|
|
|
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 ocean glimmer to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms)`,
|
|
);
|
|
|