mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-26 06:10:26 -04:00
Merge pull request #1285 from LLK/feature/device-connection
Device connection update for micro:bit and EV3 extensions
This commit is contained in:
commit
55944bc4b6
13 changed files with 1363 additions and 229 deletions
src/engine
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue