Skip to content

Commit

Permalink
Remove connect options in favour of setters (#12)
Browse files Browse the repository at this point in the history
This will allow us to create a radio bridge connection  instance
upfront. This API should probably change but let's get our internal
clients using it and evolve it with usage in mind.
  • Loading branch information
microbit-robert authored Jul 24, 2024
1 parent 5e246a6 commit a3c23c4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
22 changes: 12 additions & 10 deletions lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
AfterRequestDevice,
BeforeRequestDevice,
BoardVersion,
ConnectOptions,
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
Expand Down Expand Up @@ -62,6 +61,7 @@ export class MicrobitWebBluetoothConnection
this.availability = value;
};
private availability: boolean | undefined;
private nameFilter: string | undefined;

constructor(options: MicrobitWebBluetoothConnectionOptions = {}) {
super();
Expand Down Expand Up @@ -100,8 +100,8 @@ export class MicrobitWebBluetoothConnection
);
}

async connect(options: ConnectOptions = {}): Promise<ConnectionStatus> {
await this.connectInternal(options);
async connect(): Promise<ConnectionStatus> {
await this.connectInternal();
return this.status;
}

Expand Down Expand Up @@ -150,9 +150,9 @@ export class MicrobitWebBluetoothConnection
this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
}

private async connectInternal(options: ConnectOptions): Promise<void> {
private async connectInternal(): Promise<void> {
if (!this.connection) {
const device = await this.chooseDevice(options);
const device = await this.chooseDevice();
if (!device) {
return;
}
Expand All @@ -169,9 +169,11 @@ export class MicrobitWebBluetoothConnection
this.setStatus(ConnectionStatus.CONNECTED);
}

private async chooseDevice(
options: ConnectOptions,
): Promise<BluetoothDevice | undefined> {
setNameFilter(name: string) {
this.nameFilter = name;
}

private async chooseDevice(): Promise<BluetoothDevice | undefined> {
if (this.device) {
return this.device;
}
Expand All @@ -183,8 +185,8 @@ export class MicrobitWebBluetoothConnection
navigator.bluetooth.requestDevice({
filters: [
{
namePrefix: options.name
? `BBC micro:bit [${options.name}]`
namePrefix: this.nameFilter
? `BBC micro:bit [${this.nameFilter}]`
: "BBC micro:bit",
},
],
Expand Down
7 changes: 1 addition & 6 deletions lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ export type FlashDataSource = (
boardVersion: BoardVersion,
) => Promise<string | Uint8Array>;

export interface ConnectOptions {
// Name filter used for Web Bluetooth
name?: string;
}

export type BoardVersion = "V1" | "V2";

export class ConnectionStatusEvent extends Event {
Expand Down Expand Up @@ -182,7 +177,7 @@ export interface DeviceConnection
*
* @returns the final connection status.
*/
connect(options?: ConnectOptions): Promise<ConnectionStatus>;
connect(): Promise<ConnectionStatus>;

/**
* Get the board version.
Expand Down
2 changes: 0 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
AfterRequestDevice,
BeforeRequestDevice,
BoardVersion,
ConnectOptions,
ConnectionStatus,
ConnectionStatusEvent,
DeviceError,
Expand Down Expand Up @@ -47,7 +46,6 @@ export type {
AccelerometerDataEvent,
BoardVersion,
ButtonEvent,
ConnectOptions,
DeviceConnection,
DeviceErrorCode,
FlashDataSource,
Expand Down
25 changes: 16 additions & 9 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
* SPDX-License-Identifier: MIT
*/

import { MicrobitWebUSBConnection } from "./usb.js";
import * as protocol from "./usb-serial-protocol.js";
import { Logging, NullLogging } from "./logging.js";
import { TypedEventTarget } from "./events.js";
import { AccelerometerDataEvent } from "./accelerometer.js";
import { ButtonEvent, ButtonState } from "./buttons.js";
import {
BoardVersion,
ConnectionStatus,
ConnectionStatusEvent,
ConnectOptions,
DeviceConnection,
DeviceConnectionEventMap,
SerialDataEvent,
} from "./device.js";
import { TypedEventTarget } from "./events.js";
import { Logging, NullLogging } from "./logging.js";
import {
ServiceConnectionEventMap,
TypedServiceEventDispatcher,
} from "./service-events.js";
import { AccelerometerDataEvent } from "./accelerometer.js";
import { ButtonEvent, ButtonState } from "./buttons.js";
import * as protocol from "./usb-serial-protocol.js";
import { MicrobitWebUSBConnection } from "./usb.js";

const connectTimeoutDuration: number = 10000;

Expand All @@ -45,6 +44,7 @@ export class MicrobitRadioBridgeConnection
status: ConnectionStatus;
private logging: Logging;
private serialSession: RadioBridgeSerialSession | undefined;
private remoteDeviceId: number | undefined;

private delegateStatusListner = (e: ConnectionStatusEvent) => {
if (e.status !== ConnectionStatus.CONNECTED) {
Expand All @@ -56,7 +56,6 @@ export class MicrobitRadioBridgeConnection

constructor(
private delegate: MicrobitWebUSBConnection,
private remoteDeviceId: number,
options?: MicrobitRadioBridgeConnectionOptions,
) {
super();
Expand Down Expand Up @@ -87,10 +86,18 @@ export class MicrobitRadioBridgeConnection
this.delegate.clearDevice();
}

async connect(options: ConnectOptions): Promise<ConnectionStatus> {
setRemoteDeviceId(remoteDeviceId: number) {
this.remoteDeviceId = remoteDeviceId;
}

async connect(): Promise<ConnectionStatus> {
// TODO: previously this skipped overlapping connect attempts but that seems awkward
// can we... just not do that? or wait?

if (this.remoteDeviceId === undefined) {
throw new BridgeError(`Missing remote micro:bit ID`);
}

this.logging.event({
type: "Connect",
message: "Serial connect start",
Expand Down
11 changes: 6 additions & 5 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const createConnection = (type: "usb" | "bluetooth" | "radio") => {
case "usb":
return new MicrobitWebUSBConnection();
case "radio":
return new MicrobitRadioBridgeConnection(
// This only works with the local-sensor hex.
// To use with a remote micro:bit we need a UI flow that grabs and sets the remote id.
const connection = new MicrobitRadioBridgeConnection(
new MicrobitWebUSBConnection(),
// This only works with the local-sensor hex.
// To use with a remote micro:bit we need a UI flow that grabs the remote id.
0,
);
connection.setRemoteDeviceId(0);
return connection;
}
};

Expand Down Expand Up @@ -121,7 +122,7 @@ const createConnectSection = (type: ConnectionType): Section => {
"button",
{
onclick: () => {
void connection.connect({ name: name || undefined });
void connection.connect();
},
},
"Connect",
Expand Down

0 comments on commit a3c23c4

Please sign in to comment.