From 7dec79ea84baa7ca23149c7d57ff836a493e37b0 Mon Sep 17 00:00:00 2001 From: waltem01 Date: Thu, 14 Mar 2024 14:27:40 +0100 Subject: [PATCH] unify reading of .env for matrix parameters --- Webserver/src/lib/client/matrix.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Webserver/src/lib/client/matrix.ts b/Webserver/src/lib/client/matrix.ts index d328f6d..8ecaafc 100644 --- a/Webserver/src/lib/client/matrix.ts +++ b/Webserver/src/lib/client/matrix.ts @@ -26,14 +26,30 @@ export interface MatrixCell { color: string; } -// Create a 2D array containing MatrixCell objects with bg-color: black -export function initializeMatrix(scaling: number): MatrixCell[][] { - const fullWidth = +PUBLIC_LED_WIDTH * +PUBLIC_LED_CHAIN; - const fullHeight = +PUBLIC_LED_HEIGHT * +PUBLIC_LED_PARALLEL; +// Initialize matrix object based on environment variables +export function initializeMatrix(): Matrix { + const led_width = Number(PUBLIC_LED_WIDTH ?? '64'); + const led_height = Number(PUBLIC_LED_HEIGHT ?? '64'); - return Array.from({ length: fullHeight / scaling }, (_, y) => + const led_chain = Number(PUBLIC_LED_CHAIN ?? '1'); + const led_parallel = Number(PUBLIC_LED_PARALLEL ?? '1'); + + return { + width: led_width * led_parallel, + height: led_height * led_chain, + }; +} + +// Create a 2D array containing MatrixCell objects with bg-color: black +export function createGridArray(matrix: Matrix): MatrixCell[][] { + const { width, height, scale } = matrix; + + if (!scale) throw new Error('No scaling data provided!'); + const { factor } = scale; + + return Array.from({ length: height / factor }, (_, y) => Array.from( - { length: fullWidth / scaling }, + { length: width / factor }, (_, x) => ({ x, y, color: '000000' }) as MatrixCell ) ) as MatrixCell[][];