Skip to content

Commit 8ebaba3

Browse files
authored
Merge pull request #208 from Tomoya0302/rubytoblock
EV3のbeepを追加
2 parents 182db15 + 002b5a0 commit 8ebaba3

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import TranslateBlocks from './translate.js';
2424
import MakeyMakeyBlocks from './makeymakey.js';
2525
import MicrobitBlocks from './microbit.js';
2626
import BoostBlocks from './boost.js';
27+
import EV3Blocks from './ev3.js'
2728

2829
const SCALAR_TYPE = '';
2930
const LIST_TYPE = 'list';
@@ -450,5 +451,6 @@ TranslateBlocks(RubyGenerator);
450451
MakeyMakeyBlocks(RubyGenerator);
451452
MicrobitBlocks(RubyGenerator);
452453
BoostBlocks(RubyGenerator);
454+
EV3Blocks(RubyGenerator);
453455

454456
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)