scratch-vm/src/extensions/scratch3_microbit/index.js

647 lines
27 KiB
JavaScript
Raw Normal View History

const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const log = require('../../util/log');
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
const ScratchBLE = require('../../io/scratchBLE');
const Base64Util = require('../../util/base64-util');
/**
* Icon svg to be displayed at the left edge of each extension block, encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const blockIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4xLjAsIFNWRyBFeHBvc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SwtNi45NzgwMDEgLTYuOTc3LC02Ljk3ODAwMSBsIC0xNy40NDUsMCBtIDE3LjQ0NSwxOC42MDgwMDEgLTE3LjQ0NSwwIGMgLTYuNDEzLDAgLTExLjYzLC01LjIxNyAtMTEuNjMsLTExLjYzIEMgMCwxMy45Mzk1IDUuMjE3LDguNzIyNTAwNCAxMS42Myw4LjcyMjUwMDQgbCAxNy40NDUsMCBjIDYuNDEzLDAgMTEuNjMsNS4yMTY5OTk2IDExLjYzLDExLjYzMDAwMDYgLTEwZS00LDYuNDEzIC01LjIxNywxMS42MyAtMTEuNjMsMTEuNjMiIC8+PC9zdmc+';
/**
* Icon svg to be displayed in the menu encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const menuIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4KCjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM6c29kaXBvZGk9Imh0dHA6Ly9zb2RpcG9kaS5zb3VyY2Vmb3JnZS5uZXQvRFREL3NvZGlwb2RpLTAuZHRkIgogICB4bWxuczppbmtzY2FwZT0iaHR0cDovL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JtIDExLjYzLDE4LjAwNCBjIC0xLjI5NywwIC0yLjM0OSwxLjA1MjAwMSAtMi4zNDksMi4zNDgwMDEgMCwxLjI5NiAxLjA1MiwyLjM0OSAyLjM0OSwyLjM0OSAxLjI5NiwwIDIuMzQ3LC0xLjA1MyAyLjM0NywtMi4zNDkgMCwtMS4yOTYgLTEuMDUxLC0yLjM0ODAwMSAtMi4zNDcsLTIuMzQ4MDAxIiAvPjxwYXRoCiAgICAgc3R5bGU9ImZpbGw6IzAwMDAwMCIKICAgICBpbmtzY2FwZTpjb25uZWN0b3ItY3VydmF0dXJlPSIwIgogICAgIGlkPSJwYXRoNDMiCiAgICAgZD0ibSAxMS42MywxMy4zNzQ1IGMgLTMuODQ4LDAgLTYuOTc4LDMuMTI5IC02Ljk3OCw2Ljk3ODAwMSAwLDMuODQ4IDMuMTMsNi45NzggNi45NzgsNi45NzggbCAxNy40NDUsMCBjIDMuODQ4LDAgNi45NzcsLTMuMTMgNi45NzcsLTYuOTc4IDAsLTMuODQ5MDAxIC0zLjEyOSwtNi45NzgwMDEgLTYuOTc3LC02Ljk3ODAwMSBsIC0xNy40NDUsMCBtIDE3LjQ0NSwxOC42MDgwMDEgLTE3LjQ0NSwwIGMgLTYuNDEzLDAgLTExLjYzLC01LjIxNyAtMTEuNjMsLTExLjYzIEMgMCwxMy45Mzk1IDUuMjE3LDguNzIyNTAwNCAxMS42Myw4LjcyMjUwMDQgbCAxNy40NDUsMCBjIDYuNDEzLDAgMTEuNjMsNS4yMTY5OTk2IDExLjYzLDExLjYzMDAwMDYgLTEwZS00LDYuNDEzIC01LjIxNywxMS42MyAtMTEuNjMsMTEuNjMiIC8+PC9zdmc+';
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* Enum for micro:bit BLE command protocol.
* https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md
* @readonly
* @enum {number}
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
const BLECommand = {
CMD_PIN_CONFIG: 0x80,
CMD_DISPLAY_TEXT: 0x81,
CMD_DISPLAY_LED: 0x82
};
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
/**
* Enum for micro:bit protocol.
* https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md
* @readonly
* @enum {string}
*/
const BLEUUID = {
service: 0xf005,
rxChar: '5261da01-fa7e-42ab-850b-7c80220097cc',
txChar: '5261da02-fa7e-42ab-850b-7c80220097cc'
};
/**
* Manage communication with a MicroBit device over a Scrath Link client socket.
*/
class MicroBit {
/**
* Construct a MicroBit communication object.
* @param {Runtime} runtime - the Scratch 3.0 runtime
2018-06-19 17:59:03 -04:00
* @param {string} extensionId - the id of the extension
*/
2018-06-19 17:59:03 -04:00
constructor (runtime, extensionId) {
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* The Scratch 3.0 runtime used to trigger the green flag button.
* @type {Runtime}
* @private
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
this._runtime = runtime;
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* The ScratchBLE connection session for reading/writing device data.
* @type {ScratchBLE}
* @private
*/
2018-06-19 17:59:03 -04:00
this._ble = new ScratchBLE(this._runtime, {
filters: [
{services: [BLEUUID.service]}
]
});
this._runtime.registerExtensionDevice(extensionId, this._ble);
/**
* The most recently received value for each sensor.
* @type {Object.<string, number>}
* @private
*/
this._sensors = {
tiltX: 0,
tiltY: 0,
buttonA: 0,
buttonB: 0,
touchPins: [0, 0, 0],
gestureState: 0,
ledMatrixState: new Uint8Array(5)
};
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
/**
* The most recently received value for each gesture.
* @type {Object.<string, Object>}
* @private
*/
this._gestures = {
moving: false,
move: {
active: false,
timeout: false
},
shake: {
active: false,
timeout: false
},
jump: {
active: false,
timeout: false
}
};
}
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* @param {string} text - the text to display.
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
displayText (text) {
const output = new Uint8Array(text.length);
for (let i = 0; i < text.length; i++) {
output[i] = text.charCodeAt(i);
}
this._writeBLE(BLECommand.CMD_DISPLAY_TEXT, output);
}
/**
* @param {Uint8Array} matrix - the matrix to display.
*/
displayMatrix (matrix) {
this._writeBLE(BLECommand.CMD_DISPLAY_LED, matrix);
}
/**
* @return {number} - the latest value received for the tilt sensor's tilt about the X axis.
*/
get tiltX () {
return this._sensors.tiltX;
}
/**
* @return {number} - the latest value received for the tilt sensor's tilt about the Y axis.
*/
get tiltY () {
return this._sensors.tiltY;
}
/**
* @return {boolean} - the latest value received for the A button.
*/
get buttonA () {
return this._sensors.buttonA;
}
/**
* @return {boolean} - the latest value received for the B button.
*/
get buttonB () {
return this._sensors.buttonB;
}
/**
* @return {number} - the latest value received for the motion gesture states.
*/
get gestureState () {
return this._sensors.gestureState;
}
/**
* @return {Uint8Array} - the current state of the 5x5 LED matrix.
*/
get ledMatrixState () {
return this._sensors.ledMatrixState;
}
/**
* @param {number} pin - the pin to check touch state.
* @return {number} - the latest value received for the touch pin states.
*/
_checkPinState (pin) {
return this._sensors.touchPins[pin];
}
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* Requests connection to a device when BLE session is ready.
*/
2018-06-19 17:59:03 -04:00
// _onBLEReady () {
// this._ble.requestDevice({
// filters: [
// {services: [BLEUUID.service]}
// ]
// }, this._onBLEConnect.bind(this), this._onBLEError);
// }
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* Starts reading data from device after BLE has connected to it.
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
_onBLEConnect () {
const callback = this._processBLEData.bind(this);
this._ble.read(BLEUUID.service, BLEUUID.rxChar, true, callback);
}
/**
* @param {string} e - Error from BLE session.
*/
_onBLEError (e) {
log.error(`BLE error: ${e}`);
}
/**
* Process the sensor data from the incoming BLE characteristic.
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* @param {object} base64 - the incoming BLE data.
* @private
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
_processBLEData (base64) {
const data = Base64Util.base64ToUint8Array(base64);
this._sensors.tiltX = data[1] | (data[0] << 8);
if (this._sensors.tiltX > (1 << 15)) this._sensors.tiltX -= (1 << 16);
this._sensors.tiltY = data[3] | (data[2] << 8);
if (this._sensors.tiltY > (1 << 15)) this._sensors.tiltY -= (1 << 16);
this._sensors.buttonA = data[4];
this._sensors.buttonB = data[5];
this._sensors.touchPins[0] = data[6];
this._sensors.touchPins[1] = data[7];
this._sensors.touchPins[2] = data[8];
this._sensors.gestureState = data[9];
}
/**
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
* Write a message to the device BLE session.
* @param {number} command - the BLE command hex.
* @param {Uint8Array} message - the message to write.
* @private
*/
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
_writeBLE (command, message) {
const output = new Uint8Array(message.length + 1);
output[0] = command; // attach command to beginning of message
for (let i = 0; i < message.length; i++) {
output[i + 1] = message[i];
}
const b64enc = Base64Util.uint8ArrayToBase64(output);
this._ble.write(BLEUUID.service, BLEUUID.txChar, b64enc, 'base64');
}
}
/**
* Enum for tilt sensor direction.
* @readonly
* @enum {string}
*/
const TiltDirection = {
FRONT: 'front',
BACK: 'back',
LEFT: 'left',
RIGHT: 'right',
ANY: 'any'
};
/**
* Converting symbols to hex values
* @readonly
*/
const symbols2hex = {
'❤': 0xAAC544,
'♫': 0xF4AF78,
'☓': 0x1151151,
'✓': 0x8A88,
'↑': 0x477C84,
'↓': 0x427DC4,
'←': 0x467D84,
'→': 0x437CC4,
'◯': 0xE8C62E,
'☀': 0x1577DD5,
'☺': 0x5022E,
'!': 0x421004,
'?': 0xC91004
};
/**
* Scratch 3.0 blocks to interact with a MicroBit device.
*/
class Scratch3MicroBitBlocks {
/**
* @return {string} - the name of this extension.
*/
static get EXTENSION_NAME () {
return 'MicroBit';
}
/**
* @return {string} - the ID of this extension.
*/
static get EXTENSION_ID () {
return 'microbit';
}
/**
* @return {number} - the tilt sensor counts as "tilted" if its tilt angle meets or exceeds this threshold.
*/
static get TILT_THRESHOLD () {
return 15;
}
/**
* Construct a set of MicroBit blocks.
* @param {Runtime} runtime - the Scratch 3.0 runtime.
*/
constructor (runtime) {
/**
* The Scratch 3.0 runtime.
* @type {Runtime}
*/
this.runtime = runtime;
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
// Create a new MicroBit device instance
2018-06-19 17:59:03 -04:00
this._device = new MicroBit(this.runtime, Scratch3MicroBitBlocks.EXTENSION_ID);
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo () {
return {
id: Scratch3MicroBitBlocks.EXTENSION_ID,
name: Scratch3MicroBitBlocks.EXTENSION_NAME,
menuIconURI: menuIconURI,
blockIconURI: blockIconURI,
showStatusButton: true,
blocks: [
{
opcode: 'whenButtonPressed',
text: 'when [BTN] button pressed',
blockType: BlockType.HAT,
arguments: {
BTN: {
type: ArgumentType.STRING,
menu: 'buttons',
defaultValue: 'A'
}
}
},
{
opcode: 'whenMoved',
text: 'when moved',
blockType: BlockType.HAT
},
{
opcode: 'whenShaken',
text: 'when shaken',
blockType: BlockType.HAT
},
{
opcode: 'whenJumped',
text: 'when jumped',
blockType: BlockType.HAT
},
{
opcode: 'displayText',
text: 'display [TEXT]',
blockType: BlockType.COMMAND,
arguments: {
TEXT: {
type: ArgumentType.STRING,
defaultValue: 'Hello!'
}
}
},
{
opcode: 'displaySymbol',
text: 'display [SYMBOL]',
blockType: BlockType.COMMAND,
arguments: {
SYMBOL: {
type: ArgumentType.STRING,
menu: 'symbols',
defaultValue: '❤'
}
}
},
{
opcode: 'displayMatrix',
text: 'set light x:[X] y:[Y] [STATE]',
blockType: BlockType.COMMAND,
arguments: {
X: {
type: ArgumentType.STRING,
menu: 'rowcol',
defaultValue: '1'
},
Y: {
type: ArgumentType.STRING,
menu: 'rowcol',
defaultValue: '1'
},
STATE: {
type: ArgumentType.STRING,
menu: 'pinState',
defaultValue: 'on'
}
}
},
{
opcode: 'displayClear',
text: 'set all lights off',
blockType: BlockType.COMMAND
},
{
opcode: 'whenTilted',
text: 'when tilted [DIRECTION]',
blockType: BlockType.HAT,
arguments: {
DIRECTION: {
type: ArgumentType.STRING,
menu: 'tiltDirectionAny',
defaultValue: TiltDirection.ANY
}
}
},
{
opcode: 'isTilted',
text: 'tilted [DIRECTION]?',
blockType: BlockType.BOOLEAN,
arguments: {
DIRECTION: {
type: ArgumentType.STRING,
menu: 'tiltDirectionAny',
defaultValue: TiltDirection.ANY
}
}
},
{
opcode: 'getTiltAngle',
text: 'tilt angle [DIRECTION]',
blockType: BlockType.REPORTER,
arguments: {
DIRECTION: {
type: ArgumentType.STRING,
menu: 'tiltDirection',
defaultValue: TiltDirection.FRONT
}
}
},
{
opcode: 'whenPinConnected',
text: 'when pin [PIN] connected',
blockType: BlockType.HAT,
arguments: {
PIN: {
type: ArgumentType.STRING,
menu: 'touchPins',
defaultValue: '0'
}
}
}
],
menus: {
buttons: ['A', 'B', 'any'],
rowcol: ['1', '2', '3', '4', '5'],
pinState: ['on', 'off'],
symbols: Object.keys(symbols2hex),
tiltDirection: [TiltDirection.FRONT, TiltDirection.BACK, TiltDirection.LEFT, TiltDirection.RIGHT],
tiltDirectionAny: [
TiltDirection.FRONT, TiltDirection.BACK, TiltDirection.LEFT,
TiltDirection.RIGHT, TiltDirection.ANY
],
touchPins: ['0', '1', '2']
}
};
}
/**
* Test whether the A or B button is pressed
* @param {object} args - the block's arguments.
* @return {boolean} - true if the button is pressed.
*/
whenButtonPressed (args) {
if (args.BTN === 'any') {
return this._device.buttonA | this._device.buttonB;
} else if (args.BTN === 'A') {
return this._device.buttonA;
} else if (args.BTN === 'B') {
return this._device.buttonB;
}
return false;
}
/**
* Test whether the micro:bit is moving
* @return {boolean} - true if the micro:bit is moving.
*/
whenMoved () {
return (this._device.gestureState >> 2) & 1;
}
/**
* Test whether the micro:bit is shaken
* @return {boolean} - true if the micro:bit is shaken.
*/
whenShaken () {
return this._device.gestureState & 1;
}
/**
* Test whether the micro:bit is free falling
* @return {boolean} - true if the micro:bit is free falling.
*/
whenJumped () {
return (this._device.gestureState >> 1) & 1;
}
/**
* Display text on the 5x5 LED matrix.
* @param {object} args - the block's arguments.
* Note the limit is 19 characters
*/
displayText (args) {
const text = String(args.TEXT).substring(0, 19);
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
this._device.displayText(text);
return;
}
/**
* Display a predefined symbol on the 5x5 LED matrix.
* @param {object} args - the block's arguments.
*/
displaySymbol (args) {
const hex = symbols2hex[args.SYMBOL];
2018-06-04 15:22:12 -04:00
if (!hex) return;
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
this._device.ledMatrixState[0] = (hex >> 20) & 0x1F;
this._device.ledMatrixState[1] = (hex >> 15) & 0x1F;
this._device.ledMatrixState[2] = (hex >> 10) & 0x1F;
this._device.ledMatrixState[3] = (hex >> 5) & 0x1F;
this._device.ledMatrixState[4] = hex & 0x1F;
this._device.displayMatrix(this._device.ledMatrixState);
return;
}
/**
* Control individual LEDs on the 5x5 matrix.
* @param {object} args - the block's arguments.
*/
displayMatrix (args) {
if (args.STATE === 'on') {
this._device.ledMatrixState[args.Y - 1] |= 1 << 5 - args.X;
} else if (args.STATE === 'off') {
this._device.ledMatrixState[args.Y - 1] &= ~(1 << 5 - args.X);
} else return;
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
this._device.displayMatrix(this._device.ledMatrixState);
return;
}
/**
* Turn all 5x5 matrix LEDs off.
*/
displayClear () {
for (let i = 0; i < 5; i++) {
this._device.ledMatrixState[i] = 0;
}
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
this._device.displayMatrix(this._device.ledMatrixState);
return;
}
/**
* Test whether the tilt sensor is currently tilted.
* @param {object} args - the block's arguments.
* @property {TiltDirection} DIRECTION - the tilt direction to test (front, back, left, right, or any).
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
*/
whenTilted (args) {
return this._isTilted(args.DIRECTION);
}
/**
* Test whether the tilt sensor is currently tilted.
* @param {object} args - the block's arguments.
* @property {TiltDirection} DIRECTION - the tilt direction to test (front, back, left, right, or any).
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
*/
isTilted (args) {
return this._isTilted(args.DIRECTION);
}
/**
* @param {object} args - the block's arguments.
* @property {TiltDirection} DIRECTION - the direction (front, back, left, right) to check.
* @return {number} - the tilt sensor's angle in the specified direction.
* Note that getTiltAngle(front) = -getTiltAngle(back) and getTiltAngle(left) = -getTiltAngle(right).
*/
getTiltAngle (args) {
return this._getTiltAngle(args.DIRECTION);
}
/**
* Test whether the tilt sensor is currently tilted.
* @param {TiltDirection} direction - the tilt direction to test (front, back, left, right, or any).
* @return {boolean} - true if the tilt sensor is tilted past a threshold in the specified direction.
* @private
*/
_isTilted (direction) {
switch (direction) {
case TiltDirection.ANY:
return (Math.abs(this._device.tiltX / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD) ||
(Math.abs(this._device.tiltY / 10) >= Scratch3MicroBitBlocks.TILT_THRESHOLD);
default:
return this._getTiltAngle(direction) >= Scratch3MicroBitBlocks.TILT_THRESHOLD;
}
}
/**
* @param {TiltDirection} direction - the direction (front, back, left, right) to check.
* @return {number} - the tilt sensor's angle in the specified direction.
* Note that getTiltAngle(front) = -getTiltAngle(back) and getTiltAngle(left) = -getTiltAngle(right).
* @private
*/
_getTiltAngle (direction) {
switch (direction) {
case TiltDirection.FRONT:
return Math.round(this._device.tiltY / -10);
case TiltDirection.BACK:
return Math.round(this._device.tiltY / 10);
case TiltDirection.LEFT:
return Math.round(this._device.tiltX / -10);
case TiltDirection.RIGHT:
return Math.round(this._device.tiltX / 10);
default:
log.warn(`Unknown tilt direction in _getTiltAngle: ${direction}`);
}
}
/**
* @param {object} args - the block's arguments.
* @return {boolean} - the touch pin state.
* @private
*/
whenPinConnected (args) {
const pin = parseInt(args.PIN, 10);
if (isNaN(pin)) return;
if (pin < 0 || pin > 2) return false;
return this._device._checkPinState(pin);
}
}
module.exports = Scratch3MicroBitBlocks;