From da378545c861c90ba9e496d5d33d511ea68489e6 Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Tue, 16 Apr 2024 17:34:20 -0500 Subject: [PATCH 1/3] Correct config sizes for read/write old config --- sdk/commands/config.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sdk/commands/config.ts b/sdk/commands/config.ts index 55c9a73..9c92de9 100644 --- a/sdk/commands/config.ts +++ b/sdk/commands/config.ts @@ -343,6 +343,7 @@ const OLD_CONFIG_INIT = [ export class SMXConfig { public config: Decoded; private oldConfig: Decoded | null = null; + private oldConfigSize: number | null = null; private firmwareVersion: number; /** @@ -356,8 +357,12 @@ export class SMXConfig { if (this.firmwareVersion >= 5) { this.config = smx_config_t.decode(data.slice(2, -1), true); } else { + this.oldConfigSize = data[1]; console.log("Reading Old Config"); - this.oldConfig = smx_old_config_t.decode(data.slice(2, -1), true); + + const slicedData = data.slice(2, -1); + const oldData = Uint8Array.from({ length: 250 }, (_, i) => slicedData[i] ?? 0); + this.oldConfig = smx_old_config_t.decode(oldData, true); this.config = this.convertOldToNew(this.oldConfig); } } @@ -370,7 +375,14 @@ export class SMXConfig { if (!this.oldConfig) throw new ReferenceError("Can not encode old config as it is null"); console.log("Writing Old Config"); this.convertNewToOld(); - return Array.from(new Uint8Array(smx_old_config_t.encode(this.oldConfig, true).buffer)); + + const encodedConfig = smx_old_config_t.encode(this.oldConfig, true); + // If the old config data is less than 128 Bytes, only send the first 128 bytes + if (this.oldConfigSize && this.oldConfigSize <= 128) { + return Array.from(new Uint8Array(encodedConfig.buffer.slice(0, 128))); + } + + return Array.from(new Uint8Array(encodedConfig.buffer)); } /** From b71f33dadb708660615592d77192e062c66ef275 Mon Sep 17 00:00:00 2001 From: Noah Manneschmidt Date: Tue, 16 Apr 2024 15:45:07 -0700 Subject: [PATCH 2/3] make a nice padding util --- sdk/commands/config.ts | 5 +++-- sdk/utils.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sdk/commands/config.ts b/sdk/commands/config.ts index 9c92de9..87b29b8 100644 --- a/sdk/commands/config.ts +++ b/sdk/commands/config.ts @@ -1,6 +1,7 @@ import { StructBuffer, bits, uint8_t, uint16_t, sbytes } from "@nmann/struct-buffer"; import { EnabledSensors } from "./enabled-sensors.ts"; import { Panel } from "../api.ts"; +import { padData } from "../utils.ts"; export type Decoded = ReturnType; @@ -361,8 +362,8 @@ export class SMXConfig { console.log("Reading Old Config"); const slicedData = data.slice(2, -1); - const oldData = Uint8Array.from({ length: 250 }, (_, i) => slicedData[i] ?? 0); - this.oldConfig = smx_old_config_t.decode(oldData, true); + const paddedData = padData(slicedData, 250); + this.oldConfig = smx_old_config_t.decode(paddedData, true); this.config = this.convertOldToNew(this.oldConfig); } } diff --git a/sdk/utils.ts b/sdk/utils.ts index 8ea5a6a..7646fd6 100644 --- a/sdk/utils.ts +++ b/sdk/utils.ts @@ -4,8 +4,12 @@ import { MAX_PACKET_SIZE } from "./packet.ts"; * Pad incomming packet to `MAX_PACKET_SIZE` and convert to Uint8Array as a * final step before being sent to the device. */ -export function pad_packet(packet: Array, padding = 0): Uint8Array { - return Uint8Array.from({ length: MAX_PACKET_SIZE }, (_, i) => packet[i] ?? 0); +export function pad_packet(packet: ArrayLike): Uint8Array { + return padData(packet, MAX_PACKET_SIZE); +} + +export function padData(data: ArrayLike, size: number, paddingValue = 0): Uint8Array { + return Uint8Array.from({ length: size }, (_, i) => data[i] ?? paddingValue); } export class RGB { From 1fd53c9b6194ec6d1af7ecd556249e01e9165326 Mon Sep 17 00:00:00 2001 From: Noah Manneschmidt Date: Tue, 16 Apr 2024 15:49:50 -0700 Subject: [PATCH 3/3] add a few more explanitory comments --- sdk/commands/config.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sdk/commands/config.ts b/sdk/commands/config.ts index 87b29b8..c0402e0 100644 --- a/sdk/commands/config.ts +++ b/sdk/commands/config.ts @@ -344,6 +344,11 @@ const OLD_CONFIG_INIT = [ export class SMXConfig { public config: Decoded; private oldConfig: Decoded | null = null; + /** + * some much older pads send smaller sized config data, so we need + * to keep track of how much they sent us and send back an appropriate + * sized config in the other direction + */ private oldConfigSize: number | null = null; private firmwareVersion: number; @@ -362,7 +367,9 @@ export class SMXConfig { console.log("Reading Old Config"); const slicedData = data.slice(2, -1); - const paddedData = padData(slicedData, 250); + // handle very old stage's smaller config data by padding + // it out to the full size of the `smx_old_config_t` struct + const paddedData = padData(slicedData, smx_old_config_t.byteLength); this.oldConfig = smx_old_config_t.decode(paddedData, true); this.config = this.convertOldToNew(this.oldConfig); }