@microbit/microbit-connection 0.0.0-tab.change.177 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +94 -5
  3. package/build/cjs/accelerometer-service.js +106 -0
  4. package/build/cjs/accelerometer-service.js.map +1 -0
  5. package/build/cjs/accelerometer.js +16 -0
  6. package/build/cjs/accelerometer.js.map +1 -0
  7. package/build/cjs/async-util.js +27 -0
  8. package/build/cjs/async-util.js.map +1 -0
  9. package/build/{bluetooth-device-wrapper.d.ts → cjs/bluetooth-device-wrapper.d.ts} +7 -1
  10. package/build/cjs/bluetooth-device-wrapper.js +467 -0
  11. package/build/cjs/bluetooth-device-wrapper.js.map +1 -0
  12. package/build/cjs/bluetooth-profile.js +86 -0
  13. package/build/cjs/bluetooth-profile.js.map +1 -0
  14. package/build/cjs/bluetooth.d.ts +108 -0
  15. package/build/cjs/bluetooth.js +272 -0
  16. package/build/cjs/bluetooth.js.map +1 -0
  17. package/build/cjs/board-id.js +81 -0
  18. package/build/cjs/board-id.js.map +1 -0
  19. package/build/cjs/board-serial-info.js +51 -0
  20. package/build/cjs/board-serial-info.js.map +1 -0
  21. package/build/cjs/button-service.js +80 -0
  22. package/build/cjs/button-service.js.map +1 -0
  23. package/build/cjs/buttons.js +22 -0
  24. package/build/cjs/buttons.js.map +1 -0
  25. package/build/cjs/constants.js +73 -0
  26. package/build/cjs/constants.js.map +1 -0
  27. package/build/{device.d.ts → cjs/device.d.ts} +6 -31
  28. package/build/cjs/device.js +133 -0
  29. package/build/cjs/device.js.map +1 -0
  30. package/build/{events.d.ts → cjs/events.d.ts} +1 -2
  31. package/build/cjs/events.js +109 -0
  32. package/build/cjs/events.js.map +1 -0
  33. package/build/cjs/hex-flash-data-source.js +26 -0
  34. package/build/cjs/hex-flash-data-source.js.map +1 -0
  35. package/build/cjs/index.d.ts +17 -0
  36. package/build/cjs/index.js +36 -0
  37. package/build/cjs/index.js.map +1 -0
  38. package/build/cjs/led-service.js +120 -0
  39. package/build/{led-service.js.map → cjs/led-service.js.map} +1 -1
  40. package/build/cjs/led.js +3 -0
  41. package/build/cjs/led.js.map +1 -0
  42. package/build/{logging.d.ts → cjs/logging.d.ts} +3 -3
  43. package/build/cjs/logging.js +16 -0
  44. package/build/cjs/logging.js.map +1 -0
  45. package/build/cjs/magnetometer-service.d.ts +22 -0
  46. package/build/cjs/magnetometer-service.js +129 -0
  47. package/build/cjs/magnetometer-service.js.map +1 -0
  48. package/build/cjs/magnetometer.d.ts +9 -0
  49. package/build/cjs/magnetometer.js +16 -0
  50. package/build/cjs/magnetometer.js.map +1 -0
  51. package/build/cjs/package.json +1 -0
  52. package/build/cjs/promise-queue.js +78 -0
  53. package/build/cjs/promise-queue.js.map +1 -0
  54. package/build/cjs/serial-events.d.ts +20 -0
  55. package/build/cjs/serial-events.js +69 -0
  56. package/build/cjs/serial-events.js.map +1 -0
  57. package/build/{service-events.d.ts → cjs/service-events.d.ts} +4 -0
  58. package/build/cjs/service-events.js +39 -0
  59. package/build/cjs/service-events.js.map +1 -0
  60. package/build/cjs/setupTests.js.map +1 -0
  61. package/build/cjs/uart-service.d.ts +13 -0
  62. package/build/cjs/uart-service.js +76 -0
  63. package/build/cjs/uart-service.js.map +1 -0
  64. package/build/cjs/uart.d.ts +4 -0
  65. package/build/cjs/uart.js +16 -0
  66. package/build/cjs/uart.js.map +1 -0
  67. package/build/{usb-device-wrapper.d.ts → cjs/usb-device-wrapper.d.ts} +1 -6
  68. package/build/cjs/usb-device-wrapper.js +412 -0
  69. package/build/cjs/usb-device-wrapper.js.map +1 -0
  70. package/build/cjs/usb-partial-flashing-utils.js +133 -0
  71. package/build/cjs/usb-partial-flashing-utils.js.map +1 -0
  72. package/build/{usb-partial-flashing.d.ts → cjs/usb-partial-flashing.d.ts} +3 -3
  73. package/build/cjs/usb-partial-flashing.js +341 -0
  74. package/build/cjs/usb-partial-flashing.js.map +1 -0
  75. package/build/cjs/usb-radio-bridge.d.ts +24 -0
  76. package/build/cjs/usb-radio-bridge.js +516 -0
  77. package/build/cjs/usb-radio-bridge.js.map +1 -0
  78. package/build/cjs/usb-serial-protocol.js +183 -0
  79. package/build/cjs/usb-serial-protocol.js.map +1 -0
  80. package/build/cjs/usb.d.ts +64 -0
  81. package/build/cjs/usb.js +647 -0
  82. package/build/cjs/usb.js.map +1 -0
  83. package/build/esm/accelerometer-service.d.ts +18 -0
  84. package/build/{accelerometer-service.js → esm/accelerometer-service.js} +1 -1
  85. package/build/esm/accelerometer-service.js.map +1 -0
  86. package/build/esm/accelerometer.d.ts +9 -0
  87. package/build/esm/accelerometer.js.map +1 -0
  88. package/build/esm/async-util.d.ts +13 -0
  89. package/build/esm/async-util.js.map +1 -0
  90. package/build/esm/bluetooth-device-wrapper.d.ts +63 -0
  91. package/build/{bluetooth-device-wrapper.js → esm/bluetooth-device-wrapper.js} +39 -10
  92. package/build/esm/bluetooth-device-wrapper.js.map +1 -0
  93. package/build/esm/bluetooth-profile.d.ts +139 -0
  94. package/build/esm/bluetooth-profile.js.map +1 -0
  95. package/build/esm/bluetooth.d.ts +108 -0
  96. package/build/{bluetooth.js → esm/bluetooth.js} +29 -4
  97. package/build/esm/bluetooth.js.map +1 -0
  98. package/build/esm/board-id.d.ts +36 -0
  99. package/build/esm/board-id.js.map +1 -0
  100. package/build/esm/board-serial-info.d.ts +14 -0
  101. package/build/esm/board-serial-info.js.map +1 -0
  102. package/build/esm/button-service.d.ts +13 -0
  103. package/build/esm/button-service.js.map +1 -0
  104. package/build/esm/buttons.d.ts +10 -0
  105. package/build/esm/buttons.js.map +1 -0
  106. package/build/esm/constants.d.ts +48 -0
  107. package/build/esm/constants.js.map +1 -0
  108. package/build/esm/device.d.ts +181 -0
  109. package/build/{device.js → esm/device.js} +1 -57
  110. package/build/esm/device.js.map +1 -0
  111. package/build/esm/events.d.ts +110 -0
  112. package/build/esm/events.js.map +1 -0
  113. package/build/esm/hex-flash-data-source.d.ts +7 -0
  114. package/build/esm/hex-flash-data-source.js.map +1 -0
  115. package/build/esm/index.d.ts +17 -0
  116. package/build/esm/index.js +12 -0
  117. package/build/esm/index.js.map +1 -0
  118. package/build/esm/led-service.d.ts +20 -0
  119. package/build/esm/led-service.js.map +1 -0
  120. package/build/esm/led.d.ts +6 -0
  121. package/build/esm/led.js.map +1 -0
  122. package/build/esm/logging.d.ts +21 -0
  123. package/build/esm/logging.js.map +1 -0
  124. package/build/esm/magnetometer-service.d.ts +22 -0
  125. package/build/esm/magnetometer-service.js +125 -0
  126. package/build/esm/magnetometer-service.js.map +1 -0
  127. package/build/esm/magnetometer.d.ts +9 -0
  128. package/build/esm/magnetometer.js +12 -0
  129. package/build/esm/magnetometer.js.map +1 -0
  130. package/build/esm/package.json +1 -0
  131. package/build/esm/promise-queue.d.ts +27 -0
  132. package/build/esm/promise-queue.js.map +1 -0
  133. package/build/esm/serial-events.d.ts +20 -0
  134. package/build/esm/serial-events.js +61 -0
  135. package/build/esm/serial-events.js.map +1 -0
  136. package/build/esm/service-events.d.ts +17 -0
  137. package/build/{service-events.js → esm/service-events.js} +12 -0
  138. package/build/esm/service-events.js.map +1 -0
  139. package/build/esm/setupTests.d.ts +6 -0
  140. package/build/{setupTests.js.map → esm/setupTests.js.map} +1 -1
  141. package/build/esm/uart-service.d.ts +13 -0
  142. package/build/esm/uart-service.js +72 -0
  143. package/build/esm/uart-service.js.map +1 -0
  144. package/build/esm/uart.d.ts +4 -0
  145. package/build/esm/uart.js +12 -0
  146. package/build/esm/uart.js.map +1 -0
  147. package/build/esm/usb-device-wrapper.d.ts +47 -0
  148. package/build/{usb-device-wrapper.js → esm/usb-device-wrapper.js} +10 -9
  149. package/build/esm/usb-device-wrapper.js.map +1 -0
  150. package/build/esm/usb-partial-flashing-utils.d.ts +17 -0
  151. package/build/esm/usb-partial-flashing-utils.js.map +1 -0
  152. package/build/esm/usb-partial-flashing.d.ts +69 -0
  153. package/build/{usb-partial-flashing.js → esm/usb-partial-flashing.js} +8 -10
  154. package/build/esm/usb-partial-flashing.js.map +1 -0
  155. package/build/esm/usb-radio-bridge.d.ts +24 -0
  156. package/build/{usb-radio-bridge.js → esm/usb-radio-bridge.js} +11 -4
  157. package/build/esm/usb-radio-bridge.js.map +1 -0
  158. package/build/esm/usb-serial-protocol.d.ts +66 -0
  159. package/build/esm/usb-serial-protocol.js.map +1 -0
  160. package/build/esm/usb.d.ts +64 -0
  161. package/build/{usb.js → esm/usb.js} +228 -35
  162. package/build/esm/usb.js.map +1 -0
  163. package/package.json +21 -8
  164. package/typedoc.json +14 -0
  165. package/build/accelerometer-service.js.map +0 -1
  166. package/build/accelerometer.js.map +0 -1
  167. package/build/async-util.js.map +0 -1
  168. package/build/bluetooth-device-wrapper.js.map +0 -1
  169. package/build/bluetooth-profile.js.map +0 -1
  170. package/build/bluetooth.d.ts +0 -54
  171. package/build/bluetooth.js.map +0 -1
  172. package/build/board-id.js.map +0 -1
  173. package/build/board-serial-info.js.map +0 -1
  174. package/build/button-service.js.map +0 -1
  175. package/build/buttons.js.map +0 -1
  176. package/build/constants.js.map +0 -1
  177. package/build/device.js.map +0 -1
  178. package/build/events.js.map +0 -1
  179. package/build/hex-flash-data-source.js.map +0 -1
  180. package/build/index.d.ts +0 -10
  181. package/build/index.js +0 -8
  182. package/build/index.js.map +0 -1
  183. package/build/led.js.map +0 -1
  184. package/build/logging.js.map +0 -1
  185. package/build/promise-queue.js.map +0 -1
  186. package/build/service-events.js.map +0 -1
  187. package/build/usb-device-wrapper.js.map +0 -1
  188. package/build/usb-partial-flashing-utils.js.map +0 -1
  189. package/build/usb-partial-flashing.js.map +0 -1
  190. package/build/usb-radio-bridge.d.ts +0 -41
  191. package/build/usb-radio-bridge.js.map +0 -1
  192. package/build/usb-serial-protocol.js.map +0 -1
  193. package/build/usb.d.ts +0 -54
  194. package/build/usb.js.map +0 -1
  195. package/vite.config.ts +0 -34
  196. /package/build/{accelerometer-service.d.ts → cjs/accelerometer-service.d.ts} +0 -0
  197. /package/build/{accelerometer.d.ts → cjs/accelerometer.d.ts} +0 -0
  198. /package/build/{async-util.d.ts → cjs/async-util.d.ts} +0 -0
  199. /package/build/{bluetooth-profile.d.ts → cjs/bluetooth-profile.d.ts} +0 -0
  200. /package/build/{board-id.d.ts → cjs/board-id.d.ts} +0 -0
  201. /package/build/{board-serial-info.d.ts → cjs/board-serial-info.d.ts} +0 -0
  202. /package/build/{button-service.d.ts → cjs/button-service.d.ts} +0 -0
  203. /package/build/{buttons.d.ts → cjs/buttons.d.ts} +0 -0
  204. /package/build/{constants.d.ts → cjs/constants.d.ts} +0 -0
  205. /package/build/{hex-flash-data-source.d.ts → cjs/hex-flash-data-source.d.ts} +0 -0
  206. /package/build/{led-service.d.ts → cjs/led-service.d.ts} +0 -0
  207. /package/build/{led.d.ts → cjs/led.d.ts} +0 -0
  208. /package/build/{promise-queue.d.ts → cjs/promise-queue.d.ts} +0 -0
  209. /package/build/{setupTests.d.ts → cjs/setupTests.d.ts} +0 -0
  210. /package/build/{usb-partial-flashing-utils.d.ts → cjs/usb-partial-flashing-utils.d.ts} +0 -0
  211. /package/build/{usb-serial-protocol.d.ts → cjs/usb-serial-protocol.d.ts} +0 -0
  212. /package/build/{accelerometer.js → esm/accelerometer.js} +0 -0
  213. /package/build/{async-util.js → esm/async-util.js} +0 -0
  214. /package/build/{bluetooth-profile.js → esm/bluetooth-profile.js} +0 -0
  215. /package/build/{board-id.js → esm/board-id.js} +0 -0
  216. /package/build/{board-serial-info.js → esm/board-serial-info.js} +0 -0
  217. /package/build/{button-service.js → esm/button-service.js} +0 -0
  218. /package/build/{buttons.js → esm/buttons.js} +0 -0
  219. /package/build/{constants.js → esm/constants.js} +0 -0
  220. /package/build/{events.js → esm/events.js} +0 -0
  221. /package/build/{hex-flash-data-source.js → esm/hex-flash-data-source.js} +0 -0
  222. /package/build/{led-service.js → esm/led-service.js} +0 -0
  223. /package/build/{led.js → esm/led.js} +0 -0
  224. /package/build/{logging.js → esm/logging.js} +0 -0
  225. /package/build/{promise-queue.js → esm/promise-queue.js} +0 -0
  226. /package/build/{usb-partial-flashing-utils.js → esm/usb-partial-flashing-utils.js} +0 -0
  227. /package/build/{usb-serial-protocol.js → esm/usb-serial-protocol.js} +0 -0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2025 Micro:bit Educational Foundation and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,10 +1,99 @@
1
- # JavaScript library for micro:bit connections in browsers via USB and Bluetooth
1
+ # micro:bit connection library
2
2
 
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
+ <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>
4
4
 
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.
6
6
 
7
- The API is not stable and it's not yet recommended that third parties use this project.
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.
10
+
11
+ [Alpha releases are now on NPM](https://www.npmjs.com/package/@microbit/microbit-connection).
12
+
13
+ [micro:bit CreateAI](https://github.com/microbit-foundation/ml-trainer/) is already using this library for WebUSB and Web Bluetooth.
14
+
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.
16
+
17
+ ## Usage
18
+
19
+ ### Flash a micro:bit
20
+
21
+ Instantiate a WebUSB connection using {@link createWebUSBConnection} class and use it to connect to a micro:bit.
22
+
23
+ ```ts
24
+ import { createWebUSBConnection } from "@microbit/microbit-connection";
25
+
26
+ const usb = createWebUSBConnection();
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 createWebBluetoothConnection} class and use it to connect to a micro:bit.
84
+
85
+ ```ts
86
+ import { createWebBluetoothConnection } from "@microbit/microbit-connection";
87
+
88
+ const bluetooth = createWebBluetoothConnection();
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 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/).
8
97
 
9
98
  ## License
10
99
 
@@ -22,7 +111,7 @@ $ npx license-checker --direct --summary --production
22
111
 
23
112
  Omit the flags as desired to obtain more detail.
24
113
 
25
- ## Code of Conduct
114
+ ## Code of conduct
26
115
 
27
116
  Trust, partnership, simplicity and passion are our core values we live and
28
117
  breathe in our daily work life and within our projects. Our open-source
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccelerometerService = void 0;
4
+ const accelerometer_js_1 = require("./accelerometer.js");
5
+ const bluetooth_profile_js_1 = require("./bluetooth-profile.js");
6
+ const device_js_1 = require("./device.js");
7
+ class AccelerometerService {
8
+ constructor(accelerometerDataCharacteristic, accelerometerPeriodCharacteristic, dispatchTypedEvent, queueGattOperation) {
9
+ Object.defineProperty(this, "accelerometerDataCharacteristic", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: accelerometerDataCharacteristic
14
+ });
15
+ Object.defineProperty(this, "accelerometerPeriodCharacteristic", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: accelerometerPeriodCharacteristic
20
+ });
21
+ Object.defineProperty(this, "dispatchTypedEvent", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: dispatchTypedEvent
26
+ });
27
+ Object.defineProperty(this, "queueGattOperation", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: queueGattOperation
32
+ });
33
+ this.accelerometerDataCharacteristic.addEventListener("characteristicvaluechanged", (event) => {
34
+ const target = event.target;
35
+ const data = this.dataViewToData(target.value);
36
+ this.dispatchTypedEvent("accelerometerdatachanged", new accelerometer_js_1.AccelerometerDataEvent(data));
37
+ });
38
+ }
39
+ static async createService(gattServer, dispatcher, queueGattOperation, listenerInit) {
40
+ let accelerometerService;
41
+ try {
42
+ accelerometerService = await gattServer.getPrimaryService(bluetooth_profile_js_1.profile.accelerometer.id);
43
+ }
44
+ catch (err) {
45
+ if (listenerInit) {
46
+ dispatcher("backgrounderror", new device_js_1.BackgroundErrorEvent(err));
47
+ return;
48
+ }
49
+ else {
50
+ throw new device_js_1.DeviceError({
51
+ code: "service-missing",
52
+ message: err,
53
+ });
54
+ }
55
+ }
56
+ const accelerometerDataCharacteristic = await accelerometerService.getCharacteristic(bluetooth_profile_js_1.profile.accelerometer.characteristics.data.id);
57
+ const accelerometerPeriodCharacteristic = await accelerometerService.getCharacteristic(bluetooth_profile_js_1.profile.accelerometer.characteristics.period.id);
58
+ return new AccelerometerService(accelerometerDataCharacteristic, accelerometerPeriodCharacteristic, dispatcher, queueGattOperation);
59
+ }
60
+ dataViewToData(dataView) {
61
+ return {
62
+ x: dataView.getInt16(0, true),
63
+ y: dataView.getInt16(2, true),
64
+ z: dataView.getInt16(4, true),
65
+ };
66
+ }
67
+ async getData() {
68
+ const dataView = await this.queueGattOperation(() => this.accelerometerDataCharacteristic.readValue());
69
+ return this.dataViewToData(dataView);
70
+ }
71
+ async getPeriod() {
72
+ const dataView = await this.queueGattOperation(() => this.accelerometerPeriodCharacteristic.readValue());
73
+ return dataView.getUint16(0, true);
74
+ }
75
+ async setPeriod(value) {
76
+ if (value === 0) {
77
+ // Writing 0 causes the device to crash.
78
+ return;
79
+ }
80
+ // Allowed values: 2, 5, 10, 20, 40, 100, 1000
81
+ // Values passed are rounded up to the allowed values on device.
82
+ // Documentation for allowed values looks wrong.
83
+ // https://lancaster-university.github.io/microbit-docs/ble/profile/#about-the-accelerometer-service
84
+ const dataView = new DataView(new ArrayBuffer(2));
85
+ dataView.setUint16(0, value, true);
86
+ return this.queueGattOperation(() => this.accelerometerPeriodCharacteristic.writeValue(dataView));
87
+ }
88
+ async startNotifications(type) {
89
+ await this.characteristicForEvent(type)?.startNotifications();
90
+ }
91
+ async stopNotifications(type) {
92
+ await this.characteristicForEvent(type)?.stopNotifications();
93
+ }
94
+ characteristicForEvent(type) {
95
+ switch (type) {
96
+ case "accelerometerdatachanged": {
97
+ return this.accelerometerDataCharacteristic;
98
+ }
99
+ default: {
100
+ return undefined;
101
+ }
102
+ }
103
+ }
104
+ }
105
+ exports.AccelerometerService = AccelerometerService;
106
+ //# sourceMappingURL=accelerometer-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accelerometer-service.js","sourceRoot":"","sources":["../../lib/accelerometer-service.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccelerometerDataEvent = void 0;
4
+ class AccelerometerDataEvent extends Event {
5
+ constructor(data) {
6
+ super("accelerometerdatachanged");
7
+ Object.defineProperty(this, "data", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: data
12
+ });
13
+ }
14
+ }
15
+ exports.AccelerometerDataEvent = AccelerometerDataEvent;
16
+ //# sourceMappingURL=accelerometer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accelerometer.js","sourceRoot":"","sources":["../../lib/accelerometer.ts"],"names":[],"mappings":";;;AAMA,MAAa,sBAAuB,SAAQ,KAAK;IAC/C,YAA4B,IAAuB;QACjD,KAAK,CAAC,0BAA0B,CAAC,CAAC;QADxB;;;;mBAAgB,IAAI;WAAmB;IAEnD,CAAC;CACF;AAJD,wDAIC"}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimeoutError = void 0;
4
+ exports.withTimeout = withTimeout;
5
+ /**
6
+ * (c) 2021, Micro:bit Educational Foundation and contributors
7
+ *
8
+ * SPDX-License-Identifier: MIT
9
+ */
10
+ class TimeoutError extends Error {
11
+ }
12
+ exports.TimeoutError = TimeoutError;
13
+ /**
14
+ * Utility to time out an action after a delay.
15
+ *
16
+ * The action cannot be cancelled; it may still proceed after the timeout.
17
+ */
18
+ async function withTimeout(actionPromise, timeout) {
19
+ const timeoutPromise = new Promise((_, reject) => {
20
+ setTimeout(() => {
21
+ reject(new TimeoutError());
22
+ }, timeout);
23
+ });
24
+ // timeoutPromise never resolves so result must be from action
25
+ return Promise.race([actionPromise, timeoutPromise]);
26
+ }
27
+ //# sourceMappingURL=async-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-util.js","sourceRoot":"","sources":["../../lib/async-util.ts"],"names":[],"mappings":";;;AAYA,kCAWC;AAvBD;;;;GAIG;AACH,MAAa,YAAa,SAAQ,KAAK;CAAG;AAA1C,oCAA0C;AAE1C;;;;GAIG;AACI,KAAK,UAAU,WAAW,CAC/B,aAAyB,EACzB,OAAe;IAEf,MAAM,cAAc,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE;QAC/C,UAAU,CAAC,GAAG,EAAE;YACd,MAAM,CAAC,IAAI,YAAY,EAAE,CAAC,CAAC;QAC7B,CAAC,EAAE,OAAO,CAAC,CAAC;IACd,CAAC,CAAC,CAAC;IACH,8DAA8D;IAC9D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,cAAc,CAAC,CAAe,CAAC;AACrE,CAAC"}
@@ -7,7 +7,9 @@ import { AccelerometerService } from "./accelerometer-service.js";
7
7
  import { BoardVersion } from "./device.js";
8
8
  import { LedService } from "./led-service.js";
9
9
  import { Logging } from "./logging.js";
10
+ import { MagnetometerService } from "./magnetometer-service.js";
10
11
  import { ServiceConnectionEventMap, TypedServiceEvent, TypedServiceEventDispatcher } from "./service-events.js";
12
+ import { UARTService } from "./uart-service.js";
11
13
  export interface Service {
12
14
  startNotifications(type: TypedServiceEvent): Promise<void>;
13
15
  stopNotifications(type: TypedServiceEvent): Promise<void>;
@@ -29,10 +31,12 @@ export declare class BluetoothDeviceWrapper {
29
31
  private disconnectPromise;
30
32
  private connecting;
31
33
  private isReconnect;
32
- private reconnectReadyPromise;
34
+ private connectReadyPromise;
33
35
  private accelerometer;
34
36
  private buttons;
35
37
  private led;
38
+ private magnetometer;
39
+ private uart;
36
40
  private serviceInfo;
37
41
  boardVersion: BoardVersion | undefined;
38
42
  private disconnectedRejectionErrorFactory;
@@ -49,6 +53,8 @@ export declare class BluetoothDeviceWrapper {
49
53
  private createIfNeeded;
50
54
  getAccelerometerService(): Promise<AccelerometerService | undefined>;
51
55
  getLedService(): Promise<LedService | undefined>;
56
+ getMagnetometerService(): Promise<MagnetometerService | undefined>;
57
+ getUARTService(): Promise<UARTService | undefined>;
52
58
  startNotifications(type: TypedServiceEvent): Promise<void>;
53
59
  stopNotifications(type: TypedServiceEvent): Promise<void>;
54
60
  private disposeServices;