Skip to content

Commit 6502d1d

Browse files
authored
Merge pull request #205 from stonesaw/wedo
WeDoを追加しました
2 parents 8ebaba3 + 6091891 commit 6502d1d

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

src/lib/ruby-generator/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import MakeyMakeyBlocks from './makeymakey.js';
2525
import MicrobitBlocks from './microbit.js';
2626
import BoostBlocks from './boost.js';
2727
import EV3Blocks from './ev3.js'
28+
import WeDo2Blocks from './wedo2.js';
2829

2930
const SCALAR_TYPE = '';
3031
const LIST_TYPE = 'list';
@@ -452,5 +453,6 @@ MakeyMakeyBlocks(RubyGenerator);
452453
MicrobitBlocks(RubyGenerator);
453454
BoostBlocks(RubyGenerator);
454455
EV3Blocks(RubyGenerator);
456+
WeDo2Blocks(RubyGenerator);
455457

456458
export default RubyGenerator;

src/lib/ruby-generator/wedo2.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Define Ruby code generator for WeDo 2.0 Blocks
3+
* @param {RubyGenerator} Generator The RubyGenerator
4+
* @return {RubyGenerator} same as param.
5+
*/
6+
export default function (Generator) {
7+
Generator.wedo2_menu_MOTOR_ID = function (block) {
8+
const motor_id = Generator.quote_(Generator.getFieldValue(block, 'MOTOR_ID') || 'motor');
9+
return [motor_id, Generator.ORDER_ATOMIC]
10+
};
11+
Generator.wedo2_motorOnFor = function (block) {
12+
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
13+
const duration = Generator.valueToCode(block, 'DURATION', Generator.ORDER_NONE) || null;
14+
return `wedo2_turn_motor_on_for(${motor_id}, ${duration})\n`;
15+
};
16+
17+
Generator.wedo2_motorOn = function (block) {
18+
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
19+
return `wedo2_trun_motor_on(${motor_id})\n`;
20+
};
21+
22+
Generator.wedo2_motorOff = function (block) {
23+
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
24+
return `wedo2_trun_motor_off(${motor_id})\n`;
25+
};
26+
27+
Generator.wedo2_startMotorPower = function (block) {
28+
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
29+
const power =Generator.valueToCode(block, 'POWER', Generator.ORDER_NONE) || null;
30+
return `wedo2_set_motor_power(${motor_id}, ${power})\n`;
31+
};
32+
33+
Generator.wedo2_menu_MOTOR_DIRECTION = function (block) {
34+
const motor_direction = Generator.quote_(Generator.getFieldValue(block, 'MOTOR_DIRECTION') || 'this way');
35+
return [motor_direction, Generator.ORDER_ATOMIC];
36+
};
37+
38+
Generator.wedo2_setMotorDirection = function (block) {
39+
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
40+
const motor_direction = Generator.valueToCode(block, 'MOTOR_DIRECTION', Generator.ORDER_NONE) || null;
41+
return `wedo2_set_motor_direction(${motor_id}, ${motor_direction})\n`;
42+
};
43+
44+
Generator.wedo2_setLightHue = function (block) {
45+
const hue = Generator.valueToCode(block, 'HUE', Generator.ORDER_NONE) || null;
46+
return `wedo2_set_light_color(${hue})\n`;
47+
};
48+
49+
Generator.wedo2_menu_OP = function (block) {
50+
const op = Generator.quote_(Generator.getFieldValue(block, 'OP') || '<');
51+
return [op, Generator.ORDER_ATOMIC];
52+
};
53+
54+
Generator.wedo2_whenDistance = function (block) {
55+
block.isStatement = true;
56+
const op = Generator.valueToCode(block, 'OP', Generator.ORDER_NONE) || null;
57+
const reference = Generator.valueToCode(block, 'REFERENCE', Generator.ORDER_NONE) || null;
58+
return `wedo2_when_distance(${op}, ${reference}) do\n`;
59+
};
60+
61+
Generator.wedo2_menu_TILT_DIRECTION_ANY = function (block) {
62+
const tilt_direction_any = Generator.quote_(Generator.getFieldValue(block, 'TILT_DIRECTION_ANY') || 'any');
63+
return [tilt_direction_any, Generator.ORDER_ATOMIC];
64+
};
65+
66+
Generator.wedo2_whenTilted = function (block) {
67+
block.isStatement = true;
68+
const tilt_direction_any = Generator.valueToCode(block, 'TILT_DIRECTION_ANY', Generator.ORDER_NONE) || null;
69+
return `wedo2_when_tilted(${tilt_direction_any}) do\n`;
70+
};
71+
72+
Generator.wedo2_getDistance = function (block) {
73+
return [`wedo2_distance`, Generator.ORDER_ATOMIC];
74+
};
75+
76+
Generator.wedo2_isTilted = function (block) {
77+
const tilt_direction_any = Generator.valueToCode(block, 'TILT_DIRECTION_ANY', Generator.ORDER_NONE) || null;
78+
return `wedo2_tilted(${tilt_direction_any})\n`;
79+
};
80+
81+
Generator.wedo2_menu_TILT_DIRECTION = function (block) {
82+
const tilt_direction = Generator.quote_(Generator.getFieldValue(block, 'TILT_DIRECTION') || 'up');
83+
return [tilt_direction, Generator.ORDER_ATOMIC];
84+
};
85+
86+
Generator.wedo2_getTiltAngle = function (block) {
87+
const tilt_direction = Generator.valueToCode(block, 'TILT_DIRECTION', Generator.ORDER_NONE) || null;
88+
return [`wedo2_tilt_angle(${tilt_direction})`, Generator.ORDER_ATOMIC];
89+
};
90+
91+
return Generator;
92+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import VariablesConverter from './variables';
1919
import MyBlocksConverter from './my-blocks';
2020
import MusicConverter from './music';
2121
import EV3Converter from './ev3';
22+
import Wedo2Converter from './wedo2';
2223

2324
/**
2425
* Class for a block converter that translates ruby code into the blocks.
@@ -37,7 +38,8 @@ class RubyToBlocksConverter {
3738
VariablesConverter,
3839
MyBlocksConverter,
3940
MusicConverter,
40-
EV3Converter
41+
EV3Converter,
42+
Wedo2Converter
4143
];
4244
this.reset();
4345
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* global Opal */
2+
import _ from 'lodash';
3+
4+
const RotationStyle = [
5+
'left-right',
6+
'don\'t rotate',
7+
'all around'
8+
];
9+
10+
/**
11+
* Wedo2 converter
12+
*/
13+
const Wedo2Converter = {
14+
// eslint-disable-next-line no-unused-vars
15+
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
16+
let block;
17+
if ((this._isSelf(receiver) || receiver === Opal.nil) && !rubyBlock) {
18+
switch (name) {
19+
case 'wedo2_set_light_color':
20+
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
21+
block = this._createBlock('wedo2_setLightHue', 'statement');
22+
this._addNumberInput(block, 'HUE', 'math_number', args[0], 50);
23+
}
24+
break;
25+
}
26+
}
27+
return block;
28+
}
29+
};
30+
31+
export default Wedo2Converter;

0 commit comments

Comments
 (0)