From edcdb9ccbf25e51692485d8b3394be27f10c66a9 Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Fri, 5 Apr 2024 14:00:10 -0500 Subject: [PATCH] Testing more bacon --- sdk/smx.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/sdk/smx.ts b/sdk/smx.ts index 41fc957..f4ae7e2 100644 --- a/sdk/smx.ts +++ b/sdk/smx.ts @@ -15,7 +15,7 @@ import { SMXSensorTestData, SensorTestMode } from "./commands/sensor_test"; class SMXEvents { private dev; - private dontSend: Bacon.Property; + private startedSend$: Bacon.Bus; input$; inputState$; otherReports$; @@ -39,12 +39,20 @@ class SMXEvents { .filter((d) => d.byteLength !== 0) .withStateMachine({ currentPacket: new Uint8Array() }, handlePacket); - this.dontSend = this.otherReports$ + // this.otherReports$.onValue((value) => console.log("Packet: ", value)); + + const finishedCommand$ = this.otherReports$ .filter((e) => e.type === 'host_cmd_finished') - .map((e) => e.type !== "host_cmd_finished") - .toProperty(false); + .map((e) => e.type === 'host_cmd_finished') + .map((e) => !e); - // this.otherReports$.onValue((value) => console.log("Packet: ", value)); + this.startedSend$ = new Bacon.Bus(); + + // false means "it's ok to send", true means "don't send" + const dontSend$ = new Bacon.Bus() + .merge(finishedCommand$) // Returns false when host_cmd_finished + .merge(this.startedSend$) // Return true when starting to send + .toProperty(false); // Main USB Output this.output$ = new Bacon.Bus>(); @@ -52,17 +60,20 @@ class SMXEvents { // Config writes should only happen at most once per second. const configOutput$ = this.output$ .filter((e) => e[0] === API_COMMAND.WRITE_CONFIG_V5) - .throttle(1000) - .holdWhen(this.dontSend) - .onValue(async (value) => await this.writeToHID(value)); + .throttle(1000); const otherOutput$ = this.output$ - .filter((e) => e[0] !== API_COMMAND.WRITE_CONFIG_V5) - .holdWhen(this.dontSend) + .filter((e) => e[0] !== API_COMMAND.WRITE_CONFIG_V5); + + const combinedOutput$ = new Bacon.Bus>() + .merge(configOutput$) + .merge(otherOutput$) + .holdWhen(dontSend$) .onValue(async (value) => await this.writeToHID(value)); } private async writeToHID(value: Array) { + this.startedSend$.push(true); await send_data(this.dev, value); } }