@microbit/microbit-connection 0.0.0-alpha.6 → 0.0.0-alpha.8
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/build/accelerometer-service.d.ts +8 -9
- package/build/accelerometer-service.js +23 -26
- package/build/accelerometer-service.js.map +1 -1
- package/build/bluetooth-device-wrapper.d.ts +14 -15
- package/build/bluetooth-device-wrapper.js +76 -30
- package/build/bluetooth-device-wrapper.js.map +1 -1
- package/build/bluetooth.d.ts +3 -7
- package/build/bluetooth.js +32 -71
- package/build/bluetooth.js.map +1 -1
- package/build/button-service.d.ts +13 -0
- package/build/button-service.js +76 -0
- package/build/button-service.js.map +1 -0
- package/build/buttons.d.ts +10 -0
- package/build/buttons.js +18 -0
- package/build/buttons.js.map +1 -0
- package/build/device.d.ts +2 -2
- package/build/device.js +2 -2
- package/build/device.js.map +1 -1
- package/build/index.d.ts +4 -2
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build/logging.js +3 -1
- package/build/logging.js.map +1 -1
- package/build/service-events.d.ts +3 -0
- package/build/service-events.js +12 -0
- package/build/service-events.js.map +1 -1
- package/build/usb-device-wrapper.d.ts +1 -1
- package/build/usb-device-wrapper.js.map +1 -1
- package/build/usb-radio-bridge.d.ts +26 -18
- package/build/usb-radio-bridge.js +270 -191
- package/build/usb-radio-bridge.js.map +1 -1
- package/build/usb.d.ts +1 -0
- package/build/usb.js +4 -1
- package/build/usb.js.map +1 -1
- package/package.json +2 -1
|
@@ -1,23 +1,155 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
/**
|
|
3
2
|
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
|
|
4
3
|
*
|
|
5
4
|
* SPDX-License-Identifier: MIT
|
|
6
5
|
*/
|
|
7
6
|
import * as protocol from "./usb-serial-protocol.js";
|
|
7
|
+
import { NullLogging } from "./logging.js";
|
|
8
|
+
import { TypedEventTarget } from "./events.js";
|
|
9
|
+
import { ConnectionStatus, ConnectionStatusEvent, } from "./device.js";
|
|
10
|
+
import { AccelerometerDataEvent } from "./accelerometer.js";
|
|
11
|
+
import { ButtonEvent, ButtonState } from "./buttons.js";
|
|
8
12
|
const connectTimeoutDuration = 10000;
|
|
9
13
|
class BridgeError extends Error {
|
|
10
14
|
}
|
|
11
15
|
class RemoteError extends Error {
|
|
12
16
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Wraps around a USB connection to implement a subset of services over a serial protocol.
|
|
19
|
+
*
|
|
20
|
+
* When it connects/disconnects it affects the delegate connection.
|
|
21
|
+
*/
|
|
22
|
+
export class MicrobitRadioBridgeConnection extends TypedEventTarget {
|
|
23
|
+
constructor(delegate, remoteDeviceId, options) {
|
|
24
|
+
super();
|
|
25
|
+
Object.defineProperty(this, "delegate", {
|
|
16
26
|
enumerable: true,
|
|
17
27
|
configurable: true,
|
|
18
28
|
writable: true,
|
|
19
|
-
value:
|
|
29
|
+
value: delegate
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "remoteDeviceId", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: remoteDeviceId
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "status", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: void 0
|
|
20
42
|
});
|
|
43
|
+
Object.defineProperty(this, "logging", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: void 0
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(this, "serialSession", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: void 0
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "delegateStatusListner", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: (e) => {
|
|
60
|
+
if (e.status !== ConnectionStatus.CONNECTED) {
|
|
61
|
+
this.setStatus(e.status);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.status = ConnectionStatus.NOT_CONNECTED;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
this.logging = options?.logging ?? new NullLogging();
|
|
69
|
+
this.status = this.statusFromDelegate();
|
|
70
|
+
}
|
|
71
|
+
getBoardVersion() {
|
|
72
|
+
return this.delegate.getBoardVersion();
|
|
73
|
+
}
|
|
74
|
+
serialWrite(data) {
|
|
75
|
+
return this.delegate.serialWrite(data);
|
|
76
|
+
}
|
|
77
|
+
async initialize() {
|
|
78
|
+
await this.delegate.initialize();
|
|
79
|
+
this.setStatus(this.statusFromDelegate());
|
|
80
|
+
this.delegate.addEventListener("status", this.delegateStatusListner);
|
|
81
|
+
}
|
|
82
|
+
dispose() {
|
|
83
|
+
this.delegate.removeEventListener("status", this.delegateStatusListner);
|
|
84
|
+
this.delegate.dispose();
|
|
85
|
+
}
|
|
86
|
+
clearDevice() {
|
|
87
|
+
this.delegate.clearDevice();
|
|
88
|
+
}
|
|
89
|
+
async connect(options) {
|
|
90
|
+
// TODO: previously this skipped overlapping connect attempts but that seems awkward
|
|
91
|
+
// can we... just not do that? or wait?
|
|
92
|
+
this.logging.event({
|
|
93
|
+
type: "Connect",
|
|
94
|
+
message: "Serial connect start",
|
|
95
|
+
});
|
|
96
|
+
await this.delegate.connect();
|
|
97
|
+
try {
|
|
98
|
+
this.serialSession = new RadioBridgeSerialSession(this.logging, this.remoteDeviceId, this.delegate, this.dispatchTypedEvent.bind(this), this.setStatus.bind(this), () => {
|
|
99
|
+
// Remote connection lost
|
|
100
|
+
this.logging.event({
|
|
101
|
+
type: "Serial",
|
|
102
|
+
message: "Serial connection lost 1",
|
|
103
|
+
});
|
|
104
|
+
this.serialSession?.dispose();
|
|
105
|
+
}, () => {
|
|
106
|
+
// Remote connection... even more lost?
|
|
107
|
+
this.logging.event({
|
|
108
|
+
type: "Serial",
|
|
109
|
+
message: "Serial connection lost 2",
|
|
110
|
+
});
|
|
111
|
+
this.serialSession?.dispose();
|
|
112
|
+
});
|
|
113
|
+
await this.serialSession.connect();
|
|
114
|
+
this.logging.event({
|
|
115
|
+
type: "Connect",
|
|
116
|
+
message: "Serial connect success",
|
|
117
|
+
});
|
|
118
|
+
return this.status;
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
this.logging.error("Failed to initialise serial protocol", e);
|
|
122
|
+
this.logging.event({
|
|
123
|
+
type: "Connect",
|
|
124
|
+
message: "Serial connect failed",
|
|
125
|
+
});
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async disconnect() {
|
|
130
|
+
await this.serialSession?.dispose();
|
|
131
|
+
}
|
|
132
|
+
setStatus(status) {
|
|
133
|
+
this.status = status;
|
|
134
|
+
this.dispatchTypedEvent("status", new ConnectionStatusEvent(status));
|
|
135
|
+
}
|
|
136
|
+
statusFromDelegate() {
|
|
137
|
+
return this.delegate.status == ConnectionStatus.CONNECTED
|
|
138
|
+
? ConnectionStatus.NOT_CONNECTED
|
|
139
|
+
: this.delegate.status;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Wraps a connected delegate for a single session from attempted serial handshake to error/dispose.
|
|
144
|
+
*/
|
|
145
|
+
class RadioBridgeSerialSession {
|
|
146
|
+
processButton(button, type, sensorData) {
|
|
147
|
+
if (sensorData[button] !== this.previousButtonState[button]) {
|
|
148
|
+
this.previousButtonState[button] = sensorData[button];
|
|
149
|
+
this.dispatchTypedEvent(type, new ButtonEvent(type, sensorData[button] ? ButtonState.ShortPress : ButtonState.NotPressed));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
constructor(logging, remoteDeviceId, delegate, dispatchTypedEvent, onStatusChanged, onRemoteConnectionLost1, onRemoteConnectionLost2) {
|
|
21
153
|
Object.defineProperty(this, "logging", {
|
|
22
154
|
enumerable: true,
|
|
23
155
|
configurable: true,
|
|
@@ -30,20 +162,49 @@ export class MicrobitRadioBridgeConnection {
|
|
|
30
162
|
writable: true,
|
|
31
163
|
value: remoteDeviceId
|
|
32
164
|
});
|
|
33
|
-
Object.defineProperty(this, "
|
|
165
|
+
Object.defineProperty(this, "delegate", {
|
|
34
166
|
enumerable: true,
|
|
35
167
|
configurable: true,
|
|
36
168
|
writable: true,
|
|
37
|
-
value:
|
|
169
|
+
value: delegate
|
|
38
170
|
});
|
|
39
|
-
|
|
40
|
-
Object.defineProperty(this, "isConnecting", {
|
|
171
|
+
Object.defineProperty(this, "dispatchTypedEvent", {
|
|
41
172
|
enumerable: true,
|
|
42
173
|
configurable: true,
|
|
43
174
|
writable: true,
|
|
44
|
-
value:
|
|
175
|
+
value: dispatchTypedEvent
|
|
45
176
|
});
|
|
46
|
-
Object.defineProperty(this, "
|
|
177
|
+
Object.defineProperty(this, "onStatusChanged", {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
configurable: true,
|
|
180
|
+
writable: true,
|
|
181
|
+
value: onStatusChanged
|
|
182
|
+
});
|
|
183
|
+
Object.defineProperty(this, "onRemoteConnectionLost1", {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: true,
|
|
186
|
+
writable: true,
|
|
187
|
+
value: onRemoteConnectionLost1
|
|
188
|
+
});
|
|
189
|
+
Object.defineProperty(this, "onRemoteConnectionLost2", {
|
|
190
|
+
enumerable: true,
|
|
191
|
+
configurable: true,
|
|
192
|
+
writable: true,
|
|
193
|
+
value: onRemoteConnectionLost2
|
|
194
|
+
});
|
|
195
|
+
Object.defineProperty(this, "unprocessedData", {
|
|
196
|
+
enumerable: true,
|
|
197
|
+
configurable: true,
|
|
198
|
+
writable: true,
|
|
199
|
+
value: ""
|
|
200
|
+
});
|
|
201
|
+
Object.defineProperty(this, "previousButtonState", {
|
|
202
|
+
enumerable: true,
|
|
203
|
+
configurable: true,
|
|
204
|
+
writable: true,
|
|
205
|
+
value: { buttonA: 0, buttonB: 0 }
|
|
206
|
+
});
|
|
207
|
+
Object.defineProperty(this, "onPeriodicMessageReceived", {
|
|
47
208
|
enumerable: true,
|
|
48
209
|
configurable: true,
|
|
49
210
|
writable: true,
|
|
@@ -55,95 +216,71 @@ export class MicrobitRadioBridgeConnection {
|
|
|
55
216
|
writable: true,
|
|
56
217
|
value: void 0
|
|
57
218
|
});
|
|
58
|
-
Object.defineProperty(this, "
|
|
219
|
+
Object.defineProperty(this, "connectionCheckIntervalId", {
|
|
59
220
|
enumerable: true,
|
|
60
221
|
configurable: true,
|
|
61
222
|
writable: true,
|
|
62
|
-
value:
|
|
223
|
+
value: void 0
|
|
63
224
|
});
|
|
64
|
-
|
|
65
|
-
Object.defineProperty(this, "finalAttempt", {
|
|
225
|
+
Object.defineProperty(this, "serialErrorListener", {
|
|
66
226
|
enumerable: true,
|
|
67
227
|
configurable: true,
|
|
68
228
|
writable: true,
|
|
69
|
-
value:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.logging.event({
|
|
74
|
-
type: this.isReconnect ? "Reconnect" : "Connect",
|
|
75
|
-
message: "Serial connect start",
|
|
229
|
+
value: (e) => {
|
|
230
|
+
this.logging.error("Serial error", e);
|
|
231
|
+
void this.dispose();
|
|
232
|
+
}
|
|
76
233
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// if (onPeriodicMessageRecieved) {
|
|
99
|
-
// onPeriodicMessageRecieved();
|
|
100
|
-
// onPeriodicMessageRecieved = undefined;
|
|
101
|
-
// }
|
|
102
|
-
// onAccelerometerChange(
|
|
103
|
-
// sensorData.accelerometerX,
|
|
104
|
-
// sensorData.accelerometerY,
|
|
105
|
-
// sensorData.accelerometerZ
|
|
106
|
-
// );
|
|
107
|
-
// if (sensorData.buttonA !== previousButtonState.A) {
|
|
108
|
-
// previousButtonState.A = sensorData.buttonA;
|
|
109
|
-
// onButtonChange(sensorData.buttonA, "A");
|
|
110
|
-
// }
|
|
111
|
-
// if (sensorData.buttonB !== previousButtonState.B) {
|
|
112
|
-
// previousButtonState.B = sensorData.buttonB;
|
|
113
|
-
// onButtonChange(sensorData.buttonB, "B");
|
|
114
|
-
// }
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
const messageResponse = protocol.processResponseMessage(msg);
|
|
118
|
-
if (!messageResponse) {
|
|
119
|
-
return;
|
|
234
|
+
Object.defineProperty(this, "serialDataListener", {
|
|
235
|
+
enumerable: true,
|
|
236
|
+
configurable: true,
|
|
237
|
+
writable: true,
|
|
238
|
+
value: (event) => {
|
|
239
|
+
const { data } = event;
|
|
240
|
+
const messages = protocol.splitMessages(this.unprocessedData + data);
|
|
241
|
+
this.unprocessedData = messages.remainingInput;
|
|
242
|
+
messages.messages.forEach(async (msg) => {
|
|
243
|
+
this.lastReceivedMessageTimestamp = Date.now();
|
|
244
|
+
// Messages are either periodic sensor data or command/response
|
|
245
|
+
const sensorData = protocol.processPeriodicMessage(msg);
|
|
246
|
+
if (sensorData) {
|
|
247
|
+
this.onPeriodicMessageReceived?.();
|
|
248
|
+
this.dispatchTypedEvent("accelerometerdatachanged", new AccelerometerDataEvent({
|
|
249
|
+
x: sensorData.accelerometerX,
|
|
250
|
+
y: sensorData.accelerometerY,
|
|
251
|
+
z: sensorData.accelerometerZ,
|
|
252
|
+
}));
|
|
253
|
+
this.processButton("buttonA", "buttonachanged", sensorData);
|
|
254
|
+
this.processButton("buttonB", "buttonbchanged", sensorData);
|
|
120
255
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
256
|
+
else {
|
|
257
|
+
const messageResponse = protocol.processResponseMessage(msg);
|
|
258
|
+
if (!messageResponse) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const responseResolve = this.responseMap.get(messageResponse.messageId);
|
|
262
|
+
if (responseResolve) {
|
|
263
|
+
this.responseMap.delete(messageResponse.messageId);
|
|
264
|
+
responseResolve(messageResponse);
|
|
265
|
+
}
|
|
125
266
|
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
};
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
Object.defineProperty(this, "responseMap", {
|
|
271
|
+
enumerable: true,
|
|
272
|
+
configurable: true,
|
|
273
|
+
writable: true,
|
|
274
|
+
value: new Map()
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
async connect() {
|
|
278
|
+
this.delegate.addEventListener("serialdata", this.serialDataListener);
|
|
279
|
+
this.delegate.addEventListener("serialerror", this.serialErrorListener);
|
|
129
280
|
try {
|
|
130
|
-
await this.
|
|
281
|
+
await this.delegate.softwareReset();
|
|
131
282
|
await this.handshake();
|
|
132
|
-
|
|
133
|
-
// Check for connection lost
|
|
134
|
-
if (this.connectionCheckIntervalId === undefined) {
|
|
135
|
-
this.connectionCheckIntervalId = setInterval(async () => {
|
|
136
|
-
if (this.lastReceivedMessageTimestamp &&
|
|
137
|
-
Date.now() - this.lastReceivedMessageTimestamp > 1_000) {
|
|
138
|
-
// stateOnReconnectionAttempt();
|
|
139
|
-
}
|
|
140
|
-
if (this.lastReceivedMessageTimestamp &&
|
|
141
|
-
Date.now() - this.lastReceivedMessageTimestamp >
|
|
142
|
-
connectTimeoutDuration) {
|
|
143
|
-
await this.handleReconnect();
|
|
144
|
-
}
|
|
145
|
-
}, 1000);
|
|
146
|
-
}
|
|
283
|
+
this.onStatusChanged(ConnectionStatus.CONNECTED);
|
|
147
284
|
this.logging.log(`Serial: using remote device id ${this.remoteDeviceId}`);
|
|
148
285
|
const remoteMbIdCommand = protocol.generateCmdRemoteMbId(this.remoteDeviceId);
|
|
149
286
|
const remoteMbIdResponse = await this.sendCmdWaitResponse(remoteMbIdCommand);
|
|
@@ -151,67 +288,31 @@ export class MicrobitRadioBridgeConnection {
|
|
|
151
288
|
remoteMbIdResponse.value !== this.remoteDeviceId) {
|
|
152
289
|
throw new BridgeError(`Failed to set remote micro:bit ID. Expected ${this.remoteDeviceId}, got ${remoteMbIdResponse.value}`);
|
|
153
290
|
}
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const startCmd = protocol.generateCmdStart({
|
|
159
|
-
accelerometer: true,
|
|
160
|
-
buttons: true,
|
|
161
|
-
});
|
|
162
|
-
const periodicMessagePromise = new Promise((resolve, reject) => {
|
|
163
|
-
onPeriodicMessageRecieved = resolve;
|
|
164
|
-
setTimeout(() => {
|
|
165
|
-
onPeriodicMessageRecieved = undefined;
|
|
166
|
-
reject(new Error("Failed to receive data from remote micro:bit"));
|
|
167
|
-
}, 500);
|
|
168
|
-
});
|
|
169
|
-
const startCmdResponse = await this.sendCmdWaitResponse(startCmd);
|
|
170
|
-
if (startCmdResponse.type === protocol.ResponseTypes.Error) {
|
|
171
|
-
throw new RemoteError(`Failed to start streaming sensors data. Error response received: ${startCmdResponse.message}`);
|
|
172
|
-
}
|
|
173
|
-
if (this.isReconnect) {
|
|
174
|
-
await periodicMessagePromise;
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
periodicMessagePromise.catch(async (e) => {
|
|
178
|
-
this.logging.error("Failed to initialise serial protocol", e);
|
|
179
|
-
await this.disconnectInternal(false, "remote");
|
|
180
|
-
this.isConnecting = false;
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
// stateOnAssigned(DeviceRequestStates.INPUT, this.usb.getModelNumber());
|
|
185
|
-
// stateOnReady(DeviceRequestStates.INPUT);
|
|
186
|
-
this.logging.event({
|
|
187
|
-
type: this.isReconnect ? "Reconnect" : "Connect",
|
|
188
|
-
message: "Serial connect success",
|
|
291
|
+
// Request the micro:bit to start sending the periodic messages
|
|
292
|
+
const startCmd = protocol.generateCmdStart({
|
|
293
|
+
accelerometer: true,
|
|
294
|
+
buttons: true,
|
|
189
295
|
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
296
|
+
const periodicMessagePromise = new Promise((resolve, reject) => {
|
|
297
|
+
this.onPeriodicMessageReceived = resolve;
|
|
298
|
+
setTimeout(() => {
|
|
299
|
+
this.onPeriodicMessageReceived = undefined;
|
|
300
|
+
reject(new Error("Failed to receive data from remote micro:bit"));
|
|
301
|
+
}, 500);
|
|
196
302
|
});
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
303
|
+
const startCmdResponse = await this.sendCmdWaitResponse(startCmd);
|
|
304
|
+
if (startCmdResponse.type === protocol.ResponseTypes.Error) {
|
|
305
|
+
throw new RemoteError(`Failed to start streaming sensors data. Error response received: ${startCmdResponse.message}`);
|
|
306
|
+
}
|
|
307
|
+
// TODO: in the first-time connection case we used to move the error/disconnect to the background here, why? timing?
|
|
308
|
+
await periodicMessagePromise;
|
|
309
|
+
this.startConnectionCheck();
|
|
200
310
|
}
|
|
201
|
-
|
|
202
|
-
this.
|
|
203
|
-
this.isConnecting = false;
|
|
311
|
+
catch (e) {
|
|
312
|
+
this.dispose();
|
|
204
313
|
}
|
|
205
314
|
}
|
|
206
|
-
async
|
|
207
|
-
return this.disconnectInternal(true, "bridge");
|
|
208
|
-
}
|
|
209
|
-
stopConnectionCheck() {
|
|
210
|
-
clearInterval(this.connectionCheckIntervalId);
|
|
211
|
-
this.connectionCheckIntervalId = undefined;
|
|
212
|
-
this.lastReceivedMessageTimestamp = undefined;
|
|
213
|
-
}
|
|
214
|
-
async disconnectInternal(userDisconnect) {
|
|
315
|
+
async dispose() {
|
|
215
316
|
this.stopConnectionCheck();
|
|
216
317
|
try {
|
|
217
318
|
await this.sendCmdWaitResponse(protocol.generateCmdStop());
|
|
@@ -220,42 +321,9 @@ export class MicrobitRadioBridgeConnection {
|
|
|
220
321
|
// If this fails the remote micro:bit has already gone away.
|
|
221
322
|
}
|
|
222
323
|
this.responseMap.clear();
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// userDisconnect || this.finalAttempt
|
|
227
|
-
// ? false
|
|
228
|
-
// : this.isReconnect
|
|
229
|
-
// ? "autoReconnect"
|
|
230
|
-
// : "connect",
|
|
231
|
-
// reconnectHelp
|
|
232
|
-
// );
|
|
233
|
-
}
|
|
234
|
-
async handleReconnect() {
|
|
235
|
-
if (this.isConnecting) {
|
|
236
|
-
this.logging.log("Serial disconnect ignored... reconnect already in progress");
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
try {
|
|
240
|
-
this.stopConnectionCheck();
|
|
241
|
-
this.logging.log("Serial disconnected... automatically trying to reconnect");
|
|
242
|
-
this.responseMap.clear();
|
|
243
|
-
await this.usb.stopSerial();
|
|
244
|
-
await this.usb.softwareReset();
|
|
245
|
-
await this.reconnect();
|
|
246
|
-
}
|
|
247
|
-
catch (e) {
|
|
248
|
-
this.logging.error("Serial connect triggered by disconnect listener failed", e);
|
|
249
|
-
}
|
|
250
|
-
finally {
|
|
251
|
-
this.isConnecting = false;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
async reconnect(finalAttempt = false) {
|
|
255
|
-
this.finalAttempt = finalAttempt;
|
|
256
|
-
this.logging.log("Serial reconnect");
|
|
257
|
-
this.isReconnect = true;
|
|
258
|
-
await this.connect();
|
|
324
|
+
this.delegate.removeEventListener("serialdata", this.serialDataListener);
|
|
325
|
+
this.delegate.removeEventListener("serialerror", this.serialErrorListener);
|
|
326
|
+
this.onStatusChanged(ConnectionStatus.NOT_CONNECTED);
|
|
259
327
|
}
|
|
260
328
|
async sendCmdWaitResponse(cmd) {
|
|
261
329
|
const responsePromise = new Promise((resolve, reject) => {
|
|
@@ -265,9 +333,30 @@ export class MicrobitRadioBridgeConnection {
|
|
|
265
333
|
reject(new Error(`Timeout waiting for response ${cmd.messageId}`));
|
|
266
334
|
}, 1_000);
|
|
267
335
|
});
|
|
268
|
-
await this.
|
|
336
|
+
await this.delegate.serialWrite(cmd.message);
|
|
269
337
|
return responsePromise;
|
|
270
338
|
}
|
|
339
|
+
startConnectionCheck() {
|
|
340
|
+
// Check for connection lost
|
|
341
|
+
if (this.connectionCheckIntervalId === undefined) {
|
|
342
|
+
this.connectionCheckIntervalId = setInterval(async () => {
|
|
343
|
+
if (this.lastReceivedMessageTimestamp &&
|
|
344
|
+
Date.now() - this.lastReceivedMessageTimestamp > 1_000) {
|
|
345
|
+
this.onRemoteConnectionLost1();
|
|
346
|
+
}
|
|
347
|
+
if (this.lastReceivedMessageTimestamp &&
|
|
348
|
+
Date.now() - this.lastReceivedMessageTimestamp >
|
|
349
|
+
connectTimeoutDuration) {
|
|
350
|
+
this.onRemoteConnectionLost2();
|
|
351
|
+
}
|
|
352
|
+
}, 1000);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
stopConnectionCheck() {
|
|
356
|
+
clearInterval(this.connectionCheckIntervalId);
|
|
357
|
+
this.connectionCheckIntervalId = undefined;
|
|
358
|
+
this.lastReceivedMessageTimestamp = undefined;
|
|
359
|
+
}
|
|
271
360
|
async handshake() {
|
|
272
361
|
// There is an issue where we cannot read data out from the micro:bit serial
|
|
273
362
|
// buffer until the buffer has been filled.
|
|
@@ -305,14 +394,4 @@ export class MicrobitRadioBridgeConnection {
|
|
|
305
394
|
}
|
|
306
395
|
}
|
|
307
396
|
}
|
|
308
|
-
export const startSerialConnection = async (logging, usb, remoteDeviceId) => {
|
|
309
|
-
try {
|
|
310
|
-
const serial = new MicrobitRadioBridgeConnection(usb, logging, remoteDeviceId);
|
|
311
|
-
await serial.connect();
|
|
312
|
-
return serial;
|
|
313
|
-
}
|
|
314
|
-
catch (e) {
|
|
315
|
-
return undefined;
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
397
|
//# sourceMappingURL=usb-radio-bridge.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usb-radio-bridge.js","sourceRoot":"","sources":["../lib/usb-radio-bridge.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"usb-radio-bridge.js","sourceRoot":"","sources":["../lib/usb-radio-bridge.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,QAAQ,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAW,WAAW,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAEL,gBAAgB,EAChB,qBAAqB,GAKtB,MAAM,aAAa,CAAC;AAKrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAExD,MAAM,sBAAsB,GAAW,KAAK,CAAC;AAE7C,MAAM,WAAY,SAAQ,KAAK;CAAG;AAClC,MAAM,WAAY,SAAQ,KAAK;CAAG;AAMlC;;;;GAIG;AACH,MAAM,OAAO,6BACX,SAAQ,gBAAsE;IAe9E,YACU,QAAkC,EAClC,cAAsB,EAC9B,OAA8C;QAE9C,KAAK,EAAE,CAAC;QAJR;;;;mBAAQ,QAAQ;WAA0B;QAC1C;;;;mBAAQ,cAAc;WAAQ;QAdhC;;;;;WAAyB;QACjB;;;;;WAAiB;QACjB;;;;;WAAoD;QAEpD;;;;mBAAwB,CAAC,CAAwB,EAAE,EAAE;gBAC3D,IAAI,CAAC,CAAC,MAAM,KAAK,gBAAgB,CAAC,SAAS,EAAE,CAAC;oBAC5C,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;gBAC3B,CAA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,CAAC;gBACH,CAAC,CAAC,CAAC;gBACL,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC,CACF,CAAC;QACF,IAAI,eAAe,CAAC,KAAK,KAAK,QAAQ,CAAC,OAAO,EAAE,CAAC;YAC/C,MAAM,IAAI,WAAW,CACnB,iDAAiD,QAAQ,CAAC,OAAO,EAAE,CACpE,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
package/build/usb.d.ts
CHANGED
|
@@ -50,6 +50,7 @@ export declare class MicrobitWebUSBConnection extends TypedEventTarget<DeviceCon
|
|
|
50
50
|
private setStatus;
|
|
51
51
|
private withEnrichedErrors;
|
|
52
52
|
serialWrite(data: string): Promise<void>;
|
|
53
|
+
softwareReset(): Promise<void>;
|
|
53
54
|
private handleDisconnect;
|
|
54
55
|
clearDevice(): Promise<void>;
|
|
55
56
|
private connectInternal;
|
package/build/usb.js
CHANGED
|
@@ -362,7 +362,7 @@ export class MicrobitWebUSBConnection extends TypedEventTarget {
|
|
|
362
362
|
setStatus(newStatus) {
|
|
363
363
|
this.status = newStatus;
|
|
364
364
|
this.visibilityReconnect = false;
|
|
365
|
-
this.log("
|
|
365
|
+
this.log("USB connection status " + newStatus);
|
|
366
366
|
this.dispatchTypedEvent("status", new ConnectionStatusEvent(newStatus));
|
|
367
367
|
}
|
|
368
368
|
async withEnrichedErrors(f) {
|
|
@@ -409,6 +409,9 @@ export class MicrobitWebUSBConnection extends TypedEventTarget {
|
|
|
409
409
|
}
|
|
410
410
|
});
|
|
411
411
|
}
|
|
412
|
+
async softwareReset() {
|
|
413
|
+
await this.connection?.softwareReset();
|
|
414
|
+
}
|
|
412
415
|
async clearDevice() {
|
|
413
416
|
await this.disconnect();
|
|
414
417
|
this.device = undefined;
|