Skip to content

Commit

Permalink
Merge pull request #31 from noahm/nm/array-types
Browse files Browse the repository at this point in the history
use uint8array in most places
  • Loading branch information
noahm authored Apr 16, 2024
2 parents b8839ae + 114761a commit d1e5612
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions sdk/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class SMXConfig {
/**
* Take in the data array and decode it into this.
*/
constructor(data: Array<number>, firmwareVersion: number) {
constructor(data: Uint8Array, firmwareVersion: number) {
this.firmwareVersion = firmwareVersion;
console.log("Config Firmware Version: ", this.firmwareVersion);
console.log("CONFIG RAW DATA: ", data.toString());
Expand All @@ -375,9 +375,9 @@ export class SMXConfig {
}
}

encode(): Array<number> {
encode(): Uint8Array {
if (this.firmwareVersion >= 5) {
return Array.from(new Uint8Array(smx_config_t.encode(this.config, true).buffer));
return new Uint8Array(smx_config_t.encode(this.config, true).buffer);
}

if (!this.oldConfig) throw new ReferenceError("Can not encode old config as it is null");
Expand All @@ -387,10 +387,10 @@ export class SMXConfig {
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 new Uint8Array(encodedConfig.buffer.slice(0, 128));
}

return Array.from(new Uint8Array(encodedConfig.buffer));
return new Uint8Array(encodedConfig.buffer);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions sdk/commands/data_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export class SMXDeviceInfo {
firmware_version = 0;
player = 0;

constructor(data: Array<number>) {
constructor(data: Uint8Array) {
console.log("DEVICEINFO RAW DATA: ", data.toString());
this.#decode(data);
}

#decode(data: Array<number>) {
#decode(data: Uint8Array) {
const info_packet = data_info_packet_t.decode(data, true);

this.player = Number.parseInt(String.fromCharCode(info_packet.player)) + 1;
Expand Down
2 changes: 1 addition & 1 deletion sdk/commands/sensor_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class SMXPanelTestData {
export class SMXSensorTestData {
panels: Array<SMXPanelTestData> = [];

constructor(data: Array<number>, mode: SensorTestMode, isFsr: boolean) {
constructor(data: Uint8Array, mode: SensorTestMode, isFsr: boolean) {
/**
* The first 3 bytes are the preamble.
*
Expand Down
4 changes: 2 additions & 2 deletions sdk/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const HID_REPORT_INPUT_STATE = 0x03;
export const HID_REPORT_OUTPUT = 0x05;
export const HID_REPORT_INPUT = 0x06;

export async function send_data(dev: HIDDevice, data: Array<number>, debug = false) {
export async function send_data(dev: HIDDevice, data: Uint8Array, debug = false) {
// Split data into packets
const packets = make_packets(data);

Expand All @@ -37,7 +37,7 @@ export async function send_data(dev: HIDDevice, data: Array<number>, debug = fal
}
}

export function make_packets(data: Array<number>): Array<Uint8Array> {
export function make_packets(data: Uint8Array): Array<Uint8Array> {
const packets = [];
const data_len = data.length;
let idx = 0;
Expand Down
26 changes: 13 additions & 13 deletions sdk/smx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class SMXEvents {
/** read from this to see if we've received an ACK */
public readonly ackReports$: Bacon.EventStream<AckPacket>;
/** push to this to write commands to the stage */
public readonly output$: Bacon.Bus<number[]>;
public readonly output$: Bacon.Bus<Uint8Array>;
/** this is everything pushed to `output$` but properly throttled/timed to the device's ack responses */
public readonly eventsToSend$: Bacon.EventStream<number[]>;
public readonly eventsToSend$: Bacon.EventStream<Uint8Array>;

constructor(dev: HIDDevice) {
// Main USB Ingestor
Expand Down Expand Up @@ -56,7 +56,7 @@ class SMXEvents {
const okSend$ = finishedCommand$.startWith(true);

// Main USB Output
this.output$ = new Bacon.Bus<Array<number>>();
this.output$ = new Bacon.Bus<Uint8Array>();

// Config writes should only happen at most once per second.
const configOutput$ = this.output$
Expand Down Expand Up @@ -170,7 +170,7 @@ export class SMXStage {

const command = info.firmware_version < 5 ? API_COMMAND.WRITE_CONFIG : API_COMMAND.WRITE_CONFIG_V5;
const encoded_config = config.encode();
this.events.output$.push([command, encoded_config.length].concat(encoded_config));
this.events.output$.push(new Uint8Array([command, encoded_config.length, ...encoded_config]));

return this.events.ackReports$.firstToPromise();
}
Expand All @@ -185,7 +185,7 @@ export class SMXStage {
light_command.push(...rgb);
}

this.events.output$.push(light_command);
this.events.output$.push(new Uint8Array(light_command));
return this.events.ackReports$.firstToPromise();
}

Expand All @@ -204,38 +204,38 @@ export class SMXStage {
this.setLightStrip(new RGB(color.r, color.g, color.b));
}

this.events.output$.push([API_COMMAND.FACTORY_RESET]);
this.events.output$.push(Uint8Array.of(API_COMMAND.FACTORY_RESET));
return this.events.ackReports$.firstToPromise();
}

forceRecalibration(): Promise<AckPacket> {
this.events.output$.push([API_COMMAND.FORCE_RECALIBRATION]);
this.events.output$.push(Uint8Array.of(API_COMMAND.FORCE_RECALIBRATION));
return this.events.ackReports$.firstToPromise();
}

updateDeviceInfo(): Promise<SMXDeviceInfo> {
this.events.output$.push([API_COMMAND.GET_DEVICE_INFO]);
this.events.output$.push(Uint8Array.of(API_COMMAND.GET_DEVICE_INFO));
return this.deviceInfo$.firstToPromise();
}

async updateConfig(): Promise<SMXConfig> {
const info = await this.needsInfo();

const command = info.firmware_version < 5 ? API_COMMAND.GET_CONFIG : API_COMMAND.GET_CONFIG_V5;
this.events.output$.push([command]);
this.events.output$.push(Uint8Array.of(command));
return this.configResponse$.firstToPromise();
}

updateTestData(mode: SensorTestMode | null = null): Promise<SMXSensorTestData> {
if (mode) this.test_mode = mode;

this.events.output$.push([API_COMMAND.GET_SENSOR_TEST_DATA, this.test_mode]);
this.events.output$.push(Uint8Array.of(API_COMMAND.GET_SENSOR_TEST_DATA, this.test_mode));
return this.testDataResponse$.firstToPromise();
}

private handleConfig(data: Uint8Array): SMXConfig {
// biome-ignore lint/style/noNonNullAssertion: info should very much be defined here
this._config = new SMXConfig(Array.from(data), this.info!.firmware_version);
this._config = new SMXConfig(data, this.info!.firmware_version);

// Right now I just want to confirm that decoding and encoding gives us back the same data
const encoded_config = this._config.encode();
Expand All @@ -250,15 +250,15 @@ export class SMXStage {

private handleTestData(data: Uint8Array): SMXSensorTestData {
// biome-ignore lint/style/noNonNullAssertion: config should very much be defined here
this.test = new SMXSensorTestData(Array.from(data), this.test_mode, this.config!.flags.PlatformFlags_FSR);
this.test = new SMXSensorTestData(data, this.test_mode, this.config!.flags.PlatformFlags_FSR);

this.debug && console.log("Got Test: ", this.test);

return this.test;
}

private handleDeviceInfo(data: Uint8Array): SMXDeviceInfo {
this.info = new SMXDeviceInfo(Array.from(data));
this.info = new SMXDeviceInfo(data);

this.debug && console.log("Got Info: ", this.info);

Expand Down

0 comments on commit d1e5612

Please sign in to comment.