diff --git a/sdk/index.ts b/sdk/index.ts index c502da6..9fb09a0 100644 --- a/sdk/index.ts +++ b/sdk/index.ts @@ -2,7 +2,7 @@ import * as Bacon from "baconjs"; import type { StateF } from "baconjs/types/withstatemachine"; import { API_COMMAND } from "./api"; import { StageInputs } from "./commands/inputs"; -import { SensorTestMode } from "./commands/sensor_test"; +import { SMXSensorTestData, SensorTestMode } from "./commands/sensor_test"; import { HID_REPORT_INPUT, HID_REPORT_INPUT_STATE, @@ -13,20 +13,34 @@ import { process_packets, send_data, } from "./packet"; +import { SMXConfig } from "./commands/config"; +import { SMXDeviceInfo } from "./commands/data_info"; export async function getDeviceInfo(dev: HIDDevice) { await send_data(dev, [API_COMMAND.GET_DEVICE_INFO], true); - return process_packets(dev, true); + const packet = await process_packets(dev, API_COMMAND.GET_DEVICE_INFO, true); + return new SMXDeviceInfo(packet); } export async function getStageConfig(dev: HIDDevice) { await send_data(dev, [API_COMMAND.GET_CONFIG_V5], true); - return process_packets(dev, true); + const response = await process_packets(dev, API_COMMAND.GET_CONFIG_V5, true); + + const smxconfig = new SMXConfig(response); + + // Right now I just want to confirm that decoding and re-encoding gives back the same data + const encoded_config = smxconfig.encode(); + if (encoded_config) { + const buf = new Uint8Array(encoded_config.buffer); + console.log("Same Thing:", response.slice(2, -1).toString() === buf.toString()); + } + return smxconfig; } export async function getSensorTestData(dev: HIDDevice) { await send_data(dev, [API_COMMAND.GET_SENSOR_TEST_DATA, SensorTestMode.CalibratedValues], true); - return process_packets(dev, true); + const response = await process_packets(dev, API_COMMAND.GET_SENSOR_TEST_DATA, true); + return new SMXSensorTestData(response); } interface PacketHandlingState { diff --git a/sdk/packet.ts b/sdk/packet.ts index 7e029ee..88ee1f4 100644 --- a/sdk/packet.ts +++ b/sdk/packet.ts @@ -110,8 +110,10 @@ export async function send_data(dev: HIDDevice, data: Array, debug = fal } } -export async function process_packets(dev: HIDDevice, debug = false): Promise> { - const current_packet: Array = []; +export async function process_packets(dev: HIDDevice, responseType: number, debug = false): Promise> { + let current_packet: Array = []; + + let seenFirstPacket = false; while (true) { const data = await nextReportCommand(dev); @@ -124,6 +126,16 @@ export async function process_packets(dev: HIDDevice, debug = false): Promise { return { ...devices, @@ -62,23 +58,11 @@ export async function open_smx_device(dev: HIDDevice) { } export async function requestConfig(dev: HIDDevice) { - const response = await getStageConfig(dev); - const smxconfig = new SMXConfig(response); - - // Right now I just want to confirm that decoding and re-encoding gives back the same data - const encoded_config = smxconfig.encode(); - if (encoded_config) { - const buf = new Uint8Array(encoded_config.buffer); - console.log("Same Thing:", response.slice(2, -1).toString() === buf.toString()); - } + return getStageConfig(dev); } export async function requestTestData(dev: HIDDevice) { - const response = await getSensorTestData(dev); - if (response[0] !== API_COMMAND.GET_SENSOR_TEST_DATA) { - return null; - } - return new SMXSensorTestData(response); + return getSensorTestData(dev); } /** anything here will appear in the debug UI to dispatch at will */