Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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
49 changes: 38 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,40 @@ 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;
}

export interface DeviceRadioBridgeConnection extends DeviceConnection {
/**
* Sets remote device.
*
* @param deviceId The device id of remote micro:bit.
*/
setRemoteDeviceId(deviceId: number): void;
}
8 changes: 8 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {
DeviceConnectionEventMap,
DeviceError,
DeviceErrorCode,
DeviceRadioBridgeConnection,
DeviceWebBluetoothConnection,
DeviceWebUSBConnection,
FlashDataError,
FlashDataSource,
FlashEvent,
FlashOptions,
SerialDataEvent,
SerialErrorEvent,
SerialResetEvent,
Expand Down Expand Up @@ -56,6 +60,10 @@ export type {
ButtonState,
DeviceConnection,
DeviceErrorCode,
DeviceRadioBridgeConnection,
DeviceWebBluetoothConnection,
DeviceWebUSBConnection,
FlashOptions,
FlashDataSource,
MagnetometerData,
MagnetometerDataEvent,
Expand Down
4 changes: 2 additions & 2 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
BoardVersion,
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
DeviceConnectionEventMap,
DeviceRadioBridgeConnection,
SerialDataEvent,
SerialErrorEvent,
} from "./device.js";
Expand Down Expand Up @@ -48,7 +48,7 @@ interface ConnectCallbacks {
*/
export class MicrobitRadioBridgeConnection
extends TypedEventTarget<DeviceConnectionEventMap & ServiceConnectionEventMap>
implements DeviceConnection
implements DeviceRadioBridgeConnection
{
status: ConnectionStatus;
private logging: Logging;
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") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use ConnectionType to determine this.

Copy link
Contributor Author

@microbit-grace microbit-grace Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have addressed this here: 29257d6

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
Loading