mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
Change cleanup/disconnect behavior for BT and EV3.
This commit is contained in:
parent
6a032087ce
commit
a1243fd897
2 changed files with 28 additions and 30 deletions
|
@ -497,7 +497,7 @@ class EV3 {
|
|||
*/
|
||||
this._rateLimiter = new RateLimiter(BTSendRateMax);
|
||||
|
||||
this.disconnect = this.disconnect.bind(this);
|
||||
this.reset = this.reset.bind(this);
|
||||
this._onConnect = this._onConnect.bind(this);
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
this._pollValues = this._pollValues.bind(this);
|
||||
|
@ -583,7 +583,7 @@ class EV3 {
|
|||
this._bt = new BT(this._runtime, this._extensionId, {
|
||||
majorDeviceClass: 8,
|
||||
minorDeviceClass: 1
|
||||
}, this._onConnect, this.disconnect, this._onMessage);
|
||||
}, this._onConnect, this.reset, this._onMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -600,13 +600,28 @@ class EV3 {
|
|||
* Called by the runtime when user wants to disconnect from the EV3 peripheral.
|
||||
*/
|
||||
disconnect () {
|
||||
this._clearSensorsAndMotors();
|
||||
window.clearInterval(this._pollingIntervalID);
|
||||
this._pollingIntervalID = null;
|
||||
|
||||
if (this._bt) {
|
||||
this._bt.disconnect();
|
||||
}
|
||||
|
||||
this.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all the state and timeout/interval ids.
|
||||
*/
|
||||
reset () {
|
||||
this._sensorPorts = [];
|
||||
this._motorPorts = [];
|
||||
this._sensors = {
|
||||
distance: 0,
|
||||
brightness: 0,
|
||||
buttons: [0, 0, 0, 0]
|
||||
};
|
||||
this._motors = [null, null, null, null];
|
||||
|
||||
window.clearInterval(this._pollingIntervalID);
|
||||
this._pollingIntervalID = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -886,22 +901,6 @@ class EV3 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all the senor port and motor names, and their values.
|
||||
* @private
|
||||
*/
|
||||
_clearSensorsAndMotors () {
|
||||
this._sensorPorts = [];
|
||||
this._motorPorts = [];
|
||||
this._sensors = {
|
||||
distance: 0,
|
||||
brightness: 0,
|
||||
buttons: [0, 0, 0, 0]
|
||||
};
|
||||
this._motors = [null, null, null, null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
13
src/io/bt.js
13
src/io/bt.js
|
@ -9,10 +9,10 @@ class BT extends JSONRPC {
|
|||
* @param {string} extensionId - the id of the extension using this socket.
|
||||
* @param {object} peripheralOptions - the list of options for peripheral discovery.
|
||||
* @param {object} connectCallback - a callback for connection.
|
||||
* @param {object} disconnectCallback - a callback for disconnection.
|
||||
* @param {object} resetCallback - a callback for resetting extension state.
|
||||
* @param {object} messageCallback - a callback for message sending.
|
||||
*/
|
||||
constructor (runtime, extensionId, peripheralOptions, connectCallback, disconnectCallback = null, messageCallback) {
|
||||
constructor (runtime, extensionId, peripheralOptions, connectCallback, resetCallback = null, messageCallback) {
|
||||
super();
|
||||
|
||||
this._socket = runtime.getScratchLinkSocket('BT');
|
||||
|
@ -27,7 +27,7 @@ class BT extends JSONRPC {
|
|||
this._connectCallback = connectCallback;
|
||||
this._connected = false;
|
||||
this._characteristicDidChangeCallback = null;
|
||||
this._disconnectCallback = disconnectCallback;
|
||||
this._resetCallback = resetCallback;
|
||||
this._discoverTimeoutID = null;
|
||||
this._extensionId = extensionId;
|
||||
this._peripheralOptions = peripheralOptions;
|
||||
|
@ -152,11 +152,10 @@ class BT extends JSONRPC {
|
|||
|
||||
if (!this._connected) return;
|
||||
|
||||
// TODO: Fix branching by splitting up cleanup/disconnect in extension
|
||||
if (this._disconnectCallback) {
|
||||
this._disconnectCallback(); // must call disconnect()
|
||||
} else {
|
||||
this.disconnect();
|
||||
|
||||
if (this._resetCallback) {
|
||||
this._resetCallback();
|
||||
}
|
||||
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, {
|
||||
|
|
Loading…
Reference in a new issue