Skip to content

Commit 0cd4213

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into develop
2 parents 9fb4e62 + 8ebaba3 commit 0cd4213

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

src/lib/ruby-generator/boost.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Define Ruby code generator for BOOST Blocks
3+
* @param {RubyGenerator} Generator The RubyGenerator
4+
* @return {RubyGenerator} same as param.
5+
*/
6+
export default function (Generator) {
7+
Generator.boost_menu_MOTOR_ID = function (block) {
8+
const index = Generator.getFieldValue(block, 'MOTOR_ID') || 0;
9+
const motorid = Generator.quote_(index);
10+
return [motorid, Generator.ORDER_ATOMIC];
11+
};
12+
13+
Generator.boost_motorOnFor = function (block) {
14+
const motorid = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
15+
const duration = Generator.valueToCode(block, 'DURATION', Generator.ORDER_NONE) || null;
16+
return `boost_motor_turn_on_for(${motorid}, ${duration})\n`;
17+
};
18+
19+
Generator.boost_motorOnForRotation = function (block) {
20+
const motorid = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
21+
const rotation = Generator.valueToCode(block, 'ROTATION', Generator.ORDER_NONE) || null;
22+
return `boost_motor_turn_this_way_for(${motorid}, ${rotation})\n`;
23+
};
24+
25+
Generator.boost_motorOn = function (block) {
26+
const motorid = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
27+
return `boost_motor_turn_on_for(${motorid})\n`;
28+
};
29+
30+
Generator.boost_motorOff = function (block) {
31+
const motorid = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
32+
return `boost_motor_turn_off_for(${motorid})\n`;
33+
};
34+
35+
Generator.boost_setMotorPower = function (block) {
36+
const motorid = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
37+
const power = Generator.valueToCode(block, 'POWER', Generator.ORDER_NONE) || null;
38+
return `boost_motor_set_power_for(${motorid}, ${power})\n`;
39+
};
40+
41+
return Generator;
42+
}

src/lib/ruby-generator/ev3.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Define Ruby code generator for LEGO_EV3 Blocks
3+
* @param {RubyGenerator} Generator The RubyGenerator
4+
* @return {RubyGenerator} same as param.
5+
*/
6+
export default function (Generator) {
7+
const Ev3SensorMenu = ['1', '2', '3', '4'];
8+
const Ev3MotorMenu = ['A', 'B', 'C', 'D'];
9+
10+
Generator.ev3_menu_sensorPorts = function (block) {
11+
const index = Generator.getFieldValue(block, 'sensorPorts') || 0;
12+
const port = Generator.quote_(Ev3SensorMenu[index]);
13+
return [port, Generator.ORDER_ATOMIC];
14+
};
15+
16+
Generator.ev3_whenButtonPressed = function (block) {
17+
block.isStatement = true;
18+
const port = Generator.valueToCode(block, 'PORT', Generator.ORDER_NONE) || null;
19+
return `${Generator.spriteName()}.when(:ev3_button_pressed, ${port}) do\n`;
20+
};
21+
22+
Generator.ev3_menu_motorPorts = function (block) {
23+
const index = Generator.getFieldValue(block, 'motorPorts') || 0;
24+
const port = Generator.quote_(Ev3MotorMenu[index]);
25+
return [port, Generator.ORDER_ATOMIC];
26+
};
27+
28+
Generator.ev3_motorSetPower = function (block) {
29+
const port = Generator.valueToCode(block, 'PORT', Generator.ORDER_NONE) || null;
30+
const power = Generator.valueToCode(block, 'POWER', Generator.ORDER_NONE) || null;
31+
return `ev3_motor_set_power(${port}, ${power})\n`;
32+
};
33+
34+
Generator.ev3_motorTurnClockwise = function (block) {
35+
const port = Generator.valueToCode(block, 'PORT', Generator.ORDER_NONE) || null;
36+
const time = Generator.valueToCode(block, 'TIME', Generator.ORDER_NONE) || null;
37+
return `ev3_motor_turn_this_way_for(${port}, ${time})\n`;
38+
};
39+
40+
Generator.ev3_motorTurnCounterClockwise = function (block) {
41+
const port = Generator.valueToCode(block, 'PORT', Generator.ORDER_NONE) || null;
42+
const time = Generator.valueToCode(block, 'TIME', Generator.ORDER_NONE) || null;
43+
return `ev3_motor_turn_that_way_for(${port}, ${time})\n`;
44+
};
45+
46+
Generator.ev3_getMotorPosition = function (block) {
47+
const order = Generator.ORDER_FUNCTION_CALL;
48+
const port = Generator.valueToCode(block, 'PORT', Generator.ORDER_NONE) || null;
49+
return [`ev3_motor_position(${port})`, order];
50+
};
51+
52+
Generator.ev3_whenDistanceLessThan = function (block) {
53+
block.isStatement = true;
54+
const distance = Generator.valueToCode(block, 'DISTANCE', Generator.ORDER_NONE) || null;
55+
return `${Generator.spriteName()}.when(:ev3_distance_gt, ${distance}) do\n`;
56+
};
57+
58+
Generator.ev3_whenBrightnessLessThan = function (block) {
59+
block.isStatement = true;
60+
const distance = Generator.valueToCode(block, 'DISTANCE', Generator.ORDER_NONE) || null;
61+
return `${Generator.spriteName()}.when(:ev3_brightness_gt, ${distance}) do\n`;
62+
};
63+
64+
Generator.ev3_buttonPressed = function (block) {
65+
const order = Generator.ORDER_FUNCTION_CALL;
66+
const port = Generator.valueToCode(block, 'PORT', order) || null;
67+
return [`ev3_button_pressed?(${port})`, order];
68+
};
69+
70+
Generator.ev3_getDistance = function () {
71+
return ['ev3_distance', Generator.ORDER_ATOMIC];
72+
};
73+
74+
Generator.ev3_getBrightness = function () {
75+
return ['ev3_brightness', Generator.ORDER_ATOMIC];
76+
};
77+
78+
Generator.ev3_beep = function (block) {
79+
const note = Generator.valueToCode(block, 'NOTE', Generator.ORDER_NONE) || null;
80+
const time = Generator.valueToCode(block, 'TIME', Generator.ORDER_NONE) || null;
81+
return `ev3_beep_note(${note}, ${time})\n`;
82+
};
83+
84+
return Generator;
85+
}

src/lib/ruby-generator/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Text2SpeechBlocks from './text2speech.js';
2323
import TranslateBlocks from './translate.js';
2424
import MakeyMakeyBlocks from './makeymakey.js';
2525
import MicrobitBlocks from './microbit.js';
26+
import BoostBlocks from './boost.js';
27+
import EV3Blocks from './ev3.js'
2628

2729
const SCALAR_TYPE = '';
2830
const LIST_TYPE = 'list';
@@ -448,5 +450,7 @@ Text2SpeechBlocks(RubyGenerator);
448450
TranslateBlocks(RubyGenerator);
449451
MakeyMakeyBlocks(RubyGenerator);
450452
MicrobitBlocks(RubyGenerator);
453+
BoostBlocks(RubyGenerator);
454+
EV3Blocks(RubyGenerator);
451455

452456
export default RubyGenerator;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* global Opal */
2+
import _ from 'lodash';
3+
4+
const RotationStyle = [
5+
'left-right',
6+
'don\'t rotate',
7+
'all around'
8+
];
9+
10+
const Ev3MotorMenu = ['A', 'B', 'C', 'D'];
11+
12+
/**
13+
* EV3 converter
14+
*/
15+
const EV3Converter = {
16+
// eslint-disable-next-line no-unused-vars
17+
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
18+
let block;
19+
if ((this._isSelf(receiver) || receiver === Opal.nil) && !rubyBlock) {
20+
switch (name) {
21+
case 'ev3_beep_note':
22+
if (args.length === 2 && this._isNumberOrBlock(args[0]) && this._isNumberOrBlock(args[1])) {
23+
block = this._createBlock('ev3_beep', 'statement');
24+
this._addNoteInput(block, 'NOTE', args[0], 60);
25+
this._addNumberInput(block, 'TIME', 'math_number', args[1], 0.5);
26+
}
27+
break;
28+
}
29+
}
30+
return block;
31+
}
32+
};
33+
34+
export default EV3Converter;

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import OperatorsConverter from './operators';
1818
import VariablesConverter from './variables';
1919
import MyBlocksConverter from './my-blocks';
2020
import MusicConverter from './music';
21+
import EV3Converter from './ev3';
2122

2223
/**
2324
* Class for a block converter that translates ruby code into the blocks.
@@ -35,7 +36,8 @@ class RubyToBlocksConverter {
3536
OperatorsConverter,
3637
VariablesConverter,
3738
MyBlocksConverter,
38-
MusicConverter
39+
MusicConverter,
40+
EV3Converter
3941
];
4042
this.reset();
4143
}
@@ -462,6 +464,22 @@ class RubyToBlocksConverter {
462464
this._addInput(block, name, this._createNumberBlock(opcode, inputValue), shadowBlock);
463465
}
464466

467+
_addNoteInput (block, name, inputValue, shadowValue) {
468+
let shadowBlock;
469+
let opcode = 'note';
470+
if (!this._isNumber(inputValue)) {
471+
shadowBlock = this._createNoteBlock(opcode, shadowValue);
472+
}
473+
this._addInput(block, name, this._createNoteBlock(opcode, inputValue), shadowBlock);
474+
}
475+
476+
_createNoteBlock (opcode, value) {
477+
if (this._isNumber(value) || value === '') {
478+
return this._createFieldBlock(opcode, 'NOTE', value.toString());
479+
}
480+
return value;
481+
}
482+
465483
_addTextInput (block, name, inputValue, shadowValue) {
466484
let shadowBlock;
467485
if (!this._isString(inputValue)) {

0 commit comments

Comments
 (0)