2018-05-14 13:52:49 -04:00
|
|
|
const ArgumentType = require('../../extension-support/argument-type');
|
|
|
|
const BlockType = require('../../extension-support/block-type');
|
|
|
|
const log = require('../../util/log');
|
2018-06-20 13:59:23 -04:00
|
|
|
const BLESession = require('../../io/bleSession');
|
2018-06-18 14:56:51 -04:00
|
|
|
const Base64Util = require('../../util/base64-util');
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-07-11 11:20:19 -04:00
|
|
|
const blockIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iNDBweCIgaGVpZ2h0PSI0MHB4IiB2aWV3Qm94PSIwIDAgNDAgNDAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUwLjIgKDU1MDQ3KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5taWNyb2JpdC1ibG9jay1pY29uPC90aXRsZT4KICAgIDxkZXNjPkNyZWF0ZWQgd2l0aCBTa2V0Y2guPC9kZXNjPgogICAgPGRlZnM+PC9kZWZzPgogICAgPGcgaWQ9Im1pY3JvYml0LWJsb2NrLWljb24iIHN0cm9rZT0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPgogICAgICAgIDxnIGlkPSJtaWNyb2JpdCIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMy41MDAwMDAsIDYuMDAwM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ODc0NTEyMSwyMy4wNDkxMjcyIDMwLjM3OTE1MiwyMy41NDAwMjQ3IDI5Ljc3LDIzLjU0IFoiIGlkPSJTaGFwZSIgc3Ryb2tlPSIjMjMxRjIwIiBzdHJva2Utd2lkdGg9IjAuODkiIGZpbGw9IiMyMzFGMjAiIGZpbGwtcnVsZT0ibm9uemVybyIgb3BhY2l0eT0iMC4xIj48L3BhdGg+CiAgICA
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Icon svg to be displayed in the menu encoded as a data URI.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line max-len
|
2018-07-11 11:20:19 -04:00
|
|
|
const menuIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMjBweCIgaGVpZ2h0PSIyMHB4IiB2aWV3Qm94PSIwIDAgMjAgMjAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUwLjIgKDU1MDQ3KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5taWNyb2JpdC1tZW51LWljb248L3RpdGxlPgogICAgPGRlc2M+Q3JlYXRlZCB3aXRoIFNrZXRjaC48L2Rlc2M+CiAgICA8ZGVmcz48L2RlZnM+CiAgICA8ZyBpZD0ibWljcm9iaXQtbWVudS1pY29uIiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0ibWljcm9iaXQiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDEuNzU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43OTExNzY5IDE1LjQ0MjAyNjQsMTAuOTk1MDc5NSAxNS40NCwxMS4yMiBDMTUuNDM3MjU2MSwxMS41MjQ1NjM2IDE1LjE4OTU3NiwxMS43NzAwMTI0IDE0Ljg4NSwxMS43NyBaIiBpZD0iU2hhcGUiIHN0cm9rZT0iIzIzMUYyMCIgc3Ryb2tlLXdpZHRoPSIwLjUiIGZpbGw9IiMyMzFGMjAi
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
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}
|
2018-05-14 13:52:49 -04:00
|
|
|
*/
|
2018-06-18 14:56:51 -04:00
|
|
|
const BLECommand = {
|
|
|
|
CMD_PIN_CONFIG: 0x80,
|
|
|
|
CMD_DISPLAY_TEXT: 0x81,
|
|
|
|
CMD_DISPLAY_LED: 0x82
|
|
|
|
};
|
2018-05-14 13:52:49 -04:00
|
|
|
|
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 {
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05-14 13:52:49 -04:00
|
|
|
*/
|
2018-06-19 17:59:03 -04:00
|
|
|
constructor (runtime, extensionId) {
|
2018-06-18 14:56:51 -04:00
|
|
|
|
2018-05-14 13:52:49 -04:00
|
|
|
/**
|
2018-06-18 14:56:51 -04:00
|
|
|
* The Scratch 3.0 runtime used to trigger the green flag button.
|
|
|
|
* @type {Runtime}
|
2018-05-14 13:52:49 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-06-18 14:56:51 -04:00
|
|
|
this._runtime = runtime;
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
2018-06-20 13:59:23 -04:00
|
|
|
* The BluetoothLowEnergy connection session for reading/writing device data.
|
|
|
|
* @type {BLESession}
|
2018-05-14 13:52:49 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-06-24 22:00:23 -04:00
|
|
|
this._ble = null;
|
|
|
|
this._runtime.registerExtensionDevice(extensionId, this);
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
};
|
|
|
|
|
2018-06-18 14:56:51 -04:00
|
|
|
/**
|
|
|
|
* The most recently received value for each gesture.
|
|
|
|
* @type {Object.<string, Object>}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-05-14 13:52:49 -04:00
|
|
|
this._gestures = {
|
|
|
|
moving: false,
|
|
|
|
move: {
|
|
|
|
active: false,
|
|
|
|
timeout: false
|
|
|
|
},
|
|
|
|
shake: {
|
|
|
|
active: false,
|
|
|
|
timeout: false
|
|
|
|
},
|
|
|
|
jump: {
|
|
|
|
active: false,
|
|
|
|
timeout: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-24 22:00:23 -04:00
|
|
|
// TODO: keep here?
|
|
|
|
/**
|
|
|
|
* Called by the runtime when user wants to scan for a device.
|
|
|
|
*/
|
|
|
|
startDeviceScan () {
|
2018-06-25 13:42:48 -04:00
|
|
|
log.info('making a new BLE session');
|
2018-06-24 22:00:23 -04:00
|
|
|
this._ble = new BLESession(this._runtime, {
|
|
|
|
filters: [
|
|
|
|
{services: [BLEUUID.service]}
|
|
|
|
]
|
|
|
|
}, this._onSessionConnect.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: keep here?
|
|
|
|
/**
|
|
|
|
* Called by the runtime when user wants to connect to a certain device.
|
|
|
|
* @param {number} id - the id of the device to connect to.
|
|
|
|
*/
|
|
|
|
connectDevice (id) {
|
|
|
|
this._ble.connectDevice(id);
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:21:11 -04:00
|
|
|
disconnectSession () {
|
|
|
|
this._ble.disconnectSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPeripheralIsConnected () {
|
|
|
|
let connected = false;
|
|
|
|
if (this._ble) {
|
|
|
|
connected = this._ble.getPeripheralIsConnected();
|
|
|
|
}
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:52:49 -04:00
|
|
|
/**
|
2018-06-18 14:56:51 -04:00
|
|
|
* @param {string} text - the text to display.
|
2018-06-21 14:36:10 -04:00
|
|
|
* @return {Promise} - a Promise that resolves when writing to device.
|
2018-05-14 13:52:49 -04:00
|
|
|
*/
|
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);
|
|
|
|
}
|
2018-06-21 14:36:10 -04:00
|
|
|
return this._writeSessionData(BLECommand.CMD_DISPLAY_TEXT, output);
|
2018-06-18 14:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Uint8Array} matrix - the matrix to display.
|
2018-06-21 14:36:10 -04:00
|
|
|
* @return {Promise} - a Promise that resolves when writing to device.
|
2018-06-18 14:56:51 -04:00
|
|
|
*/
|
|
|
|
displayMatrix (matrix) {
|
2018-06-21 14:36:10 -04:00
|
|
|
return this._writeSessionData(BLECommand.CMD_DISPLAY_LED, matrix);
|
2018-05-14 13:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-18 14:56:51 -04:00
|
|
|
* Starts reading data from device after BLE has connected to it.
|
2018-05-14 13:52:49 -04:00
|
|
|
*/
|
2018-06-20 13:59:23 -04:00
|
|
|
_onSessionConnect () {
|
|
|
|
const callback = this._processSessionData.bind(this);
|
2018-06-18 14:56:51 -04:00
|
|
|
this._ble.read(BLEUUID.service, BLEUUID.rxChar, true, callback);
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:52:49 -04:00
|
|
|
/**
|
|
|
|
* Process the sensor data from the incoming BLE characteristic.
|
2018-06-18 14:56:51 -04:00
|
|
|
* @param {object} base64 - the incoming BLE data.
|
2018-05-14 13:52:49 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-06-20 13:59:23 -04:00
|
|
|
_processSessionData (base64) {
|
2018-06-18 14:56:51 -04:00
|
|
|
const data = Base64Util.base64ToUint8Array(base64);
|
2018-05-14 13:52:49 -04:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.
|
2018-06-21 14:36:10 -04:00
|
|
|
* @return {Promise} - a Promise that resolves when writing to device.
|
2018-05-14 13:52:49 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2018-06-20 13:59:23 -04:00
|
|
|
_writeSessionData (command, message) {
|
2018-06-18 14:56:51 -04:00
|
|
|
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];
|
|
|
|
}
|
2018-06-21 14:36:10 -04:00
|
|
|
const data = Base64Util.uint8ArrayToBase64(output);
|
|
|
|
return this._ble.write(BLEUUID.service, BLEUUID.txChar, data, 'base64');
|
2018-05-14 13:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
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);
|
2018-05-14 13:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {object} metadata for this extension and its blocks.
|
|
|
|
*/
|
|
|
|
getInfo () {
|
|
|
|
return {
|
|
|
|
id: Scratch3MicroBitBlocks.EXTENSION_ID,
|
|
|
|
name: Scratch3MicroBitBlocks.EXTENSION_NAME,
|
|
|
|
menuIconURI: menuIconURI,
|
|
|
|
blockIconURI: blockIconURI,
|
2018-06-13 15:53:05 -04:00
|
|
|
showStatusButton: true,
|
2018-05-14 13:52:49 -04:00
|
|
|
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.
|
2018-06-21 14:36:10 -04:00
|
|
|
* @return {Promise} - a Promise that resolves when writing to device.
|
2018-06-04 14:52:52 -04:00
|
|
|
* Note the limit is 19 characters
|
2018-05-14 13:52:49 -04:00
|
|
|
*/
|
|
|
|
displayText (args) {
|
2018-06-04 14:52:52 -04:00
|
|
|
const text = String(args.TEXT).substring(0, 19);
|
2018-06-21 14:36:10 -04:00
|
|
|
return this._device.displayText(text);
|
2018-05-14 13:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a predefined symbol on the 5x5 LED matrix.
|
|
|
|
* @param {object} args - the block's arguments.
|
2018-06-21 14:36:10 -04:00
|
|
|
* @return {Promise} - a Promise that resolves when writing to device.
|
2018-05-14 13:52:49 -04:00
|
|
|
*/
|
|
|
|
displaySymbol (args) {
|
|
|
|
const hex = symbols2hex[args.SYMBOL];
|
2018-06-04 15:22:12 -04:00
|
|
|
if (!hex) return;
|
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;
|
2018-06-21 14:36:10 -04:00
|
|
|
return this._device.displayMatrix(this._device.ledMatrixState);
|
2018-05-14 13:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2018-06-18 14:56:51 -04:00
|
|
|
this._device.displayMatrix(this._device.ledMatrixState);
|
2018-05-14 13:52:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn all 5x5 matrix LEDs off.
|
|
|
|
*/
|
|
|
|
displayClear () {
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
this._device.ledMatrixState[i] = 0;
|
|
|
|
}
|
2018-06-18 14:56:51 -04:00
|
|
|
this._device.displayMatrix(this._device.ledMatrixState);
|
2018-05-14 13:52:49 -04:00
|
|
|
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;
|