diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 24ef571ab..35c4e4c02 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -28,6 +28,26 @@ const Ev3PairingPin = '1234'; */ const BTSendRateMax = 40; +/** + * Enum for Ev3 byte specs used as parameter encodings to various argument values. + * Found in the 'EV3 Firmware Developer Kit', section4, page 9, at + * https://education.lego.com/en-us/support/mindstorms-ev3/developer-kits. + * + * The format for these values is: + * 0xxxxxxx for Short Format + * 1ttt-bbb for Long Format + * + * @readonly + * @enum {number} + */ +const Ev3ParamEncoding = { + ONE_BYTE: 0x81, // = 0b1000-001 = constant value, 1 byte to follow + TWO_BYTES: 0x82, // = 0b1000-010 = constant value, 2 bytes to follow + FOUR_BYTES: 0x83, // = 0b1000-011 = constant value, 4 bytes to follow + GLOBAL_ONE_BYTE: 0xE1, // = 0b1110-001 = size of global var, 1 byte to follow + GLOBAL_INDEX_0: 0x20 // = 0b00100000 = global var index "0" +}; + /** * Enum for Ev3 direct command types. * Found in the 'EV3 Communication Developer Kit', section 4, page 24, at @@ -63,26 +83,6 @@ const Ev3Opcode = { OPINPUT_READSI: 0x9D }; -/** - * Enum for Ev3 byte values used as arguments to various opcodes. - * Found in the 'EV3 Firmware Developer Kit', section4, page 9, at - * https://education.lego.com/en-us/support/mindstorms-ev3/developer-kits. - * - * The format for these values is: - * 0xxxxxxx for Short Format - * 1ttt-bbb for Long Format - * - * @readonly - * @enum {number} - */ -const Ev3ByteValue = { - ONE_BYTE: 0x81, // = 0b1000-001 = constant value, 1 byte to follow - TWO_BYTES: 0x82, // = 0b1000-010 = constant value, 2 bytes to follow - FOUR_BYTES: 0x83, // = 0b1000-011 = constant value, 4 bytes to follow - GLOBAL_ONE_BYTE: 0xE1, // = 0b1110-001 = size of global var, 1 byte to follow - GLOBAL_INDEX_0: 0x20 // = 0b00100000 = global var index "0" -}; - /** * Enum for Ev3 values used as arguments to various opcodes. * Found in the 'EV3 Firmware Developer Kit', section4, page 10-onwards, at @@ -90,7 +90,7 @@ const Ev3ByteValue = { * @readonly * @enum {number} */ -const Ev3Value = { +const Ev3Args = { LAYER: 0x00, // always 0, chained EV3s not supported COAST: 0x00, BRAKE: 0x01, @@ -301,7 +301,7 @@ class EV3Motor { const port = this._portMask(this._index); let n = milliseconds; let speed = this._power * this._direction; - const ramp = Ev3Value.LONG_RAMP; + const ramp = Ev3Args.LONG_RAMP; let byteCommand = []; byteCommand[0] = Ev3Opcode.OPOUTPUT_TIME_SPEED; @@ -327,16 +327,16 @@ class EV3Motor { // Generate motor command values const runcmd = this._runValues(run); byteCommand = byteCommand.concat([ - Ev3Value.LAYER, + Ev3Args.LAYER, port, - Ev3ByteValue.ONE_BYTE, + Ev3ParamEncoding.ONE_BYTE, dir & 0xff, - Ev3ByteValue.ONE_BYTE, + Ev3ParamEncoding.ONE_BYTE, rampup ]).concat(runcmd.concat([ - Ev3ByteValue.ONE_BYTE, + Ev3ParamEncoding.ONE_BYTE, rampdown, - Ev3Value.BRAKE + Ev3Args.BRAKE ])); const cmd = this._parent.generateCommand( @@ -380,9 +380,9 @@ class EV3Motor { Ev3Command.DIRECT_COMMAND_NO_REPLY, [ Ev3Opcode.OPOUTPUT_STOP, - Ev3Value.LAYER, + Ev3Args.LAYER, this._portMask(this._index), // port output bit field - Ev3Value.COAST + Ev3Args.COAST ] ); @@ -398,7 +398,7 @@ class EV3Motor { // If run duration is less than max 16-bit integer if (run < 0x7fff) { return [ - Ev3ByteValue.TWO_BYTES, + Ev3ParamEncoding.TWO_BYTES, run & 0xff, (run >> 8) & 0xff ]; @@ -406,7 +406,7 @@ class EV3Motor { // Run forever return [ - Ev3ByteValue.FOUR_BYTES, + Ev3ParamEncoding.FOUR_BYTES, run & 0xff, (run >> 8) & 0xff, (run >> 16) & 0xff, @@ -550,12 +550,12 @@ class EV3 { [ Ev3Opcode.OPSOUND, Ev3Opcode.OPSOUND_CMD_TONE, - Ev3ByteValue.ONE_BYTE, + Ev3ParamEncoding.ONE_BYTE, 2, - Ev3ByteValue.TWO_BYTES, + Ev3ParamEncoding.TWO_BYTES, freq, freq >> 8, - Ev3ByteValue.TWO_BYTES, + Ev3ParamEncoding.TWO_BYTES, time, time >> 8 ] @@ -735,11 +735,11 @@ class EV3 { if (this._pollingCounter % 20 === 0) { // GET DEVICE LIST cmds[0] = Ev3Opcode.OPINPUT_DEVICE_LIST; - cmds[1] = Ev3ByteValue.ONE_BYTE; + cmds[1] = Ev3ParamEncoding.ONE_BYTE; cmds[2] = 33; // 0x21 ARRAY // TODO: document cmds[3] = 96; // 0x60 CHANGED // TODO: document - cmds[4] = Ev3ByteValue.GLOBAL_ONE_BYTE; - cmds[5] = Ev3ByteValue.GLOBAL_INDEX_0; + cmds[4] = Ev3ParamEncoding.GLOBAL_ONE_BYTE; + cmds[5] = Ev3ParamEncoding.GLOBAL_INDEX_0; // Command and payload lengths allocation = 33; @@ -754,11 +754,11 @@ class EV3 { for (let i = 0; i < 4; i++) { if (this._sensorPorts[i] !== 'none') { cmds[index + 0] = Ev3Opcode.OPINPUT_READSI; - cmds[index + 1] = Ev3Value.LAYER; + cmds[index + 1] = Ev3Args.LAYER; cmds[index + 2] = i; // PORT - cmds[index + 3] = Ev3Value.DO_NOT_CHANGE_TYPE; + cmds[index + 3] = Ev3Args.DO_NOT_CHANGE_TYPE; cmds[index + 4] = Ev3Mode[this._sensorPorts[i]]; - cmds[index + 5] = Ev3ByteValue.GLOBAL_ONE_BYTE; + cmds[index + 5] = Ev3ParamEncoding.GLOBAL_ONE_BYTE; cmds[index + 6] = sensorCount * 4; // GLOBAL INDEX index += 7; } @@ -768,9 +768,9 @@ class EV3 { // GET MOTOR POSITION VALUES, EVEN IF NO MOTOR PRESENT for (let i = 0; i < 4; i++) { cmds[index + 0] = Ev3Opcode.OPOUTPUT_GET_COUNT; - cmds[index + 1] = Ev3Value.LAYER; + cmds[index + 1] = Ev3Args.LAYER; cmds[index + 2] = i; // PORT TODO: explain incorrect documentation as 'Output bit field' - cmds[index + 3] = Ev3ByteValue.GLOBAL_ONE_BYTE; + cmds[index + 3] = Ev3ParamEncoding.GLOBAL_ONE_BYTE; cmds[index + 4] = sensorCount * 4; // GLOBAL INDEX index += 5; sensorCount++;