Skip to content

Commit 0a6e5e5

Browse files
Use TypeDocs to document API (#52)
Export a few missing types flagged by the docs generation. Some odd cases remain but not clear that exporting is the right plan.
1 parent f2714b6 commit 0a6e5e5

File tree

9 files changed

+350
-13
lines changed

9 files changed

+350
-13
lines changed

.github/workflows/build-docs.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: build-docs
2+
on:
3+
release:
4+
types: [created]
5+
push:
6+
branches:
7+
- "**"
8+
tags:
9+
- "**"
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Setup Pages
17+
uses: actions/configure-pages@v5
18+
- run: npm ci
19+
- name: Build docs
20+
run: npm run docs
21+
- name: Upload artifact
22+
uses: actions/upload-pages-artifact@v3
23+
with:
24+
path: ./docs/build
25+
26+
deploy:
27+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
28+
permissions:
29+
pages: write
30+
id-token: write
31+
environment:
32+
name: github-pages
33+
url: ${{ steps.deployment.outputs.page_url }}
34+
runs-on: ubuntu-latest
35+
needs: build
36+
steps:
37+
- name: Deploy to GitHub Pages
38+
id: deployment
39+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules
1111
dist
1212
dist-ssr
1313
*.local
14+
docs
1415

1516
# Editor directories and files
1617
.vscode/*

README.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,99 @@
1-
# JavaScript library for micro:bit connections in browsers via USB and Bluetooth
1+
# micro:bit connection library
22

3-
This project is a work in progress. We are extracting WebUSB and Web Bluetooth code from the [micro:bit Python Editor](https://github.com/microbit-foundation/python-editor-v3/) and other projects.
3+
[This documentation is best viewed on the documentation site rather than GitHub](https://microbit-foundation.github.io/microbit-connection/).
44

5-
It is intended to be used by other Micro:bit Educational Foundation projects that need to connect to a BBC micro:bit.
5+
This is a JavaScript library for micro:bit connections in browsers via USB and Bluetooth.
66

7-
The API is not stable and it's not yet recommended that third parties use this project unless they are happy to update usage as the API evolves.
7+
This project is a work in progress. We are extracting WebUSB and Web Bluetooth code from the [micro:bit Python Editor](https://github.com/microbit-foundation/python-editor-v3/) and other projects. The API is not stable and it's not yet recommended that third parties use this project unless they are happy to update usage as the API evolves.
8+
9+
[Demo page](https://microbit-connection.pages.dev/) for this library.
810

911
[Alpha releases are now on NPM](https://www.npmjs.com/package/@microbit/microbit-connection).
1012

13+
[micro:bit CreateAI](https://github.com/microbit-foundation/ml-trainer/) is already using this library for WebUSB and Web Bluetooth.
14+
1115
[This Python Editor PR](https://github.com/microbit-foundation/python-editor-v3/pull/1190) tracks updating the micro:bit Python Editor to use this library.
1216

13-
[micro:bit CreateAI](https://github.com/microbit-foundation/ml-trainer/) is already using this library for WebUSB and Web Bluetooth.
17+
## Usage
18+
19+
### Flash a micro:bit
20+
21+
Instantiate a WebUSB connection using {@link MicrobitWebUSBConnection} class and use it to connect to a micro:bit.
22+
23+
```ts
24+
import { MicrobitWebUSBConnection } from "@microbit/microbit-connection";
25+
26+
const usb = new MicrobitWebUSBConnection();
27+
const connectionStatus = await usb.connect();
28+
29+
console.log("Connection status: ", connectionStatus);
30+
```
31+
32+
{@link ConnectionStatus | Connection status} is `"CONNECTED"` if connection succeeds.
33+
34+
Flash a universal hex that supports both V1 and V2:
35+
36+
```ts
37+
import { createUniversalHexFlashDataSource } from "@microbit/microbit-connection";
38+
39+
await usb.flash(createUniversalHexFlashDataSource(universalHexString), {
40+
partial: true,
41+
progress: (percentage: number | undefined) => {
42+
console.log(percentage);
43+
},
44+
});
45+
```
46+
47+
This code will also work for non-universal hex files so is a good default for unknown hex files.
48+
49+
Alternatively, you can create and flash a hex for a specific micro:bit version by providing a function that takes a {@link BoardVersion} and returns a hex.
50+
This can reduce download size or help integrate with APIs that produce a hex for a particular device version.
51+
This example uses the [@microbit/microbit-fs library](https://microbit-foundation.github.io/microbit-fs/) which can return a hex based on board id.
52+
53+
```ts
54+
import { MicropythonFsHex, microbitBoardId } from "@microbit/microbit-fs";
55+
import { BoardId } from "@microbit/microbit-connection";
56+
57+
const micropythonFs = new MicropythonFsHex([
58+
{ hex: microPythonV1HexFile, boardId: microbitBoardId.V1 },
59+
{ hex: microPythonV2HexFile, boardId: microbitBoardId.V2 },
60+
]);
61+
// Add files to MicroPython file system here (omitted for simplicity)
62+
// Flash the device
63+
await usb.flash(
64+
async (boardVersion) => {
65+
const boardId = BoardId.forVersion(boardVersion).id;
66+
return micropythonFs.getIntelHex(boardId);
67+
},
68+
{
69+
partial: true,
70+
progress: (percentage: number | undefined) => {
71+
console.log(percentage);
72+
},
73+
},
74+
);
75+
```
76+
77+
For more examples of using other methods in the {@link MicrobitWebUSBConnection} 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/).
78+
79+
### Connect via Bluetooth
80+
81+
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.
82+
83+
Instantiate a Bluetooth connection using {@link MicrobitWebBluetoothConnection} class and use it to connect to a micro:bit.
84+
85+
```ts
86+
import { MicrobitWebBluetoothConnection } from "@microbit/microbit-connection";
87+
88+
const bluetooth = new MicrobitWebBluetoothConnection();
89+
const connectionStatus = await bluetooth.connect();
90+
91+
console.log("Connection status: ", connectionStatus);
92+
```
93+
94+
{@link ConnectionStatus | Connection status} is `"CONNECTED"` if connection succeeds.
95+
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/).
1497

1598
## License
1699

@@ -28,7 +111,7 @@ $ npx license-checker --direct --summary --production
28111

29112
Omit the flags as desired to obtain more detail.
30113

31-
## Code of Conduct
114+
## Code of conduct
32115

33116
Trust, partnership, simplicity and passion are our core values we live and
34117
breathe in our daily work life and within our projects. Our open-source

lib/bluetooth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class MicrobitWebBluetoothConnection
4949
private device: BluetoothDevice | undefined;
5050

5151
private logging: Logging;
52-
connection: BluetoothDeviceWrapper | undefined;
52+
private connection: BluetoothDeviceWrapper | undefined;
5353

5454
private availabilityListener = (e: Event) => {
5555
// TODO: is this called? is `value` correct?

lib/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { AccelerometerData, AccelerometerDataEvent } from "./accelerometer.js";
2-
import { MicrobitWebBluetoothConnection } from "./bluetooth.js";
2+
import {
3+
MicrobitWebBluetoothConnection,
4+
MicrobitWebBluetoothConnectionOptions,
5+
} from "./bluetooth.js";
36
import { BoardId } from "./board-id.js";
47
import { ButtonEvent, ButtonEventType, ButtonState } from "./buttons.js";
58
import {
69
AfterRequestDevice,
10+
BackgroundErrorEvent,
711
BeforeRequestDevice,
812
BoardVersion,
913
ConnectionStatus,
@@ -15,19 +19,30 @@ import {
1519
FlashDataError,
1620
FlashDataSource,
1721
FlashEvent,
22+
FlashOptions,
1823
SerialDataEvent,
1924
SerialErrorEvent,
2025
SerialResetEvent,
2126
} from "./device.js";
2227
import { TypedEventTarget } from "./events.js";
2328
import { createUniversalHexFlashDataSource } from "./hex-flash-data-source.js";
29+
import { LedMatrix } from "./led.js";
30+
import { Logging, LoggingEvent } from "./logging.js";
2431
import { MagnetometerData, MagnetometerDataEvent } from "./magnetometer.js";
2532
import { ServiceConnectionEventMap } from "./service-events.js";
26-
import { MicrobitRadioBridgeConnection } from "./usb-radio-bridge.js";
27-
import { MicrobitWebUSBConnection } from "./usb.js";
33+
import { UARTDataEvent } from "./uart.js";
34+
import {
35+
MicrobitRadioBridgeConnection,
36+
MicrobitRadioBridgeConnectionOptions,
37+
} from "./usb-radio-bridge.js";
38+
import {
39+
MicrobitWebUSBConnection,
40+
MicrobitWebUSBConnectionOptions,
41+
} from "./usb.js";
2842

2943
export {
3044
AfterRequestDevice,
45+
BackgroundErrorEvent,
3146
BeforeRequestDevice,
3247
BoardId,
3348
ConnectionStatus,
@@ -45,6 +60,7 @@ export {
4560
SerialResetEvent,
4661
ServiceConnectionEventMap,
4762
TypedEventTarget,
63+
UARTDataEvent,
4864
};
4965

5066
export type {
@@ -57,6 +73,13 @@ export type {
5773
DeviceConnection,
5874
DeviceErrorCode,
5975
FlashDataSource,
76+
FlashOptions,
77+
LedMatrix,
78+
Logging,
79+
LoggingEvent,
6080
MagnetometerData,
6181
MagnetometerDataEvent,
82+
MicrobitRadioBridgeConnectionOptions,
83+
MicrobitWebBluetoothConnectionOptions,
84+
MicrobitWebUSBConnectionOptions,
6285
};

lib/logging.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
*
44
* SPDX-License-Identifier: MIT
55
*/
6-
export interface Event {
6+
export interface LoggingEvent {
77
type: string;
88
message?: string;
99
value?: number;
1010
detail?: any;
1111
}
1212

1313
export interface Logging {
14-
event(event: Event): void;
14+
event(event: LoggingEvent): void;
1515
error(message: string, e: unknown): void;
1616
log(e: any): void;
1717
}
1818

1919
export class NullLogging implements Logging {
20-
event(_event: Event): void {
20+
event(_event: LoggingEvent): void {
2121
console.log(_event);
2222
}
2323
error(_m: string, _e: unknown): void {

0 commit comments

Comments
 (0)