|
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. |
2 | 4 |
|
3 | 5 | 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. |
4 | 6 |
|
5 | 7 | It is intended to be used by other Micro:bit Educational Foundation projects that need to connect to a BBC micro:bit. |
6 | 8 |
|
7 | 9 | 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 | 10 |
|
| 11 | +[Demo site](https://microbit-connection.pages.dev/) for this library. |
| 12 | + |
9 | 13 | [Alpha releases are now on NPM](https://www.npmjs.com/package/@microbit/microbit-connection). |
10 | 14 |
|
11 | 15 | [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. |
12 | 16 |
|
13 | 17 | [micro:bit CreateAI](https://github.com/microbit-foundation/ml-trainer/) is already using this library for WebUSB and Web Bluetooth. |
14 | 18 |
|
| 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 | + |
15 | 88 | ## License |
16 | 89 |
|
17 | 90 | This software is under the MIT open source license. |
|
0 commit comments