unify reading of .env for matrix parameters

This commit is contained in:
waltem01 2024-03-14 14:27:40 +01:00
parent bdf2df59c3
commit 7dec79ea84

View File

@ -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[][];