Skip to content

Commit 42a5480

Browse files
WIP DeviceWebUSBConnection, DeviceWebBluetoothConnection interfaces
For mocking connections.
1 parent bb2eb6b commit 42a5480

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

lib/bluetooth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
BoardVersion,
1616
ConnectionStatus,
1717
ConnectionStatusEvent,
18-
DeviceConnection,
1918
DeviceConnectionEventMap,
19+
DeviceWebBluetoothConnection,
2020
} from "./device.js";
2121
import { TypedEventTarget } from "./events.js";
2222
import { LedMatrix } from "./led.js";
@@ -38,7 +38,7 @@ export interface MicrobitWebBluetoothConnectionOptions {
3838
*/
3939
export class MicrobitWebBluetoothConnection
4040
extends TypedEventTarget<DeviceConnectionEventMap & ServiceConnectionEventMap>
41-
implements DeviceConnection
41+
implements DeviceWebBluetoothConnection
4242
{
4343
status: ConnectionStatus = ConnectionStatus.SUPPORT_NOT_KNOWN;
4444

lib/device.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,10 @@ export interface DeviceConnection
212212
/**
213213
* Get the board version.
214214
*
215-
* @returns the board version or null if there is no connection.
215+
* @returns the board version or undefined if there is no connection.
216216
*/
217217
getBoardVersion(): BoardVersion | undefined;
218218

219-
/**
220-
* Flash the micro:bit.
221-
*
222-
* @param dataSource The data to use.
223-
* @param options Flash options and progress callback.
224-
*/
225-
flash?(dataSource: FlashDataSource, options: {}): Promise<void>;
226-
227219
/**
228220
* Disconnect from the device.
229221
*/
@@ -232,7 +224,7 @@ export interface DeviceConnection
232224
/**
233225
* Write serial data to the device.
234226
*
235-
* Does nothting if there is no connection.
227+
* Does nothing if there is no connection.
236228
*
237229
* @param data The data to write.
238230
* @returns A promise that resolves when the write is complete.
@@ -242,5 +234,31 @@ export interface DeviceConnection
242234
/**
243235
* Clear device to enable chooseDevice.
244236
*/
245-
clearDevice(): void;
237+
clearDevice(): Promise<void> | void;
238+
}
239+
240+
export interface DeviceWebUSBConnection extends DeviceConnection {
241+
/**
242+
* Get the deviceId.
243+
*
244+
* @returns the device id or undefined if there is no connection.
245+
*/
246+
getDeviceId(): number | undefined;
247+
248+
/**
249+
* Flash the micro:bit.
250+
*
251+
* @param dataSource The data to use.
252+
* @param options Flash options and progress callback.
253+
*/
254+
flash(dataSource: FlashDataSource, options: {}): Promise<void>;
255+
}
256+
257+
export interface DeviceWebBluetoothConnection extends DeviceConnection {
258+
/**
259+
* Sets micro:bit name filter for device requesting.
260+
*
261+
* @param name The name of the micro:bit.
262+
*/
263+
setNameFilter(name: string): void;
246264
}

lib/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import {
1212
DeviceConnectionEventMap,
1313
DeviceError,
1414
DeviceErrorCode,
15+
DeviceWebBluetoothConnection,
16+
DeviceWebUSBConnection,
1517
FlashDataError,
1618
FlashDataSource,
1719
FlashEvent,
20+
FlashOptions,
1821
SerialDataEvent,
1922
SerialErrorEvent,
2023
SerialResetEvent,
@@ -56,6 +59,9 @@ export type {
5659
ButtonState,
5760
DeviceConnection,
5861
DeviceErrorCode,
62+
DeviceWebBluetoothConnection,
63+
DeviceWebUSBConnection,
64+
FlashOptions,
5965
FlashDataSource,
6066
MagnetometerData,
6167
MagnetometerDataEvent,

lib/usb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
BoardVersion,
1111
ConnectionStatus,
1212
ConnectionStatusEvent,
13-
DeviceConnection,
1413
DeviceConnectionEventMap,
1514
DeviceError,
1615
FlashDataError,
1716
FlashDataSource,
1817
FlashEvent,
1918
FlashOptions,
19+
DeviceWebUSBConnection as DeviceWebUSBConnection,
2020
SerialDataEvent,
2121
SerialErrorEvent,
2222
SerialResetEvent,
@@ -46,7 +46,7 @@ export interface MicrobitWebUSBConnectionOptions {
4646
*/
4747
export class MicrobitWebUSBConnection
4848
extends TypedEventTarget<DeviceConnectionEventMap>
49-
implements DeviceConnection
49+
implements DeviceWebUSBConnection
5050
{
5151
status: ConnectionStatus =
5252
navigator.usb && !isChromeOS105()

src/demo.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ConnectionStatus,
1313
ConnectionStatusEvent,
1414
DeviceConnection,
15+
DeviceWebUSBConnection,
1516
SerialDataEvent,
1617
} from "../lib/device";
1718
import { createUniversalHexFlashDataSource } from "../lib/hex-flash-data-source";
@@ -194,7 +195,7 @@ const createConnectSection = (type: ConnectionType): Section => {
194195
};
195196

196197
const createFlashSection = (): Section => {
197-
if (!connection.flash) {
198+
if (typeof connection["flash"] !== "function") {
198199
return {};
199200
}
200201
const dom = crelt(
@@ -209,16 +210,17 @@ const createFlashSection = (): Section => {
209210
const file = (e.currentTarget as HTMLInputElement).files?.item(0);
210211
if (file) {
211212
const text = await file.text();
212-
if (connection.flash) {
213-
console.time("flash");
214-
await connection.flash(createUniversalHexFlashDataSource(text), {
213+
console.time("flash");
214+
await (connection as DeviceWebUSBConnection).flash(
215+
createUniversalHexFlashDataSource(text),
216+
{
215217
partial: true,
216218
progress: (percentage: number | undefined) => {
217219
console.log(percentage);
218220
},
219-
});
220-
console.timeEnd("flash");
221-
}
221+
},
222+
);
223+
console.timeEnd("flash");
222224
}
223225
},
224226
}),

0 commit comments

Comments
 (0)