167 lines
4.2 KiB
JavaScript
167 lines
4.2 KiB
JavaScript
const dgram = require('dgram');
|
|
|
|
const {
|
|
addHexColor,
|
|
createFrame,
|
|
fadeFrame,
|
|
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 PATH_FADE = 0.85;
|
|
const PULSE_LENGTH = 6;
|
|
|
|
const paletteStops = [
|
|
{ stop: 0.0, color: hexToRgb('020209') },
|
|
{ stop: 0.3, color: hexToRgb('023047') },
|
|
{ stop: 0.6, color: hexToRgb('115173') },
|
|
{ stop: 0.8, color: hexToRgb('1ca78f') },
|
|
{ stop: 1.0, color: hexToRgb('94fdf3') },
|
|
];
|
|
|
|
const accentColors = ['14f5ff', 'a7ff4d', 'ffcc3f'];
|
|
|
|
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 circuit-pulse.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 paths = createPaths(width, height);
|
|
const pulses = createPulses(paths.length);
|
|
const frameTimeSeconds = intervalMs / 1000;
|
|
|
|
if (isBroadcast) {
|
|
socket.bind(() => {
|
|
socket.setBroadcast(true);
|
|
});
|
|
}
|
|
|
|
socket.on('error', (error) => {
|
|
console.error('Socket error:', error.message);
|
|
});
|
|
|
|
function createPaths(matrixWidth, matrixHeight) {
|
|
const horizontalStep = Math.max(2, Math.floor(matrixHeight / 4));
|
|
const verticalStep = Math.max(2, Math.floor(matrixWidth / 4));
|
|
const generatedPaths = [];
|
|
|
|
for (let y = 1; y < matrixHeight; y += horizontalStep) {
|
|
const path = [];
|
|
for (let x = 0; x < matrixWidth; ++x) {
|
|
path.push({ x, y });
|
|
}
|
|
generatedPaths.push(path);
|
|
}
|
|
|
|
for (let x = 2; x < matrixWidth; x += verticalStep) {
|
|
const path = [];
|
|
for (let y = 0; y < matrixHeight; ++y) {
|
|
path.push({ x, y });
|
|
}
|
|
generatedPaths.push(path);
|
|
}
|
|
|
|
return generatedPaths;
|
|
}
|
|
|
|
function createPulses(count) {
|
|
const pulseList = [];
|
|
for (let index = 0; index < count; ++index) {
|
|
pulseList.push(spawnPulse(index));
|
|
}
|
|
return pulseList;
|
|
}
|
|
|
|
function spawnPulse(pathIndex) {
|
|
const color = accentColors[pathIndex % accentColors.length];
|
|
return {
|
|
pathIndex,
|
|
position: 0,
|
|
speed: 3 + Math.random() * 2,
|
|
color,
|
|
};
|
|
}
|
|
|
|
function updatePulse(pulse, deltaSeconds) {
|
|
pulse.position += pulse.speed * deltaSeconds;
|
|
const path = paths[pulse.pathIndex];
|
|
|
|
if (!path || path.length === 0) {
|
|
return;
|
|
}
|
|
|
|
if (pulse.position >= path.length + PULSE_LENGTH) {
|
|
Object.assign(pulse, spawnPulse(pulse.pathIndex));
|
|
pulse.position = 0;
|
|
}
|
|
}
|
|
|
|
function renderPulse(pulse) {
|
|
const path = paths[pulse.pathIndex];
|
|
if (!path) {
|
|
return;
|
|
}
|
|
|
|
for (let offset = 0; offset < PULSE_LENGTH; ++offset) {
|
|
const index = Math.floor(pulse.position) - offset;
|
|
if (index < 0 || index >= path.length) {
|
|
continue;
|
|
}
|
|
|
|
const { x, y } = path[index];
|
|
const intensity = Math.max(0, 1 - offset / PULSE_LENGTH);
|
|
const baseColor = samplePalette(paletteStops, intensity);
|
|
frame[toIndex(x, y, width)] = baseColor;
|
|
addHexColor(frame, toIndex(x, y, width), pulse.color, intensity * 1.4);
|
|
}
|
|
}
|
|
|
|
function generateFrame() {
|
|
fadeFrame(frame, PATH_FADE);
|
|
|
|
pulses.forEach((pulse) => {
|
|
updatePulse(pulse, frameTimeSeconds);
|
|
renderPulse(pulse);
|
|
});
|
|
|
|
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 circuit pulse to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms, paths=${paths.length})`
|
|
);
|
|
|