Skip to content

Commit 3ab9a52

Browse files
Generate docs using TypeDocs, document usage in README.md
1 parent f2714b6 commit 3ab9a52

File tree

5 files changed

+267
-2
lines changed

5 files changed

+267
-2
lines changed

.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: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,90 @@
1-
# JavaScript library for micro:bit connections in browsers via USB and Bluetooth
1+
# micro:bit connection library
2+
3+
This is a JavaScript library for micro:bit connections in browsers via USB and Bluetooth.
24

35
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.
46

57
It is intended to be used by other Micro:bit Educational Foundation projects that need to connect to a BBC micro:bit.
68

79
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.
810

11+
[Demo site](https://microbit-connection.pages.dev/) for this library.
12+
913
[Alpha releases are now on NPM](https://www.npmjs.com/package/@microbit/microbit-connection).
1014

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

1317
[micro:bit CreateAI](https://github.com/microbit-foundation/ml-trainer/) is already using this library for WebUSB and Web Bluetooth.
1418

19+
## Usage
20+
21+
### Flash a micro:bit
22+
23+
Instantiate a WebUSB connection using {@link MicrobitWebUSBConnection} class and use it to connect to a micro:bit.
24+
25+
```ts
26+
import { MicrobitWebUSBConnection } from "@microbit/microbit-connection";
27+
28+
const usb = new MicrobitWebUSBConnection();
29+
const connectionStatus = await usb.connect();
30+
31+
console.log("Connection status: ", connectionStatus);
32+
```
33+
34+
{@link ConnectionStatus | Connection status} is `"CONNECTED"` if connection succeeds.
35+
36+
Create a Universal Hex that supports both V1 and V2 micro:bits using {@link createUniversalHexFlashDataSource} and flash the micro:bit.
37+
38+
```ts
39+
import { createUniversalHexFlashDataSource } from "@microbit/microbit-connection";
40+
41+
const universalHex = createUniversalHexFlashDataSource(text);
42+
await usb.flash(universalHex, {
43+
partial: true,
44+
progress: (percentage: number | undefined) => {
45+
console.log(percentage);
46+
},
47+
});
48+
```
49+
50+
Alternatively, you can create and flash a hex that supports a specific micro:bit version (V1 or V2) using the [@microbit/microbit-fs library](https://microbit-foundation.github.io/microbit-fs/). Below is an example of creating a hex for V1 and using it to flash the micro:bit.
51+
52+
```ts
53+
import { MicropythonFsHex } from "@microbit/microbit-fs";
54+
import { BoardId } from "@microbit/microbit-connection";
55+
56+
const boardId = BoardId.forVersion("V1").id;
57+
const micropythonFs = new MicropythonFsHex([{ hex: intelHexString, boardId }]);
58+
const hex = micropythonFs.getIntelHex(boardId);
59+
await usb.flash(async () => hex, {
60+
partial: true,
61+
progress: (percentage: number | undefined) => {
62+
console.log(percentage);
63+
},
64+
});
65+
```
66+
67+
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/).
68+
69+
### Connect via Bluetooth
70+
71+
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.
72+
73+
Instantiate a Bluetooth connection using {@link MicrobitWebBluetoothConnection} class and use it to connect to a micro:bit.
74+
75+
```ts
76+
import { MicrobitWebBluetoothConnection } from "@microbit/microbit-connection";
77+
78+
const bluetooth = new MicrobitWebBluetoothConnection();
79+
const connectionStatus = await bluetooth.connect();
80+
81+
console.log("Connection status: ", connectionStatus);
82+
```
83+
84+
{@link ConnectionStatus | Connection status} is `"CONNECTED"` if connection succeeds.
85+
86+
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/).
87+
1588
## License
1689

1790
This software is under the MIT open source license.

package-lock.json

Lines changed: 176 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
"build": "npm run build:lib && npm run build:demo",
2626
"ci": "npm run build:lib && npm run test && npx prettier --check lib src",
2727
"test": "vitest",
28-
"preview": "vite preview"
28+
"preview": "vite preview",
29+
"docs": "typedoc"
2930
},
3031
"devDependencies": {
3132
"@types/node": "^20.14.10",
3233
"jsdom": "^24.1.0",
3334
"prettier": "3.3.2",
35+
"typedoc": "^0.27.6",
3436
"typescript": "^5.2.2",
3537
"vite": "^5.3.1",
3638
"vitest": "^2.0.0"

0 commit comments

Comments
 (0)