mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2025-01-12 11:31:45 +00:00
sending multiple instructions at once
This commit is contained in:
parent
6333a2bac7
commit
802084be76
@ -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', {
|
|
||||||
|
// Array of instructions for drawing lines and shapes
|
||||||
|
const instructions = [
|
||||||
|
lineInstruction('line', middle.x, middle.y, clock.positions?.second!),
|
||||||
|
lineInstruction('line', middle.x, middle.y, clock.positions?.minute!),
|
||||||
|
lineInstruction('line', middle.x, middle.y, clock.positions?.hour!),
|
||||||
|
{
|
||||||
|
endpoint: 'circle',
|
||||||
x: middle.x,
|
x: middle.x,
|
||||||
y: middle.y,
|
y: middle.y,
|
||||||
r: ((clock.body / 100) * matrix.height).toFixed(0)
|
r: ((clock.body / 100) * matrix.height).toFixed(0) // Calculate circle radius based on clock body size
|
||||||
});
|
},
|
||||||
// Display the seconds of the clock, converting position coordinates to strings
|
{
|
||||||
await displayClockPart('seconds', {
|
endpoint: 'update' // Update instruction for the clock display
|
||||||
x: middle.x,
|
}
|
||||||
y: middle.y,
|
];
|
||||||
position: convertNumbers(clock.positions?.second!)
|
|
||||||
});
|
// Create a new FormData object to send instructions to the server
|
||||||
// Display the minutes of the clock, converting position coordinates to strings
|
const fdata = new FormData();
|
||||||
await displayClockPart('minutes', {
|
fdata.append('endpoint', 'instructions');
|
||||||
x: middle.x,
|
fdata.append('instructions', JSON.stringify(instructions));
|
||||||
y: middle.y,
|
|
||||||
position: convertNumbers(clock.positions?.minute!)
|
// Send instructions to the server for clock display
|
||||||
});
|
await redirectAPI({ fdata });
|
||||||
// Display the hours of the clock, converting position coordinates to strings
|
|
||||||
await displayClockPart('hours', {
|
|
||||||
x: middle.x,
|
|
||||||
y: middle.y,
|
|
||||||
position: convertNumbers(clock.positions?.hour!)
|
|
||||||
});
|
|
||||||
// Update the clock by fetching the update API endpoint
|
|
||||||
await fetch('/api/redirect?endpoint=update');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user