|
1 | 1 | /* global Opal */
|
2 | 2 | import _ from 'lodash';
|
3 | 3 |
|
| 4 | +const MicroBit = 'microbit'; |
4 | 5 | /**
|
5 | 6 | * MicroBit converter
|
6 | 7 | */
|
7 | 8 | const MicroBitConverter = {
|
| 9 | + // eslint-disable-next-line no-unused-vars |
| 10 | + onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) { |
| 11 | + let block; |
| 12 | + if ((this._isSelf(receiver) || receiver === Opal.nil) && |
| 13 | + name === 'when' && |
| 14 | + args.length >= 1 && args[0].type === 'sym' && |
| 15 | + rubyBlockArgs && rubyBlockArgs.length === 0 && |
| 16 | + rubyBlock) { |
| 17 | + switch (args[0].value) { |
| 18 | + case 'microbit_button_pressed': |
| 19 | + if (args.length === 2 && this._isString(args[1])) { |
| 20 | + block = this._createBlock('microbit_whenButtonPressed', 'hat'); |
| 21 | + this._addFieldInput( |
| 22 | + block, 'BTN', 'microbit_menu_buttons', 'buttons', |
| 23 | + args[1], 'A' |
| 24 | + ); |
| 25 | + this._setParent(rubyBlock, block); |
| 26 | + } |
| 27 | + break; |
| 28 | + case 'microbit_gesture': |
| 29 | + if (args.length === 2 && this._isString(args[1])) { |
| 30 | + block = this._createBlock('microbit_whenGesture', 'hat'); |
| 31 | + this._addFieldInput( |
| 32 | + block, 'GESTURE', 'microbit_menu_gestures', 'gestures', |
| 33 | + args[1], 'moved' |
| 34 | + ); |
| 35 | + this._setParent(rubyBlock, block); |
| 36 | + } |
| 37 | + break; |
| 38 | + case 'microbit_tilted': |
| 39 | + if (args.length === 2 && this._isString(args[1])) { |
| 40 | + block = this._createBlock('microbit_whenTilted', 'hat'); |
| 41 | + this._addFieldInput( |
| 42 | + block, 'DIRECTION', 'microbit_menu_tiltDirectionAny', 'tiltDirectionAny', |
| 43 | + args[1], 'any' |
| 44 | + ); |
| 45 | + this._setParent(rubyBlock, block); |
| 46 | + } |
| 47 | + break; |
| 48 | + case 'microbit_pin_connected': |
| 49 | + if (args.length === 2 && this._isNumber(args[1])) { |
| 50 | + block = this._createBlock('microbit_whenPinConnected', 'hat'); |
| 51 | + this._addFieldInput( |
| 52 | + block, 'PIN', 'microbit_menu_touchPins', 'touchPins', |
| 53 | + args[1], '0' |
| 54 | + ); |
| 55 | + this._setParent(rubyBlock, block); |
| 56 | + } |
| 57 | + break; |
| 58 | + } |
| 59 | + } else if (this._isSelf(receiver) || receiver === Opal.nil) { |
| 60 | + switch (name) { |
| 61 | + case 'microbit': |
| 62 | + if (args.length === 0) { |
| 63 | + block = this._createRubyExpressionBlock(MicroBit); |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + } else if (this._equalRubyExpression(receiver, MicroBit)) { |
| 68 | + switch (name) { |
| 69 | + case 'button_pressed?': |
| 70 | + if (args.length === 1 && this._isString(args[0])) { |
| 71 | + block = this._changeBlock(receiver, 'microbit_isButtonPressed', 'value_boolean'); |
| 72 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 73 | + delete receiver.inputs.EXPRESSION; |
| 74 | + |
| 75 | + this._addFieldInput( |
| 76 | + block, 'BTN', 'microbit_menu_buttons', 'buttons', |
| 77 | + args[0], 'A' |
| 78 | + ); |
| 79 | + } |
| 80 | + break; |
| 81 | + case 'display': |
| 82 | + if (args.length === 5 && args.every((i) => { return this._isString(i); })) { |
| 83 | + block = this._changeBlock(receiver, 'microbit_displaySymbol', 'statement'); |
| 84 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 85 | + delete receiver.inputs.EXPRESSION; |
| 86 | + |
| 87 | + var matrix = ''; |
| 88 | + for (const arg of args) { |
| 89 | + matrix += arg; |
| 90 | + } |
| 91 | + matrix = matrix.replace(/[1-9]/g, '1').replace(/[^1-9]/g, '0'); |
| 92 | + this._addFieldInput( |
| 93 | + block, 'MATRIX', 'matrix', 'MATRIX', |
| 94 | + matrix, null |
| 95 | + ); |
| 96 | + } |
| 97 | + break; |
| 98 | + case 'display_text': |
| 99 | + if (args.length === 1 && this._isString(args[0])) { |
| 100 | + block = this._changeBlock(receiver, 'microbit_displayText', 'statement'); |
| 101 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 102 | + delete receiver.inputs.EXPRESSION; |
| 103 | + |
| 104 | + this._addTextInput(block, 'TEXT', args[0], 'Hello!'); |
| 105 | + } |
| 106 | + break; |
| 107 | + case 'clear_display': |
| 108 | + if (args.length === 0) { |
| 109 | + block = this._changeBlock(receiver, 'microbit_displayClear', 'statement'); |
| 110 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 111 | + delete receiver.inputs.EXPRESSION; |
| 112 | + } |
| 113 | + break; |
| 114 | + case 'tilted?': |
| 115 | + if (args.length === 1 && this._isString(args[0])) { |
| 116 | + block = this._changeBlock(receiver, 'microbit_isTilted', 'value_boolean'); |
| 117 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 118 | + delete receiver.inputs.EXPRESSION; |
| 119 | + |
| 120 | + this._addFieldInput( |
| 121 | + block, 'DIRECTION', 'microbit_menu_tiltDirectionAny', 'tiltDirectionAny', |
| 122 | + args[0], 'any' |
| 123 | + ); |
| 124 | + } |
| 125 | + break; |
| 126 | + case 'tilt_angle': |
| 127 | + if (args.length === 1 && this._isString(args[0])) { |
| 128 | + block = this._changeBlock(receiver, 'microbit_getTiltAngle', 'value'); |
| 129 | + delete this._context.blocks[receiver.inputs.EXPRESSION.block]; |
| 130 | + delete receiver.inputs.EXPRESSION; |
| 131 | + |
| 132 | + this._addFieldInput( |
| 133 | + block, 'DIRECTION', 'microbit_menu_tiltDirection', 'tiltDirection', |
| 134 | + args[0], 'front' |
| 135 | + ); |
| 136 | + } |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + return block; |
| 141 | + } |
8 | 142 | };
|
9 | 143 |
|
10 | 144 | export default MicroBitConverter;
|
0 commit comments