mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Feature/critical extension fixes (#1383)
* Resolves - Turning micro bit power off doesn't disconnect microbit #1366. * Resolves EV3 motor block causes distance to return 0 #1363. * Fixing linting. * Tweak micro:bit auto-disconnect timeout.
This commit is contained in:
parent
dc612fb4a1
commit
6444cf071d
4 changed files with 26 additions and 6 deletions
|
@ -682,6 +682,10 @@ class EV3 {
|
|||
const array = Base64Util.base64ToUint8Array(message);
|
||||
// log.info(`received array: ${array}`);
|
||||
|
||||
if (array.length < 35) { // TODO: find safer solution
|
||||
return; // don't parse results that aren't sensor data list or device list
|
||||
}
|
||||
|
||||
if (this._updateDevices) {
|
||||
// READ DEVICE LIST
|
||||
this._sensorPorts[0] = EV_DEVICE_TYPES[array[5]] ? EV_DEVICE_TYPES[array[5]] : 'none';
|
||||
|
|
|
@ -24,6 +24,8 @@ const BLECommand = {
|
|||
CMD_DISPLAY_LED: 0x82
|
||||
};
|
||||
|
||||
const BLETimeout = 4500; // TODO: might need tweaking based on how long the device takes to start sending data
|
||||
|
||||
/**
|
||||
* Enum for micro:bit protocol.
|
||||
* https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md
|
||||
|
@ -98,6 +100,13 @@ class MicroBit {
|
|||
timeout: false
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Interval ID for data reading timeout.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this._timeoutID = null;
|
||||
}
|
||||
|
||||
// TODO: keep here?
|
||||
|
@ -122,6 +131,7 @@ class MicroBit {
|
|||
}
|
||||
|
||||
disconnectSession () {
|
||||
window.clearInterval(this._timeoutID);
|
||||
this._ble.disconnectSession();
|
||||
}
|
||||
|
||||
|
@ -209,6 +219,7 @@ class MicroBit {
|
|||
_onSessionConnect () {
|
||||
const callback = this._processSessionData.bind(this);
|
||||
this._ble.read(BLEUUID.service, BLEUUID.rxChar, true, callback);
|
||||
this._timeoutID = window.setInterval(this.disconnectSession.bind(this), BLETimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -217,6 +228,7 @@ class MicroBit {
|
|||
* @private
|
||||
*/
|
||||
_processSessionData (base64) {
|
||||
// parse data
|
||||
const data = Base64Util.base64ToUint8Array(base64);
|
||||
|
||||
this._sensors.tiltX = data[1] | (data[0] << 8);
|
||||
|
@ -232,6 +244,10 @@ class MicroBit {
|
|||
this._sensors.touchPins[2] = data[8];
|
||||
|
||||
this._sensors.gestureState = data[9];
|
||||
|
||||
// cancel disconnect timeout and start a new one
|
||||
window.clearInterval(this._timeoutID);
|
||||
this._timeoutID = window.setInterval(this.disconnectSession.bind(this), BLETimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const JSONRPCWebSocket = require('../util/jsonrpc-web-socket');
|
||||
const log = require('../util/log');
|
||||
// const log = require('../util/log');
|
||||
const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/ble';
|
||||
|
||||
class BLESession extends JSONRPCWebSocket {
|
||||
|
@ -144,9 +144,9 @@ class BLESession extends JSONRPCWebSocket {
|
|||
});
|
||||
}
|
||||
|
||||
_sendError (e) {
|
||||
_sendError (/* e */) {
|
||||
this._connected = false;
|
||||
log.error(`BLESession error: ${JSON.stringify(e)}`);
|
||||
// log.error(`BLESession error: ${JSON.stringify(e)}`);
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const JSONRPCWebSocket = require('../util/jsonrpc-web-socket');
|
||||
const log = require('../util/log');
|
||||
// const log = require('../util/log');
|
||||
const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/bt';
|
||||
|
||||
class BTSession extends JSONRPCWebSocket {
|
||||
|
@ -112,9 +112,9 @@ class BTSession extends JSONRPCWebSocket {
|
|||
}
|
||||
}
|
||||
|
||||
_sendError (e) {
|
||||
_sendError (/* e */) {
|
||||
this._connected = false;
|
||||
log.error(`BTSession error: ${JSON.stringify(e)}`);
|
||||
// log.error(`BTSession error: ${JSON.stringify(e)}`);
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue