|
| 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 | +} |
0 commit comments