|
| 1 | +/** |
| 2 | + * Define Ruby code generator for Smalrubot S1 Blocks |
| 3 | + * @param {RubyGenerator} Generator The RubyGenerator |
| 4 | + * @return {RubyGenerator} same as param. |
| 5 | + */ |
| 6 | +export default function (Generator) { |
| 7 | + Generator.smalrubotS1_action = function (block) { |
| 8 | + const action = Generator.quote_( |
| 9 | + Generator.getFieldValue(block, 'ACTION', Generator.ORDER_NONE) || 'forward' |
| 10 | + ); |
| 11 | + return `smalrubot_s1.action(${action})\n`; |
| 12 | + }; |
| 13 | + |
| 14 | + Generator.smalrubotS1_actionAndStopAfter = function (block) { |
| 15 | + const action = Generator.quote_( |
| 16 | + Generator.getFieldValue(block, 'ACTION', Generator.ORDER_NONE) || 'forward' |
| 17 | + ); |
| 18 | + const secs = Generator.valueToCode(block, 'SECS', Generator.ORDER_NONE) || 1; |
| 19 | + return `smalrubot_s1.action(${action}, ${secs})\n`; |
| 20 | + }; |
| 21 | + |
| 22 | + Generator.smalrubotS1_bendArm = function (block) { |
| 23 | + const degree = Generator.valueToCode(block, 'DEGREE', Generator.ORDER_NONE) || 90; |
| 24 | + const secs = Generator.valueToCode(block, 'SECS', Generator.ORDER_NONE) || 1; |
| 25 | + return `smalrubot_s1.bend_arm(${degree}, ${secs})\n`; |
| 26 | + }; |
| 27 | + |
| 28 | + Generator.smalrubotS1_getSensorValue = function (block) { |
| 29 | + const position = Generator.quote_( |
| 30 | + Generator.getFieldValue(block, 'POSITION', Generator.ORDER_NONE) || 'left' |
| 31 | + ); |
| 32 | + return [`smalrubot_s1.sensor_value(${position})`, Generator.ORDER_ATOMIC]; |
| 33 | + }; |
| 34 | + |
| 35 | + Generator.smalrubotS1_turnLedOn = function (block) { |
| 36 | + const position = Generator.quote_( |
| 37 | + Generator.getFieldValue(block, 'POSITION', Generator.ORDER_NONE) || 'left' |
| 38 | + ); |
| 39 | + return `smalrubot_s1.led(${position}, true)\n`; |
| 40 | + }; |
| 41 | + |
| 42 | + Generator.smalrubotS1_turnLedOff = function (block) { |
| 43 | + const position = Generator.quote_( |
| 44 | + Generator.getFieldValue(block, 'POSITION', Generator.ORDER_NONE) || 'left' |
| 45 | + ); |
| 46 | + return `smalrubot_s1.led(${position}, false)\n`; |
| 47 | + }; |
| 48 | + |
| 49 | + Generator.smalrubotS1_getMotorSpeed = function (block) { |
| 50 | + const position = Generator.quote_( |
| 51 | + Generator.getFieldValue(block, 'POSITION', Generator.ORDER_NONE) || 'left' |
| 52 | + ); |
| 53 | + return [`smalrubot_s1.get_motor_speed(${position})`, Generator.ORDER_ATOMIC]; |
| 54 | + }; |
| 55 | + |
| 56 | + Generator.smalrubotS1_setMotorSpeed = function (block) { |
| 57 | + const position = Generator.quote_( |
| 58 | + Generator.getFieldValue(block, 'POSITION', Generator.ORDER_NONE) || 'left' |
| 59 | + ); |
| 60 | + const speed = Generator.valueToCode(block, 'SPEED', Generator.ORDER_NONE) || 1; |
| 61 | + return `smalrubot_s1.set_motor_speed(${position}, ${speed})\n`; |
| 62 | + }; |
| 63 | + |
| 64 | + return Generator; |
| 65 | +} |
0 commit comments