sending multiple instructions at once

This commit is contained in:
waltem01 2024-03-15 07:56:19 +01:00
parent 6333a2bac7
commit 802084be76

View File

@ -38,18 +38,7 @@
speed: number; speed: number;
} }
// Data structure to keep track of params when displaying clock parts on matrix // System variables
interface DisplayParams {
x: string;
y: string;
r?: string;
position?: {
x: string;
y: string;
};
}
//.System variables
let clockEnabled: boolean = false; let clockEnabled: boolean = false;
let matrix: Matrix; let matrix: Matrix;
const clock: Clock = { const clock: Clock = {
@ -64,7 +53,7 @@
minute: 0, minute: 0,
hour: 0 hour: 0
}, },
speed: 1.25 speed: 0.4
}; };
// Constant system values // Constant system values
const TWO_PI = 2 * Math.PI; const TWO_PI = 2 * Math.PI;
@ -115,76 +104,61 @@
if (clockEnabled) await displayMatrixClock(); if (clockEnabled) await displayMatrixClock();
} }
// Displays a clock part on the matrix display // Converts the x and y values from percentage to matrix coordinates
async function displayClockPart( function coordsToMatrix(x: number, y: number): { x: number; y: number } {
part: 'body' | 'seconds' | 'minutes' | 'hours', // Calculate the matrix coordinates based on the percentage values of x and y
params: DisplayParams
) {
const fdata = new FormData();
if (part === 'body') {
// If part is 'body', append the 'r' parameter to the form data
fdata.append('endpoint', 'circle');
fdata.append('x', params.x);
fdata.append('y', params.y);
fdata.append('r', params.r!);
} else {
// If part is not 'body', convert the position coordinates to matrix coordinates and append to form data
const coords = coordsToMatrix(params.position!.x, params.position!.y);
fdata.append('endpoint', 'line');
fdata.append('x1', params.x);
fdata.append('y1', params.y);
fdata.append('x2', coords.x);
fdata.append('y2', coords.y);
}
return await redirectAPI({ fdata });
}
// Converts the x and y number values in the given positions object to strings
function convertNumbers(positions: { x: number; y: number }): { x: string; y: string } {
return { return {
x: positions.x.toString(), x: Math.round((Number(x) / 100) * matrix.width),
y: positions.y.toString() y: Math.round((Number(y) / 100) * matrix.height)
}; };
} }
// Converts the x and y values from percentage to matrix coordinates // Function to generate line instructions with start and end points
function coordsToMatrix(x: string, y: string): { x: string; y: string } { function lineInstruction(
// Calculate the matrix coordinates based on the percentage values of x and y endpoint: string,
return convertNumbers({ x1: number,
x: Math.round((Number(x) / 100) * matrix.width), y1: number,
y: Math.round((Number(y) / 100) * matrix.height) position: { x: number; y: number }
}); ) {
// Calculate the matrix coordinates of the ending position
const coords = coordsToMatrix(position.x, position.y);
return {
endpoint,
x1,
y1,
x2: coords.x,
y2: coords.y
};
} }
// Displays the clock on the matrix display, converting number values to strings where necessary // Displays the clock on the matrix display
async function displayMatrixClock() { async function displayMatrixClock() {
const middle = coordsToMatrix('50', '50'); // Calculate the middle coordinates of the matrix
// Display the body of the clock const middle = coordsToMatrix(50, 50);
await displayClockPart('body', {
x: middle.x, // Array of instructions for drawing lines and shapes
y: middle.y, const instructions = [
r: ((clock.body / 100) * matrix.height).toFixed(0) lineInstruction('line', middle.x, middle.y, clock.positions?.second!),
}); lineInstruction('line', middle.x, middle.y, clock.positions?.minute!),
// Display the seconds of the clock, converting position coordinates to strings lineInstruction('line', middle.x, middle.y, clock.positions?.hour!),
await displayClockPart('seconds', { {
x: middle.x, endpoint: 'circle',
y: middle.y, x: middle.x,
position: convertNumbers(clock.positions?.second!) y: middle.y,
}); r: ((clock.body / 100) * matrix.height).toFixed(0) // Calculate circle radius based on clock body size
// Display the minutes of the clock, converting position coordinates to strings },
await displayClockPart('minutes', { {
x: middle.x, endpoint: 'update' // Update instruction for the clock display
y: middle.y, }
position: convertNumbers(clock.positions?.minute!) ];
});
// Display the hours of the clock, converting position coordinates to strings // Create a new FormData object to send instructions to the server
await displayClockPart('hours', { const fdata = new FormData();
x: middle.x, fdata.append('endpoint', 'instructions');
y: middle.y, fdata.append('instructions', JSON.stringify(instructions));
position: convertNumbers(clock.positions?.hour!)
}); // Send instructions to the server for clock display
// Update the clock by fetching the update API endpoint await redirectAPI({ fdata });
await fetch('/api/redirect?endpoint=update');
} }
onMount(() => { onMount(() => {