Skip to content

Commit

Permalink
Merge pull request #30 from noahm/fc/fix-old-config
Browse files Browse the repository at this point in the history
Correct config sizes for read/write old config
  • Loading branch information
noahm authored Apr 16, 2024
2 parents feb0e7e + 1fd53c9 commit b8839ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
24 changes: 22 additions & 2 deletions sdk/commands/config.ts
Original file line number Diff line number Diff line change
@@ -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<Struct extends { decode(...args: unknown[]): unknown }> = ReturnType<Struct["decode"]>;

Expand Down Expand Up @@ -343,6 +344,12 @@ const OLD_CONFIG_INIT = [
export class SMXConfig {
public config: Decoded<typeof smx_config_t>;
private oldConfig: Decoded<typeof smx_old_config_t> | 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;

/**
Expand All @@ -356,8 +363,14 @@ 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);
// 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);
}
}
Expand All @@ -370,7 +383,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));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>, padding = 0): Uint8Array {
return Uint8Array.from({ length: MAX_PACKET_SIZE }, (_, i) => packet[i] ?? 0);
export function pad_packet(packet: ArrayLike<number>): Uint8Array {
return padData(packet, MAX_PACKET_SIZE);
}

export function padData(data: ArrayLike<number>, size: number, paddingValue = 0): Uint8Array {
return Uint8Array.from({ length: size }, (_, i) => data[i] ?? paddingValue);
}

export class RGB {
Expand Down

0 comments on commit b8839ae

Please sign in to comment.