Skip to content

Commit

Permalink
process_packets now only processes a given command
Browse files Browse the repository at this point in the history
  • Loading branch information
noahm committed Apr 2, 2024
1 parent 8f91345 commit 64616ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
22 changes: 18 additions & 4 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions sdk/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ export async function send_data(dev: HIDDevice, data: Array<number>, debug = fal
}
}

export async function process_packets(dev: HIDDevice, debug = false): Promise<Array<number>> {
const current_packet: Array<number> = [];
export async function process_packets(dev: HIDDevice, responseType: number, debug = false): Promise<Array<number>> {
let current_packet: Array<number> = [];

let seenFirstPacket = false;

while (true) {
const data = await nextReportCommand(dev);
Expand All @@ -124,6 +126,16 @@ export async function process_packets(dev: HIDDevice, debug = false): Promise<Ar
if (handle_packet(new Uint8Array(data.buffer), current_packet)) {
break;
}

if (!seenFirstPacket) {
if (current_packet[0] === responseType) {
seenFirstPacket = true;
} else {
// we just picked up some other data that has nothing to do with
// what we're waiting on, so just discard it and keep waiting
current_packet = [];
}
}
}

// TODO: Handle Acknowledgements
Expand Down
24 changes: 4 additions & 20 deletions ui/pad-coms.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { getDeviceInfo, getSensorTestData, getStageConfig } from "../sdk";
import { API_COMMAND, SMX_USB_PRODUCT_ID, SMX_USB_VENDOR_ID } from "../sdk/api";
import { SMXConfig } from "../sdk/commands/config";
import { SMXDeviceInfo } from "../sdk/commands/data_info";
import { SMXSensorTestData } from "../sdk/commands/sensor_test";
import { SMX_USB_PRODUCT_ID, SMX_USB_VENDOR_ID } from "../sdk/api";
import { devices$, nextStatusTextLine$, statusText$, uiState } from "./state";

// function formatDataForDisplay(data: DataView) {
Expand Down Expand Up @@ -47,8 +44,7 @@ export async function open_smx_device(dev: HIDDevice) {
await dev.open();
}
// Get the device info an find the player number
const packet = await getDeviceInfo(dev);
const device_info = new SMXDeviceInfo(packet);
const device_info = await getDeviceInfo(dev);
uiState.set(devices$, (devices) => {
return {
...devices,
Expand All @@ -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 */
Expand Down

0 comments on commit 64616ec

Please sign in to comment.