|
| 1 | +/** |
| 2 | + * Define Ruby code generator for micro:bit MORE Blocks |
| 3 | + * @param {RubyGenerator} Generator The RubyGenerator |
| 4 | + * @return {RubyGenerator} same as param. |
| 5 | + */ |
| 6 | +export default function (Generator) { |
| 7 | + Generator.microbitMore_whenButtonPressed = function (block) { |
| 8 | + block.isStatement = true; |
| 9 | + const btn = Generator.valueToCode(block, 'BTN', Generator.ORDER_NONE) || null; |
| 10 | + return `${Generator.spriteName()}.when(:mbit_more_button_pressed, ${btn}) do\n`; |
| 11 | + }; |
| 12 | + |
| 13 | + Generator.microbitMore_isButtonPressed = function (block) { |
| 14 | + const btn = Generator.valueToCode(block, 'BTN', Generator.ORDER_NONE) || null; |
| 15 | + return `mbit_more.button_pressed?(${btn})\n`; |
| 16 | + }; |
| 17 | + |
| 18 | + Generator.microbitMore_whenGesture = function (block) { |
| 19 | + block.isStatement = true; |
| 20 | + const gesture = Generator.valueToCode(block, 'GESTURE', Generator.ORDER_NONE) || null; |
| 21 | + return `${Generator.spriteName()}.when(:mbit_more_gesture, ${gesture}) do\n`; |
| 22 | + }; |
| 23 | + |
| 24 | + Generator.microbitMore_displaySymbol = function (block) { |
| 25 | + let matrix = Generator.valueToCode(block, 'MATRIX', Generator.ORDER_NONE) || null; |
| 26 | + matrix = Generator.prefixLines(matrix, Generator.INDENT); |
| 27 | + return `mbit_more.display(\n${matrix}\n)\n`; |
| 28 | + }; |
| 29 | + |
| 30 | + Generator.microbitMore_displayText = function (block) { |
| 31 | + const text = Generator.valueToCode(block, 'TEXT', Generator.ORDER_NONE) || null; |
| 32 | + return `mbit_more.display_text(${text})\n`; |
| 33 | + }; |
| 34 | + |
| 35 | + Generator.microbitMore_displayClear = function () { |
| 36 | + return `mbit_more.clear_display\n`; |
| 37 | + }; |
| 38 | + |
| 39 | + Generator.microbitMore_whenTilted = function (block) { |
| 40 | + block.isStatement = true; |
| 41 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 42 | + return `${Generator.spriteName()}.when(:mbit_more_tilted, ${direction}) do\n`; |
| 43 | + }; |
| 44 | + |
| 45 | + Generator.microbitMore_isTilted = function (block) { |
| 46 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 47 | + return [`mbit_more.tilted?(${direction})`, Generator.ORDER_ATOMIC]; |
| 48 | + }; |
| 49 | + |
| 50 | + Generator.microbitMore_getTiltAngle = function (block) { |
| 51 | + const direction = Generator.valueToCode(block, 'DIRECTION', Generator.ORDER_NONE) || null; |
| 52 | + return [`mbit_more.tilt_angle(${direction})`, Generator.ORDER_ATOMIC]; |
| 53 | + }; |
| 54 | + |
| 55 | + Generator.microbitMore_whenPinConnected = function (block) { |
| 56 | + block.isStatement = true; |
| 57 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 58 | + return `${Generator.spriteName()}.when(:mbit_more_pin_connected, ${pin}) do\n`; |
| 59 | + }; |
| 60 | + |
| 61 | + Generator.microbitMore_isPinConnected = function (block) { |
| 62 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 63 | + return `mbit_more.pin_connected?(${pin})\n`; |
| 64 | + }; |
| 65 | + |
| 66 | + Generator.microbitMore_getLightLevel = function () { |
| 67 | + return `mbit_more.light_level\n`; |
| 68 | + }; |
| 69 | + |
| 70 | + Generator.microbitMore_getTemperature = function () { |
| 71 | + return `mbit_more.temperature\n`; |
| 72 | + }; |
| 73 | + |
| 74 | + Generator.microbitMore_getCompassHeading = function () { |
| 75 | + return `mbit_more.compass_heading\n`; |
| 76 | + }; |
| 77 | + |
| 78 | + Generator.microbitMore_getPitch = function () { |
| 79 | + return `mbit_more.pitch\n`; |
| 80 | + }; |
| 81 | + |
| 82 | + Generator.microbitMore_getRoll = function () { |
| 83 | + return `mbit_more.roll\n`; |
| 84 | + }; |
| 85 | + |
| 86 | + Generator.microbitMore_getMagneticForce = function (block) { |
| 87 | + const axis = Generator.valueToCode(block, 'AXIS', Generator.ORDER_NONE) || null; |
| 88 | + return `mbit_more.get_magnetic_force(${axis})\n`; |
| 89 | + }; |
| 90 | + |
| 91 | + Generator.microbitMore_getAcceleration = function (block) { |
| 92 | + const axis = Generator.valueToCode(block, 'AXIS', Generator.ORDER_NONE) || null; |
| 93 | + return `mbit_more.get_acceleration(${axis})\n`; |
| 94 | + }; |
| 95 | + |
| 96 | + Generator.microbitMore_getAnalogValue = function (block) { |
| 97 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 98 | + return `mbit_more.get_analog_value(${pin})\n`; |
| 99 | + }; |
| 100 | + |
| 101 | + Generator.microbitMore_getDigitalValue = function (block) { |
| 102 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 103 | + return `mbit_more.get_digital_value(${pin})\n`; |
| 104 | + }; |
| 105 | + |
| 106 | + Generator.microbitMore_setPinMode = function (block) { |
| 107 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 108 | + const mode = Generator.quote_(Generator.getFieldValue(block, 'MODE')) || null; |
| 109 | + return `mbit_more.set_pin_mode(${pin}, ${mode})\n`; |
| 110 | + }; |
| 111 | + |
| 112 | + Generator.microbitMore_setOutput = function (block) { |
| 113 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 114 | + const level = Generator.valueToCode(block, 'LEVEL', Generator.ORDER_NONE) || null; |
| 115 | + return `mbit_more.set_output(${pin}, ${level})\n`; |
| 116 | + }; |
| 117 | + |
| 118 | + Generator.microbitMore_setPWM = function (block) { |
| 119 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 120 | + const level = Generator.valueToCode(block, 'LEVEL', Generator.ORDER_NONE) || null; |
| 121 | + return `mbit_more.set_pwm(${pin}, ${level})\n`; |
| 122 | + }; |
| 123 | + |
| 124 | + Generator.microbitMore_setServo = function (block) { |
| 125 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 126 | + const angle = Generator.valueToCode(block, 'ANGLE', Generator.ORDER_NONE) || null; |
| 127 | + const range = Generator.valueToCode(block, 'RANGE', Generator.ORDER_NONE) || 2000; |
| 128 | + const center = Generator.valueToCode(block, 'CENTER', Generator.ORDER_NONE) || 1500; |
| 129 | + return `mbit_more.set_servo(${pin}, ${angle}, ${range}, ${center})\n`; |
| 130 | + }; |
| 131 | + |
| 132 | + Generator.microbitMore_setPinEventType = function (block) { |
| 133 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 134 | + const eventType = Generator.getFieldValue(block, 'EVENT_TYPE') || null; |
| 135 | + return `mbit_more.set_pin_event_type(${pin}, ${eventType})\n`; |
| 136 | + }; |
| 137 | + |
| 138 | + Generator.microbitMore_whenPinEvent = function (block) { |
| 139 | + block.isStatement = true; |
| 140 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 141 | + const event = Generator.getFieldValue(block, 'EVENT') || null; |
| 142 | + return `${Generator.spriteName()}.when(:mbit_more_pin_event, ${pin}, ${event}) do\n`; |
| 143 | + }; |
| 144 | + |
| 145 | + Generator.microbitMore_getPinEventTimestamp = function (block) { |
| 146 | + const pin = Generator.valueToCode(block, 'PIN', Generator.ORDER_NONE) || null; |
| 147 | + const event = Generator.getFieldValue(block, 'EVENT') || null; |
| 148 | + return `mbit_more.get_pin_event_timestamp(${pin}, ${event})\n`; |
| 149 | + }; |
| 150 | + |
| 151 | + Generator.microbitMore_getSharedData = function (block) { |
| 152 | + const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_NONE) || null; |
| 153 | + return `mbit_more.get_shared_data(${index})\n`; |
| 154 | + }; |
| 155 | + |
| 156 | + Generator.microbitMore_setSharedData = function (block) { |
| 157 | + const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_NONE) || null; |
| 158 | + const value = Generator.valueToCode(block, 'VALUE', Generator.ORDER_NONE) || null; |
| 159 | + return `mbit_more.set_shared_data(${index}, ${value})\n`; |
| 160 | + }; |
| 161 | + |
| 162 | + Generator.microbitMore_whenConnectionChanged = function (block) { |
| 163 | + block.isStatement = true; |
| 164 | + const state = Generator.getFieldValue(block, 'STATE') || null; |
| 165 | + return `${Generator.spriteName()}.when(:mbit_more_connection_changed, ${state}) do\n`; |
| 166 | + }; |
| 167 | + |
| 168 | + Generator.microbitMore_menu_buttons = function (block) { |
| 169 | + const buttons = Generator.quote_(Generator.getFieldValue(block, 'buttons') || 'A'); |
| 170 | + return [buttons, Generator.ORDER_ATOMIC]; |
| 171 | + }; |
| 172 | + |
| 173 | + Generator.microbitMore_menu_gestures = function (block) { |
| 174 | + const gestures = Generator.quote_(Generator.getFieldValue(block, 'gestures') || 'moved'); |
| 175 | + return [gestures, Generator.ORDER_ATOMIC]; |
| 176 | + }; |
| 177 | + |
| 178 | + Generator.microbitMore_menu_tiltDirectionAny = function (block) { |
| 179 | + const tiltDirectionAny = Generator.quote_(Generator.getFieldValue(block, 'tiltDirectionAny') || 'any'); |
| 180 | + return [tiltDirectionAny, Generator.ORDER_ATOMIC]; |
| 181 | + }; |
| 182 | + |
| 183 | + Generator.microbitMore_menu_tiltDirection = function (block) { |
| 184 | + const tiltDirection = Generator.quote_(Generator.getFieldValue(block, 'tiltDirection') || 'front'); |
| 185 | + return [tiltDirection, Generator.ORDER_ATOMIC]; |
| 186 | + }; |
| 187 | + |
| 188 | + Generator.microbitMore_menu_touchPins = function (block) { |
| 189 | + const touchPins = Generator.getFieldValue(block, 'touchPins') || 0; |
| 190 | + return [touchPins, Generator.ORDER_ATOMIC]; |
| 191 | + }; |
| 192 | + |
| 193 | + Generator.microbitMore_menu_gpio = function (block) { |
| 194 | + const gpio = Generator.getFieldValue(block, 'gpio') || 0; |
| 195 | + return [gpio, Generator.ORDER_ATOMIC]; |
| 196 | + }; |
| 197 | + |
| 198 | + Generator.microbitMore_menu_axis = function (block) { |
| 199 | + const axis = Generator.quote_(Generator.getFieldValue(block, 'axis') || 'absolute'); |
| 200 | + return [axis, Generator.ORDER_ATOMIC]; |
| 201 | + }; |
| 202 | + |
| 203 | + Generator.microbitMore_menu_analogIn = function (block) { |
| 204 | + const analogIn = Generator.getFieldValue(block, 'analogIn') || 0; |
| 205 | + return [analogIn, Generator.ORDER_ATOMIC]; |
| 206 | + }; |
| 207 | + |
| 208 | + Generator.microbitMore_menu_pinMode = function (block) { |
| 209 | + const pinMode = Generator.quote_(Generator.getFieldValue(block, 'pinMode') || 'pullUp'); |
| 210 | + return [pinMode, Generator.ORDER_ATOMIC]; |
| 211 | + }; |
| 212 | + |
| 213 | + Generator.microbitMore_menu_digitalValue = function (block) { |
| 214 | + const digitalValue = Generator.getFieldValue(block, 'digitalValue') || 0; |
| 215 | + return [digitalValue, Generator.ORDER_ATOMIC]; |
| 216 | + }; |
| 217 | + |
| 218 | + Generator.microbitMore_menu_pinEventTypeMenu = function (block) { |
| 219 | + const pinEventTypeMenu = Generator.getFieldValue(block, 'pinEventTypeMenu') || 0; |
| 220 | + return [pinEventTypeMenu, Generator.ORDER_ATOMIC]; |
| 221 | + }; |
| 222 | + |
| 223 | + Generator.microbitMore_menu_pinEventTimestampMenu = function (block) { |
| 224 | + const pinEventTimestampMenu = Generator.getFieldValue(block, 'pinEventTimestampMenu') || 5; |
| 225 | + return [pinEventTimestampMenu, Generator.ORDER_ATOMIC]; |
| 226 | + }; |
| 227 | + |
| 228 | + Generator.microbitMore_menu_sharedDataIndex = function (block) { |
| 229 | + const sharedDataIndex = Generator.quote_(Generator.getFieldValue(block, 'sharedDataIndex') || 'absolute'); |
| 230 | + return [sharedDataIndex, Generator.ORDER_ATOMIC]; |
| 231 | + }; |
| 232 | + |
| 233 | + Generator.microbitMore_menu_connectionStateMenu = function (block) { |
| 234 | + const connectionStateMenu = |
| 235 | + Generator.quote_(Generator.getFieldValue(block, 'connectionStateMenu') || 'connected'); |
| 236 | + return [connectionStateMenu, Generator.ORDER_ATOMIC]; |
| 237 | + }; |
| 238 | + |
| 239 | + Generator.matrix = function (block) { |
| 240 | + let matrix = Generator.getFieldValue(block, 'MATRIX') || '0000000000000000000000000'; |
| 241 | + matrix = matrix.replace(/0/g, '.'); |
| 242 | + matrix = matrix.match(/.{5}/g).map(s => Generator.quote_(s)); |
| 243 | + return [matrix.join(',\n'), Generator.ORDER_ATOMIC]; |
| 244 | + }; |
| 245 | + |
| 246 | + return Generator; |
| 247 | +} |
0 commit comments