Skip to content
Merged
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
6 changes: 6 additions & 0 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class MicrobitWebUSBConnection
? ConnectionStatus.NO_AUTHORIZED_DEVICE
: ConnectionStatus.NOT_SUPPORTED;

private exclusionFilters: USBDeviceFilter[] | undefined;
/**
* The USB device we last connected to.
* Cleared if it is disconnected.
Expand Down Expand Up @@ -175,6 +176,10 @@ export class MicrobitWebUSBConnection
}
}

setRequestDeviceExclusionFilters(exclusionFilters: USBDeviceFilter[]) {
this.exclusionFilters = exclusionFilters;
}

async connect(): Promise<ConnectionStatus> {
return this.withEnrichedErrors(async () => {
await this.connectInternal();
Expand Down Expand Up @@ -421,6 +426,7 @@ export class MicrobitWebUSBConnection
}
this.dispatchTypedEvent("beforerequestdevice", new BeforeRequestDevice());
this.device = await navigator.usb.requestDevice({
exclusionFilters: this.exclusionFilters,
filters: [{ vendorId: 0x0d28, productId: 0x0204 }],
});
this.dispatchTypedEvent("afterrequestdevice", new AfterRequestDevice());
Expand Down
29 changes: 29 additions & 0 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ recreateUi("usb");
const createConnectSection = (type: ConnectionType): Section => {
const statusParagraph = crelt("p");
let name = "";
let exclusionFilters = JSON.stringify([{ serialNumber: "XXXX" }]);
const dom = crelt(
"section",
crelt("h2", "Connect"),
Expand Down Expand Up @@ -119,10 +120,38 @@ const createConnectSection = (type: ConnectionType): Section => {
},
}),
),
type === "usb"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have once again made this demo messier. Ideas for improvement are welcome.

? crelt(
"label",
"Exclusion filters",
crelt("input", {
type: "text",
value: exclusionFilters,
onchange: (e: Event) => {
exclusionFilters = (e.currentTarget as HTMLInputElement).value;
},
}),
)
: undefined,
crelt(
"button",
{
onclick: () => {
if (type === "usb") {
let parsedExclusionFilters;
try {
if (exclusionFilters) {
parsedExclusionFilters = JSON.parse(exclusionFilters);
}
} catch (err) {
console.error("Invalid exclusion filters");
}
(
connection as MicrobitWebUSBConnection
).setRequestDeviceExclusionFilters(parsedExclusionFilters);
} else if (type === "bluetooth") {
(connection as MicrobitWebBluetoothConnection).setNameFilter(name);
}
void connection.connect();
},
},
Expand Down