Skip to content

Commit

Permalink
Add brushless to server
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 committed Sep 3, 2023
1 parent abd1dec commit f061831
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/ebb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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"));
})

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')
Expand Down
9 changes: 7 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}
)
Expand Down
13 changes: 10 additions & 3 deletions src/ebb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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<void> {
const pin = this.brushless ? 5 : 4;
return this.command(`S2,${height},${pin},${rate},${delay}`);
Expand Down
17 changes: 9 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}});
}
Expand Down Expand Up @@ -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());
Expand All @@ -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...`);
Expand All @@ -286,13 +287,13 @@ async function* ebbs(path?: string) {
}
}

export async function connectEBB(path: string | undefined): Promise<EBB | null> {
export async function connectEBB(hardware: string, path?: string): Promise<EBB | null> {
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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f061831

Please sign in to comment.