|
| 1 | +/** |
| 2 | + * Define Ruby code generator for micro:bit Blocks |
| 3 | + * @param {RubyGenerator} Generator The RubyGenerator |
| 4 | + * @return {RubyGenerator} same as param. |
| 5 | + */ |
| 6 | +export default function (Generator) { |
| 7 | + Generator.microbit_whenButtonPressed = function (block) { |
| 8 | + block.isStatement = true; |
| 9 | + const btn = Generator.valueToCode(block, 'BTN', Generator.ORDER_NONE) || null; |
| 10 | + return `${Generator.spriteName()}.when(:microbit_button_pressed, ${btn}) do\n`; |
| 11 | + }; |
| 12 | + |
| 13 | + Generator.microbit_isButtonPressed = function (block) { |
| 14 | + const btn = Generator.valueToCode(block, 'BTN', Generator.ORDER_NONE) || null; |
| 15 | + return `microbit.button_pressed?(${btn})\n`; |
| 16 | + }; |
| 17 | + |
| 18 | + Generator.microbit_whenGesture = function (block) { |
| 19 | + block.isStatement = true; |
| 20 | + const gesture = Generator.valueToCode(block, 'GESTURE', Generator.ORDER_NONE) || null; |
| 21 | + return `${Generator.spriteName()}.when(:microbit_gesture, ${gesture}) do\n`; |
| 22 | + }; |
| 23 | + |
| 24 | + Generator.microbit_displaySymbol = function (block) { |
| 25 | + let matrix = Generator.valueToCode(block, 'MATRIX', Generator.ORDER_NONE) || null; |
| 26 | + matrix = Generator.prefixLines(matrix, Generator.INDENT); |
| 27 | + return `microbit.display(\n${matrix}\n)\n`; |
| 28 | + }; |
| 29 | + |
| 30 | + Generator.microbit_displayText = function (block) { |
| 31 | + const text = Generator.valueToCode(block, 'TEXT', Generator.ORDER_NONE) || null; |
| 32 | + return `microbit.display_text(${text})\n`; |
| 33 | + }; |
| 34 | + |
| 35 | + Generator.microbit_displayClear = function () { |
| 36 | + return `microbit.clear_display\n`; |
| 37 | + }; |
| 38 | + |
| 39 | + Generator.microbit_whenTilted = function (block) { |
| 40 | + block.isStatement = true; |
| 41 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 42 | + return `${Generator.spriteName()}.when(:microbit_tilted, ${direction}) do\n`; |
| 43 | + }; |
| 44 | + |
| 45 | + Generator.microbit_isTilted = function (block) { |
| 46 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 47 | + return `microbit.tilted?(${direction})\n`; |
| 48 | + }; |
| 49 | + |
| 50 | + Generator.microbit_getTiltAngle = function (block) { |
| 51 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 52 | + return `microbit.tilt_angle(${direction})\n`; |
| 53 | + }; |
| 54 | + |
| 55 | + Generator.microbit_whenPinConnected = function (block) { |
| 56 | + block.isStatement = true; |
| 57 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 58 | + return `${Generator.spriteName()}.when(:microbit_pin_connected, ${pin}) do\n`; |
| 59 | + }; |
| 60 | + |
| 61 | + Generator.microbit_menu_buttons = function (block) { |
| 62 | + const buttons = Generator.quote_(Generator.getFieldValue(block, 'buttons') || 'A'); |
| 63 | + return [buttons, Generator.ORDER_ATOMIC]; |
| 64 | + }; |
| 65 | + |
| 66 | + Generator.microbit_menu_gestures = function (block) { |
| 67 | + const gestures = Generator.quote_(Generator.getFieldValue(block, 'gestures') || 'moved'); |
| 68 | + return [gestures, Generator.ORDER_ATOMIC]; |
| 69 | + }; |
| 70 | + |
| 71 | + Generator.microbit_menu_tiltDirectionAny = function (block) { |
| 72 | + const tiltDirectionAny = Generator.quote_(Generator.getFieldValue(block, 'tiltDirectionAny') || 'any'); |
| 73 | + return [tiltDirectionAny, Generator.ORDER_ATOMIC]; |
| 74 | + }; |
| 75 | + |
| 76 | + Generator.microbit_menu_tiltDirection = function (block) { |
| 77 | + const tiltDirection = Generator.quote_(Generator.getFieldValue(block, 'tiltDirection') || 'front'); |
| 78 | + return [tiltDirection, Generator.ORDER_ATOMIC]; |
| 79 | + }; |
| 80 | + |
| 81 | + Generator.microbit_menu_touchPins = function (block) { |
| 82 | + const touchPins = Generator.getFieldValue(block, 'touchPins') || '0'; |
| 83 | + return [touchPins, Generator.ORDER_ATOMIC]; |
| 84 | + }; |
| 85 | + |
| 86 | + Generator.matrix = function (block) { |
| 87 | + let matrix = Generator.getFieldValue(block, 'MATRIX') || '0000000000000000000000000'; |
| 88 | + matrix = matrix.replace(/0/g, ' '); |
| 89 | + matrix = matrix.match(/.{5}/g).map(s => Generator.quote_(`${s}:`)); |
| 90 | + return [matrix.join('\n'), Generator.ORDER_ATOMIC]; |
| 91 | + }; |
| 92 | + |
| 93 | + return Generator; |
| 94 | +} |
0 commit comments