Merge pull request from LLK/feature/device-connection

Device connection update for micro:bit and EV3 extensions
This commit is contained in:
Ray Schamp 2018-07-09 14:26:06 -04:00 committed by GitHub
commit 55944bc4b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1363 additions and 229 deletions
src/engine

View file

@ -254,6 +254,8 @@ class Runtime extends EventEmitter {
video: new Video(this)
};
this.extensionDevices = {};
/**
* A runtime profiler that records timed events for later playback to
* diagnose Scratch performance.
@ -394,6 +396,30 @@ class Runtime extends EventEmitter {
return 'EXTENSION_ADDED';
}
/**
* Event name for updating the available set of peripheral devices.
* @const {string}
*/
static get PERIPHERAL_LIST_UPDATE () {
return 'PERIPHERAL_LIST_UPDATE';
}
/**
* Event name for reporting that a peripheral has connected.
* @const {string}
*/
static get PERIPHERAL_CONNECTED () {
return 'PERIPHERAL_CONNECTED';
}
/**
* Event name for reporting that a peripheral has encountered an error.
* @const {string}
*/
static get PERIPHERAL_ERROR () {
return 'PERIPHERAL_ERROR';
}
/**
* Event name for reporting that blocksInfo was updated.
* @const {string}
@ -867,6 +893,36 @@ class Runtime extends EventEmitter {
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
}
registerExtensionDevice (extensionId, device) {
this.extensionDevices[extensionId] = device;
}
startDeviceScan (extensionId) {
if (this.extensionDevices[extensionId]) {
this.extensionDevices[extensionId].startDeviceScan();
}
}
connectToPeripheral (extensionId, peripheralId) {
if (this.extensionDevices[extensionId]) {
this.extensionDevices[extensionId].connectDevice(peripheralId);
}
}
disconnectExtensionSession (extensionId) {
if (this.extensionDevices[extensionId]) {
this.extensionDevices[extensionId].disconnectSession();
}
}
getPeripheralIsConnected (extensionId) {
let isConnected = false;
if (this.extensionDevices[extensionId]) {
isConnected = this.extensionDevices[extensionId].getPeripheralIsConnected();
}
return isConnected;
}
/**
* Retrieve the function associated with the given opcode.
* @param {!string} opcode The opcode to look up.