Skip to content

Commit dc51d00

Browse files
Added connection-specific interfaces and factory functions for exporting instead (#50)
* Added specific interfaces for different connection types. This is helpful for mocking. * Exported new factory functions for creating the connections instead of the class. * Updated demo and docs to match changes.
1 parent d54c691 commit dc51d00

13 files changed

+368
-181
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ node_modules
1111
dist
1212
dist-ssr
1313
*.local
14-
docs
14+
docs/build
1515

1616
# Editor directories and files
1717
.vscode/*

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# micro:bit connection library
22

3-
[This documentation is best viewed on the documentation site rather than GitHub](https://microbit-foundation.github.io/microbit-connection/).
3+
<a href="https://microbit-foundation.github.io/microbit-connection/" class="typedoc-ignore">This documentation is best viewed on the documentation site rather than GitHub or NPM package site.</a>
44

55
This is a JavaScript library for micro:bit connections in browsers via USB and Bluetooth.
66

@@ -18,12 +18,12 @@ This project is a work in progress. We are extracting WebUSB and Web Bluetooth c
1818

1919
### Flash a micro:bit
2020

21-
Instantiate a WebUSB connection using {@link MicrobitWebUSBConnection} class and use it to connect to a micro:bit.
21+
Instantiate a WebUSB connection using {@link createWebUSBConnection} class and use it to connect to a micro:bit.
2222

2323
```ts
24-
import { MicrobitWebUSBConnection } from "@microbit/microbit-connection";
24+
import { createWebUSBConnection } from "@microbit/microbit-connection";
2525

26-
const usb = new MicrobitWebUSBConnection();
26+
const usb = createWebUSBConnection();
2727
const connectionStatus = await usb.connect();
2828

2929
console.log("Connection status: ", connectionStatus);
@@ -80,20 +80,20 @@ For more examples of using other methods in the {@link MicrobitWebUSBConnection}
8080

8181
By default, the micro:bit's Bluetooth service is not enabled. Visit our [Bluetooth tech site page](https://tech.microbit.org/bluetooth/) to download a hex file that would enable the bluetooth service.
8282

83-
Instantiate a Bluetooth connection using {@link MicrobitWebBluetoothConnection} class and use it to connect to a micro:bit.
83+
Instantiate a Bluetooth connection using {@link createWebBluetoothConnection} class and use it to connect to a micro:bit.
8484

8585
```ts
86-
import { MicrobitWebBluetoothConnection } from "@microbit/microbit-connection";
86+
import { createWebBluetoothConnection } from "@microbit/microbit-connection";
8787

88-
const bluetooth = new MicrobitWebBluetoothConnection();
88+
const bluetooth = createWebBluetoothConnection();
8989
const connectionStatus = await bluetooth.connect();
9090

9191
console.log("Connection status: ", connectionStatus);
9292
```
9393

9494
{@link ConnectionStatus | Connection status} is `"CONNECTED"` if connection succeeds.
9595

96-
For more examples of using other methods in the {@link MicrobitWebBluetoothConnection} class, see our [demo code](https://github.com/microbit-foundation/microbit-connection/blob/main/src/demo.ts) for our [demo site](https://microbit-connection.pages.dev/).
96+
For more examples of using other methods in the {@link createWebBluetoothConnection} class, see our [demo code](https://github.com/microbit-foundation/microbit-connection/blob/main/src/demo.ts) for our [demo site](https://microbit-connection.pages.dev/).
9797

9898
## License
9999

docs/custom.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Styling for hiding elements in the generated TypeDoc site. */
2+
a.typedoc-ignore {
3+
display: none;
4+
}

lib/bluetooth.ts

+116-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,126 @@ export interface MicrobitWebBluetoothConnectionOptions {
3333
logging?: Logging;
3434
}
3535

36+
export interface MicrobitWebBluetoothConnection
37+
extends DeviceConnection<ServiceConnectionEventMap> {
38+
/**
39+
* Sets micro:bit name filter for device requesting.
40+
*
41+
* @param name The name of the micro:bit.
42+
*/
43+
setNameFilter(name: string): void;
44+
45+
/**
46+
* Gets micro:bit accelerometer data.
47+
*
48+
* @returns accelerometer data or undefined if there is no connection.
49+
*/
50+
getAccelerometerData(): Promise<AccelerometerData | undefined>;
51+
52+
/**
53+
* Gets micro:bit accelerometer period.
54+
*
55+
* @returns accelerometer period or undefined if there is no connection.
56+
*/
57+
getAccelerometerPeriod(): Promise<number | undefined>;
58+
59+
/**
60+
* Sets micro:bit accelerometer period.
61+
*
62+
* @param value The accelerometer period.
63+
*/
64+
setAccelerometerPeriod(value: number): Promise<void>;
65+
66+
/**
67+
* Sets micro:bit LED text.
68+
*
69+
* @param text The text displayed on micro:bit LED.
70+
*/
71+
setLedText(text: string): Promise<void>;
72+
73+
/**
74+
* Gets micro:bit LED scrolling delay.
75+
*
76+
* @returns LED scrolling delay in milliseconds.
77+
*/
78+
getLedScrollingDelay(): Promise<number | undefined>;
79+
80+
/**
81+
* Sets micro:bit LED scrolling delay.
82+
*
83+
* @param delayInMillis LED scrolling delay in milliseconds.
84+
*/
85+
setLedScrollingDelay(delayInMillis: number): Promise<void>;
86+
87+
/**
88+
* Gets micro:bit LED matrix.
89+
*
90+
* @returns a boolean matrix representing the micro:bit LED display.
91+
*/
92+
getLedMatrix(): Promise<LedMatrix | undefined>;
93+
94+
/**
95+
* Sets micro:bit LED matrix.
96+
*
97+
* @param matrix an boolean matrix representing the micro:bit LED display.
98+
*/
99+
setLedMatrix(matrix: LedMatrix): Promise<void>;
100+
101+
/**
102+
* Gets micro:bit magnetometer data.
103+
*
104+
* @returns magnetometer data.
105+
*/
106+
getMagnetometerData(): Promise<MagnetometerData | undefined>;
107+
108+
/**
109+
* Gets micro:bit magnetometer bearing.
110+
*
111+
* @returns magnetometer bearing.
112+
*/
113+
getMagnetometerBearing(): Promise<number | undefined>;
114+
115+
/**
116+
* Gets micro:bit magnetometer period.
117+
*
118+
* @returns magnetometer period.
119+
*/
120+
getMagnetometerPeriod(): Promise<number | undefined>;
121+
122+
/**
123+
* Sets micro:bit magnetometer period.
124+
*
125+
* @param value magnetometer period.
126+
*/
127+
setMagnetometerPeriod(value: number): Promise<void>;
128+
129+
/**
130+
* Triggers micro:bit magnetometer calibration.
131+
*/
132+
triggerMagnetometerCalibration(): Promise<void>;
133+
134+
/**
135+
* Write UART messages.
136+
*
137+
* @param data UART message.
138+
*/
139+
writeUART(data: Uint8Array): Promise<void>;
140+
}
141+
142+
/**
143+
* A Bluetooth connection factory.
144+
*/
145+
export const createWebBluetoothConnection = (
146+
options?: MicrobitWebBluetoothConnectionOptions,
147+
): MicrobitWebBluetoothConnection =>
148+
new MicrobitWebBluetoothConnectionImpl(options);
149+
36150
/**
37151
* A Bluetooth connection to a micro:bit device.
38152
*/
39-
export class MicrobitWebBluetoothConnection
153+
class MicrobitWebBluetoothConnectionImpl
40154
extends TypedEventTarget<DeviceConnectionEventMap & ServiceConnectionEventMap>
41-
implements DeviceConnection
155+
implements MicrobitWebBluetoothConnection
42156
{
43157
status: ConnectionStatus = ConnectionStatus.SUPPORT_NOT_KNOWN;
44158

lib/device.ts

+7-45
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
*
44
* SPDX-License-Identifier: MIT
55
*/
6-
import { TypedEventTarget } from "./events.js";
7-
import { UARTDataEvent } from "./uart.js";
6+
import { TypedEventTarget, ValueIsEvent } from "./events.js";
87

98
/**
109
* Specific identified error types.
@@ -134,30 +133,6 @@ export class ConnectionStatusEvent extends Event {
134133
}
135134
}
136135

137-
export class SerialDataEvent extends Event {
138-
constructor(public readonly data: string) {
139-
super("serialdata");
140-
}
141-
}
142-
143-
export class SerialResetEvent extends Event {
144-
constructor() {
145-
super("serialreset");
146-
}
147-
}
148-
149-
export class SerialErrorEvent extends Event {
150-
constructor(public readonly error: unknown) {
151-
super("serialerror");
152-
}
153-
}
154-
155-
export class FlashEvent extends Event {
156-
constructor() {
157-
super("flash");
158-
}
159-
}
160-
161136
export class BeforeRequestDevice extends Event {
162137
constructor() {
163138
super("beforerequestdevice");
@@ -178,18 +153,13 @@ export class BackgroundErrorEvent extends Event {
178153

179154
export class DeviceConnectionEventMap {
180155
"status": ConnectionStatusEvent;
181-
"serialdata": SerialDataEvent;
182-
"serialreset": Event;
183-
"serialerror": SerialErrorEvent;
184-
"uartdata": UARTDataEvent;
185-
"flash": Event;
156+
"backgrounderror": BackgroundErrorEvent;
186157
"beforerequestdevice": Event;
187158
"afterrequestdevice": Event;
188-
"backgrounderror": BackgroundErrorEvent;
189159
}
190160

191-
export interface DeviceConnection
192-
extends TypedEventTarget<DeviceConnectionEventMap> {
161+
export interface DeviceConnection<M extends ValueIsEvent<M>>
162+
extends TypedEventTarget<DeviceConnectionEventMap & M> {
193163
status: ConnectionStatus;
194164

195165
/**
@@ -212,18 +182,10 @@ export interface DeviceConnection
212182
/**
213183
* Get the board version.
214184
*
215-
* @returns the board version or null if there is no connection.
185+
* @returns the board version or undefined if there is no connection.
216186
*/
217187
getBoardVersion(): BoardVersion | undefined;
218188

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-
227189
/**
228190
* Disconnect from the device.
229191
*/
@@ -232,7 +194,7 @@ export interface DeviceConnection
232194
/**
233195
* Write serial data to the device.
234196
*
235-
* Does nothting if there is no connection.
197+
* Does nothing if there is no connection.
236198
*
237199
* @param data The data to write.
238200
* @returns A promise that resolves when the write is complete.
@@ -242,5 +204,5 @@ export interface DeviceConnection
242204
/**
243205
* Clear device to enable chooseDevice.
244206
*/
245-
clearDevice(): void;
207+
clearDevice(): Promise<void> | void;
246208
}

lib/events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export type TypedEventListenerOrEventListenerObject<M, T extends keyof M> =
4040
| TypedEventListener<M, T>
4141
| TypedEventListenerObject<M, T>;
4242

43-
type ValueIsEvent<T> = {
43+
export type ValueIsEvent<T> = {
4444
[key in keyof T]: Event;
4545
};
4646

lib/index.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AccelerometerData, AccelerometerDataEvent } from "./accelerometer.js";
22
import {
3+
createWebBluetoothConnection,
34
MicrobitWebBluetoothConnection,
45
MicrobitWebBluetoothConnectionOptions,
56
} from "./bluetooth.js";
@@ -18,24 +19,29 @@ import {
1819
DeviceErrorCode,
1920
FlashDataError,
2021
FlashDataSource,
21-
FlashEvent,
2222
FlashOptions,
23-
SerialDataEvent,
24-
SerialErrorEvent,
25-
SerialResetEvent,
2623
} from "./device.js";
2724
import { TypedEventTarget } from "./events.js";
2825
import { createUniversalHexFlashDataSource } from "./hex-flash-data-source.js";
2926
import { LedMatrix } from "./led.js";
3027
import { Logging, LoggingEvent } from "./logging.js";
3128
import { MagnetometerData, MagnetometerDataEvent } from "./magnetometer.js";
29+
import {
30+
FlashEvent,
31+
SerialConnectionEventMap,
32+
SerialDataEvent,
33+
SerialErrorEvent,
34+
SerialResetEvent,
35+
} from "./serial-events.js";
3236
import { ServiceConnectionEventMap } from "./service-events.js";
3337
import { UARTDataEvent } from "./uart.js";
3438
import {
39+
createRadioBridgeConnection,
3540
MicrobitRadioBridgeConnection,
3641
MicrobitRadioBridgeConnectionOptions,
3742
} from "./usb-radio-bridge.js";
3843
import {
44+
createWebUSBConnection,
3945
MicrobitWebUSBConnection,
4046
MicrobitWebUSBConnectionOptions,
4147
} from "./usb.js";
@@ -47,14 +53,15 @@ export {
4753
BoardId,
4854
ConnectionStatus,
4955
ConnectionStatusEvent,
56+
createRadioBridgeConnection,
5057
createUniversalHexFlashDataSource,
58+
createWebBluetoothConnection,
59+
createWebUSBConnection,
5160
DeviceConnectionEventMap,
5261
DeviceError,
5362
FlashDataError,
5463
FlashEvent,
55-
MicrobitRadioBridgeConnection,
56-
MicrobitWebBluetoothConnection,
57-
MicrobitWebUSBConnection,
64+
SerialConnectionEventMap,
5865
SerialDataEvent,
5966
SerialErrorEvent,
6067
SerialResetEvent,
@@ -79,7 +86,10 @@ export type {
7986
LoggingEvent,
8087
MagnetometerData,
8188
MagnetometerDataEvent,
89+
MicrobitRadioBridgeConnection,
8290
MicrobitRadioBridgeConnectionOptions,
91+
MicrobitWebBluetoothConnection,
8392
MicrobitWebBluetoothConnectionOptions,
93+
MicrobitWebUSBConnection,
8494
MicrobitWebUSBConnectionOptions,
8595
};

0 commit comments

Comments
 (0)