diff --git a/src/__tests__/ebb.test.ts b/src/__tests__/ebb.test.ts index 49c62538..eff32749 100644 --- a/src/__tests__/ebb.test.ts +++ b/src/__tests__/ebb.test.ts @@ -29,7 +29,7 @@ describe("EBB", () => { it("firmware version", async () => { const port = await openTestPort(); - const ebb = new EBB(port); + const ebb = new EBB(port, 'Axidraw'); (port as any)._port.binding.emitData(Buffer.from('aoeu\r\n')); expect(await ebb.firmwareVersion()).toEqual('aoeu'); expect((port as any)._port.binding.recording).toEqual(Buffer.from("V\r")); @@ -37,7 +37,7 @@ describe("EBB", () => { it("enable motors", async () => { const port = await openTestPort(); - const ebb = new EBB(port); + const ebb = new EBB(port, 'Axidraw'); const oldWrite = (port as any)._port.write; (port as any)._port.write = (data: string | Buffer | number[], ...args: any[]) => { if (data.toString() === 'V\r') diff --git a/src/cli.ts b/src/cli.ts index 52d1c656..01e7d629 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -42,10 +42,15 @@ export function cli(argv: string[]): void { .option("firmware-version", { describe: "print the device's firmware version and exit", type: "boolean" + }) + .option("hardware", { + describe: "select hardware type", + default: "Axidraw", + type: "string", }), args => { if (args["firmware-version"]) { - connectEBB(args.device).then(async (ebb) => { + connectEBB(args.device, args["hardware"]).then(async (ebb) => { if (!ebb) { console.error(`No EBB connected`); return process.exit(1); @@ -55,7 +60,7 @@ export function cli(argv: string[]): void { await ebb.close(); }); } else { - startServer(args.port, args.device, args["enable-cors"], args["max-payload-size"]); + startServer(args.port, args.device, args["enable-cors"], args["max-payload-size"], args["hardware"].toLowerCase()); } } ) diff --git a/src/ebb.ts b/src/ebb.ts index f4f9f438..4e2a984c 100644 --- a/src/ebb.ts +++ b/src/ebb.ts @@ -22,9 +22,15 @@ export class EBB { private cachedFirmwareVersion: [number, number, number] | undefined = undefined; - private brushless = false; // brushless pen servo - - public constructor(port: SerialPort) { + public brushless = false; // brushless pen servo + + public constructor(port: SerialPort, hardware: string) { + console.log(">>>"); + console.log(hardware); + switch (hardware) { + case "axidraw": this.brushless = false; break + case "axidrawbrushless": this.brushless = true; console.log("brushless"); break + } this.port = port; this.writer = this.port.writable.getWriter() this.commandQueue = []; @@ -157,6 +163,7 @@ export class EBB { await this.command(`SR,${(timeout * 1000) | 0}${power != null ? `,${power ? 1 : 0}` : ''}`) } + public setPenHeight(height: number, rate: number, delay = 0): Promise { const pin = this.brushless ? 5 : 4; return this.command(`S2,${height},${pin},${rate},${delay}`); diff --git a/src/server.ts b/src/server.ts index b27de499..5216c61d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -11,9 +11,10 @@ import { EBB } from "./ebb"; import { Axidraw, PenMotion, Motion, Plan } from "./planning"; import { formatDuration } from "./util"; -export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb") { - const app = express(); +export function startServer(port: number, device: string | null = null, enableCors = false, + maxPayloadSize = "200mb", hardware: string) { + const app = express(); app.use("/", express.static(path.join(__dirname, "..", "ui"))); app.use(express.json({limit: maxPayloadSize})); if (enableCors) { @@ -221,7 +222,7 @@ export function startServer(port: number, device: string | null = null, enableCo return new Promise((resolve) => { server.listen(port, () => { async function connect() { - for await (const d of ebbs(device)) { + for await (const d of ebbs(hardware, device)) { ebb = d; broadcast({c: "dev", p: {path: ebb ? /*ebb.port.path*/"/dev/XXX" : null}}); } @@ -265,7 +266,7 @@ async function waitForEbb() { } } -async function* ebbs(path?: string) { +async function* ebbs(hardware: string, path?: string) { while (true) { try { const com = path || (await waitForEbb()); @@ -274,7 +275,7 @@ async function* ebbs(path?: string) { const closed = new Promise((resolve) => { port.addEventListener('disconnect', resolve, { once: true }) }); - yield new EBB(port); + yield new EBB(port, hardware); await closed; yield null; console.error(`Lost connection to EBB, reconnecting...`); @@ -286,13 +287,13 @@ async function* ebbs(path?: string) { } } -export async function connectEBB(path: string | undefined): Promise { +export async function connectEBB(hardware: string, path?: string): Promise { if (path) { - return new EBB(new SerialPortSerialPort(path)); + return new EBB(new SerialPortSerialPort(path), hardware); } else { const ebbs = await listEBBs(); if (ebbs.length) { - return new EBB(new SerialPortSerialPort(ebbs[0])); + return new EBB(new SerialPortSerialPort(ebbs[0]), hardware); } else { return null; } diff --git a/src/ui.tsx b/src/ui.tsx index ce14f6fd..157f0365 100644 --- a/src/ui.tsx +++ b/src/ui.tsx @@ -127,7 +127,8 @@ class WebSerialDriver implements Driver { // (pyserial defaults to 9600) await port.open({ baudRate: 9600 }) const { usbVendorId, usbProductId } = port.getInfo() - return new WebSerialDriver(new EBB(port), `${usbVendorId.toString(16).padStart(4, '0')}:${usbProductId.toString(16).padStart(4, '0')}`) + // doesn't support brushless yet, only 'axidraw' + return new WebSerialDriver(new EBB(port, 'axidraw'), `${usbVendorId.toString(16).padStart(4, '0')}:${usbProductId.toString(16).padStart(4, '0')}`) } private _name: string