Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
feat: implement WebUSB FTDI fallback driver (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverben authored Apr 21, 2024
1 parent ff74848 commit 15949b3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Playwright Tests
on:
pull_request:
branches: [main, master]
branches:
- main
jobs:
test:
timeout-minutes: 60
Expand All @@ -10,21 +11,27 @@ jobs:
image: mcr.microsoft.com/playwright:v1.42.1-jammy
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
cache: yarn

- name: Install node modules
run: yarn --frozen-lockfile --prefer-offline

- name: Run linter
run: yarn lint

- name: Run ESLint
run: npx eslint .

- name: Run prettier
run: npx prettier -c ./src

- name: Run Playwright tests
run: yarn playwright test

- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN yarn install --frozen-lockfile && yarn build

FROM nginx:stable

COPY --from=builder /build/dist/ /usr/share/nginx/html
COPY --from=builder /build/dist/browser /usr/share/nginx/html
18 changes: 7 additions & 11 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser-esbuild",
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/",
"index": "src/index.html",
"main": "src/main.ts",
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
Expand Down Expand Up @@ -96,8 +96,6 @@
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
Expand All @@ -118,18 +116,16 @@
"with": "src/environments/environment.dev.ts"
}
],
"optimization": true,
"optimization": false,
"outputHashing": "all",
"sourceMap": false,
"sourceMap": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"extractLicenses": false,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
"maximumWarning": "5mb",
"maximumError": "10mb"
},
{
"type": "anyComponentStyle",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
"@blockly/field-bitmap": "^4.0.13",
"@blockly/workspace-backpack": "^5.3.3",
"@fortawesome/fontawesome-free": "^6.1.1",
"@leaphy-robotics/avrdude-webassembly": "^1.4.0",
"@leaphy-robotics/avrdude-webassembly": "1.5.0",
"@leaphy-robotics/dfu-util-wasm": "1.0.2",
"@leaphy-robotics/leaphy-blocks": "2.0.3",
"@leaphy-robotics/picotool-wasm": "1.0.2",
"@leaphy-robotics/webusb-ftdi": "^1.0.0",
"@ngx-translate/core": "^14.0.0",
"@ngx-translate/http-loader": "^7.0.0",
"@serialport/parser-readline": "^10.3.0",
Expand Down
1 change: 1 addition & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ export default defineConfig({
command: "yarn start",
url: "http://localhost:4200/",
reuseExistingServer: !process.env.CI,
timeout: 5 * 60 * 1000,
},
});
9 changes: 7 additions & 2 deletions src/app/services/arduino-uploader/protocols/avrdude/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ export default class Avrdude extends BaseProtocol {

// create a promise that resolves when the port.ondisconnect event is fired
const disconnectPromise = new Promise((resolve) => {
// todo: add compatibility for fallback devices if required, currently no avrdude devices support this
if (!(this.port instanceof SerialPort)) return;
// todo: add disconnect events for fallback, driver currently does not support this
if (
typeof SerialPort === "undefined" ||
!(this.port instanceof SerialPort)
)
return;

this.port.ondisconnect = resolve;
});
Expand Down Expand Up @@ -73,6 +77,7 @@ export default class Avrdude extends BaseProtocol {
}
throw new Error("Avrdude failed");
}
await this.port.open({ baudRate: 115200 });
this.uploadState.statusMessage = "UPDATE_COMPLETE";
return;
}
Expand Down
20 changes: 15 additions & 5 deletions src/app/state/robot.wired.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Injectable } from "@angular/core";
import { ChartDataset } from "chart.js";
import { ReplaySubject, BehaviorSubject, Observable } from "rxjs";
import { map, scan } from "rxjs/operators";
import { SerialPort as MockedSerialPort } from "web-serial-polyfill";
import { SerialPort as MockedCDCSerialPort } from "web-serial-polyfill";
import MockedFTDISerialPort from "@leaphy-robotics/webusb-ftdi";

export type LeaphyPort = SerialPort | MockedSerialPort;
export type LeaphyPort =
| SerialPort
| MockedCDCSerialPort
| MockedFTDISerialPort;

@Injectable({
providedIn: "root",
Expand Down Expand Up @@ -107,8 +111,12 @@ export class RobotWiredState {
}
} else if (navigator.usb) {
if (!forcePrompt) {
const devices = await navigator.usb.getDevices();
if (devices[0]) port = new MockedSerialPort(devices[0]);
const [device] = await navigator.usb.getDevices();
if (device) {
if (device.vendorId === 1027)
port = new MockedFTDISerialPort(device);
else port = new MockedCDCSerialPort(device);
}
}
if (!port && allowPrompt) {
const device = await navigator.usb.requestDevice({
Expand All @@ -119,7 +127,9 @@ export class RobotWiredState {
),
});
try {
port = new MockedSerialPort(device);
if (device.vendorId === 1027)
port = new MockedFTDISerialPort(device);
else port = new MockedCDCSerialPort(device);
} catch {
throw new Error("WebUSB device is not supported");
}
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1829,10 +1829,12 @@
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.4.0.tgz#08d6c5e20cf7e4cc02fd181c4b0c225cd31dbb60"
integrity sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA==

"@leaphy-robotics/avrdude-webassembly@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@leaphy-robotics/avrdude-webassembly/-/avrdude-webassembly-1.4.0.tgz#ee5babb8c3202f5346dba45bb07263ae2ce9c185"
integrity sha512-S4vPUIsVWQuEL+GCXfAH+CisJSITNd8cs1PqzBMVwjrbFzs3XnXj/5OJ1xlgOwQkmFHQnmyWcRkQG2C43VGosg==
"@leaphy-robotics/[email protected]":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@leaphy-robotics/avrdude-webassembly/-/avrdude-webassembly-1.5.0.tgz#8c1bea9864b0d4064b8b79ef4b1121a327c10703"
integrity sha512-8vy45vH7pnMwKBixNeTNWwt8fbuyeREK5oPzPiKiqkQ+AXv+aWi3BojlJyZlVsO10nJpcwgPlDYDRjbDS5Hkuw==
dependencies:
"@leaphy-robotics/webusb-ftdi" "^1.0.0"

"@leaphy-robotics/[email protected]":
version "1.0.2"
Expand All @@ -1851,6 +1853,11 @@
resolved "https://registry.yarnpkg.com/@leaphy-robotics/picotool-wasm/-/picotool-wasm-1.0.2.tgz#648150c5e29204f063750e5348b66dae3f5db14a"
integrity sha512-ux3RyjjcHJopr/zOyTkEEtZTgzYTNd5zKNAMXvaVNDqRJIm9jCVTUytIpYPkUV7myXAt23jXl5dIVhsCiEDsqw==

"@leaphy-robotics/webusb-ftdi@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@leaphy-robotics/webusb-ftdi/-/webusb-ftdi-1.0.0.tgz#999a9de85cef82747f09b1dfa6e01374bd85ba07"
integrity sha512-/M3XdqQ9h0EPlPdE1FRCZYz8VUzP4JZe1sidUNPMOBmzwALiXElPa6jLu2eArPupt23cN1PN9yXNpSjOwtRi0Q==

"@leichtgewicht/ip-codec@^2.0.1":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b"
Expand Down

0 comments on commit 15949b3

Please sign in to comment.