mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-17 16:59:43 -04:00
Refactor for hardware extensions (#1555)
* Beginning refactor: renaming 'device' to 'peripheral', shortening function names, reordering functions, etc. * Continuing refactoring: renaming some functions to be more verbose in the runtime, adding JSDocs, etc. * Changing 'device' to 'peripheral', etc. * Changing 'session' to 'socket'. * Fixing EV3 menus and menu arg validation, reordering functions, etc. * Add _send, add some references to documentation, etc. * Factored out _outputCommand and _inputCommand, renamed some enums, etc. * Fixed _outputCommand, some other minor cleanup. * Make _outputCommand and _inputCommand public. * Added TODO. * Renamed BLE UUID enums to be clearer. * Change WeDo2 in comments to WeDo 2.0, etc. * Changed some WeDo2Motor command names, cleaned up some JSDocs. * Beginning a major EV3 refactor. * WeDo2 formatting and comment changes. * Motor refactoring in EV3: motorTurnClockwise and motorTurnCounterClockwise initial working state. * Add reminders to possibly cast motor menu args in WeDo2. * Continue to move motor commands in EV3 to EV3Motor class, don't create new EV3Motor on every poll cycle, etc. * Factoring EV3 polling value commands, etc. * Fixing EV3 motor power, position and button pressed, and some commenting, etc. * Move EV3 motor position parsing to EV3Motor class, move directCommand and directCompoundCommand functions, some commenting, etc. * Changed WeDo2 motor label enum name. * Removed some EV3 motor functions that aren't needed, changed menu label enum names, moved some opcodes up to enums. * Fixing comments and documentation. * Some commenting. * Adding further documentation and references to PDFs, changed reply check to be safer, etc. * Some comment changes. * Moving some functions around in EV3 and WeDo2 to match. * Commenting, etc. * Some renaming of session, etc. * Fix stopAllMotors in EV3. * Fixing clearing of motors in EV3. * Some comment changes. * Change runtime .extensions/registerExtension to .peripheralExtensions/registerPeripheralExtension. * Renaming outputCommand/inputCommand to generateOutputCommand/generateInputCommand, etc. * Moved motorCommandIDs to EV3Motor class, renamed directCommand to generateCommand, etc. * Adding a reminder to rename something. * JSDoc fix in EV3Motor class. * Fixing microbit function name. * Adding a todo item. * Changing Ev3 menu formats to be backwards compatible, moving a BLE function up. * Fixing EV3 ports again, and button pressed returning a boolean. * Fixing menu value to be a string in EV3.
This commit is contained in:
parent
b41423bdfa
commit
ec432e3b2f
9 changed files with 1476 additions and 1234 deletions
src/engine
|
@ -264,7 +264,10 @@ class Runtime extends EventEmitter {
|
|||
video: new Video(this)
|
||||
};
|
||||
|
||||
this.extensionDevices = {};
|
||||
/**
|
||||
* A list of extensions, used to manage hardware connection.
|
||||
*/
|
||||
this.peripheralExtensions = {};
|
||||
|
||||
/**
|
||||
* A runtime profiler that records timed events for later playback to
|
||||
|
@ -928,32 +931,56 @@ class Runtime extends EventEmitter {
|
|||
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
|
||||
}
|
||||
|
||||
registerExtensionDevice (extensionId, device) {
|
||||
this.extensionDevices[extensionId] = device;
|
||||
/**
|
||||
* Register an extension that communications with a hardware peripheral by id,
|
||||
* to have access to it and its peripheral functions in the future.
|
||||
* @param {string} extensionId - the id of the extension.
|
||||
* @param {object} extension - the extension to register.
|
||||
*/
|
||||
registerPeripheralExtension (extensionId, extension) {
|
||||
this.peripheralExtensions[extensionId] = extension;
|
||||
}
|
||||
|
||||
startDeviceScan (extensionId) {
|
||||
if (this.extensionDevices[extensionId]) {
|
||||
this.extensionDevices[extensionId].startDeviceScan();
|
||||
/**
|
||||
* Tell the specified extension to scan for a peripheral.
|
||||
* @param {string} extensionId - the id of the extension.
|
||||
*/
|
||||
scanForPeripheral (extensionId) {
|
||||
if (this.peripheralExtensions[extensionId]) {
|
||||
this.peripheralExtensions[extensionId].scan();
|
||||
}
|
||||
}
|
||||
|
||||
connectToPeripheral (extensionId, peripheralId) {
|
||||
if (this.extensionDevices[extensionId]) {
|
||||
this.extensionDevices[extensionId].connectDevice(peripheralId);
|
||||
/**
|
||||
* Connect to the extension's specified peripheral.
|
||||
* @param {string} extensionId - the id of the extension.
|
||||
* @param {number} peripheralId - the id of the peripheral.
|
||||
*/
|
||||
connectPeripheral (extensionId, peripheralId) {
|
||||
if (this.peripheralExtensions[extensionId]) {
|
||||
this.peripheralExtensions[extensionId].connect(peripheralId);
|
||||
}
|
||||
}
|
||||
|
||||
disconnectExtensionSession (extensionId) {
|
||||
if (this.extensionDevices[extensionId]) {
|
||||
this.extensionDevices[extensionId].disconnectSession();
|
||||
/**
|
||||
* Disconnect from the extension's connected peripheral.
|
||||
* @param {string} extensionId - the id of the extension.
|
||||
*/
|
||||
disconnectPeripheral (extensionId) {
|
||||
if (this.peripheralExtensions[extensionId]) {
|
||||
this.peripheralExtensions[extensionId].disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the extension has a currently connected peripheral.
|
||||
* @param {string} extensionId - the id of the extension.
|
||||
* @return {boolean} - whether the extension has a connected peripheral.
|
||||
*/
|
||||
getPeripheralIsConnected (extensionId) {
|
||||
let isConnected = false;
|
||||
if (this.extensionDevices[extensionId]) {
|
||||
isConnected = this.extensionDevices[extensionId].getPeripheralIsConnected();
|
||||
if (this.peripheralExtensions[extensionId]) {
|
||||
isConnected = this.peripheralExtensions[extensionId].isConnected();
|
||||
}
|
||||
return isConnected;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue