Skip to content

Commit

Permalink
WIP DeviceWebUSBConnection, DeviceWebBluetoothConnection interfaces
Browse files Browse the repository at this point in the history
For mocking connections.
  • Loading branch information
microbit-grace committed Jan 23, 2025
1 parent bb2eb6b commit 42a5480
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
BoardVersion,
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
DeviceConnectionEventMap,
DeviceWebBluetoothConnection,
} from "./device.js";
import { TypedEventTarget } from "./events.js";
import { LedMatrix } from "./led.js";
Expand All @@ -38,7 +38,7 @@ export interface MicrobitWebBluetoothConnectionOptions {
*/
export class MicrobitWebBluetoothConnection
extends TypedEventTarget<DeviceConnectionEventMap & ServiceConnectionEventMap>
implements DeviceConnection
implements DeviceWebBluetoothConnection
{
status: ConnectionStatus = ConnectionStatus.SUPPORT_NOT_KNOWN;

Expand Down
40 changes: 29 additions & 11 deletions lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,10 @@ export interface DeviceConnection
/**
* Get the board version.
*
* @returns the board version or null if there is no connection.
* @returns the board version or undefined if there is no connection.
*/
getBoardVersion(): BoardVersion | undefined;

/**
* Flash the micro:bit.
*
* @param dataSource The data to use.
* @param options Flash options and progress callback.
*/
flash?(dataSource: FlashDataSource, options: {}): Promise<void>;

/**
* Disconnect from the device.
*/
Expand All @@ -232,7 +224,7 @@ export interface DeviceConnection
/**
* Write serial data to the device.
*
* Does nothting if there is no connection.
* Does nothing if there is no connection.
*
* @param data The data to write.
* @returns A promise that resolves when the write is complete.
Expand All @@ -242,5 +234,31 @@ export interface DeviceConnection
/**
* Clear device to enable chooseDevice.
*/
clearDevice(): void;
clearDevice(): Promise<void> | void;
}

export interface DeviceWebUSBConnection extends DeviceConnection {
/**
* Get the deviceId.
*
* @returns the device id or undefined if there is no connection.
*/
getDeviceId(): number | undefined;

/**
* Flash the micro:bit.
*
* @param dataSource The data to use.
* @param options Flash options and progress callback.
*/
flash(dataSource: FlashDataSource, options: {}): Promise<void>;
}

export interface DeviceWebBluetoothConnection extends DeviceConnection {
/**
* Sets micro:bit name filter for device requesting.
*
* @param name The name of the micro:bit.
*/
setNameFilter(name: string): void;
}
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import {
DeviceConnectionEventMap,
DeviceError,
DeviceErrorCode,
DeviceWebBluetoothConnection,
DeviceWebUSBConnection,
FlashDataError,
FlashDataSource,
FlashEvent,
FlashOptions,
SerialDataEvent,
SerialErrorEvent,
SerialResetEvent,
Expand Down Expand Up @@ -56,6 +59,9 @@ export type {
ButtonState,
DeviceConnection,
DeviceErrorCode,
DeviceWebBluetoothConnection,
DeviceWebUSBConnection,
FlashOptions,
FlashDataSource,
MagnetometerData,
MagnetometerDataEvent,
Expand Down
4 changes: 2 additions & 2 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
BoardVersion,
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
DeviceConnectionEventMap,
DeviceError,
FlashDataError,
FlashDataSource,
FlashEvent,
FlashOptions,
DeviceWebUSBConnection as DeviceWebUSBConnection,
SerialDataEvent,
SerialErrorEvent,
SerialResetEvent,
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface MicrobitWebUSBConnectionOptions {
*/
export class MicrobitWebUSBConnection
extends TypedEventTarget<DeviceConnectionEventMap>
implements DeviceConnection
implements DeviceWebUSBConnection
{
status: ConnectionStatus =
navigator.usb && !isChromeOS105()
Expand Down
16 changes: 9 additions & 7 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
DeviceWebUSBConnection,
SerialDataEvent,
} from "../lib/device";
import { createUniversalHexFlashDataSource } from "../lib/hex-flash-data-source";
Expand Down Expand Up @@ -194,7 +195,7 @@ const createConnectSection = (type: ConnectionType): Section => {
};

const createFlashSection = (): Section => {
if (!connection.flash) {
if (typeof connection["flash"] !== "function") {
return {};
}
const dom = crelt(
Expand All @@ -209,16 +210,17 @@ const createFlashSection = (): Section => {
const file = (e.currentTarget as HTMLInputElement).files?.item(0);
if (file) {
const text = await file.text();
if (connection.flash) {
console.time("flash");
await connection.flash(createUniversalHexFlashDataSource(text), {
console.time("flash");
await (connection as DeviceWebUSBConnection).flash(
createUniversalHexFlashDataSource(text),
{
partial: true,
progress: (percentage: number | undefined) => {
console.log(percentage);
},
});
console.timeEnd("flash");
}
},
);
console.timeEnd("flash");
}
},
}),
Expand Down

0 comments on commit 42a5480

Please sign in to comment.