Skip to content

Commit

Permalink
Added more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Apr 8, 2024
1 parent d642e49 commit 1ecef20
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
51 changes: 44 additions & 7 deletions sdk/smx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SMXDeviceInfo } from "./commands/data_info";
import { StageInputs } from "./commands/inputs";
import { HID_REPORT_INPUT, HID_REPORT_INPUT_STATE, send_data } from "./packet";
import { SMXSensorTestData, SensorTestMode } from "./commands/sensor_test";
import { RGB } from "./utils";

/**
* Class purely to set up in/out event stream "pipes" to properly throttle and sync input/output from a stage
Expand Down Expand Up @@ -72,7 +73,7 @@ class SMXEvents {
export class SMXStage {
private dev: HIDDevice;
private readonly events: SMXEvents;
private test_mode: SensorTestMode = SensorTestMode.CalibratedValues; // TODO: Maybe we just let this be public
private test_mode: SensorTestMode = SensorTestMode.CalibratedValues;
private debug = true;

info: SMXDeviceInfo | null = null;
Expand Down Expand Up @@ -130,12 +131,7 @@ export class SMXStage {

/**
* TODO: To Implement:
*
* WRITE_CONFIG_V5
* FACTORY_RESET
* SET_LIGHT_STRIP
* FORCE_RECALIBRATION
*
* Stretch Goal:
* SET_PANEL_TEST_MODE
*
Expand All @@ -155,6 +151,47 @@ export class SMXStage {
return this.events.ackReports$.firstToPromise();
}

setLightStrip(color: RGB): Promise<AckPacket | undefined> {
if (!this.info) return Promise.resolve(undefined);

const led_strip_index = 0; // Always 0
const number_of_leds = 44; // Always 44 (Unless some older or newer versions have more/less?)
const rgb = color.toArray();
const light_command = [API_COMMAND.SET_LIGHT_STRIP, led_strip_index, number_of_leds];

for (let i = 0; i < number_of_leds; i++) {
light_command.push(...rgb);
}

console.log(light_command);
this.events.output$.push(light_command);

return this.events.ackReports$.firstToPromise();
}

factoryReset(): Promise<AckPacket | undefined> {
if (!this.info || !this.config) return Promise.resolve(undefined);

/**
* Factor reset resets the platform strip color saved to the
* configuration, but it doesn't actually apply it to the lights.
*
* Do this for firmware v5 and up.
*/
if (this.info.firmware_version >= 5) {
const color = this.config.config.platformStripColor;
this.setLightStrip(new RGB(color.r, color.g, color.b));
}

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

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

updateDeviceInfo(): Promise<SMXDeviceInfo> {
this.events.output$.push([API_COMMAND.GET_DEVICE_INFO]);
return this.deviceInfo$.firstToPromise();
Expand Down
20 changes: 20 additions & 0 deletions sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ import { MAX_PACKET_SIZE } from "./packet.ts";
export function pad_packet(packet: Array<number>, padding = 0): Uint8Array {
return Uint8Array.from({ length: MAX_PACKET_SIZE }, (_, i) => packet[i] ?? 0);
}

export class RGB {
public r: number;
public g: number;
public b: number;

constructor(r: number, g: number, b: number) {
this.r = this.clamp(r);
this.g = this.clamp(g);
this.b = this.clamp(b);
}

private clamp(value: number) {
return value < 0 ? 0 : value > 255 ? 255 : value;
}

toArray(): Array<number> {
return [this.r, this.g, this.b];
}
}
2 changes: 2 additions & 0 deletions ui/pad-coms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ export const DEBUG_COMMANDS: Record<string, FunctionKeys<SMXStage>> = {
requestConfig: "updateConfig",
writeConfig: "writeConfig",
requestTestData: "updateTestData",
forceRecalibration: "forceRecalibration",
factoryReset: "factoryReset",
};
4 changes: 2 additions & 2 deletions ui/stage/stage-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function useTestData(stage: SMXStage | undefined) {
return;
}
let testMode = SensorTestMode.UncalibratedValues;
switch(testDataMode){
switch (testDataMode) {
case "calibrated":
testMode = SensorTestMode.CalibratedValues;
break;
Expand All @@ -36,7 +36,7 @@ function useTestData(stage: SMXStage | undefined) {
break;
case "tare":
testMode = SensorTestMode.Tare;
};
}
const handle = setInterval(() => stage.updateTestData(testMode), UI_UPDATE_RATE);
return () => clearInterval(handle);
}, [stage, testDataMode]);
Expand Down

0 comments on commit 1ecef20

Please sign in to comment.