Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusion filters to requestDevice for usb connections #28

Merged
merged 1 commit into from
Sep 11, 2024
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