diff --git a/src/blocks/arduino.ts b/src/blocks/arduino.ts index 5f9ec0d..17fd793 100644 --- a/src/blocks/arduino.ts +++ b/src/blocks/arduino.ts @@ -1,5 +1,38 @@ import { BlockDefinition } from "blockly/core/blocks"; +const displayPinNumbers = [ + ["1", "0"], + ["2", "1"], + ["3", "2"], +]; + +const rgbColor = [ + ["%{BKY_LEAPHY_RGB_COLOR_RED}", "0"], + ["%{BKY_LEAPHY_RGB_COLOR_GREEN}", "1"], + ["%{BKY_LEAPHY_RGB_COLOR_BLUE}", "2"], +]; + +const rgbColorRaw = [ + ["%{BKY_LEAPHY_RGB_RAW_COLOR_RED}", "0"], + ["%{BKY_LEAPHY_RGB_RAW_COLOR_GREEN}", "1"], + ["%{BKY_LEAPHY_RGB_RAW_COLOR_BLUE}", "2"], +]; + +const apds9960RgbColor = [ + ["%{BKY_COLOUR_RGB_RED}", "0"], + ["%{BKY_COLOUR_RGB_GREEN}", "1"], + ["%{BKY_COLOUR_RGB_BLUE}", "2"], + ["%{BKY_COLOUR_RGB_AMBIENT}", "3"], +]; + +const ledstripDemoOptions = [ + ["%{BKY_LEAPHY_LED_STRIP_LIGHTBANK}", "0"], + ["%{BKY_LEAPHY_LED_STRIP_BREATHE}", "1"], + ["%{BKY_LEAPHY_LED_STRIP_GULF}", "3"], + ["%{BKY_LEAPHY_LED_STRIP_RAINBOW}", "4"], + ["%{BKY_LEAPHY_LED_STRIP_COLORGULF}", "5"], +]; + const blocks: BlockDefinition = [ { type: "time_delay", @@ -14,6 +47,557 @@ const blocks: BlockDefinition = [ tooltip: "%{BKY_ARD_TIME_DELAY_TIP}", helpUrl: "http://arduino.cc/en/Reference/Delay", }, + { + type: "leaphy_start", + lastDummyAlign0: "CENTRE", + message0: "%%{BKY_LEAPHY_START}", + style: "leaphy_blocks", + extensions: ["appendStatementInputStack"], + isDeletable: false, + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_serial_print_line", + message0: "%%{BKY_LEAPHY_SERIAL_PRINT} %1 %2", + args0: [ + { type: "input_dummy" }, + { type: "input_value", name: "VALUE" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_serial_print_value", + message0: "%%{BKY_LEAPHY_SERIAL_PRINT} %1 %2 = %3 %4", + args0: [ + { type: "input_dummy" }, + { type: "input_value", name: "NAME" }, + { type: "input_dummy" }, + { type: "input_value", name: "VALUE" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_serial_available", + message0: "%%{BKY_LEAPHY_SERIAL_AVAILABLE}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_serial_read_line", + message0: "%%{BKY_LEAPHY_SERIAL_READ_LINE}", + style: "leaphy_blocks", + output: "String", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_rgb_color", + message0: "%1", + args0: [ + { + type: "field_dropdown", + name: "COLOR_TYPE", + options: rgbColor, + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_rgb_color_raw", + message0: "%1", + args0: [ + { + type: "field_dropdown", + name: "COLOR_TYPE_RAW", + options: rgbColorRaw, + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_rgb_raw_color_red", + message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_RED}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_rgb_raw_color_green", + message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_GREEN}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_rgb_raw_color_blue", + message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_BLUE}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_i2c_rgb_color", + message0: "%%{BKY_LEAPHY_RGB_READ_SENSOR} %1", + args0: [ + { + type: "field_dropdown", + name: "COLOR_TYPE", + options: apds9960RgbColor, + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_i2c_gesture", + message0: "%%{BKY_LEAPHY_GET_GESTURE}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_led_set_strip", + message0: + "%%{BKY_LEAPHY_LED_SET_STRIP} %1 %%{BKY_LEAPHY_LED_SET_PIN} %2 %%{BKY_LEAPHY_LED_SET_LEDS} %3", + args0: [ + { type: "input_dummy" }, + { type: "input_value", name: "LED_SET_PIN", check: "Number" }, + { type: "input_value", name: "LED_SET_LEDS", check: "Number" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_led_set_basic", + message0: + "%%{BKY_LEAPHY_LED_BASIC_LED} %1 %%{BKY_LEAPHY_LED_BASIC_RED} %2 %%{BKY_LEAPHY_LED_BASIC_GREEN} %3 %%{BKY_LEAPHY_LED_BASIC_BLUE} %4", + args0: [ + { type: "input_value", name: "LED_SET_LED", check: "Number" }, + { type: "input_value", name: "LED_BASIC_RED", check: "Number" }, + { + type: "input_value", + name: "LED_BASIC_GREEN", + check: "Number", + }, + { + type: "input_value", + name: "LED_BASIC_BLUE", + check: "Number", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_led_set_speed", + message0: "%%{BKY_LEAPHY_LED_SET_SPEEDVALUE} %1", + args0: [ + { + type: "input_value", + name: "LED_SET_SPEEDVALUE", + check: "Number", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_display_clear", + message0: "%%{BKY_LEAPHY_DISPLAY_CLEAR}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_display_display", + message0: "%%{BKY_LEAPHY_DISPLAY_DISPLAY}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_display_print_line", + message0: "%%{BKY_LEAPHY_DISPLAY_PRINT} %1 %2 %3", + args0: [ + { type: "input_dummy" }, + { + type: "field_dropdown", + name: "DISPLAY_ROW", + options: displayPinNumbers, + }, + { type: "input_value", name: "VALUE" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + // "extensions": "updateDisplay", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_display_set_text_size", + message0: "%%{BKY_LEAPHY_DISPLAY_SET_TEXT_SIZE} %1", + args0: [{ type: "input_value", name: "NUM", check: "Number" }], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + // "extensions": "updateDisplay", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_display_print_value", + message0: "%%{BKY_LEAPHY_DISPLAY_PRINT} %1 %2 %3 = %4 %5", + args0: [ + { type: "input_dummy" }, + { + type: "field_dropdown", + name: "DISPLAY_ROW", + options: displayPinNumbers, + }, + { type: "input_value", name: "NAME" }, + { type: "input_dummy" }, + { type: "input_value", name: "VALUE" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + // "extensions": "updateDisplay", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_use_lsm9ds1", + message0: "%1 %2", + args0: [ + { + type: "field_dropdown", + name: "SENSOR", + options: [ + ["Gyro", "g.gyro"], + ["Compass", "m.magnetic"], + ["Accelerator", "a.acceleration"], + ], + }, + { + type: "field_dropdown", + name: "AXIS", + options: [ + ["X-axis", ".x"], + ["Y-axis", ".y"], + ["Z-axis", ".z"], + ], + }, + ], + inputsInline: true, + output: "Number", + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_update_lsm9ds1", + message0: "Update Lsm9ds1", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_compass_degrees", + message0: "%%{BKY_LEAPHY_COMPASS}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_gas_sensor", + message0: "%%{BKY_LEAPHY_CHOOSE_GAS} %1", + args0: [ + { + type: "field_dropdown", + name: "GAS", + options: [ + ["TVOC", "TVOC"], + ["eCO2", "eCO2"], + ["Raw H2", "Raw H2"], + ["Raw Ethanol", "RAWETHANOL"], + ], + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "i2c_use_channel", + message0: "%{BKY_USE_I2C_CHANNEL} %1", + args0: [ + { + type: "field_dropdown", + name: "CHANNEL", + options: [ + ["0", "0"], + ["1", "1"], + ["2", "2"], + ["3", "3"], + ["4", "4"], + ["5", "5"], + ["6", "6"], + ["7", "7"], + ], + }, + ], + message1: "%1", + args1: [ + { + type: "input_statement", + name: "DO", + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "%{BKY_USE_I2C_CHANNEL_TOOLTIP}", + helpUrl: "", + }, + { + type: "i2c_list_devices", + message0: "%{BKY_I2C_LIST_DEVICES}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_tof_get_distance", + message0: "%%{BKY_LEAPHY_TOF_GET_DISTANCE}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_get_air_pressure", + message0: "%%{BKY_LEAPHY_GET_AIR_PRESSURE}", + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_segment_set", + message0: `%{BKY_LEAPHY_SEGMENT_SET}`, + args0: [ + { + type: "input_value", + name: "NUM", + check: "Number", + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_segment_clear", + message0: "%{BKY_LEAPHY_SEGMENT_CLEAR}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_segment_set_brightness", + message0: "%{BKY_LEAPHY_SEGMENT_SET_BRIGHTNESS}", + args0: [ + { + type: "input_value", + name: "BRIGHTNESS", + check: "Number", + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_matrix_set", + message0: "%{BKY_LEAPHY_MATRIX_SET}", + args0: [ + { + type: "input_value", + name: "X", + check: "Number", + }, + { + type: "input_value", + name: "Y", + check: "Number", + }, + { + type: "input_value", + name: "ON", + check: "Boolean", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_matrix_set_brightness", + message0: "%{BKY_LEAPHY_MATRIX_SET_BRIGHTNESS}", + args0: [ + { + type: "input_value", + name: "BRIGHTNESS", + check: "Number", + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_matrix_clear", + message0: "%{BKY_LEAPHY_MATRIX_CLEAR}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_matrix_fill", + message0: "%{BKY_LEAPHY_MATRIX_FILL}", + args0: [ + { type: "input_dummy" }, + { + type: "field_bitmap", + name: "MATRIX", + width: 8, + height: 8, + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_sound_play", + message0: "%{BKY_LEAPHY_SOUND_PLAY}", + args0: [ + { + type: "input_value", + name: "ITEM", + check: "Number", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_sound_stop", + message0: "%{BKY_LEAPHY_SOUND_STOP}", + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_sound_set_volume", + message0: "%{BKY_LEAPHY_SOUND_SET_VOLUME}", + args0: [ + { + type: "input_value", + name: "VOLUME", + check: "Number", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_led_strip_demo", + message0: "%%{BKY_LEAPHY_LED_STRIP_DEMO} %1 %2 %3 %4 %5", + args0: [ + { + type: "field_dropdown", + name: "DEMO_TYPE", + options: ledstripDemoOptions, + }, + { type: "input_dummy" }, + { + type: "input_value", + name: "LED_STRIP_DEMO_RED", + check: "Number", + }, + { + type: "input_value", + name: "LED_STRIP_DEMO_GREEN", + check: "Number", + }, + { + type: "input_value", + name: "LED_STRIP_DEMO_BLUE", + check: "Number", + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "", + helpUrl: "", + }, ]; export { blocks }; diff --git a/src/blocks/blocks.ts b/src/blocks/blocks.ts index 7d54f82..ee87b6e 100644 --- a/src/blocks/blocks.ts +++ b/src/blocks/blocks.ts @@ -1,6 +1,6 @@ import * as lists from "./lists"; import * as texts from "./text"; -import * as leaphyCommon from "./leaphy_common"; +import * as leaphyCommon from "./variable_blocks"; import * as leaphyOriginal from "./leaphy_original"; import * as leaphyFlitz from "./leaphy_flitz"; import * as leaphyClick from "./leaphy_click"; @@ -149,18 +149,18 @@ function getBlocks(boardType = "l_uno") { ); // Add all blocks from each independent module in one list - const block = [ - ...lists.blocks, - ...texts.blocks, - ...leaphyCommon.default(board), - ...leaphyOriginal.blocks, - ...leaphyFlitz.blocks, - ...leaphyClick.blocks, - ...arduino.blocks, - ...loops.blocks, - ]; - + const block = leaphyCommon.default(board); return { block }; } -export default getBlocks; +const constantBlocks = [ + ...lists.blocks, + ...texts.blocks, + ...leaphyOriginal.blocks, + ...leaphyFlitz.blocks, + ...leaphyClick.blocks, + ...arduino.blocks, + ...loops.blocks, +]; + +export { getBlocks, constantBlocks }; diff --git a/src/blocks/leaphy_common.ts b/src/blocks/leaphy_common.ts deleted file mode 100644 index 2097c34..0000000 --- a/src/blocks/leaphy_common.ts +++ /dev/null @@ -1,779 +0,0 @@ -import { Board } from "./blocks"; -import { BlockDefinition } from "blockly/core/blocks"; - -const rgbColor = [ - ["%{BKY_LEAPHY_RGB_COLOR_RED}", "0"], - ["%{BKY_LEAPHY_RGB_COLOR_GREEN}", "1"], - ["%{BKY_LEAPHY_RGB_COLOR_BLUE}", "2"], -]; - -const rgbColorRaw = [ - ["%{BKY_LEAPHY_RGB_RAW_COLOR_RED}", "0"], - ["%{BKY_LEAPHY_RGB_RAW_COLOR_GREEN}", "1"], - ["%{BKY_LEAPHY_RGB_RAW_COLOR_BLUE}", "2"], -]; - -const apds9960RgbColor = [ - ["%{BKY_COLOUR_RGB_RED}", "0"], - ["%{BKY_COLOUR_RGB_GREEN}", "1"], - ["%{BKY_COLOUR_RGB_BLUE}", "2"], - ["%{BKY_COLOUR_RGB_AMBIENT}", "3"], -]; - -const ledstripDemoOptions = [ - ["%{BKY_LEAPHY_LED_STRIP_LIGHTBANK}", "0"], - ["%{BKY_LEAPHY_LED_STRIP_BREATHE}", "1"], - ["%{BKY_LEAPHY_LED_STRIP_GULF}", "3"], - ["%{BKY_LEAPHY_LED_STRIP_RAINBOW}", "4"], - ["%{BKY_LEAPHY_LED_STRIP_COLORGULF}", "5"], -]; - -const displayPinNumbers = [ - ["1", "0"], - ["2", "1"], - ["3", "2"], -]; - -function getBlocks(board: Board): BlockDefinition { - return [ - { - type: "digital_read", - message0: "%%{BKY_LEAPHY_DIGITAL_READ} %1", - args0: [ - { - type: "field_dropdown", - name: "PIN", - options: board.digitalPins, - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "analog_read", - message0: "%%{BKY_LEAPHY_ANALOG_READ} %1", - args0: [ - { - type: "field_dropdown", - name: "PIN", - options: board.analogPins, - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_start", - lastDummyAlign0: "CENTRE", - message0: "%%{BKY_LEAPHY_START}", - style: "leaphy_blocks", - extensions: ["appendStatementInputStack"], - isDeletable: false, - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_serial_print_line", - message0: "%%{BKY_LEAPHY_SERIAL_PRINT} %1 %2", - args0: [ - { type: "input_dummy" }, - { type: "input_value", name: "VALUE" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_serial_print_value", - message0: "%%{BKY_LEAPHY_SERIAL_PRINT} %1 %2 = %3 %4", - args0: [ - { type: "input_dummy" }, - { type: "input_value", name: "NAME" }, - { type: "input_dummy" }, - { type: "input_value", name: "VALUE" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_serial_available", - message0: "%%{BKY_LEAPHY_SERIAL_AVAILABLE}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_serial_read_line", - message0: "%%{BKY_LEAPHY_SERIAL_READ_LINE}", - style: "leaphy_blocks", - output: "String", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_rgb_color", - message0: "%1", - args0: [ - { - type: "field_dropdown", - name: "COLOR_TYPE", - options: rgbColor, - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_rgb_color_raw", - message0: "%1", - args0: [ - { - type: "field_dropdown", - name: "COLOR_TYPE_RAW", - options: rgbColorRaw, - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_rgb_raw_color_red", - message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_RED}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_rgb_raw_color_green", - message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_GREEN}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_rgb_raw_color_blue", - message0: "%%{BKY_LEAPHY_RGB_RAW_COLOR_BLUE}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_i2c_rgb_color", - message0: "%%{BKY_LEAPHY_RGB_READ_SENSOR} %1", - args0: [ - { - type: "field_dropdown", - name: "COLOR_TYPE", - options: apds9960RgbColor, - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_i2c_gesture", - message0: "%%{BKY_LEAPHY_GET_GESTURE}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_led_set_strip", - message0: - "%%{BKY_LEAPHY_LED_SET_STRIP} %1 %%{BKY_LEAPHY_LED_SET_PIN} %2 %%{BKY_LEAPHY_LED_SET_LEDS} %3", - args0: [ - { type: "input_dummy" }, - { type: "input_value", name: "LED_SET_PIN", check: "Number" }, - { type: "input_value", name: "LED_SET_LEDS", check: "Number" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_led_set_basic", - message0: - "%%{BKY_LEAPHY_LED_BASIC_LED} %1 %%{BKY_LEAPHY_LED_BASIC_RED} %2 %%{BKY_LEAPHY_LED_BASIC_GREEN} %3 %%{BKY_LEAPHY_LED_BASIC_BLUE} %4", - args0: [ - { type: "input_value", name: "LED_SET_LED", check: "Number" }, - { type: "input_value", name: "LED_BASIC_RED", check: "Number" }, - { - type: "input_value", - name: "LED_BASIC_GREEN", - check: "Number", - }, - { - type: "input_value", - name: "LED_BASIC_BLUE", - check: "Number", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_led_set_speed", - message0: "%%{BKY_LEAPHY_LED_SET_SPEEDVALUE} %1", - args0: [ - { - type: "input_value", - name: "LED_SET_SPEEDVALUE", - check: "Number", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_led_strip_demo", - message0: "%%{BKY_LEAPHY_LED_STRIP_DEMO} %1 %2 %3 %4 %5", - args0: [ - { - type: "field_dropdown", - name: "DEMO_TYPE", - options: ledstripDemoOptions, - }, - { type: "input_dummy" }, - { - type: "input_value", - name: "LED_STRIP_DEMO_RED", - check: "Number", - }, - { - type: "input_value", - name: "LED_STRIP_DEMO_GREEN", - check: "Number", - }, - { - type: "input_value", - name: "LED_STRIP_DEMO_BLUE", - check: "Number", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_servo_write", - message0: - "%%{" + - board.servoName + - "} %1 %2 %%{BKY_ARD_SERVO_WRITE_TO} %3 %%{BKY_ARD_SERVO_WRITE_DEG_180}", - args0: [ - { - type: "field_dropdown", - name: "SERVO_PIN", - options: board.pwmPins, - }, - { type: "input_dummy" }, - { type: "input_value", name: "SERVO_ANGLE", check: "Number" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - // "extensions": "refreshServoPinFields", - tooltip: "%{BKY_ARD_SERVO_WRITE_TIP}", - helpUrl: "http://arduino.cc/en/Reference/ServoWrite", - }, - { - type: "leaphy_servo_read", - message0: "%%{BKY_ARD_SERVO_READ} %1", - args0: [ - { - type: "field_dropdown", - name: "SERVO_PIN", - options: board.digitalPins, - }, - ], - output: "Number", - style: "leaphy_blocks", - // "extensions": "returnAndUpdateServoRead", - tooltip: "%{BKY_ARD_SERVO_READ_TIP}", - helpUrl: "http://arduino.cc/en/Reference/ServoRead", - }, - { - type: "leaphy_io_digitalwrite", - message0: "%%{BKY_ARD_DIGITALWRITE} %1 %%{BKY_ARD_WRITE_TO} %2", - args0: [ - { - type: "field_dropdown", - name: "PIN", - options: board.digitalPins, - }, - { type: "input_value", name: "STATE", check: "Boolean" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "%{BKY_ARD_DIGITALWRITE_TIP}", - helpUrl: "http://arduino.cc/en/Reference/DigitalWrite", - }, - { - type: "leaphy_io_analogwrite", - message0: "%%{BKY_ARD_ANALOGWRITE} %1 %%{BKY_ARD_WRITE_TO} %2", - args0: [ - { type: "field_dropdown", name: "PIN", options: board.pwmPins }, - { type: "input_value", name: "NUM", check: "Number" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - // "extensions": "inputAndUpdateAnalog", - tooltip: "%{BKY_ARD_ANALOGWRITE_TIP}", - helpUrl: "http://arduino.cc/en/Reference/AnalogWrite", - }, - { - type: "leaphy_sonar_read", - message0: - "%%{BKY_LEAPHY_SONAR_READ_TRIG} %1 %%{BKY_LEAPHY_SONAR_READ_ECHO} %2", - args0: [ - { - type: "field_dropdown", - name: "TRIG_PIN", - options: board.digitalPins, - }, - { - type: "field_dropdown", - name: "ECHO_PIN", - options: board.digitalPins, - }, - ], - output: "Number", - style: "leaphy_blocks", - tooltip: "%{BKY_LEAPHY_SONAR_READ_TIP}", - helpUrl: "", - }, - { - type: "leaphy_display_clear", - message0: "%%{BKY_LEAPHY_DISPLAY_CLEAR}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_display_display", - message0: "%%{BKY_LEAPHY_DISPLAY_DISPLAY}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_display_print_line", - message0: "%%{BKY_LEAPHY_DISPLAY_PRINT} %1 %2 %3", - args0: [ - { type: "input_dummy" }, - { - type: "field_dropdown", - name: "DISPLAY_ROW", - options: displayPinNumbers, - }, - { type: "input_value", name: "VALUE" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - // "extensions": "updateDisplay", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_display_set_text_size", - message0: "%%{BKY_LEAPHY_DISPLAY_SET_TEXT_SIZE} %1", - args0: [{ type: "input_value", name: "NUM", check: "Number" }], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - // "extensions": "updateDisplay", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_display_print_value", - message0: "%%{BKY_LEAPHY_DISPLAY_PRINT} %1 %2 %3 = %4 %5", - args0: [ - { type: "input_dummy" }, - { - type: "field_dropdown", - name: "DISPLAY_ROW", - options: displayPinNumbers, - }, - { type: "input_value", name: "NAME" }, - { type: "input_dummy" }, - { type: "input_value", name: "VALUE" }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - // "extensions": "updateDisplay", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_use_lsm9ds1", - message0: "%1 %2", - args0: [ - { - type: "field_dropdown", - name: "SENSOR", - options: [ - ["Gyro", "g.gyro"], - ["Compass", "m.magnetic"], - ["Accelerator", "a.acceleration"], - ], - }, - { - type: "field_dropdown", - name: "AXIS", - options: [ - ["X-axis", ".x"], - ["Y-axis", ".y"], - ["Z-axis", ".z"], - ], - }, - ], - inputsInline: true, - output: "Number", - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_update_lsm9ds1", - message0: "Update Lsm9ds1", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_compass_degrees", - message0: "%%{BKY_LEAPHY_COMPASS}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_gas_sensor", - message0: "%%{BKY_LEAPHY_CHOOSE_GAS} %1", - args0: [ - { - type: "field_dropdown", - name: "GAS", - options: [ - ["TVOC", "TVOC"], - ["eCO2", "eCO2"], - ["Raw H2", "Raw H2"], - ["Raw Ethanol", "RAWETHANOL"], - ], - }, - ], - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "i2c_use_channel", - message0: "%{BKY_USE_I2C_CHANNEL} %1", - args0: [ - { - type: "field_dropdown", - name: "CHANNEL", - options: [ - ["0", "0"], - ["1", "1"], - ["2", "2"], - ["3", "3"], - ["4", "4"], - ["5", "5"], - ["6", "6"], - ["7", "7"], - ], - }, - ], - message1: "%1", - args1: [ - { - type: "input_statement", - name: "DO", - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - tooltip: "%{BKY_USE_I2C_CHANNEL_TOOLTIP}", - helpUrl: "", - }, - { - type: "i2c_list_devices", - message0: "%{BKY_I2C_LIST_DEVICES}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_tof_get_distance", - message0: "%%{BKY_LEAPHY_TOF_GET_DISTANCE}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_get_air_pressure", - message0: "%%{BKY_LEAPHY_GET_AIR_PRESSURE}", - style: "leaphy_blocks", - output: "Number", - tooltip: "", - helpUrl: "", - }, - { - type: "leaphy_segment_init", - message0: "%{BKY_LEAPHY_SEGMENT_INIT}", - args0: [ - { - type: "field_dropdown", - name: "CLK", - options: board.digitalPins, - }, - { - type: "field_dropdown", - name: "DIO", - options: board.digitalPins, - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_segment_set", - message0: `%{BKY_LEAPHY_SEGMENT_SET}`, - args0: [ - { - type: "input_value", - name: "NUM", - check: "Number", - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_segment_clear", - message0: "%{BKY_LEAPHY_SEGMENT_CLEAR}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_segment_set_brightness", - message0: "%{BKY_LEAPHY_SEGMENT_SET_BRIGHTNESS}", - args0: [ - { - type: "input_value", - name: "BRIGHTNESS", - check: "Number", - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_matrix_init", - message0: "%{BKY_LEAPHY_MATRIX_INIT}", - args0: [ - { - type: "field_dropdown", - name: "DIN", - options: board.digitalPins, - }, - { - type: "field_dropdown", - name: "CLK", - options: board.digitalPins, - }, - { - type: "field_dropdown", - name: "CS", - options: board.digitalPins, - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_matrix_set", - message0: "%{BKY_LEAPHY_MATRIX_SET}", - args0: [ - { - type: "input_value", - name: "X", - check: "Number", - }, - { - type: "input_value", - name: "Y", - check: "Number", - }, - { - type: "input_value", - name: "ON", - check: "Boolean", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_matrix_set_brightness", - message0: "%{BKY_LEAPHY_MATRIX_SET_BRIGHTNESS}", - args0: [ - { - type: "input_value", - name: "BRIGHTNESS", - check: "Number", - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_matrix_clear", - message0: "%{BKY_LEAPHY_MATRIX_CLEAR}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_matrix_fill", - message0: "%{BKY_LEAPHY_MATRIX_FILL}", - args0: [ - { type: "input_dummy" }, - { - type: "field_bitmap", - name: "MATRIX", - width: 8, - height: 8, - }, - ], - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_sound_init", - message0: "%{BKY_LEAPHY_SOUND_INIT}", - args0: [ - { - type: "field_dropdown", - name: "RX", - options: board.digitalPins, - }, - { - type: "field_dropdown", - name: "TX", - options: board.digitalPins, - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_sound_play", - message0: "%{BKY_LEAPHY_SOUND_PLAY}", - args0: [ - { - type: "input_value", - name: "ITEM", - check: "Number", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_sound_stop", - message0: "%{BKY_LEAPHY_SOUND_STOP}", - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - { - type: "leaphy_sound_set_volume", - message0: "%{BKY_LEAPHY_SOUND_SET_VOLUME}", - args0: [ - { - type: "input_value", - name: "VOLUME", - check: "Number", - }, - ], - inputsInline: true, - previousStatement: null, - nextStatement: null, - style: "leaphy_blocks", - }, - ]; -} - -export default getBlocks; diff --git a/src/blocks/variable_blocks.ts b/src/blocks/variable_blocks.ts new file mode 100644 index 0000000..08be710 --- /dev/null +++ b/src/blocks/variable_blocks.ts @@ -0,0 +1,196 @@ +import { Board } from "./blocks"; +import { BlockDefinition } from "blockly/core/blocks"; + +function getBlocks(board: Board): BlockDefinition { + return [ + { + type: "digital_read", + message0: "%%{BKY_LEAPHY_DIGITAL_READ} %1", + args0: [ + { + type: "field_dropdown", + name: "PIN", + options: board.digitalPins, + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "analog_read", + message0: "%%{BKY_LEAPHY_ANALOG_READ} %1", + args0: [ + { + type: "field_dropdown", + name: "PIN", + options: board.analogPins, + }, + ], + style: "leaphy_blocks", + output: "Number", + tooltip: "", + helpUrl: "", + }, + { + type: "leaphy_servo_write", + message0: + "%%{" + + board.servoName + + "} %1 %2 %%{BKY_ARD_SERVO_WRITE_TO} %3 %%{BKY_ARD_SERVO_WRITE_DEG_180}", + args0: [ + { + type: "field_dropdown", + name: "SERVO_PIN", + options: board.pwmPins, + }, + { type: "input_dummy" }, + { type: "input_value", name: "SERVO_ANGLE", check: "Number" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + // "extensions": "refreshServoPinFields", + tooltip: "%{BKY_ARD_SERVO_WRITE_TIP}", + helpUrl: "http://arduino.cc/en/Reference/ServoWrite", + }, + { + type: "leaphy_servo_read", + message0: "%%{BKY_ARD_SERVO_READ} %1", + args0: [ + { + type: "field_dropdown", + name: "SERVO_PIN", + options: board.digitalPins, + }, + ], + output: "Number", + style: "leaphy_blocks", + // "extensions": "returnAndUpdateServoRead", + tooltip: "%{BKY_ARD_SERVO_READ_TIP}", + helpUrl: "http://arduino.cc/en/Reference/ServoRead", + }, + { + type: "leaphy_io_digitalwrite", + message0: "%%{BKY_ARD_DIGITALWRITE} %1 %%{BKY_ARD_WRITE_TO} %2", + args0: [ + { + type: "field_dropdown", + name: "PIN", + options: board.digitalPins, + }, + { type: "input_value", name: "STATE", check: "Boolean" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + tooltip: "%{BKY_ARD_DIGITALWRITE_TIP}", + helpUrl: "http://arduino.cc/en/Reference/DigitalWrite", + }, + { + type: "leaphy_io_analogwrite", + message0: "%%{BKY_ARD_ANALOGWRITE} %1 %%{BKY_ARD_WRITE_TO} %2", + args0: [ + { type: "field_dropdown", name: "PIN", options: board.pwmPins }, + { type: "input_value", name: "NUM", check: "Number" }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + // "extensions": "inputAndUpdateAnalog", + tooltip: "%{BKY_ARD_ANALOGWRITE_TIP}", + helpUrl: "http://arduino.cc/en/Reference/AnalogWrite", + }, + { + type: "leaphy_sonar_read", + message0: + "%%{BKY_LEAPHY_SONAR_READ_TRIG} %1 %%{BKY_LEAPHY_SONAR_READ_ECHO} %2", + args0: [ + { + type: "field_dropdown", + name: "TRIG_PIN", + options: board.digitalPins, + }, + { + type: "field_dropdown", + name: "ECHO_PIN", + options: board.digitalPins, + }, + ], + output: "Number", + style: "leaphy_blocks", + tooltip: "%{BKY_LEAPHY_SONAR_READ_TIP}", + helpUrl: "", + }, + + { + type: "leaphy_segment_init", + message0: "%{BKY_LEAPHY_SEGMENT_INIT}", + args0: [ + { + type: "field_dropdown", + name: "CLK", + options: board.digitalPins, + }, + { + type: "field_dropdown", + name: "DIO", + options: board.digitalPins, + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_matrix_init", + message0: "%{BKY_LEAPHY_MATRIX_INIT}", + args0: [ + { + type: "field_dropdown", + name: "DIN", + options: board.digitalPins, + }, + { + type: "field_dropdown", + name: "CLK", + options: board.digitalPins, + }, + { + type: "field_dropdown", + name: "CS", + options: board.digitalPins, + }, + ], + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + { + type: "leaphy_sound_init", + message0: "%{BKY_LEAPHY_SOUND_INIT}", + args0: [ + { + type: "field_dropdown", + name: "RX", + options: board.digitalPins, + }, + { + type: "field_dropdown", + name: "TX", + options: board.digitalPins, + }, + ], + inputsInline: true, + previousStatement: null, + nextStatement: null, + style: "leaphy_blocks", + }, + ]; +} + +export default getBlocks; diff --git a/src/generators/arduino.ts b/src/generators/arduino.ts index a7958b5..2becfc7 100644 --- a/src/generators/arduino.ts +++ b/src/generators/arduino.ts @@ -396,7 +396,7 @@ const generator = new Arduino(); import * as arduino from "./arduino/arduino"; import * as leaphy_click from "./arduino/leaphy_click"; -import * as leaphy_common from "./arduino/leaphy_common"; +import * as leaphy_common from "./arduino/variable_blocks"; import * as leaphy_extra from "./arduino/leaphy_extra"; import * as leaphy_flitz from "./arduino/leaphy_flitz"; import * as leaphy_original from "./arduino/leaphy_original"; diff --git a/src/generators/arduino/arduino.ts b/src/generators/arduino/arduino.ts index 5238d2d..3618a7f 100644 --- a/src/generators/arduino/arduino.ts +++ b/src/generators/arduino/arduino.ts @@ -1,3 +1,4 @@ +import { addI2CDeclarations } from "./i2c"; import { Arduino } from "../arduino"; function getCodeGenerators(arduino: Arduino) { @@ -11,5 +12,382 @@ function getCodeGenerators(arduino: Arduino) { return "delay(" + delayTime + ");\n"; }; + + arduino.forBlock["leaphy_serial_available"] = function () { + arduino.addSetup("serial", "Serial.begin(115200);", false); + const code = "Serial.available()"; + return [code, arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_serial_read_line"] = function () { + arduino.addSetup("serial", "Serial.begin(115200);", false); + const code = "Serial.readStringUntil('\\n')"; + return [code, arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_serial_print_line"] = function (block) { + arduino.addSetup("serial", "Serial.begin(115200);", false); + const value = + arduino.valueToCode(block, "VALUE", arduino.ORDER_ATOMIC) || "0"; + return "Serial.println(" + value + ");\n"; + }; + + arduino.forBlock["leaphy_serial_print_value"] = function (block) { + arduino.addSetup("serial", "Serial.begin(115200);", false); + const name = + arduino.valueToCode(block, "NAME", arduino.ORDER_ATOMIC) || "0"; + const value = + arduino.valueToCode(block, "VALUE", arduino.ORDER_ATOMIC) || "0"; + return ( + `Serial.print(${name});\n` + + `Serial.print(" = ");\n` + + `Serial.println(${value});\n` + ); + }; + + arduino.forBlock["leaphy_compass_degrees"] = function () { + arduino.addInclude("leaphy_compass", "#include "); + arduino.addDeclaration("leaphy_compass", "QMC5883LCompass compass;"); + const setup = arduino.addI2CSetup( + "compass", + "compass.init();\n compass.setMagneticDeclination(2, 30);\n", + ); + arduino.addDeclaration( + "leaphy_compass_read", + "int getCompassDegrees() {\n" + + " " + + setup + + "\n" + + " compass.read();\n" + + " int azimuth = compass.getAzimuth();\n" + + " return round((azimuth > -0.5) ? azimuth : azimuth + 360);\n" + + "}\n", + ); + return ["getCompassDegrees()", arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_start"] = function (block) { + // Define the Start procedure + const funcName = "leaphyProgram"; + let branch = arduino.statementToCode(block, "STACK"); + if (arduino.STATEMENT_PREFIX) { + const id = block.id.replace(/\$/g, "$$$$"); // Issue 251. + branch = + arduino.prefixLines( + arduino.STATEMENT_PREFIX.replace(/%1/g, "'" + id + "'"), + arduino.INDENT, + ) + branch; + } + if (arduino.INFINITE_LOOP_TRAP) { + branch = + arduino.INFINITE_LOOP_TRAP.replace( + /%1/g, + "'" + block.id + "'", + ) + branch; + } + const returnType = "void"; + let code = returnType + " " + funcName + "() {\n" + branch + "}"; + + code = arduino.scrub_(block, code); + arduino.addDeclaration(funcName, code, true); + arduino.addSetup("userSetupCode", funcName + "();", false); + return null; + }; + + arduino.forBlock["leaphy_tof_get_distance"] = function () { + arduino.addInclude("leaphy_tof", "#include "); + arduino.addDeclaration("leaphy_tof", "Adafruit_VL53L0X i2c_distance;"); + const setup = arduino.addI2CSetup( + "tof", + "i2c_distance.begin();\n" + + " i2c_distance.setMeasurementTimingBudgetMicroSeconds(20000);\n", + ); + arduino.addDeclaration( + "leaphy_tof_read", + "int getTOF() {\n" + + ` ${setup}\n` + + " VL53L0X_RangingMeasurementData_t measure;\n" + + " i2c_distance.rangingTest(&measure, false);\n" + + " if (measure.RangeStatus == 4) return -1;\n" + + " return measure.RangeMilliMeter;\n" + + "}", + ); + return ["getTOF()", arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_get_air_pressure"] = function () { + arduino.addInclude("bmp280", "#include "); + arduino.addDeclaration("bmp280", "Adafruit_BMP280 bmp280;"); + const setup = arduino.addI2CSetup( + "bmp280", + "bmp280.begin(BMP280_ADDRESS_ALT);\n" + + " bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL,\n" + + " Adafruit_BMP280::SAMPLING_X2,\n" + + " Adafruit_BMP280::SAMPLING_X16,\n" + + " Adafruit_BMP280::FILTER_X16,\n" + + " Adafruit_BMP280::STANDBY_MS_500);\n", + ); + arduino.addDeclaration( + "bmp280_get_air_pressure", + "double getAirPressure() {\n" + + ` ${setup}\n` + + " return bmp280.readPressure() / 100;\n" + + "}", + ); + return ["getAirPressure()", arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_gas_sensor"] = function (block) { + arduino.addInclude("leaphy_gas_sensor", "#include "); + arduino.addDeclaration("leaphy_gas_sensor", "Adafruit_SGP30 sgp;"); + const setup = arduino.addI2CSetup( + "gas", + "if (! sgp.begin()) return -1;\n", + ); + + const gasValue = block.getFieldValue("GAS"); + let code = ""; + if (gasValue === "TVOC") { + code = "getGasValueTVOC()"; + arduino.addDeclaration( + "leaphy_gas_valueTVOC", + "int getGasValueTVOC() {\n" + + " " + + setup + + " sgp.IAQmeasure();\n" + + " return " + + "sgp.TVOC" + + ";\n}\n", + ); + } else if (gasValue === "eCO2") { + code = "getGasValueCOTWO()"; + arduino.addDeclaration( + "leaphy_gas_valueCOTWO", + "int getGasValueCOTWO() {\n" + + " " + + setup + + " sgp.IAQmeasure();\n" + + " return " + + "sgp.eCO2" + + ";\n}\n", + ); + } else if (gasValue === "Raw H2") { + code = "getGasValueHTWO()"; + arduino.addDeclaration( + "leaphy_gas_valueHTWO", + "int getGasValueHTWO() {\n" + + " " + + setup + + " sgp.IAQmeasureRaw();\n" + + " return " + + "sgp.rawH2" + + ";\n}\n", + ); + } else if (gasValue === "RAWETHANOL") { + code = "getGasValueETHANOL()"; + arduino.addDeclaration( + "leaphy_gas_valueETHANOL", + "int getGasValueETHANOL() {\n" + + " " + + setup + + " sgp.IAQmeasureRaw();\n" + + " return " + + "sgp.rawEthanol" + + ";\n}\n", + ); + } + return [code, arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_i2c_rgb_color"] = function (block) { + const setup = arduino.addI2CSetup("apds9960", "APDS.begin();\n"); + + const rgb_declaration = + "int r[8], g[8], b[8], a[8];\n" + + "int getAPDS9960Color(int colorType) {\n" + + " " + + setup + + " uint8_t channel = i2cGetChannel();\n" + + " if (APDS.colorAvailable()) {\n" + + " APDS.readColor(r[channel], g[channel], b[channel], a[channel]);\n" + + " }\n" + + " switch(colorType) {\n" + + " case 0:\n" + + " return r[channel];\n" + + " case 1:\n" + + " return g[channel];\n" + + " case 2:\n" + + " return b[channel];\n" + + " case 3:\n" + + " return a[channel];\n" + + " }\n" + + "}\n"; + const colorType = block.getFieldValue("COLOR_TYPE"); + + arduino.addInclude("apds9960", "#include "); + arduino.addDeclaration("apds9960_rgb", rgb_declaration); + const code = "getAPDS9960Color(" + colorType + ")"; + return [code, arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["leaphy_i2c_gesture"] = function () { + const setup = arduino.addI2CSetup("apds9960", "APDS.begin();\n"); + const gesture_declaration = + "int gesture[8];\n" + + "int getAPDS9960Gesture() {\n" + + " " + + setup + + " uint8_t channel = i2cGetChannel();\n" + + " if (APDS.gestureAvailable()) {\n" + + " gesture[channel] = APDS.readGesture();\n" + + " }\n" + + " return gesture[channel];\n" + + "}\n"; + arduino.addInclude("apds9960", "#include "); + arduino.addDeclaration("apds9960_gesture", gesture_declaration); + const code = "getAPDS9960Gesture()"; + return [code, arduino.ORDER_ATOMIC]; + }; + + arduino.forBlock["i2c_use_channel"] = function (block) { + const channel = block.getFieldValue("CHANNEL"); + const innerCode = arduino.statementToCode(block, "DO"); + + addI2CDeclarations(); + + return ( + "i2cSelectChannel(" + + channel + + ");\n" + + innerCode + + "i2cRestoreChannel();\n" + ); + }; + + arduino.forBlock["i2c_list_devices"] = function () { + const LIST_DEVICES = + "void i2cListDevices() {\n" + + " for (int channel = 0; channel < 8; channel++) {\n" + + ' Serial.print("Scanning channel ");\n' + + " Serial.print(channel);\n" + + ' Serial.println(":");\n' + + " \n" + + " i2cSelectChannel(channel);\n" + + " \n" + + " for (DeviceAddress address : deviceMap) {\n" + + " Wire.beginTransmission(address.address);\n" + + " int error = Wire.endTransmission();\n" + + " \n" + + " if (error == 0) {\n" + + ' Serial.print("Found: ");\n' + + " Serial.print(address.device);\n" + + ' Serial.print(" at address 0x");\n' + + " \n" + + " if (address.address < 16) {\n" + + ' Serial.print("0");\n' + + " }\n" + + " Serial.println(address.address, HEX);\n" + + " }\n" + + " }\n" + + " \n" + + " i2cRestoreChannel();\n" + + " }\n" + + "}\n"; + + const DEVICE_CHANNEL_MAP = + "struct DeviceAddress { \n" + + " uint8_t address;\n" + + " char* device;\n" + + "};\n" + + "\n" + + "DeviceAddress deviceMap[] = {\n" + + ' {0x0D, "Compass"},\n' + + ' {0x29, "Color Sensor / ToF Sensor"},\n' + + ' {0x39, "RGB + Gesture Sensor"},\n' + + ' {0x3C, "Screen"},\n' + + ' {0x58, "Gas Sensor"},\n' + + ' {0x76, "Air Pressure Sensor"}\n' + + "};\n"; + + arduino.addSetup("serial", "Serial.begin(115200);", false); + addI2CDeclarations(); + arduino.addInclude("i2c_device_map", DEVICE_CHANNEL_MAP); + arduino.addDeclaration("i2c_list_devices", LIST_DEVICES); + + return "i2cListDevices();\n"; + }; + + arduino.forBlock["leaphy_segment_set"] = function (block) { + const num = + arduino.valueToCode(block, "NUM", arduino.ORDER_ATOMIC) || "0"; + + return `segment_display.showNumberDec(${num});\n`; + }; + + arduino.forBlock["leaphy_segment_clear"] = function () { + return "segment_display.clear();\n"; + }; + + arduino.forBlock["leaphy_segment_set_brightness"] = function (block) { + const brightness = + arduino.valueToCode(block, "BRIGHTNESS", arduino.ORDER_ATOMIC) || + "0"; + + return `segment_display.setBrightness(${brightness}/100*255);\n`; + }; + + arduino.forBlock["leaphy_matrix_set"] = function (block) { + const x = arduino.valueToCode(block, "X", arduino.ORDER_ATOMIC) || "0"; + const y = arduino.valueToCode(block, "Y", arduino.ORDER_ATOMIC) || "0"; + const on = + arduino.valueToCode(block, "ON", arduino.ORDER_ATOMIC) || "0"; + + return `matrix.setLed(0, ${y}, ${x}, ${on});\n`; + }; + + arduino.forBlock["leaphy_matrix_set_brightness"] = function (block) { + const brightness = + arduino.valueToCode(block, "BRIGHTNESS", arduino.ORDER_ATOMIC) || + "0"; + + return `matrix.setIntensity(0, ${brightness}/100*16);\n`; + }; + + arduino.forBlock["leaphy_matrix_clear"] = function () { + return `matrix.clearDisplay(0);\n`; + }; + + arduino.forBlock["leaphy_matrix_fill"] = function (block) { + const matrix = block.getFieldValue("MATRIX"); + + return ( + `matrix.setRow(0, 0, B${matrix[0].join("")});\n` + + `matrix.setRow(0, 1, B${matrix[1].join("")});\n` + + `matrix.setRow(0, 2, B${matrix[2].join("")});\n` + + `matrix.setRow(0, 3, B${matrix[3].join("")});\n` + + `matrix.setRow(0, 4, B${matrix[4].join("")});\n` + + `matrix.setRow(0, 5, B${matrix[5].join("")});\n` + + `matrix.setRow(0, 6, B${matrix[6].join("")});\n` + + `matrix.setRow(0, 7, B${matrix[7].join("")});\n` + ); + }; + + arduino.forBlock["leaphy_sound_play"] = function (block) { + const item = + arduino.valueToCode(block, "ITEM", arduino.ORDER_ATOMIC) || "0"; + + return `mp3.playWithIndex(${item});\n`; + }; + + arduino.forBlock["leaphy_sound_stop"] = function () { + return "mp3.stopPlay();\n"; + }; + + arduino.forBlock["leaphy_sound_set_volume"] = function (block) { + const volume = + arduino.valueToCode(block, "VOLUME", arduino.ORDER_ATOMIC) || "0"; + + return `mp3.setVolume(${volume}/100.0*30.0);\n`; + }; } export default getCodeGenerators; diff --git a/src/generators/arduino/leaphy_common.ts b/src/generators/arduino/leaphy_common.ts deleted file mode 100644 index 51dbc39..0000000 --- a/src/generators/arduino/leaphy_common.ts +++ /dev/null @@ -1,431 +0,0 @@ -import { addI2CDeclarations } from "./i2c"; -import { Arduino } from "../arduino"; - -function getCodeGenerators(arduino: Arduino) { - arduino.forBlock["leaphy_start"] = function (block) { - // Define the Start procedure - const funcName = "leaphyProgram"; - let branch = arduino.statementToCode(block, "STACK"); - if (arduino.STATEMENT_PREFIX) { - const id = block.id.replace(/\$/g, "$$$$"); // Issue 251. - branch = - arduino.prefixLines( - arduino.STATEMENT_PREFIX.replace(/%1/g, "'" + id + "'"), - arduino.INDENT, - ) + branch; - } - if (arduino.INFINITE_LOOP_TRAP) { - branch = - arduino.INFINITE_LOOP_TRAP.replace( - /%1/g, - "'" + block.id + "'", - ) + branch; - } - const returnType = "void"; - let code = returnType + " " + funcName + "() {\n" + branch + "}"; - - code = arduino.scrub_(block, code); - arduino.addDeclaration(funcName, code, true); - arduino.addSetup("userSetupCode", funcName + "();", false); - return null; - }; - - arduino.forBlock["leaphy_serial_available"] = function () { - arduino.addSetup("serial", "Serial.begin(115200);", false); - const code = "Serial.available()"; - return [code, arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_serial_read_line"] = function () { - arduino.addSetup("serial", "Serial.begin(115200);", false); - const code = "Serial.readStringUntil('\\n')"; - return [code, arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_serial_print_line"] = function (block) { - arduino.addSetup("serial", "Serial.begin(115200);", false); - const value = - arduino.valueToCode(block, "VALUE", arduino.ORDER_ATOMIC) || "0"; - return "Serial.println(" + value + ");\n"; - }; - - arduino.forBlock["leaphy_serial_print_value"] = function (block) { - arduino.addSetup("serial", "Serial.begin(115200);", false); - const name = - arduino.valueToCode(block, "NAME", arduino.ORDER_ATOMIC) || "0"; - const value = - arduino.valueToCode(block, "VALUE", arduino.ORDER_ATOMIC) || "0"; - return ( - `Serial.print(${name});\n` + - `Serial.print(" = ");\n` + - `Serial.println(${value});\n` - ); - }; - - arduino.forBlock["leaphy_compass_degrees"] = function () { - arduino.addInclude("leaphy_compass", "#include "); - arduino.addDeclaration("leaphy_compass", "QMC5883LCompass compass;"); - const setup = arduino.addI2CSetup( - "compass", - "compass.init();\n compass.setMagneticDeclination(2, 30);\n", - ); - arduino.addDeclaration( - "leaphy_compass_read", - "int getCompassDegrees() {\n" + - " " + - setup + - "\n" + - " compass.read();\n" + - " int azimuth = compass.getAzimuth();\n" + - " return round((azimuth > -0.5) ? azimuth : azimuth + 360);\n" + - "}\n", - ); - return ["getCompassDegrees()", arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_tof_get_distance"] = function () { - arduino.addInclude("leaphy_tof", "#include "); - arduino.addDeclaration("leaphy_tof", "Adafruit_VL53L0X i2c_distance;"); - const setup = arduino.addI2CSetup( - "tof", - "i2c_distance.begin();\n" + - " i2c_distance.setMeasurementTimingBudgetMicroSeconds(20000);\n", - ); - arduino.addDeclaration( - "leaphy_tof_read", - "int getTOF() {\n" + - ` ${setup}\n` + - " VL53L0X_RangingMeasurementData_t measure;\n" + - " i2c_distance.rangingTest(&measure, false);\n" + - " if (measure.RangeStatus == 4) return -1;\n" + - " return measure.RangeMilliMeter;\n" + - "}", - ); - return ["getTOF()", arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_get_air_pressure"] = function () { - arduino.addInclude("bmp280", "#include "); - arduino.addDeclaration("bmp280", "Adafruit_BMP280 bmp280;"); - const setup = arduino.addI2CSetup( - "bmp280", - "bmp280.begin(BMP280_ADDRESS_ALT);\n" + - " bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL,\n" + - " Adafruit_BMP280::SAMPLING_X2,\n" + - " Adafruit_BMP280::SAMPLING_X16,\n" + - " Adafruit_BMP280::FILTER_X16,\n" + - " Adafruit_BMP280::STANDBY_MS_500);\n", - ); - arduino.addDeclaration( - "bmp280_get_air_pressure", - "double getAirPressure() {\n" + - ` ${setup}\n` + - " return bmp280.readPressure() / 100;\n" + - "}", - ); - return ["getAirPressure()", arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_gas_sensor"] = function (block) { - arduino.addInclude("leaphy_gas_sensor", "#include "); - arduino.addDeclaration("leaphy_gas_sensor", "Adafruit_SGP30 sgp;"); - const setup = arduino.addI2CSetup( - "gas", - "if (! sgp.begin()) return -1;\n", - ); - - const gasValue = block.getFieldValue("GAS"); - let code = ""; - if (gasValue === "TVOC") { - code = "getGasValueTVOC()"; - arduino.addDeclaration( - "leaphy_gas_valueTVOC", - "int getGasValueTVOC() {\n" + - " " + - setup + - " sgp.IAQmeasure();\n" + - " return " + - "sgp.TVOC" + - ";\n}\n", - ); - } else if (gasValue === "eCO2") { - code = "getGasValueCOTWO()"; - arduino.addDeclaration( - "leaphy_gas_valueCOTWO", - "int getGasValueCOTWO() {\n" + - " " + - setup + - " sgp.IAQmeasure();\n" + - " return " + - "sgp.eCO2" + - ";\n}\n", - ); - } else if (gasValue === "Raw H2") { - code = "getGasValueHTWO()"; - arduino.addDeclaration( - "leaphy_gas_valueHTWO", - "int getGasValueHTWO() {\n" + - " " + - setup + - " sgp.IAQmeasureRaw();\n" + - " return " + - "sgp.rawH2" + - ";\n}\n", - ); - } else if (gasValue === "RAWETHANOL") { - code = "getGasValueETHANOL()"; - arduino.addDeclaration( - "leaphy_gas_valueETHANOL", - "int getGasValueETHANOL() {\n" + - " " + - setup + - " sgp.IAQmeasureRaw();\n" + - " return " + - "sgp.rawEthanol" + - ";\n}\n", - ); - } - return [code, arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_i2c_rgb_color"] = function (block) { - const setup = arduino.addI2CSetup("apds9960", "APDS.begin();\n"); - - const rgb_declaration = - "int r[8], g[8], b[8], a[8];\n" + - "int getAPDS9960Color(int colorType) {\n" + - " " + - setup + - " uint8_t channel = i2cGetChannel();\n" + - " if (APDS.colorAvailable()) {\n" + - " APDS.readColor(r[channel], g[channel], b[channel], a[channel]);\n" + - " }\n" + - " switch(colorType) {\n" + - " case 0:\n" + - " return r[channel];\n" + - " case 1:\n" + - " return g[channel];\n" + - " case 2:\n" + - " return b[channel];\n" + - " case 3:\n" + - " return a[channel];\n" + - " }\n" + - "}\n"; - const colorType = block.getFieldValue("COLOR_TYPE"); - - arduino.addInclude("apds9960", "#include "); - arduino.addDeclaration("apds9960_rgb", rgb_declaration); - const code = "getAPDS9960Color(" + colorType + ")"; - return [code, arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["leaphy_i2c_gesture"] = function () { - const setup = arduino.addI2CSetup("apds9960", "APDS.begin();\n"); - const gesture_declaration = - "int gesture[8];\n" + - "int getAPDS9960Gesture() {\n" + - " " + - setup + - " uint8_t channel = i2cGetChannel();\n" + - " if (APDS.gestureAvailable()) {\n" + - " gesture[channel] = APDS.readGesture();\n" + - " }\n" + - " return gesture[channel];\n" + - "}\n"; - arduino.addInclude("apds9960", "#include "); - arduino.addDeclaration("apds9960_gesture", gesture_declaration); - const code = "getAPDS9960Gesture()"; - return [code, arduino.ORDER_ATOMIC]; - }; - - arduino.forBlock["i2c_use_channel"] = function (block) { - const channel = block.getFieldValue("CHANNEL"); - const innerCode = arduino.statementToCode(block, "DO"); - - addI2CDeclarations(); - - return ( - "i2cSelectChannel(" + - channel + - ");\n" + - innerCode + - "i2cRestoreChannel();\n" - ); - }; - - arduino.forBlock["i2c_list_devices"] = function () { - const LIST_DEVICES = - "void i2cListDevices() {\n" + - " for (int channel = 0; channel < 8; channel++) {\n" + - ' Serial.print("Scanning channel ");\n' + - " Serial.print(channel);\n" + - ' Serial.println(":");\n' + - " \n" + - " i2cSelectChannel(channel);\n" + - " \n" + - " for (DeviceAddress address : deviceMap) {\n" + - " Wire.beginTransmission(address.address);\n" + - " int error = Wire.endTransmission();\n" + - " \n" + - " if (error == 0) {\n" + - ' Serial.print("Found: ");\n' + - " Serial.print(address.device);\n" + - ' Serial.print(" at address 0x");\n' + - " \n" + - " if (address.address < 16) {\n" + - ' Serial.print("0");\n' + - " }\n" + - " Serial.println(address.address, HEX);\n" + - " }\n" + - " }\n" + - " \n" + - " i2cRestoreChannel();\n" + - " }\n" + - "}\n"; - - const DEVICE_CHANNEL_MAP = - "struct DeviceAddress { \n" + - " uint8_t address;\n" + - " char* device;\n" + - "};\n" + - "\n" + - "DeviceAddress deviceMap[] = {\n" + - ' {0x0D, "Compass"},\n' + - ' {0x29, "Color Sensor / ToF Sensor"},\n' + - ' {0x39, "RGB + Gesture Sensor"},\n' + - ' {0x3C, "Screen"},\n' + - ' {0x58, "Gas Sensor"},\n' + - ' {0x76, "Air Pressure Sensor"}\n' + - "};\n"; - - arduino.addSetup("serial", "Serial.begin(115200);", false); - addI2CDeclarations(); - arduino.addInclude("i2c_device_map", DEVICE_CHANNEL_MAP); - arduino.addDeclaration("i2c_list_devices", LIST_DEVICES); - - return "i2cListDevices();\n"; - }; - - arduino.forBlock["leaphy_segment_init"] = function (block) { - const clk = block.getFieldValue("CLK"); - const dio = block.getFieldValue("DIO"); - - arduino.addInclude("tm1637", "#include "); - arduino.addDeclaration( - "segment", - `TM1637Display segment_display(${clk}, ${dio});`, - ); - arduino.addSetup( - "segment", - "segment_display.setBrightness(255);\n", - false, - ); - - return ""; - }; - - arduino.forBlock["leaphy_segment_set"] = function (block) { - const num = - arduino.valueToCode(block, "NUM", arduino.ORDER_ATOMIC) || "0"; - - return `segment_display.showNumberDec(${num});\n`; - }; - - arduino.forBlock["leaphy_segment_clear"] = function () { - return "segment_display.clear();\n"; - }; - - arduino.forBlock["leaphy_segment_set_brightness"] = function (block) { - const brightness = - arduino.valueToCode(block, "BRIGHTNESS", arduino.ORDER_ATOMIC) || - "0"; - - return `segment_display.setBrightness(${brightness}/100*255);\n`; - }; - - arduino.forBlock["leaphy_matrix_init"] = function (block) { - const din = block.getFieldValue("DIN"); - const clk = block.getFieldValue("CLK"); - const cs = block.getFieldValue("CS"); - - arduino.addInclude("matrix", "#include "); - arduino.addDeclaration( - "matrix", - `LedControl matrix = LedControl(${din}, ${clk}, ${cs}, 1);`, - ); - arduino.addSetup( - "matrix", - "matrix.shutdown(0, false);\n" + - " matrix.setIntensity(0, 8);\n" + - " matrix.clearDisplay(0);", - ); - - return ""; - }; - - arduino.forBlock["leaphy_matrix_set"] = function (block) { - const x = arduino.valueToCode(block, "X", arduino.ORDER_ATOMIC) || "0"; - const y = arduino.valueToCode(block, "Y", arduino.ORDER_ATOMIC) || "0"; - const on = - arduino.valueToCode(block, "ON", arduino.ORDER_ATOMIC) || "0"; - - return `matrix.setLed(0, ${y}, ${x}, ${on});\n`; - }; - - arduino.forBlock["leaphy_matrix_set_brightness"] = function (block) { - const brightness = - arduino.valueToCode(block, "BRIGHTNESS", arduino.ORDER_ATOMIC) || - "0"; - - return `matrix.setIntensity(0, ${brightness}/100*16);\n`; - }; - - arduino.forBlock["leaphy_matrix_clear"] = function () { - return `matrix.clearDisplay(0);\n`; - }; - - arduino.forBlock["leaphy_matrix_fill"] = function (block) { - const matrix = block.getFieldValue("MATRIX"); - - return ( - `matrix.setRow(0, 0, B${matrix[0].join("")});\n` + - `matrix.setRow(0, 1, B${matrix[1].join("")});\n` + - `matrix.setRow(0, 2, B${matrix[2].join("")});\n` + - `matrix.setRow(0, 3, B${matrix[3].join("")});\n` + - `matrix.setRow(0, 4, B${matrix[4].join("")});\n` + - `matrix.setRow(0, 5, B${matrix[5].join("")});\n` + - `matrix.setRow(0, 6, B${matrix[6].join("")});\n` + - `matrix.setRow(0, 7, B${matrix[7].join("")});\n` - ); - }; - - arduino.forBlock["leaphy_sound_init"] = function (block) { - const RX = block.getFieldValue("RX"); - const TX = block.getFieldValue("TX"); - - arduino.addInclude("sound", "#include "); - arduino.addDeclaration("sound", `MP3 mp3(${RX}, ${TX});`); - - return ""; - }; - - arduino.forBlock["leaphy_sound_play"] = function (block) { - const item = - arduino.valueToCode(block, "ITEM", arduino.ORDER_ATOMIC) || "0"; - - return `mp3.playWithIndex(${item});\n`; - }; - - arduino.forBlock["leaphy_sound_stop"] = function () { - return "mp3.stopPlay();\n"; - }; - - arduino.forBlock["leaphy_sound_set_volume"] = function (block) { - const volume = - arduino.valueToCode(block, "VOLUME", arduino.ORDER_ATOMIC) || "0"; - - return `mp3.setVolume(${volume}/100.0*30.0);\n`; - }; -} - -export default getCodeGenerators; diff --git a/src/generators/arduino/variable_blocks.ts b/src/generators/arduino/variable_blocks.ts new file mode 100644 index 0000000..a55e079 --- /dev/null +++ b/src/generators/arduino/variable_blocks.ts @@ -0,0 +1,53 @@ +import { Arduino } from "../arduino"; + +function getCodeGenerators(arduino: Arduino) { + arduino.forBlock["leaphy_segment_init"] = function (block) { + const clk = block.getFieldValue("CLK"); + const dio = block.getFieldValue("DIO"); + + arduino.addInclude("tm1637", "#include "); + arduino.addDeclaration( + "segment", + `TM1637Display segment_display(${clk}, ${dio});`, + ); + arduino.addSetup( + "segment", + "segment_display.setBrightness(255);\n", + false, + ); + + return ""; + }; + + arduino.forBlock["leaphy_matrix_init"] = function (block) { + const din = block.getFieldValue("DIN"); + const clk = block.getFieldValue("CLK"); + const cs = block.getFieldValue("CS"); + + arduino.addInclude("matrix", "#include "); + arduino.addDeclaration( + "matrix", + `LedControl matrix = LedControl(${din}, ${clk}, ${cs}, 1);`, + ); + arduino.addSetup( + "matrix", + "matrix.shutdown(0, false);\n" + + " matrix.setIntensity(0, 8);\n" + + " matrix.clearDisplay(0);", + ); + + return ""; + }; + + arduino.forBlock["leaphy_sound_init"] = function (block) { + const RX = block.getFieldValue("RX"); + const TX = block.getFieldValue("TX"); + + arduino.addInclude("sound", "#include "); + arduino.addDeclaration("sound", `MP3 mp3(${RX}, ${TX});`); + + return ""; + }; +} + +export default getCodeGenerators; diff --git a/src/index.ts b/src/index.ts index a234660..12f6673 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import translations from "./msg/translations"; import arduino from "./generators/arduino"; -import getBlocks from "./blocks/blocks"; +import { getBlocks, constantBlocks } from "./blocks/blocks"; export * as THEME from "./theme/theme"; export * as CATEGORIES from "./categories/all"; export * as EXTENSIONS from "./blocks/extensions"; -export { translations, arduino, getBlocks }; +export { translations, arduino, getBlocks, constantBlocks };