Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/ebb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export class EBB {
}
}

/** Send the new General Query command to get limit switch, button and fifo status */
public async queryGeneral(): Promise<number> {
return parseInt(await this.query("QG"), 16);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly, this is only available in the 3.0 firmware, right?

If so, this should probably check the firmware version first:

private cachedFirmwareVersion: [number, number, number] | undefined = undefined;

}

/** Send a raw command to the EBB and expect a single "OK" line in return. */
public async command(cmd: string): Promise<void> {
try {
Expand Down Expand Up @@ -230,6 +235,24 @@ export class EBB {
}
}

public async sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

public async checkButtonAndWait() {
if (!await this.queryButton())
return;
console.log("BUTTON PAUSED");
// wait for the button to have been released, then pressed again
// is there a better way to do this?
while (await this.queryButton()) await this.sleep(100);
console.log("BUTTON released");
while (!await this.queryButton()) await this.sleep(100);
console.log("BUTTON RESTART");
while (await this.queryButton()) await this.sleep(100);
console.log("BUTTON released");
}

public async executeBlockWithLM(block: Block): Promise<void> {
const [errX, stepsX] = modf((block.p2.x - block.p1.x) * this.stepMultiplier + this.error.x);
const [errY, stepsY] = modf((block.p2.y - block.p1.y) * this.stepMultiplier + this.error.y);
Expand All @@ -251,6 +274,7 @@ export class EBB {
*/
public async executeXYMotionWithLM(plan: XYMotion): Promise<void> {
for (const block of plan.blocks) {
await this.checkButtonAndWait();
await this.executeBlockWithLM(block);
}
}
Expand Down Expand Up @@ -381,7 +405,8 @@ export class EBB {
}

public async queryButton(): Promise<boolean> {
return (await this.queryM("QB"))[0] === "1";
const resp = await this.queryGeneral();
return (resp & (1<<5)) != 0;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ async function* ebbs(path?: string) {
const closed = new Promise((resolve) => {
port.addEventListener('disconnect', resolve, { once: true })
});
yield new EBB(port);
const ebb = new EBB(port);
ebb.firmwareVersion().then((version) => console.log("Firmware version", version));
ebb.queryGeneral(); // clear the button press status on startup
yield ebb;
await closed;
yield null;
console.error(`Lost connection to EBB, reconnecting...`);
Expand Down
Loading