Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct config sizes for read/write old config #30

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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