@microbit/microbit-connection 0.0.0-alpha.1
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.
- package/README.md +36 -0
- package/build/accelerometer-service.d.ts +17 -0
- package/build/accelerometer-service.js +84 -0
- package/build/accelerometer-service.js.map +1 -0
- package/build/accelerometer.d.ts +9 -0
- package/build/accelerometer.js +12 -0
- package/build/accelerometer.js.map +1 -0
- package/build/async-util.d.ts +13 -0
- package/build/async-util.js +22 -0
- package/build/async-util.js.map +1 -0
- package/build/bluetooth-device-wrapper.d.ts +39 -0
- package/build/bluetooth-device-wrapper.js +316 -0
- package/build/bluetooth-device-wrapper.js.map +1 -0
- package/build/bluetooth-profile.d.ts +139 -0
- package/build/bluetooth-profile.js +83 -0
- package/build/bluetooth-profile.js.map +1 -0
- package/build/bluetooth.d.ts +50 -0
- package/build/bluetooth.js +247 -0
- package/build/bluetooth.js.map +1 -0
- package/build/board-id.d.ts +35 -0
- package/build/board-id.js +69 -0
- package/build/board-id.js.map +1 -0
- package/build/board-serial-info.d.ts +14 -0
- package/build/board-serial-info.js +47 -0
- package/build/board-serial-info.js.map +1 -0
- package/build/constants.d.ts +47 -0
- package/build/constants.js +69 -0
- package/build/constants.js.map +1 -0
- package/build/device.d.ts +202 -0
- package/build/device.js +153 -0
- package/build/device.js.map +1 -0
- package/build/events.d.ts +101 -0
- package/build/events.js +19 -0
- package/build/events.js.map +1 -0
- package/build/hex-flash-data-source.d.ts +9 -0
- package/build/hex-flash-data-source.js +50 -0
- package/build/hex-flash-data-source.js.map +1 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +7 -0
- package/build/index.js.map +1 -0
- package/build/logging.d.ts +21 -0
- package/build/logging.js +10 -0
- package/build/logging.js.map +1 -0
- package/build/service-events.d.ts +9 -0
- package/build/service-events.js +11 -0
- package/build/service-events.js.map +1 -0
- package/build/setupTests.d.ts +6 -0
- package/build/setupTests.js.map +1 -0
- package/build/usb-device-wrapper.d.ts +46 -0
- package/build/usb-device-wrapper.js +347 -0
- package/build/usb-device-wrapper.js.map +1 -0
- package/build/usb-partial-flashing-utils.d.ts +17 -0
- package/build/usb-partial-flashing-utils.js +122 -0
- package/build/usb-partial-flashing-utils.js.map +1 -0
- package/build/usb-partial-flashing.d.ts +63 -0
- package/build/usb-partial-flashing.js +270 -0
- package/build/usb-partial-flashing.js.map +1 -0
- package/build/usb-radio-bridge.d.ts +28 -0
- package/build/usb-radio-bridge.js +318 -0
- package/build/usb-radio-bridge.js.map +1 -0
- package/build/usb-serial-protocol.d.ts +66 -0
- package/build/usb-serial-protocol.js +171 -0
- package/build/usb-serial-protocol.js.map +1 -0
- package/build/usb.d.ts +61 -0
- package/build/usb.js +451 -0
- package/build/usb.js.map +1 -0
- package/package.json +32 -0
- package/vite.config.ts +32 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
export declare const profile: {
|
|
2
|
+
uart: {
|
|
3
|
+
id: string;
|
|
4
|
+
characteristics: {
|
|
5
|
+
tx: {
|
|
6
|
+
id: string;
|
|
7
|
+
};
|
|
8
|
+
rx: {
|
|
9
|
+
id: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
accelerometer: {
|
|
14
|
+
id: string;
|
|
15
|
+
characteristics: {
|
|
16
|
+
data: {
|
|
17
|
+
id: string;
|
|
18
|
+
};
|
|
19
|
+
period: {
|
|
20
|
+
id: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
deviceInformation: {
|
|
25
|
+
id: string;
|
|
26
|
+
characteristics: {
|
|
27
|
+
modelNumber: {
|
|
28
|
+
id: string;
|
|
29
|
+
};
|
|
30
|
+
serialNumber: {
|
|
31
|
+
id: string;
|
|
32
|
+
};
|
|
33
|
+
firmwareRevision: {
|
|
34
|
+
id: string;
|
|
35
|
+
};
|
|
36
|
+
hardwareRevision: {
|
|
37
|
+
id: string;
|
|
38
|
+
};
|
|
39
|
+
manufacturer: {
|
|
40
|
+
id: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
dfuControl: {
|
|
45
|
+
id: string;
|
|
46
|
+
characteristics: {
|
|
47
|
+
control: {
|
|
48
|
+
id: string;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
led: {
|
|
53
|
+
id: string;
|
|
54
|
+
characteristics: {
|
|
55
|
+
matrixState: {
|
|
56
|
+
id: string;
|
|
57
|
+
};
|
|
58
|
+
text: {
|
|
59
|
+
id: string;
|
|
60
|
+
};
|
|
61
|
+
scrollingDelay: {
|
|
62
|
+
id: string;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
ioPin: {
|
|
67
|
+
id: string;
|
|
68
|
+
characteristics: {
|
|
69
|
+
pinData: {
|
|
70
|
+
id: string;
|
|
71
|
+
};
|
|
72
|
+
pinAdConfiguration: {
|
|
73
|
+
id: string;
|
|
74
|
+
};
|
|
75
|
+
pinIoConfiguration: {
|
|
76
|
+
id: string;
|
|
77
|
+
};
|
|
78
|
+
pwmControl: {
|
|
79
|
+
id: string;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
button: {
|
|
84
|
+
id: string;
|
|
85
|
+
characteristics: {
|
|
86
|
+
a: {
|
|
87
|
+
id: string;
|
|
88
|
+
};
|
|
89
|
+
b: {
|
|
90
|
+
id: string;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
event: {
|
|
95
|
+
id: string;
|
|
96
|
+
characteristics: {
|
|
97
|
+
microBitRequirements: {
|
|
98
|
+
id: string;
|
|
99
|
+
};
|
|
100
|
+
microBitEvent: {
|
|
101
|
+
id: string;
|
|
102
|
+
};
|
|
103
|
+
clientRequirements: {
|
|
104
|
+
id: string;
|
|
105
|
+
};
|
|
106
|
+
clientEvent: {
|
|
107
|
+
id: string;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
magnetometer: {
|
|
112
|
+
id: string;
|
|
113
|
+
characteristics: {
|
|
114
|
+
data: {
|
|
115
|
+
id: string;
|
|
116
|
+
};
|
|
117
|
+
period: {
|
|
118
|
+
id: string;
|
|
119
|
+
};
|
|
120
|
+
bearing: {
|
|
121
|
+
id: string;
|
|
122
|
+
};
|
|
123
|
+
calibration: {
|
|
124
|
+
id: string;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
temperature: {
|
|
129
|
+
id: string;
|
|
130
|
+
characteristics: {
|
|
131
|
+
data: {
|
|
132
|
+
id: string;
|
|
133
|
+
};
|
|
134
|
+
period: {
|
|
135
|
+
id: string;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Very incomplete BT profile
|
|
2
|
+
export const profile = {
|
|
3
|
+
uart: {
|
|
4
|
+
id: "6e400001-b5a3-f393-e0a9-e50e24dcca9e",
|
|
5
|
+
characteristics: {
|
|
6
|
+
tx: { id: "6e400002-b5a3-f393-e0a9-e50e24dcca9e" },
|
|
7
|
+
rx: { id: "6e400003-b5a3-f393-e0a9-e50e24dcca9e" },
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
accelerometer: {
|
|
11
|
+
id: "e95d0753-251d-470a-a062-fa1922dfa9a8",
|
|
12
|
+
characteristics: {
|
|
13
|
+
data: { id: "e95dca4b-251d-470a-a062-fa1922dfa9a8" },
|
|
14
|
+
period: { id: "e95dfb24-251d-470a-a062-fa1922dfa9a8" },
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
deviceInformation: {
|
|
18
|
+
id: "0000180a-0000-1000-8000-00805f9b34fb",
|
|
19
|
+
characteristics: {
|
|
20
|
+
modelNumber: { id: "00002a24-0000-1000-8000-00805f9b34fb" },
|
|
21
|
+
serialNumber: { id: "00002a25-0000-1000-8000-00805f9b34fb" },
|
|
22
|
+
firmwareRevision: { id: "00002a26-0000-1000-8000-00805f9b34fb" },
|
|
23
|
+
hardwareRevision: { id: "00002a27-0000-1000-8000-00805f9b34fb" },
|
|
24
|
+
manufacturer: { id: "00002a29-0000-1000-8000-00805f9b34fb" },
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
dfuControl: {
|
|
28
|
+
id: "e95d93b0-251d-470a-a062-fa1922dfa9a8",
|
|
29
|
+
characteristics: {
|
|
30
|
+
control: { id: "e95d93b1-251d-470a-a062-fa1922dfa9a8" },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
led: {
|
|
34
|
+
id: "e95dd91d-251d-470a-a062-fa1922dfa9a8",
|
|
35
|
+
characteristics: {
|
|
36
|
+
matrixState: { id: "e95d7b77-251d-470a-a062-fa1922dfa9a8" },
|
|
37
|
+
text: { id: "e95d93ee-251d-470a-a062-fa1922dfa9a8" },
|
|
38
|
+
scrollingDelay: { id: "e95d0d2d-251d-470a-a062-fa1922dfa9a8" },
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
ioPin: {
|
|
42
|
+
id: "e95d127b-251d-470a-a062-fa1922dfa9a8",
|
|
43
|
+
characteristics: {
|
|
44
|
+
pinData: { id: "e95d8d00-251d-470a-a062-fa1922dfa9a8" },
|
|
45
|
+
pinAdConfiguration: { id: "e95d5899-251d-470a-a062-fa1922dfa9a8" },
|
|
46
|
+
pinIoConfiguration: { id: "e95db9fe-251d-470a-a062-fa1922dfa9a8" },
|
|
47
|
+
pwmControl: { id: "e95dd822-251d-470a-a062-fa1922dfa9a8" },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
button: {
|
|
51
|
+
id: "e95d9882-251d-470a-a062-fa1922dfa9a8",
|
|
52
|
+
characteristics: {
|
|
53
|
+
a: { id: "e95dda90-251d-470a-a062-fa1922dfa9a8" },
|
|
54
|
+
b: { id: "e95dda91-251d-470a-a062-fa1922dfa9a8" },
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
event: {
|
|
58
|
+
id: "e95d93af-251d-470a-a062-fa1922dfa9a8",
|
|
59
|
+
characteristics: {
|
|
60
|
+
microBitRequirements: { id: "e95db84c-251d-470a-a062-fa1922dfa9a8" },
|
|
61
|
+
microBitEvent: { id: "e95d9775-251d-470a-a062-fa1922dfa9a8" },
|
|
62
|
+
clientRequirements: { id: "e95d23c4-251d-470a-a062-fa1922dfa9a8" },
|
|
63
|
+
clientEvent: { id: "e95d5404-251d-470a-a062-fa1922dfa9a8" },
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
magnetometer: {
|
|
67
|
+
id: "e95df2d8-251d-470a-a062-fa1922dfa9a8",
|
|
68
|
+
characteristics: {
|
|
69
|
+
data: { id: "e95dfb11-251d-470a-a062-fa1922dfa9a8" },
|
|
70
|
+
period: { id: "e95d386c-251d-470a-a062-fa1922dfa9a8" },
|
|
71
|
+
bearing: { id: "e95d9715-251d-470a-a062-fa1922dfa9a8" },
|
|
72
|
+
calibration: { id: "e95db358-251d-470a-a062-fa1922dfa9a8" },
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
temperature: {
|
|
76
|
+
id: "e95d6100-251d-470a-a062-fa1922dfa9a8",
|
|
77
|
+
characteristics: {
|
|
78
|
+
data: { id: "e95d9250-251d-470a-a062-fa1922dfa9a8" },
|
|
79
|
+
period: { id: "e95d1b25-251d-470a-a062-fa1922dfa9a8" },
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=bluetooth-profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bluetooth-profile.js","sourceRoot":"","sources":["../lib/bluetooth-profile.ts"],"names":[],"mappings":"AAAA,6BAA6B;AAC7B,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,IAAI,EAAE;QACJ,EAAE,EAAE,sCAAsC;QAC1C,eAAe,EAAE;YACf,EAAE,EAAE,EAAE,EAAE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;QACN,EAAE,EAAE,sCAAsC;QAC1C,eAAe,EAAE;YACf,CAAC,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACjD,CAAC,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;SAClD;KACF;IACD,KAAK,EAAE;QACL,EAAE,EAAE,sCAAsC;QAC1C,eAAe,EAAE;YACf,oBAAoB,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACpE,aAAa,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YAC7D,kBAAkB,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YAClE,WAAW,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;SAC5D;KACF;IACD,YAAY,EAAE;QACZ,EAAE,EAAE,sCAAsC;QAC1C,eAAe,EAAE;YACf,IAAI,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACpD,MAAM,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACtD,OAAO,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACvD,WAAW,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;SAC5D;KACF;IACD,WAAW,EAAE;QACX,EAAE,EAAE,sCAAsC;QAC1C,eAAe,EAAE;YACf,IAAI,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;YACpD,MAAM,EAAE,EAAE,EAAE,EAAE,sCAAsC,EAAE;SACvD;KACF;CACF,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (c) 2021, Micro:bit Educational Foundation and contributors
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
import { AccelerometerData } from "./accelerometer";
|
|
7
|
+
import { BluetoothDeviceWrapper } from "./bluetooth-device-wrapper";
|
|
8
|
+
import { BoardVersion, ConnectOptions, ConnectionStatus, DeviceConnection, DeviceConnectionEventMap } from "./device";
|
|
9
|
+
import { TypedEventTarget } from "./events";
|
|
10
|
+
import { Logging } from "./logging";
|
|
11
|
+
import { ServiceConnectionEventMap } from "./service-events";
|
|
12
|
+
export interface MicrobitWebBluetoothConnectionOptions {
|
|
13
|
+
logging?: Logging;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A Bluetooth connection to a micro:bit device.
|
|
17
|
+
*/
|
|
18
|
+
export declare class MicrobitWebBluetoothConnection extends TypedEventTarget<DeviceConnectionEventMap & ServiceConnectionEventMap> implements DeviceConnection {
|
|
19
|
+
status: ConnectionStatus;
|
|
20
|
+
/**
|
|
21
|
+
* The USB device we last connected to.
|
|
22
|
+
* Cleared if it is disconnected.
|
|
23
|
+
*/
|
|
24
|
+
private device;
|
|
25
|
+
private logging;
|
|
26
|
+
connection: BluetoothDeviceWrapper | undefined;
|
|
27
|
+
private _addEventListener;
|
|
28
|
+
private _removeEventListener;
|
|
29
|
+
constructor(options?: MicrobitWebBluetoothConnectionOptions);
|
|
30
|
+
private log;
|
|
31
|
+
initialize(): Promise<void>;
|
|
32
|
+
dispose(): void;
|
|
33
|
+
connect(options?: ConnectOptions): Promise<ConnectionStatus>;
|
|
34
|
+
getBoardVersion(): BoardVersion | undefined;
|
|
35
|
+
private startSerialInternal;
|
|
36
|
+
private stopSerialInternal;
|
|
37
|
+
disconnect(): Promise<void>;
|
|
38
|
+
private setStatus;
|
|
39
|
+
serialWrite(data: string): Promise<void>;
|
|
40
|
+
clearDevice(): Promise<void>;
|
|
41
|
+
private connectInternal;
|
|
42
|
+
private chooseDevice;
|
|
43
|
+
getAccelerometerData(): Promise<AccelerometerData | undefined>;
|
|
44
|
+
getAccelerometerPeriod(): Promise<number | undefined>;
|
|
45
|
+
setAccelerometerPeriod(value: number): Promise<void>;
|
|
46
|
+
private startAccelerometerNotifications;
|
|
47
|
+
private stopAccelerometerNotifications;
|
|
48
|
+
private startNotifications;
|
|
49
|
+
private stopNotifications;
|
|
50
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { createBluetoothDeviceWrapper, } from "./bluetooth-device-wrapper";
|
|
2
|
+
import { profile } from "./bluetooth-profile";
|
|
3
|
+
import { AfterRequestDevice, BeforeRequestDevice, ConnectionStatus, ConnectionStatusEvent, SerialResetEvent, } from "./device";
|
|
4
|
+
import { TypedEventTarget } from "./events";
|
|
5
|
+
import { NullLogging } from "./logging";
|
|
6
|
+
const requestDeviceTimeoutDuration = 30000;
|
|
7
|
+
/**
|
|
8
|
+
* A Bluetooth connection to a micro:bit device.
|
|
9
|
+
*/
|
|
10
|
+
export class MicrobitWebBluetoothConnection extends TypedEventTarget {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
super();
|
|
13
|
+
// TODO: when do we call getAvailable() ?
|
|
14
|
+
Object.defineProperty(this, "status", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: navigator.bluetooth
|
|
19
|
+
? ConnectionStatus.NO_AUTHORIZED_DEVICE
|
|
20
|
+
: ConnectionStatus.NOT_SUPPORTED
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* The USB device we last connected to.
|
|
24
|
+
* Cleared if it is disconnected.
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(this, "device", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "logging", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "connection", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "_addEventListener", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: this.addEventListener
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "_removeEventListener", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: this.removeEventListener
|
|
55
|
+
});
|
|
56
|
+
this.logging = options.logging || new NullLogging();
|
|
57
|
+
this.addEventListener = (type, ...args) => {
|
|
58
|
+
this._addEventListener(type, ...args);
|
|
59
|
+
this.startNotifications(type);
|
|
60
|
+
};
|
|
61
|
+
this.removeEventListener = (type, ...args) => {
|
|
62
|
+
this.stopNotifications(type);
|
|
63
|
+
this._removeEventListener(type, ...args);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
log(v) {
|
|
67
|
+
this.logging.log(v);
|
|
68
|
+
}
|
|
69
|
+
async initialize() {
|
|
70
|
+
if (navigator.bluetooth) {
|
|
71
|
+
// TODO: availabilitychanged
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
dispose() {
|
|
75
|
+
if (navigator.bluetooth) {
|
|
76
|
+
// TODO: availabilitychanged
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async connect(options = {}) {
|
|
80
|
+
await this.connectInternal(options);
|
|
81
|
+
return this.status;
|
|
82
|
+
}
|
|
83
|
+
getBoardVersion() {
|
|
84
|
+
return this.connection?.boardVersion;
|
|
85
|
+
}
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
async startSerialInternal() {
|
|
88
|
+
if (!this.connection) {
|
|
89
|
+
// As connecting then starting serial are async we could disconnect between them,
|
|
90
|
+
// so handle this gracefully.
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// TODO
|
|
94
|
+
}
|
|
95
|
+
// @ts-ignore
|
|
96
|
+
async stopSerialInternal() {
|
|
97
|
+
if (this.connection) {
|
|
98
|
+
// TODO
|
|
99
|
+
this.dispatchTypedEvent("serialreset", new SerialResetEvent());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async disconnect() {
|
|
103
|
+
try {
|
|
104
|
+
if (this.connection) {
|
|
105
|
+
await this.connection.disconnect();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
this.log("Error during disconnection:\r\n" + e);
|
|
110
|
+
this.logging.event({
|
|
111
|
+
type: "Bluetooth-error",
|
|
112
|
+
message: "error-disconnecting",
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
this.connection = undefined;
|
|
117
|
+
this.setStatus(ConnectionStatus.NOT_CONNECTED);
|
|
118
|
+
this.logging.log("Disconnection complete");
|
|
119
|
+
this.logging.event({
|
|
120
|
+
type: "Bluetooth-info",
|
|
121
|
+
message: "disconnected",
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
setStatus(newStatus) {
|
|
126
|
+
this.status = newStatus;
|
|
127
|
+
this.log("Device status " + newStatus);
|
|
128
|
+
this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus));
|
|
129
|
+
}
|
|
130
|
+
serialWrite(data) {
|
|
131
|
+
if (this.connection) {
|
|
132
|
+
// TODO
|
|
133
|
+
}
|
|
134
|
+
return Promise.resolve();
|
|
135
|
+
}
|
|
136
|
+
async clearDevice() {
|
|
137
|
+
await this.disconnect();
|
|
138
|
+
this.device = undefined;
|
|
139
|
+
this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
|
|
140
|
+
}
|
|
141
|
+
async connectInternal(options) {
|
|
142
|
+
if (!this.connection) {
|
|
143
|
+
const device = await this.chooseDevice(options);
|
|
144
|
+
if (!device) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.connection = await createBluetoothDeviceWrapper(device, this.logging, (type, event) => this.dispatchTypedEvent(type, event));
|
|
148
|
+
}
|
|
149
|
+
// TODO: timeout unification?
|
|
150
|
+
// Connection happens inside createBluetoothDeviceWrapper.
|
|
151
|
+
// await this.connection?.connect();
|
|
152
|
+
this.setStatus(ConnectionStatus.CONNECTED);
|
|
153
|
+
}
|
|
154
|
+
async chooseDevice(options) {
|
|
155
|
+
if (this.device) {
|
|
156
|
+
return this.device;
|
|
157
|
+
}
|
|
158
|
+
this.dispatchTypedEvent("beforerequestdevice", new BeforeRequestDevice());
|
|
159
|
+
try {
|
|
160
|
+
// In some situations the Chrome device prompt simply doesn't appear so we time this out after 30 seconds and reload the page
|
|
161
|
+
// TODO: give control over this to the caller
|
|
162
|
+
const result = await Promise.race([
|
|
163
|
+
navigator.bluetooth.requestDevice({
|
|
164
|
+
filters: [
|
|
165
|
+
{
|
|
166
|
+
namePrefix: options.name
|
|
167
|
+
? `BBC micro:bit [${options.name}]`
|
|
168
|
+
: "BBC micro:bit",
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
optionalServices: [
|
|
172
|
+
profile.accelerometer.id,
|
|
173
|
+
profile.button.id,
|
|
174
|
+
profile.deviceInformation.id,
|
|
175
|
+
profile.dfuControl.id,
|
|
176
|
+
profile.event.id,
|
|
177
|
+
profile.ioPin.id,
|
|
178
|
+
profile.led.id,
|
|
179
|
+
profile.magnetometer.id,
|
|
180
|
+
profile.temperature.id,
|
|
181
|
+
profile.uart.id,
|
|
182
|
+
],
|
|
183
|
+
}),
|
|
184
|
+
new Promise((resolve) => setTimeout(() => resolve("timeout"), requestDeviceTimeoutDuration)),
|
|
185
|
+
]);
|
|
186
|
+
if (result === "timeout") {
|
|
187
|
+
// btSelectMicrobitDialogOnLoad.set(true);
|
|
188
|
+
window.location.reload();
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
this.device = result;
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
catch (e) {
|
|
195
|
+
this.logging.error("Bluetooth request device failed/cancelled", e);
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
this.dispatchTypedEvent("afterrequestdevice", new AfterRequestDevice());
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async getAccelerometerData() {
|
|
203
|
+
const accelerometerService = await this.connection?.getAccelerometerService();
|
|
204
|
+
return accelerometerService?.getData();
|
|
205
|
+
}
|
|
206
|
+
async getAccelerometerPeriod() {
|
|
207
|
+
const accelerometerService = await this.connection?.getAccelerometerService();
|
|
208
|
+
return accelerometerService?.getPeriod();
|
|
209
|
+
}
|
|
210
|
+
async setAccelerometerPeriod(value) {
|
|
211
|
+
const accelerometerService = await this.connection?.getAccelerometerService();
|
|
212
|
+
accelerometerService?.setPeriod(value);
|
|
213
|
+
}
|
|
214
|
+
async startAccelerometerNotifications() {
|
|
215
|
+
const accelerometerService = await this.connection?.getAccelerometerService();
|
|
216
|
+
accelerometerService?.startNotifications();
|
|
217
|
+
if (this.connection) {
|
|
218
|
+
this.connection.serviceListeners.accelerometerdatachanged.notifying =
|
|
219
|
+
true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async stopAccelerometerNotifications() {
|
|
223
|
+
const accelerometerService = await this.connection?.getAccelerometerService();
|
|
224
|
+
if (this.connection) {
|
|
225
|
+
this.connection.serviceListeners.accelerometerdatachanged.notifying =
|
|
226
|
+
false;
|
|
227
|
+
}
|
|
228
|
+
accelerometerService?.stopNotifications();
|
|
229
|
+
}
|
|
230
|
+
async startNotifications(type) {
|
|
231
|
+
switch (type) {
|
|
232
|
+
case "accelerometerdatachanged": {
|
|
233
|
+
this.startAccelerometerNotifications();
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async stopNotifications(type) {
|
|
239
|
+
switch (type) {
|
|
240
|
+
case "accelerometerdatachanged": {
|
|
241
|
+
this.stopAccelerometerNotifications();
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=bluetooth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bluetooth.js","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AC;QAC3C,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,wBAAwB,CAAC,SAAS;gBACjE,IAAI,CAAC;QACT,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,8BAA8B;QAC1C,MAAM,oBAAoB,GACxB,MAAM,IAAI,CAAC,UAAU,EAAE,uBAAuB,EAAE,CAAC;QACnD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,wBAAwB,CAAC,SAAS;gBACjE,KAAK,CAAC;QACV,CAAC;QACD,oBAAoB,EAAE,iBAAiB,EAAE,CAAC;IAC5C,CAAC;IAEO,KAAK,CAAC,kBAAkB,CAAC,IAAY;QAC3C,QAAQ,IAAyB,EAAE,CAAC;YAClC,KAAK,0BAA0B,CAAC,CAAC,CAAC;gBAChC,IAAI,CAAC,+BAA+B,EAAE,CAAC;gBACvC,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,iBAAiB,CAAC,IAAY;QAC1C,QAAQ,IAAyB,EAAE,CAAC;YAClC,KAAK,0BAA0B,CAAC,CAAC,CAAC;gBAChC,IAAI,CAAC,8BAA8B,EAAE,CAAC;gBACtC,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (c) 2021, Micro:bit Educational Foundation and contributors
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
import { BoardVersion } from "./device";
|
|
7
|
+
/**
|
|
8
|
+
* Validates micro:bit board IDs.
|
|
9
|
+
*/
|
|
10
|
+
export declare class BoardId {
|
|
11
|
+
id: number;
|
|
12
|
+
private static v1Normalized;
|
|
13
|
+
private static v2Normalized;
|
|
14
|
+
constructor(id: number);
|
|
15
|
+
toBoardVersion(): BoardVersion;
|
|
16
|
+
isV1(): boolean;
|
|
17
|
+
isV2(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Return the board ID using the default ID for the board type.
|
|
20
|
+
* Used to integrate with MicropythonFsHex.
|
|
21
|
+
*/
|
|
22
|
+
normalize(): BoardId;
|
|
23
|
+
/**
|
|
24
|
+
* toString matches the input to parse.
|
|
25
|
+
*
|
|
26
|
+
* @returns the ID as a string.
|
|
27
|
+
*/
|
|
28
|
+
toString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* @param value The ID as a hex string with no 0x prefix (e.g. 9900).
|
|
31
|
+
* @returns the valid board ID
|
|
32
|
+
* @throws if the ID isn't known.
|
|
33
|
+
*/
|
|
34
|
+
static parse(value: string): BoardId;
|
|
35
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (c) 2021, Micro:bit Educational Foundation and contributors
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Validates micro:bit board IDs.
|
|
8
|
+
*/
|
|
9
|
+
export class BoardId {
|
|
10
|
+
constructor(id) {
|
|
11
|
+
Object.defineProperty(this, "id", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: id
|
|
16
|
+
});
|
|
17
|
+
if (!this.isV1() && !this.isV2()) {
|
|
18
|
+
throw new Error(`Could not recognise the Board ID ${id.toString(16)}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
toBoardVersion() {
|
|
22
|
+
return this.isV1() ? "V1" : "V2";
|
|
23
|
+
}
|
|
24
|
+
isV1() {
|
|
25
|
+
return this.id === 0x9900 || this.id === 0x9901;
|
|
26
|
+
}
|
|
27
|
+
isV2() {
|
|
28
|
+
return (this.id === 0x9903 ||
|
|
29
|
+
this.id === 0x9904 ||
|
|
30
|
+
this.id === 0x9905 ||
|
|
31
|
+
this.id === 0x9906);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Return the board ID using the default ID for the board type.
|
|
35
|
+
* Used to integrate with MicropythonFsHex.
|
|
36
|
+
*/
|
|
37
|
+
normalize() {
|
|
38
|
+
return this.isV1() ? BoardId.v1Normalized : BoardId.v2Normalized;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* toString matches the input to parse.
|
|
42
|
+
*
|
|
43
|
+
* @returns the ID as a string.
|
|
44
|
+
*/
|
|
45
|
+
toString() {
|
|
46
|
+
return this.id.toString(16);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param value The ID as a hex string with no 0x prefix (e.g. 9900).
|
|
50
|
+
* @returns the valid board ID
|
|
51
|
+
* @throws if the ID isn't known.
|
|
52
|
+
*/
|
|
53
|
+
static parse(value) {
|
|
54
|
+
return new BoardId(parseInt(value, 16));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
Object.defineProperty(BoardId, "v1Normalized", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: new BoardId(0x9900)
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(BoardId, "v2Normalized", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: new BoardId(0x9903)
|
|
68
|
+
});
|
|
69
|
+
//# sourceMappingURL=board-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"board-id.js","sourceRoot":"","sources":["../lib/board-id.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;GAEG;AACH,MAAM,OAAO,OAAO;IAIlB,YAAmB,EAAU;QAAjB;;;;mBAAO,EAAE;WAAQ;QAC3B,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,oCAAoC,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QACzE,CAAC;IACH,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACnC,CAAC;IAED,IAAI;QACF,OAAO,IAAI,CAAC,EAAE,KAAK,MAAM,IAAI,IAAI,CAAC,EAAE,KAAK,MAAM,CAAC;IAClD,CAAC;IAED,IAAI;QACF,OAAO,CACL,IAAI,CAAC,EAAE,KAAK,MAAM;YAClB,IAAI,CAAC,EAAE,KAAK,MAAM;YAClB,IAAI,CAAC,EAAE,KAAK,MAAM;YAClB,IAAI,CAAC,EAAE,KAAK,MAAM,CACnB,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IACnE,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,KAAa;QACxB,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC;IAC1C,CAAC;;AAlDc;;;;WAAe,IAAI,OAAO,CAAC,MAAM,CAAC;EAAtB,CAAuB;AACnC;;;;WAAe,IAAI,OAAO,CAAC,MAAM,CAAC;EAAtB,CAAuB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (c) 2021, Micro:bit Educational Foundation and contributors
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
import { BoardId } from "./board-id";
|
|
7
|
+
export declare class BoardSerialInfo {
|
|
8
|
+
id: BoardId;
|
|
9
|
+
familyId: string;
|
|
10
|
+
hic: string;
|
|
11
|
+
constructor(id: BoardId, familyId: string, hic: string);
|
|
12
|
+
static parse(device: USBDevice, log: (msg: string) => void): BoardSerialInfo;
|
|
13
|
+
eq(other: BoardSerialInfo): boolean;
|
|
14
|
+
}
|