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:
Evelyn Eastmond 2018-07-24 17:08:48 +02:00 committed by Eric Rosenbaum
parent dc612fb4a1
commit 6444cf071d
4 changed files with 26 additions and 6 deletions

View file

@ -682,6 +682,10 @@ class EV3 {
const array = Base64Util.base64ToUint8Array(message); const array = Base64Util.base64ToUint8Array(message);
// log.info(`received array: ${array}`); // 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) { if (this._updateDevices) {
// READ DEVICE LIST // READ DEVICE LIST
this._sensorPorts[0] = EV_DEVICE_TYPES[array[5]] ? EV_DEVICE_TYPES[array[5]] : 'none'; this._sensorPorts[0] = EV_DEVICE_TYPES[array[5]] ? EV_DEVICE_TYPES[array[5]] : 'none';

View file

@ -24,6 +24,8 @@ const BLECommand = {
CMD_DISPLAY_LED: 0x82 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. * Enum for micro:bit protocol.
* https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md * https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md
@ -98,6 +100,13 @@ class MicroBit {
timeout: false timeout: false
} }
}; };
/**
* Interval ID for data reading timeout.
* @type {number}
* @private
*/
this._timeoutID = null;
} }
// TODO: keep here? // TODO: keep here?
@ -122,6 +131,7 @@ class MicroBit {
} }
disconnectSession () { disconnectSession () {
window.clearInterval(this._timeoutID);
this._ble.disconnectSession(); this._ble.disconnectSession();
} }
@ -209,6 +219,7 @@ class MicroBit {
_onSessionConnect () { _onSessionConnect () {
const callback = this._processSessionData.bind(this); const callback = this._processSessionData.bind(this);
this._ble.read(BLEUUID.service, BLEUUID.rxChar, true, callback); 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 * @private
*/ */
_processSessionData (base64) { _processSessionData (base64) {
// parse data
const data = Base64Util.base64ToUint8Array(base64); const data = Base64Util.base64ToUint8Array(base64);
this._sensors.tiltX = data[1] | (data[0] << 8); this._sensors.tiltX = data[1] | (data[0] << 8);
@ -232,6 +244,10 @@ class MicroBit {
this._sensors.touchPins[2] = data[8]; this._sensors.touchPins[2] = data[8];
this._sensors.gestureState = data[9]; 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);
} }
/** /**

View file

@ -1,5 +1,5 @@
const JSONRPCWebSocket = require('../util/jsonrpc-web-socket'); 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'; const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/ble';
class BLESession extends JSONRPCWebSocket { class BLESession extends JSONRPCWebSocket {
@ -144,9 +144,9 @@ class BLESession extends JSONRPCWebSocket {
}); });
} }
_sendError (e) { _sendError (/* e */) {
this._connected = false; 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); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
} }

View file

@ -1,5 +1,5 @@
const JSONRPCWebSocket = require('../util/jsonrpc-web-socket'); 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'; const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/bt';
class BTSession extends JSONRPCWebSocket { class BTSession extends JSONRPCWebSocket {
@ -112,9 +112,9 @@ class BTSession extends JSONRPCWebSocket {
} }
} }
_sendError (e) { _sendError (/* e */) {
this._connected = false; 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); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
} }