Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/ruby-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import MakeyMakeyBlocks from './makeymakey.js';
import MicrobitBlocks from './microbit.js';
import BoostBlocks from './boost.js';
import EV3Blocks from './ev3.js'
import WeDo2Blocks from './wedo2.js';

const SCALAR_TYPE = '';
const LIST_TYPE = 'list';
Expand Down Expand Up @@ -452,5 +453,6 @@ MakeyMakeyBlocks(RubyGenerator);
MicrobitBlocks(RubyGenerator);
BoostBlocks(RubyGenerator);
EV3Blocks(RubyGenerator);
WeDo2Blocks(RubyGenerator);

export default RubyGenerator;
92 changes: 92 additions & 0 deletions src/lib/ruby-generator/wedo2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Define Ruby code generator for WeDo 2.0 Blocks
* @param {RubyGenerator} Generator The RubyGenerator
* @return {RubyGenerator} same as param.
*/
export default function (Generator) {
Generator.wedo2_menu_MOTOR_ID = function (block) {
const motor_id = Generator.quote_(Generator.getFieldValue(block, 'MOTOR_ID') || 'motor');
return [motor_id, Generator.ORDER_ATOMIC]
};
Generator.wedo2_motorOnFor = function (block) {
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
const duration = Generator.valueToCode(block, 'DURATION', Generator.ORDER_NONE) || null;
return `wedo2_turn_motor_on_for(${motor_id}, ${duration})\n`;
};

Generator.wedo2_motorOn = function (block) {
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
return `wedo2_trun_motor_on(${motor_id})\n`;
};

Generator.wedo2_motorOff = function (block) {
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
return `wedo2_trun_motor_off(${motor_id})\n`;
};

Generator.wedo2_startMotorPower = function (block) {
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
const power =Generator.valueToCode(block, 'POWER', Generator.ORDER_NONE) || null;
return `wedo2_set_motor_power(${motor_id}, ${power})\n`;
};

Generator.wedo2_menu_MOTOR_DIRECTION = function (block) {
const motor_direction = Generator.quote_(Generator.getFieldValue(block, 'MOTOR_DIRECTION') || 'this way');
return [motor_direction, Generator.ORDER_ATOMIC];
};

Generator.wedo2_setMotorDirection = function (block) {
const motor_id = Generator.valueToCode(block, 'MOTOR_ID', Generator.ORDER_NONE) || null;
const motor_direction = Generator.valueToCode(block, 'MOTOR_DIRECTION', Generator.ORDER_NONE) || null;
return `wedo2_set_motor_direction(${motor_id}, ${motor_direction})\n`;
};

Generator.wedo2_setLightHue = function (block) {
const hue = Generator.valueToCode(block, 'HUE', Generator.ORDER_NONE) || null;
return `wedo2_set_light_color(${hue})\n`;
};

Generator.wedo2_menu_OP = function (block) {
const op = Generator.quote_(Generator.getFieldValue(block, 'OP') || '<');
return [op, Generator.ORDER_ATOMIC];
};

Generator.wedo2_whenDistance = function (block) {
block.isStatement = true;
const op = Generator.valueToCode(block, 'OP', Generator.ORDER_NONE) || null;
const reference = Generator.valueToCode(block, 'REFERENCE', Generator.ORDER_NONE) || null;
return `wedo2_when_distance(${op}, ${reference}) do\n`;
};

Generator.wedo2_menu_TILT_DIRECTION_ANY = function (block) {
const tilt_direction_any = Generator.quote_(Generator.getFieldValue(block, 'TILT_DIRECTION_ANY') || 'any');
return [tilt_direction_any, Generator.ORDER_ATOMIC];
};

Generator.wedo2_whenTilted = function (block) {
block.isStatement = true;
const tilt_direction_any = Generator.valueToCode(block, 'TILT_DIRECTION_ANY', Generator.ORDER_NONE) || null;
return `wedo2_when_tilted(${tilt_direction_any}) do\n`;
};

Generator.wedo2_getDistance = function (block) {
return [`wedo2_distance`, Generator.ORDER_ATOMIC];
};

Generator.wedo2_isTilted = function (block) {
const tilt_direction_any = Generator.valueToCode(block, 'TILT_DIRECTION_ANY', Generator.ORDER_NONE) || null;
return `wedo2_tilted(${tilt_direction_any})\n`;
};

Generator.wedo2_menu_TILT_DIRECTION = function (block) {
const tilt_direction = Generator.quote_(Generator.getFieldValue(block, 'TILT_DIRECTION') || 'up');
return [tilt_direction, Generator.ORDER_ATOMIC];
};

Generator.wedo2_getTiltAngle = function (block) {
const tilt_direction = Generator.valueToCode(block, 'TILT_DIRECTION', Generator.ORDER_NONE) || null;
return [`wedo2_tilt_angle(${tilt_direction})`, Generator.ORDER_ATOMIC];
};

return Generator;
}
4 changes: 3 additions & 1 deletion src/lib/ruby-to-blocks-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import VariablesConverter from './variables';
import MyBlocksConverter from './my-blocks';
import MusicConverter from './music';
import EV3Converter from './ev3';
import Wedo2Converter from './wedo2';

/**
* Class for a block converter that translates ruby code into the blocks.
Expand All @@ -37,7 +38,8 @@ class RubyToBlocksConverter {
VariablesConverter,
MyBlocksConverter,
MusicConverter,
EV3Converter
EV3Converter,
Wedo2Converter
];
this.reset();
}
Expand Down
31 changes: 31 additions & 0 deletions src/lib/ruby-to-blocks-converter/wedo2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global Opal */
import _ from 'lodash';

const RotationStyle = [
'left-right',
'don\'t rotate',
'all around'
];

/**
* Wedo2 converter
*/
const Wedo2Converter = {
// eslint-disable-next-line no-unused-vars
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
let block;
if ((this._isSelf(receiver) || receiver === Opal.nil) && !rubyBlock) {
switch (name) {
case 'wedo2_set_light_color':
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
block = this._createBlock('wedo2_setLightHue', 'statement');
this._addNumberInput(block, 'HUE', 'math_number', args[0], 50);
}
break;
}
}
return block;
}
};

export default Wedo2Converter;