Skip to content

Commit

Permalink
Attempt to clean up packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Apr 4, 2024
1 parent 812be57 commit 5172853
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions sdk/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function send_data(dev: HIDDevice, data: Array<number>, debug = fal
export async function process_packets(dev: HIDDevice, responseType: number, debug = false): Promise<Array<number>> {
let current_packet: Array<number> = [];
let seenFirstPacket = false;
let reading_packet = false;
let readingPacket = false;

while (true) {
const data = await nextReportCommand(dev);
Expand All @@ -124,12 +124,23 @@ export async function process_packets(dev: HIDDevice, responseType: number, debu
}

const data_buffer = new Uint8Array(data.buffer);
if (responseType !== 121) {
console.log(data_buffer);
}

// If we see an ACK Command just ignore it and restart
if (data_buffer[0] === ACK_COMMAND) {
readingPacket = false;
seenFirstPacket = false;
current_packet = [];
continue;
}

// Wait until we see a packet start flag so we don't grab a
// packet half way through
if (!reading_packet) {
if (!readingPacket) {
if (data_buffer[0] & PACKET_FLAG_START_OF_COMMAND) {
reading_packet = true;
readingPacket = true;
} else {
continue;
}
Expand All @@ -140,6 +151,7 @@ export async function process_packets(dev: HIDDevice, responseType: number, debu
}

if (!seenFirstPacket) {
console.log(responseType, current_packet[0]);
if (current_packet[0] === responseType) {
seenFirstPacket = true;
} else {
Expand All @@ -152,7 +164,7 @@ export async function process_packets(dev: HIDDevice, responseType: number, debu
// If we've gotten here and our current packet is empty, that means
// we flushed it at some point. Restart
if (current_packet.length === 0) {
reading_packet = false;
readingPacket = false;
}
}

Expand Down

0 comments on commit 5172853

Please sign in to comment.