Skip to content

Commit d8b5309

Browse files
Update demo to get and set device ID
1 parent 1bcf66b commit d8b5309

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/demo.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
MicrobitRadioBridgeConnection,
3030
} from "../lib/usb-radio-bridge";
3131
import "./demo.css";
32+
import { Logging } from "../lib/logging";
3233

3334
type ConnectionType = "usb" | "bluetooth" | "radio";
3435

@@ -37,16 +38,31 @@ type TypedConnection =
3738
| { type: "bluetooth"; connection: MicrobitWebBluetoothConnection }
3839
| { type: "usb"; connection: MicrobitWebUSBConnection };
3940

41+
class NullLogging implements Logging {
42+
event(e: unknown): void {
43+
console.log(e);
44+
}
45+
error(e: unknown): void {
46+
console.log(e);
47+
}
48+
log(e: unknown): void {
49+
console.log(e);
50+
}
51+
}
52+
53+
const logging = new NullLogging();
54+
4055
const createConnections = (
4156
type: "usb" | "bluetooth" | "radio",
4257
): TypedConnection => {
4358
switch (type) {
4459
case "bluetooth":
45-
return { type, connection: createWebBluetoothConnection() };
60+
return { type, connection: createWebBluetoothConnection({ logging }) };
4661
case "usb":
4762
return {
4863
type,
4964
connection: createWebUSBConnection({
65+
logging,
5066
deviceSelectionMode: DeviceSelectionMode.UseAnyAllowed,
5167
}),
5268
};
@@ -55,10 +71,10 @@ const createConnections = (
5571
// To use with a remote micro:bit we need a UI flow that grabs and sets the remote id.
5672
const connection = createRadioBridgeConnection(
5773
createWebUSBConnection({
74+
logging,
5875
deviceSelectionMode: DeviceSelectionMode.UseAnyAllowed,
5976
}),
6077
);
61-
connection.setRemoteDeviceId(0);
6278
return { type, connection };
6379
}
6480
};
@@ -109,6 +125,7 @@ const createConnectSection = (): Section => {
109125
const statusParagraph = crelt("p");
110126
let name = "";
111127
let exclusionFilters = JSON.stringify([{ serialNumber: "XXXX" }]);
128+
let remoteDeviceId = 0;
112129
const dom = crelt(
113130
"section",
114131
crelt("h2", "Connect"),
@@ -158,6 +175,21 @@ const createConnectSection = (): Section => {
158175
}),
159176
)
160177
: undefined,
178+
type === "radio"
179+
? crelt(
180+
"label",
181+
"Remote device id",
182+
crelt("input", {
183+
type: "number",
184+
value: remoteDeviceId,
185+
onchange: (e: Event) => {
186+
remoteDeviceId = parseInt(
187+
(e.currentTarget as HTMLInputElement).value,
188+
);
189+
},
190+
}),
191+
)
192+
: undefined,
161193
crelt(
162194
"button",
163195
{
@@ -174,6 +206,8 @@ const createConnectSection = (): Section => {
174206
connection.setRequestDeviceExclusionFilters(parsedExclusionFilters);
175207
} else if (type === "bluetooth") {
176208
connection.setNameFilter(name);
209+
} else if (type === "radio") {
210+
connection.setRemoteDeviceId(remoteDeviceId);
177211
}
178212
void connection.connect();
179213
},
@@ -196,6 +230,9 @@ const createConnectSection = (): Section => {
196230
statusParagraph.textContent = status.toString();
197231
};
198232
const handleDisplayStatusChange = (event: ConnectionStatusEvent) => {
233+
if (type === "usb" && event.status === ConnectionStatus.CONNECTED) {
234+
console.log("Device id:", (connection as any).getDeviceId());
235+
}
199236
displayStatus(event.status);
200237
};
201238
const backgroundErrorListener = (event: BackgroundErrorEvent) => {

0 commit comments

Comments
 (0)