Skip to content

Commit

Permalink
Testing more bacon
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Apr 5, 2024
1 parent 47f6af8 commit edcdb9c
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions sdk/smx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { SMXSensorTestData, SensorTestMode } from "./commands/sensor_test";

class SMXEvents {
private dev;
private dontSend: Bacon.Property<boolean>;
private startedSend$: Bacon.Bus<boolean>;
input$;
inputState$;
otherReports$;
Expand All @@ -39,30 +39,41 @@ 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<boolean>();

// false means "it's ok to send", true means "don't send"
const dontSend$ = new Bacon.Bus<boolean>()
.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<Array<number>>();

// 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<Array<number>>()
.merge(configOutput$)
.merge(otherOutput$)
.holdWhen(dontSend$)
.onValue(async (value) => await this.writeToHID(value));
}

private async writeToHID(value: Array<number>) {
this.startedSend$.push(true);
await send_data(this.dev, value);
}
}
Expand Down

0 comments on commit edcdb9c

Please sign in to comment.