-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
125 lines (114 loc) · 3.9 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* (c) 2024, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import "./demo.css";
import { MicrobitWebUSBConnection } from "../lib/webusb";
import { HexFlashDataSource } from "../lib/hex-flash-data-source";
import { ConnectionStatus, DeviceConnection } from "../lib/device";
import { MicrobitWebBluetoothConnection } from "../lib/bluetooth";
import { AccelerometerDataEvent } from "../lib/accelerometer";
document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<section id="flash">
<h2>Connect and flash</h2>
<label><div>Name</div>
<input id="name" type="text">
</label>
<select class="transport">
<option value="usb">WebUSB</option>
<option value="bluetooth">Web Bluetooth</option>
</select>
<button class="connect">Connect</button>
<button class="disconnect">Disconnect</button>
<p class="status"></p>
<label><div>File to flash</div><input type="file"/></label>
<button class="flash">Flash</button>
<div class="acc-controls">
<button class="acc-data-get">Get accelerometer data</button>
<button class="acc-data-listen">Listen to accelerometer data</button>
<button class="acc-data-stop">Stop accelerometer data</button>
</div>
</section>
`;
const transport = document.querySelector(
"#flash > .transport",
)! as HTMLSelectElement;
const connect = document.querySelector("#flash > .connect")!;
const disconnect = document.querySelector("#flash > .disconnect")!;
const flash = document.querySelector("#flash > .flash")!;
const fileInput = document.querySelector(
"#flash input[type=file]",
)! as HTMLInputElement;
const statusParagraph = document.querySelector("#flash > .status")!;
const accDataGet = document.querySelector(
"#flash > .acc-controls > .acc-data-get",
)!;
const accDataListen = document.querySelector(
"#flash > .acc-controls > .acc-data-listen",
)!;
const accDataStop = document.querySelector(
"#flash > .acc-controls > .acc-data-stop",
)!;
let connection: DeviceConnection = new MicrobitWebUSBConnection();
const displayStatus = (status: ConnectionStatus) => {
statusParagraph.textContent = status.toString();
};
const switchTransport = async () => {
await connection.disconnect();
connection.dispose();
switch (transport.value) {
case "bluetooth": {
connection = new MicrobitWebBluetoothConnection();
break;
}
case "usb": {
connection = new MicrobitWebUSBConnection();
break;
}
}
await connection.initialize();
};
transport.addEventListener("change", switchTransport);
void switchTransport();
connection.addEventListener("status", (event) => {
displayStatus(event.status);
});
displayStatus(connection.status);
connect.addEventListener("click", async () => {
await connection.connect();
});
disconnect.addEventListener("click", async () => {
await connection.disconnect();
});
flash.addEventListener("click", async () => {
const file = fileInput.files?.item(0);
if (file) {
const text = await file.text();
await connection.flash(new HexFlashDataSource(text), {
partial: true,
progress: (percentage: number | undefined) => {
console.log(percentage);
},
});
}
});
accDataGet.addEventListener("click", async () => {
const acc = await connection.getAccelerometer();
const data = await acc?.getData();
console.log("Get accelerometer data", data);
});
const accChangedListener = (event: AccelerometerDataEvent) => {
console.log(event.data);
};
accDataListen.addEventListener("click", async () => {
const acc = await connection.getAccelerometer();
console.log("Stream accelerometer data");
acc?.addEventListener("accelerometerdatachanged", accChangedListener);
acc?.startNotifications();
});
accDataStop.addEventListener("click", async () => {
const acc = await connection.getAccelerometer();
acc?.removeEventListener("accelerometerdatachanged", accChangedListener);
acc?.stopNotifications();
});