Skip to content

Commit 55944bc

Browse files
authored
Merge pull request scratchfoundation#1285 from LLK/feature/device-connection
Device connection update for micro:bit and EV3 extensions
2 parents 665bf9a + e02dd35 commit 55944bc

File tree

13 files changed

+1363
-229
lines changed

13 files changed

+1363
-229
lines changed

package-lock.json

Lines changed: 27 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/engine/runtime.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class Runtime extends EventEmitter {
254254
video: new Video(this)
255255
};
256256

257+
this.extensionDevices = {};
258+
257259
/**
258260
* A runtime profiler that records timed events for later playback to
259261
* diagnose Scratch performance.
@@ -394,6 +396,30 @@ class Runtime extends EventEmitter {
394396
return 'EXTENSION_ADDED';
395397
}
396398

399+
/**
400+
* Event name for updating the available set of peripheral devices.
401+
* @const {string}
402+
*/
403+
static get PERIPHERAL_LIST_UPDATE () {
404+
return 'PERIPHERAL_LIST_UPDATE';
405+
}
406+
407+
/**
408+
* Event name for reporting that a peripheral has connected.
409+
* @const {string}
410+
*/
411+
static get PERIPHERAL_CONNECTED () {
412+
return 'PERIPHERAL_CONNECTED';
413+
}
414+
415+
/**
416+
* Event name for reporting that a peripheral has encountered an error.
417+
* @const {string}
418+
*/
419+
static get PERIPHERAL_ERROR () {
420+
return 'PERIPHERAL_ERROR';
421+
}
422+
397423
/**
398424
* Event name for reporting that blocksInfo was updated.
399425
* @const {string}
@@ -867,6 +893,36 @@ class Runtime extends EventEmitter {
867893
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
868894
}
869895

896+
registerExtensionDevice (extensionId, device) {
897+
this.extensionDevices[extensionId] = device;
898+
}
899+
900+
startDeviceScan (extensionId) {
901+
if (this.extensionDevices[extensionId]) {
902+
this.extensionDevices[extensionId].startDeviceScan();
903+
}
904+
}
905+
906+
connectToPeripheral (extensionId, peripheralId) {
907+
if (this.extensionDevices[extensionId]) {
908+
this.extensionDevices[extensionId].connectDevice(peripheralId);
909+
}
910+
}
911+
912+
disconnectExtensionSession (extensionId) {
913+
if (this.extensionDevices[extensionId]) {
914+
this.extensionDevices[extensionId].disconnectSession();
915+
}
916+
}
917+
918+
getPeripheralIsConnected (extensionId) {
919+
let isConnected = false;
920+
if (this.extensionDevices[extensionId]) {
921+
isConnected = this.extensionDevices[extensionId].getPeripheralIsConnected();
922+
}
923+
return isConnected;
924+
}
925+
870926
/**
871927
* Retrieve the function associated with the given opcode.
872928
* @param {!string} opcode The opcode to look up.

src/extension-support/extension-manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const Scratch3SpeakBlocks = require('../extensions/scratch3_speak');
1515
const Scratch3TranslateBlocks = require('../extensions/scratch3_translate');
1616
const Scratch3VideoSensingBlocks = require('../extensions/scratch3_video_sensing');
1717
const Scratch3SpeechBlocks = require('../extensions/scratch3_speech');
18+
const Scratch3Ev3Blocks = require('../extensions/scratch3_ev3');
1819

1920
const builtinExtensions = {
2021
pen: Scratch3PenBlocks,
@@ -24,7 +25,8 @@ const builtinExtensions = {
2425
speak: Scratch3SpeakBlocks,
2526
translate: Scratch3TranslateBlocks,
2627
videoSensing: Scratch3VideoSensingBlocks,
27-
speech: Scratch3SpeechBlocks
28+
speech: Scratch3SpeechBlocks,
29+
ev3: Scratch3Ev3Blocks
2830
};
2931

3032
/**

0 commit comments

Comments
 (0)