"use strict"; // Serial Blockly.Blocks['serial_write_data'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_on).appendField(new Blockly.FieldDropdown([[Blockly.Msg.serialcomm_write_on_iot, "iot"], [Blockly.Msg.serialcomm_write_on_ai, "ai"]]), "type"); this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_title_text); this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_data_bps_title).appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps").appendField(Blockly.Msg.serialcomm_write_data_bps_attrib); /*this.appendValueInput("first_input") .setCheck(null) .appendField("Field 0");*/ this.itemCount_ = 1; this.updateShape_(); this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item'])); this.setColour("#22b845"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('type'); var TOOLTIPS = { 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot), 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai) }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); }, mutationToDom: function mutationToDom() { var container = document.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, domToMutation: function domToMutation(xmlElement) { this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); this.updateShape_(); }, decompose: function decompose(workspace) { var containerBlock = workspace.newBlock('serial_write_data_create_with_container'); containerBlock.initSvg(); var connection = containerBlock.getInput('STACK').connection; for (var i = 0; i < this.itemCount_; i++) { var itemBlock = workspace.newBlock('serial_write_data_create_with_item'); itemBlock.initSvg(); connection.connect(itemBlock.previousConnection); connection = itemBlock.nextConnection; } return containerBlock; }, compose: function compose(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs. var connections = []; while (itemBlock) { connections.push(itemBlock.valueConnection_); itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } // Disconnect any children that don't belong. for (var i = 0; i < this.itemCount_; i++) { var connection = this.getInput('ADD' + i).connection.targetConnection; if (connection && connections.indexOf(connection) == -1) { connection.disconnect(); } } this.itemCount_ = connections.length; this.updateShape_(); // Reconnect any child blocks. for (var i = 0; i < this.itemCount_; i++) { Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i); } }, saveConnections: function saveConnections(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); var i = 0; while (itemBlock) { var input = this.getInput('ADD' + i); itemBlock.valueConnection_ = input && input.connection.targetConnection; i++; itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } }, updateShape_: function updateShape_() { for (var i = 0; i < this.itemCount_; i++) { if (!this.getInput('ADD' + i)) { var input = this.appendValueInput('ADD' + i); input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last); //input.appendField("資料 " + (i + 1) + ":"); //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i); // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i) } } while (this.getInput('ADD' + i)) { this.removeInput('ADD' + i); i++; } } }; Blockly.Blocks['serial_write_data_create_with_container'] = { init: function init() { this.setColour("#22b845"); this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_item); this.appendStatementInput('STACK'); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Blocks['serial_write_data_create_with_item'] = { init: function init() { this.setColour("#22b845"); this.appendDummyInput().appendField(Blockly.Msg.serialcomm_write_data); this.setPreviousStatement(true); this.setNextStatement(true); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Python['serial_write_data'] = function (block) { var api = block.getFieldValue('t_api'); var number_uart_bps = block.getFieldValue('uart_bps'); var dropdown_type = block.getFieldValue('type'); if (dropdown_type == "ai") { Blockly.Python.definitions_['import_machine_uart'] = 'import machine, time\n' + 'from fpioa_manager import fm\n' + '\n'; Blockly.Python.addVariable('uart_data', 'uart_data = ""', true); Blockly.Python.definitions_['uart_setup'] = '' + 'fm.register(13,fm.fpioa.UART2_TX)\n' + 'fm.register(14,fm.fpioa.UART2_RX)\n' + 'ai_uart = machine.UART(machine.UART.UART2,' + number_uart_bps + ',bits=8,parity=None,stop=1)\n' + ''; // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || ""; item_value = ''; var ai_uart_data = ""; for (var n = 0; n < this.itemCount_; n++) { item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || ''; ai_uart_data += 'str(' + item_value + ') + \"|\" + '; } var ai_uart_data_final = ai_uart_data + ""; var code = '' + 'uart_data = ' + ai_uart_data_final.slice(0, -8) + '\n' + 'ai_uart.write(b"SOF|" + str(uart_data) + "|\\r\\n")\n' + 'time.sleep_ms(20)\n' + ''; } else if (dropdown_type == "iot") { Blockly.Python.definitions_['import_machine_uart'] = 'from machine import UART, Pin\n' + 'import time\n' + '\n'; Blockly.Python.definitions_['uart_setup'] = '' + '_iot_uart = UART(2, tx=17, rx=16)\n' + '_iot_uart.init(115200, bits=8, parity=None, stop=1)\n' + ''; Blockly.Python.addVariable('_iot_uart_data', '_iot_uart_data = ""', true); item_value = ''; var iot_uart_data = ""; for (var n = 0; n < this.itemCount_; n++) { item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || ''; iot_uart_data += 'str(' + item_value + ') + \"|\" + '; } var iot_uart_data_final = iot_uart_data + ""; var code = '' + '_iot_uart_data = ' + iot_uart_data_final.slice(0, -8) + '\n' + '_iot_uart.write("SOF|" + str(_iot_uart_data) + "|\\r\\n")\n' + 'time.sleep_ms(20)\n' + ''; } return code; }; Blockly.Blocks['serial_send_data_to_microbit'] = { init: function init() { var types = getLocalStorage("type"); this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" })); types == 1 ? this.appendValueInput("NAME").appendField(Blockly.Msg.serial_send_data_on).setCheck(null).appendField(new Blockly.FieldDropdown([["A.I.", "ai"], ["IoT", "iot"]]), "module_type_microbit").appendField(Blockly.Msg.serial_send_data_on_send_data) : this.appendValueInput("NAME").appendField(Blockly.Msg.serial_send_data_on).setCheck(null).appendField(new Blockly.FieldDropdown([["IoT", "iot"], ["A.I.", "ai"]]), "module_type_microbit").appendField(Blockly.Msg.serial_send_data_on_send_data); this.setInputsInline(false); this.setColour("#22b845"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('module_type_microbit'); var TOOLTIPS = { 'iot': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "IoT"), 'ai': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "A.I.") }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); } }; Blockly.Python['serial_send_data_to_microbit'] = function (block) { var dropdown_module_type = block.getFieldValue('module_type_microbit'); var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || ""; // TODO: Assemble Python into code variable. var code = ''; if (dropdown_module_type == 'iot') { Blockly.Python.definitions_['import_microbit_iot_uart'] = "from machine import UART, Pin \nimport time\n"; var iot = "\n_iot_microbit_uart = UART(2, tx=17, rx=16)\n_iot_microbit_uart.init(115200, bits=8, parity=None, stop=1)"; Blockly.Python.addVariable('_iot_microbit_uart', iot, true); code = '\n' + "_iot_microbit_uart_data =" + value_name + "\n" + '_iot_microbit_uart.write(str(_iot_uart_data))\n' + "time.sleep_ms(20)\n" + ''; } else { Blockly.Python.definitions_['import_microbit_ai_uart'] = "import machine, time \nfrom fpioa_manager import fm\n"; var ai = "\nfm.register(13,fm.fpioa.UART2_TX)\nfm.register(14,fm.fpioa.UART2_RX)\nai_microbit_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)"; Blockly.Python.addVariable('ai_microbit_uart', ai, true); code = '\n' + "uart_microbit_data =" + value_name + "\n" + 'ai_microbit_uart.write(b""+str(uart_microbit_data)+"")\n' + "time.sleep_ms(20)\n" + ''; } return code; }; /* ____ / ___| ___ _ ____ _____ \___ \ / _ \ '__\ \ / / _ \ ___) | __/ | \ V / (_) | |____/ \___|_| \_/ \___/ */ var SERVO_BLOCK_COLOR = "#5bb2d6"; var ai_servo_timer = ["Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM", "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM"]; var ai_servo_pin = ['0', '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24']; Blockly.Blocks['extension_servo_setup_on_ai'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.servo_setup_ai); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(servoCloor); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['extension_servo_setup_on_ai'] = function (block) { Blockly.Python.definitions_.extension_servo_import = "from machine import Timer,PWM"; Blockly.Python.addSetup("extension_servo_setup", '' + 'def _ai_servo_write(servo,angle):\n' + '\tangle = angle - 90\n' + '\tleftSpan = 180 - 0\n' + '\trightSpan = 175 - 5\n' + '\tvalueScaled = float(angle - 0) / float(leftSpan)\n' + '\tservo.duty(((10 + (valueScaled * rightSpan))+90)/180*10+2.5)\n' + ''); var code = '\n'; return code; }; Blockly.Blocks['extension_servo_write_on_ai'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.servo_set_gpio_ai).appendField(new Blockly.FieldDropdown([["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["13", "4"], ["14", "5"], ["15", "6"], ["17", "7"], ["21", "8"], ["22", "9"], ["23", "10"], ["24", "11"]]), "gpio"); this.appendValueInput("degree").setCheck(null).appendField(Blockly.Msg.servo_rotate_to_ai); this.appendDummyInput().appendField(Blockly.Msg.servo_degree_ai); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(servoCloor); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['extension_servo_write_on_ai'] = function (block) { var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC); var value_gpio = block.getFieldValue('gpio'); Blockly.Python.addSetup("extension_servo_write_ai_" + value_gpio, '' + '_servo_tim1 = Timer(' + ai_servo_timer[value_gpio] + ')\n' + '_servo_gpio' + ai_servo_pin[value_gpio] + ' = PWM(_servo_tim1, freq=50, duty=0, pin=' + ai_servo_pin[value_gpio] + ')\n' + ''); var real_servo_degree = value_degree; var code = '' + '_ai_servo_write(_servo_gpio' + ai_servo_pin[value_gpio] + ',' + real_servo_degree + ')\n' + ''; return code; }; var servoCloor = "#386dc8"; Blockly.Blocks['esp32_main_controller_servo_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.servo_setup); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(servoCloor); this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['esp32_main_controller_servo_setup'] = function (block) { // TODO: Assemble Python into code variable. Blockly.Python.definitions_['definition_servo_setup'] = '' + 'from machine import Pin, PWM\n' + ''; var code = '\n'; return code; }; Blockly.Blocks['esp32_main_controller_servo_set'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.servo_set_gpio); this.appendValueInput("pin_select").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.servo_rotate_to); this.appendValueInput("degree").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.servo_degree); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(servoCloor); this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['esp32_main_controller_servo_set'] = function (block) { var value_pin_select = Blockly.Python.valueToCode(block, 'pin_select', Blockly.Python.ORDER_ATOMIC); var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. Blockly.Python.addVariable('S' + value_pin_select, 'S' + value_pin_select + ' = PWM(Pin(' + value_pin_select + '), freq=50, duty=0)', true); Blockly.Python.definitions_['definition_servo_set'] = '' + 'def Servo(servo,angle):\n' + ' angle = angle - 90\n' + ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' + ''; var code = '' + 'Servo(S' + value_pin_select + ',' + value_degree + ')\n' + ''; return code; }; /* __ __ _ | \/ | ___ | |_ ___ _ __ | |\/| |/ _ \| __/ _ \| '__| | | | | (_) | || (_) | | |_| |_|\___/ \__\___/|_| */ var MOTOR_BLOCK_COLOR = SERVO_BLOCK_COLOR; Blockly.Blocks['esp32_motor_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(MOTOR_BLOCK_COLOR); this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['esp32_motor_setup'] = function (block) { // TODO: Assemble Python into code variable. Blockly.Python.definitions_['esp32_controller_set_motor'] = '' + 'import pca9685, motor\n' + '\n' + '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' + '_iot_motor = motor.DCMotors(_iot_motor_i2c)'; var code = ''; return code; }; Blockly.Blocks['esp32_motor_run'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_turn).appendField(new Blockly.FieldDropdown([["A", "0"], ["B", "1"], ["C", "3"], ["D", "2"]]), "motor_type").appendField(Blockly.Msg.x_motor_set_motor_speed); this.appendValueInput("speed").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.x_motor_exec).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_motor_cw, "pos"], [Blockly.Msg.x_motor_acw, "neg"]]), "direction").appendField(Blockly.Msg.x_motor_set_motor_turns); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(MOTOR_BLOCK_COLOR); this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['esp32_motor_run'] = function (block) { var dropdown_motor_type = block.getFieldValue('motor_type'); var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('direction'); if (dropdown_direction == "pos") { value_speed = value_speed; } else if (dropdown_direction == "neg") { value_speed = -value_speed; } var code = '_iot_motor.speed(' + dropdown_motor_type + ',(' + value_speed + '*16))\n'; return code; }; Blockly.Blocks['ai_motor_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_ai); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(MOTOR_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_motor_setup'] = function (block) { // TODO: Assemble Python into code variable. Blockly.Python.definitions_['ai_controller_set_motor'] = '' + 'import pca9685, motor\n' + 'from machine import I2C\n' + '\n' + '_ai_motor_scl = 30\n' + '_ai_motor_sda = 29\n' + '\n' + '_ai_motor_i2c = I2C(I2C.I2C0, freq=100000, scl=_ai_motor_scl, sda=_ai_motor_sda)\n' + '_ai_motor = motor.DCMotors(_ai_motor_i2c)\n' + ''; var code = ''; return code; }; Blockly.Blocks['ai_motor_run'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_motor_set_motor_turn_ai).appendField(new Blockly.FieldDropdown([["A", "0"], ["B", "1"], ["C", "3"], ["D", "2"]]), "motor_type").appendField(Blockly.Msg.x_motor_set_motor_speed_ai); this.appendValueInput("speed").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.x_motor_exec_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_motor_cw_ai, "pos"], [Blockly.Msg.x_motor_acw_ai, "neg"]]), "direction").appendField(Blockly.Msg.x_motor_set_motor_turns_ai); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(MOTOR_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_motor_run'] = function (block) { var dropdown_motor_type = block.getFieldValue('motor_type'); var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('direction'); if (dropdown_direction == "pos") { value_speed = value_speed; } else if (dropdown_direction == "neg") { value_speed = -value_speed; } var code = '_ai_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n'; return code; }; /* _ _____ ____ __ __ _ _ | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __ | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ / | |___| |___| |_| | | | | | (_| | |_| | | |> < |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\ */ var AILEDCOLOR = "#e8795b"; Blockly.Blocks['ai_led_matrix_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_setup_ai); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_setup'] = function (block) { Blockly.Python.definitions_['ai_controller_led_setup'] = '' + 'from modules import ws2812\n' + '\n' + '_ai_led_matrix_total = 36\n' + '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' + ''; // TODO: Assemble Python into code variable. var code = ''; return code; }; Blockly.Blocks['ai_led_matrix_xy'] = { init: function init() { this.appendValueInput("x").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai_x); this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai_y); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_xy'] = function (block) { var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC); var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = value_x + ',' + value_y; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['ai_led_matrix_wh'] = { init: function init() { this.appendValueInput("w").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_ai); this.appendValueInput("h").setCheck(null).appendField(Blockly.Msg.x_led_matrix_height_ai); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_wh'] = function (block) { var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC); var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = value_w + ',' + value_h; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['ai_led_matrix_color_picker'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_color_ai).appendField(new Blockly.FieldColour("#ff0000"), "color"); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_color_picker'] = function (block) { var color = block.getFieldValue('color'); var d = 0, e = 0, f = 0; try { 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16)); } catch (g) { } // TODO: Assemble Python into code variable. var code = d + ',' + e + ',' + f; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['ai_led_matrix_draw_pixel'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_title_ai); this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai); this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_color_ai); this.appendValueInput("coordinate").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) { var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC); var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = '_a = _ai_led_matrix.set_led((((' + value_coordinate.substr(3, 1) + '*6)//6)-1)*6+(' + value_coordinate.substr(1, 1) + '-1),(' + value_color + '))\n'; return code; }; Blockly.Blocks['ai_led_matrix_show_above'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_show_above_ai); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['ai_led_matrix_show_above'] = function (block) { // TODO: Assemble Python into code variable. var code = '_ai_led_matrix.display()\n'; return code; }; Blockly.Blocks['iot_led_matrix_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_setup_iot); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_setup'] = function (block) { Blockly.Python.definitions_['iot_controller_led_setup'] = '' + 'import machine, neopixel, time\n' + '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(15), 36)\n' + '\n'; // TODO: Assemble Python into code variable. var code = ''; return code; }; Blockly.Blocks['iot_led_matrix_xy'] = { init: function init() { this.appendValueInput("x").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot_x); this.appendValueInput("y").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot_y); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_xy'] = function (block) { var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC); var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = value_x + ',' + value_y; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_led_matrix_wh'] = { init: function init() { this.appendValueInput("w").setCheck(null).appendField(Blockly.Msg.x_led_matrix_width_iot); this.appendValueInput("h").setCheck(null).appendField(Blockly.Msg.x_led_matrix_height_iot); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_wh'] = function (block) { var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC); var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = value_w + ',' + value_h; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_led_matrix_color_picker'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_color_iot).appendField(new Blockly.FieldColour("#ff0000"), "color"); this.setInputsInline(true); this.setOutput(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_color_picker'] = function (block) { var color = block.getFieldValue('color'); var d = 0, e = 0, f = 0; try { 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16)); } catch (g) { } // TODO: Assemble Python into code variable. var code = d + ',' + e + ',' + f; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_led_matrix_draw_pixel'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_title_iot); this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot); this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_color_iot); this.appendValueInput("coordinate").setCheck(null).appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) { var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC); var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255) var code = '_6x6_led_matrix[(((' + value_coordinate.substr(3, 1) + '*6)//6)-1)*6+(' + value_coordinate.substr(1, 1) + '-1)] = (' + value_color + ')\n'; return code; }; Blockly.Blocks['iot_led_matrix_show_above'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_led_matrix_show_above_iot); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(AILEDCOLOR); this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['iot_led_matrix_show_above'] = function (block) { // TODO: Assemble Python into code variable. var code = '_6x6_led_matrix.write()\n'; return code; }; /* _ _____ ____ ____ _ _ | | | ____| _ \ / ___|| |_ _ __(_)_ __ | | | _| | | | | \___ \| __| '__| | '_ \ | |___| |___| |_| | ___) | |_| | | | |_) | |_____|_____|____/ |____/ \__|_| |_| .__/ |_| */ var ESP32_LED_STRIP_COLOR = "#3dade2"; Blockly.Blocks['esp32_main_controller_led_strip_setup'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.ledstrip_setup_text).appendField(new Blockly.FieldVariable("STRIP"), "varitem").appendField(Blockly.Msg.ledstrip_setup); this.appendValueInput("io").setCheck(null).appendField(Blockly.Msg.ledstrip_set_gpio); this.appendValueInput("count").setCheck(null).appendField(Blockly.Msg.ledstrip_set_total_led); this.setInputsInline(false); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(ESP32_LED_STRIP_COLOR); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('type'); var TOOLTIPS = { 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot), 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai) }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); } }; Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) { var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE); var value_io = Blockly.Python.valueToCode(block, 'io', Blockly.Python.ORDER_ATOMIC); var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC); Blockly.Python.definitions_['led_strip_definition'] = 'import machine, neopixel\n'; Blockly.Python.addVariable(variable_name, variable_name + ' = neopixel.NeoPixel(machine.Pin(' + value_io + '), ' + value_count + ')', true); // TODO: Assemble Python into code variable. var code = '\n'; return code; }; Blockly.Blocks['esp32_main_controller_led_strip_set'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.ledstrip_set_light_up).appendField(new Blockly.FieldVariable("STRIP"), "varitem"); this.appendValueInput("location").setCheck(null).appendField(Blockly.Msg.ledstrip_set_no); this.appendValueInput("color").setCheck(null).appendField(Blockly.Msg.ledstrip_set_color); this.setInputsInline(true); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(ESP32_LED_STRIP_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) { var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE); var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC); var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. if (value_color.charAt(0) != '#') { var code = '' + 'np[' + value_location + '] = ' + value_color + '\n' + 'np.write()\n' + '\n'; } else if (value_color.charAt(0) == '#') { var d = 0, e = 0, f = 0; try { 7 == color.length && (d = parseInt(color.substring(1, 3), 16), e = parseInt(color.substring(3, 5), 16), f = parseInt(color.substring(5, 7), 16)); } catch (g) { } var code = '' + 'np[' + value_location + '] = (' + d + ',' + e + ',' + f + ')\n' + 'np.write()\n' + '\n'; } return code; }; /* ____ ____ _ / ___| __ _ _ __ ___ ___| _ \ __ _ __| | | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` | | |_| | (_| | | | | | | __/ __/ (_| | (_| | \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_| */ var GAMEPAD_COLOR = "#183895"; Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('type'); var TOOLTIPS = { 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"), 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"), 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"), 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"), 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"), 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R") }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); } }; Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_p' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_iot_read_gamepad_button_released'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('type'); var TOOLTIPS = { 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"), 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"), 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"), 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"), 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"), 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R") }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); } }; Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_p' + dropdown_type + '.value() == 0'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); var thisBlock = this; this.setTooltip(function () { var mode = thisBlock.getFieldValue('type'); var TOOLTIPS = { 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"), 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"), 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"), 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"), 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"), 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R") }; return TOOLTIPS[mode]; }); this.setHelpUrl(""); } }; Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def_iot'] = '' + '_px = Pin(21, Pin.IN,Pin.PULL_UP) \n' + '_py = Pin(22, Pin.IN,Pin.PULL_UP) \n' + '_pa = Pin(4, Pin.IN,Pin.PULL_UP) \n' + '_pb = Pin(2, Pin.IN,Pin.PULL_UP) \n' + '_pl = Pin(17, Pin.IN,Pin.PULL_UP) \n' + '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_p' + dropdown_type + '.value()'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_iot_read_gamepad_joystick'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"], [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]]), "type").appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n'; Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' + '_adcX = machine.ADC(Pin(36))\n' + '_adcY = machine.ADC(Pin(39))\n' + '\n' + '_adcX.atten(ADC.ATTN_11DB)\n' + '_adcY.atten(ADC.ATTN_11DB)\n' + '_adcX.width(ADC.WIDTH_10BIT)\n' + '_adcY.width(ADC.WIDTH_10BIT)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_adc' + dropdown_type + '.read()'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"], [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"], [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"], [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"], [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"]]), "type"); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def_iot'] = '' + 'from machine import ADC, Pin\n' + '\n'; Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' + '_adcX = machine.ADC(Pin(36))\n' + '_adcY = machine.ADC(Pin(39))\n' + '\n' + '_adcX.atten(ADC.ATTN_11DB)\n' + '_adcY.atten(ADC.ATTN_11DB)\n' + '_adcX.width(ADC.WIDTH_10BIT)\n' + '_adcY.width(ADC.WIDTH_10BIT)\n' + '\n'; Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' + 'def _read_direction():\n' + ' if (_adcY > 300) and (_adcY < 500) and (_adcX > 300) and (_adcX < 500):\n' + ' return "still" \n' + ' elif _adcX > 500:\n' + ' return "left"\n' + ' elif _adcX < 300:\n' + ' return "right"\n' + ' elif _adcY > 500:\n' + ' return "top"\n' + ' elif _adcY < 300:\n' + ' return "bottom"\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_read_direction() == "' + dropdown_type + '"'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 17, 15]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_gp_button_' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_ai_read_gamepad_button_released'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 15, 17]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_gp_button_' + dropdown_type + '.value() == 1'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai).appendField(new Blockly.FieldDropdown([["X", "x"], ["Y", "y"], ["A", "a"], ["B", "b"], ["L", "l"], ["R", "r"]]), "type").appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_all_def'] = '' + 'from fpioa_manager import *\n' + 'from Maix import FPIOA, GPIO\n' + '\n'; Blockly.Python.definitions_['gamepad_button_def'] = '' + '_gp_side_button = [13, 14]\n' + '_gp_right_button = [29, 30, 15, 17]\n' + 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' + 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' + 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' + 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' + 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' + 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' + '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' + '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' + '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' + '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' + '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' + '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_gp_button_' + dropdown_type + '.value()'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_ai_read_gamepad_joystick'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"], [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]]), "type").appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_joystick_def'] = '' + 'from machine import I2C\n' + 'from ads1x15 import ADS1115\n' + '\n' + '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=24, sda=23)\n' + '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' + '\n'; // TODO: Assemble Python into code variable. var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai).appendField(new Blockly.FieldDropdown([[Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"], [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"], [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"], [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"], [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"]]), "type"); this.setInputsInline(true); this.setOutput(true, null); this.setColour(GAMEPAD_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) { var dropdown_type = block.getFieldValue('type'); Blockly.Python.definitions_['gamepad_joystick_def'] = '' + 'from machine import I2C\n' + 'from ads1x15 import ADS1115\n' + '\n' + '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=24, sda=23)\n' + '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' + '\n'; // TODO: Assemble Python into code variable. '\n'; Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' + 'def _read_direction():\n' + ' if (int(_joystick_adc.read(channel1=0)/25.75) > 400) and (int(_joystick_adc.read(channel1=0)/25.75) < 600) and (int(_joystick_adc.read(channel1=1)/25.75) > 400) and (int(_joystick_adc.read(channel1=1)/25.75) < 600):\n' + ' return "still" \n' + ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' + ' return "left"\n' + ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' + ' return "right"\n' + ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' + ' return "top"\n' + ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' + ' return "bottom"\n' + '\n'; // TODO: Assemble Python into code variable. var code = '_read_direction() == "' + dropdown_type + '"'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; /* __ ___ _____ _____ /\ \ \/ __\ \_ \___/__ \ / \/ /__\//_____ / /\/ _ \ / /\/ / /\ / \/ \_____/\/ /_| (_) / / \_\ \/\_____/ \____/ \___/\/ Blockly.Python.addVariable('', '', true); Blockly.Python.definitions_['definition'] = ''; Blockly.Python.addSetup("setup", ''); */ var ESP32_NBIOT_COLOR = "#629978"; Blockly.Blocks['esp32_nbiot_setup'] = { init: function init() { var type = getLocalStorage("type"); this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, { alt: "*", flipRtl: "FALSE" })); type == 1 ? this.appendDummyInput().appendField(new Blockly.FieldDropdown([[Blockly.Msg.nbiot_module_which_module_ai, "ai"], [Blockly.Msg.nbiot_module_which_module_iot, "iot"]]), "module_type") : this.appendDummyInput().appendField(new Blockly.FieldDropdown([[Blockly.Msg.nbiot_module_which_module_iot, "iot"], [Blockly.Msg.nbiot_module_which_module_ai, "ai"]]), "module_type"); this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_init); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(ESP32_NBIOT_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_setup'] = function (block) { // TODO: Assemble Python into code variable. var dropdown_module_type = block.getFieldValue('module_type'); if (dropdown_module_type == "iot") { Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true); Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true); Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true); Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true); Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true); Blockly.Python.definitions_['import_definition'] = '' + 'from machine import UART\n' + 'from machine import Timer\n' + 'import ubinascii as binascii\n' + 'import time\n' + '\n' + '_nbiot_serial = UART(2,rx=17, tx=16, baudrate=115200, timeout=10)\n' + '\n' + '_nbiot_serial_timer = Timer(1)\n' + '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' + '\n' + 'def _read_nbiot_serial(uart):\n' + ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' + ' if _nbiot_serial.any():\n' + ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' + ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' + ' _nbiot_readings = _nbiot_serial_read.strip()\n' + '\n' + ' if _nbiot_serial_read == "+CPIN: READY":\n' + ' _nbiot_power_on_status = True\n' + ' print("NB-IoT Module Initiated!\\n")\n' + ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' + '\n' + ' if "+IP" in _nbiot_serial_read:\n' + ' _nbiot_internet_status = True\n' + ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' + ''; var code = ''; } else if (dropdown_module_type == "ai") { Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true); Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true); Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true); Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true); Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true); Blockly.Python.definitions_['import_definition'] = '' + 'import machine, time\n' + 'from fpioa_manager import fm\n' + 'from machine import Timer\n' + '\n' + 'fm.register(14,fm.fpioa.UART2_TX)\n' + 'fm.register(13,fm.fpioa.UART2_RX)\n' + '\n' + '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' + '\n' + 'def _on_timer(timer):\n' + ' _read_nbiot_serial(_nbiot_serial)\n' + '\n' + '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' + '_periodic_tim.start()\n' + '\n' + 'def _read_nbiot_serial(uart):\n' + ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' + ' if _nbiot_serial.any():\n' + ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' + ' print(_nbiot_serial_read)\n' + ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' + ' _nbiot_readings = _nbiot_serial_read.strip()\n' + '\n' + ' if _nbiot_serial_read == "+CPIN: READY":\n' + ' _nbiot_power_on_status = True\n' + ' print("NB-IoT Module Initiated!\\n")\n' + ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' + '\n' + ' if "+IP" in _nbiot_serial_read:\n' + ' _nbiot_internet_status = True\n' + ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' + ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' + '\n' + '_nbiot_serial_read = ""\n' + '_nbiot_readings = ""\n' + '_nbiot_power_on_status = False\n' + '_nbiot_internet_status = False\n' + '_nbiot_internet_ip_address = ""\n' + ''; var code = ''; } return code; }; Blockly.Blocks['esp32_nbiot_get_power_status'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_power); this.setOutput(true, null); this.setColour(ESP32_NBIOT_COLOR); this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_get_power_status'] = function (block) { // TODO: Assemble Python into code variable. var code = '_nbiot_power_on_status'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['esp32_nbiot_get_network_status'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_connectivity); this.setOutput(true, null); this.setColour(ESP32_NBIOT_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_get_network_status'] = function (block) { // TODO: Assemble Python into code variable. var code = '_nbiot_internet_status'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['esp32_nbiot_get_network_ip'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_module_ip); this.setOutput(true, null); this.setColour(ESP32_NBIOT_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) { // TODO: Assemble Python into code variable. var code = '_nbiot_internet_ip_address'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['esp32_nbiot_onenet_init'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_title); this.appendValueInput("product_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_productid); this.appendValueInput("device_id").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_deviceid); this.appendValueInput("device_apikey").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_connect_device_api); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(ONENET_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_onenet_init'] = function (block) { var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC); var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC); var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true); Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true); Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true); Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true); Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true); Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true); Blockly.Python.addSetup("setup", '' + '_nbiot_onenet_init_state = False\n' + '_nbiot_onenet_connection_status = False\n' + '_nbiot_onenet_read_anything = False\n' + '_nbiot_onenet_product_id = ' + value_product_id + '\n' + '_nbiot_onenet_device_id = ' + value_device_id + '\n' + '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' + '\n' + 'def _nbiot_send_onenet_data(data):\n' + ' _nbiot_send_message = data\n' + ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' + ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' + ' _nbiot_send_at_content = "AT+MQTTPUB=$dp,0,1,0," + str(len(_nbiot_send_message)+len(_nbiot_send_header[1:7].replace("0",""))) + ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1]) + str(_nbiot_send_data_content)\n' + ' return _nbiot_send_at_content\n' + '\n' + 'def _nbiot_read_onenet_data(raw_response):\n' + ' _nbiot_receive = raw_response\n' + ' if "+MQTTPUBLISH" not in raw_response:\n' + ' return ""\n' + ' elif "+MQTTPUBLISH" in raw_response:\n' + ' _nbiot_occur = -1\n' + ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' + ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' + '\n' + 'def _read_nbiot_onenet(_incoming_data):\n' + ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' + '\n' + ' if "+MQTTOPEN: OK" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = True\n' + ' # print("OneNET Connected!")\n' + ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' + ' _nbiot_onenet_connection_status = False\n' + ' # print("OneNET Lost Connection!")\n' + '\n' + ' if "+MQTTPUBLISH" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = True\n' + ' else:\n' + ' _nbiot_onenet_read_anything = False\n' + '\n' + ' if "+MQTTDISC" in _incoming_data:\n' + ' _nbiot_onenet_read_anything = False\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + '\n' + 'def _init_onenet_connection():\n' + ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' + ' print("Enabling OneNET Connection...")\n' + ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' + ' time.sleep_ms(100)\n' + ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' + ' time.sleep_ms(100)\n' + ''); var code = '' + 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' + ' _init_onenet_connection()\n' + ' _nbiot_onenet_init_state = True\n' + '\n' + '_read_nbiot_onenet(_nbiot_readings)\n' + ''; return code; }; Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_connectivity); this.setOutput(true, null); this.setColour(ONENET_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) { // TODO: Assemble Python into code variable. var code = '_nbiot_onenet_connection_status'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_service_nbiot_onenet'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_send_title); /*this.appendValueInput("first_input") .setCheck(null) .appendField("Field 0");*/ this.itemCount_ = 1; this.updateShape_(); this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item'])); this.setColour(ONENET_BLOCK_COLOR); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setTooltip(""); this.setHelpUrl(""); }, mutationToDom: function mutationToDom() { var container = document.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, domToMutation: function domToMutation(xmlElement) { this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); this.updateShape_(); }, decompose: function decompose(workspace) { var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container'); containerBlock.initSvg(); var connection = containerBlock.getInput('STACK').connection; for (var i = 0; i < this.itemCount_; i++) { var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item'); itemBlock.initSvg(); connection.connect(itemBlock.previousConnection); connection = itemBlock.nextConnection; } return containerBlock; }, compose: function compose(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs. var connections = []; while (itemBlock) { connections.push(itemBlock.valueConnection_); itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } // Disconnect any children that don't belong. for (var i = 0; i < this.itemCount_; i++) { var connection = this.getInput('ADD' + i).connection.targetConnection; if (connection && connections.indexOf(connection) == -1) { connection.disconnect(); } } this.itemCount_ = connections.length; this.updateShape_(); // Reconnect any child blocks. for (var i = 0; i < this.itemCount_; i++) { Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i); } }, saveConnections: function saveConnections(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); var i = 0; while (itemBlock) { var input = this.getInput('ADD' + i); itemBlock.valueConnection_ = input && input.connection.targetConnection; i++; itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } }, updateShape_: function updateShape_() { for (var i = 0; i < this.itemCount_; i++) { if (!this.getInput('ADD' + i)) { var input = this.appendValueInput('ADD' + i); input.appendField(Blockly.Msg.nbiot_onenet_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":"); //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i); // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i) } } while (this.getInput('ADD' + i)) { this.removeInput('ADD' + i); i++; } } }; Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = { init: function init() { this.setColour(ONENET_BLOCK_COLOR); this.appendDummyInput().appendField("Items"); this.appendStatementInput('STACK'); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = { init: function init() { this.setColour(ONENET_BLOCK_COLOR); this.appendDummyInput().appendField("Field"); this.setPreviousStatement(true); this.setNextStatement(true); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Python['iot_service_nbiot_onenet'] = function (block) { // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || ""; var item_field = '', item_value = ''; var nbiot_onenet_data = "'{"; for (var n = 0; n < this.itemCount_; n++) { item_field = block.getFieldValue("field" + n); item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || ''; nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",'; } var nbiot_onenet_data_final = nbiot_onenet_data + "}'"; var code = '' + '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' + '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' + '_data_for_at = _data_add_rn.encode()\n' + '_nbiot_serial.write(_data_for_at)\n' + // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' + '\n' + ''; return code; }; Blockly.Blocks['iot_service_nbiot_onenet_read'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg); this.appendValueInput("message").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when); this.appendStatementInput("exec").setCheck(null).appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(ONENET_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) { var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC); var statements_exec = Blockly.Python.statementToCode(block, 'exec'); // TODO: Assemble Python into code variable. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' + statements_exec + '\n'; return code; }; Blockly.Blocks['esp32_nbiot_onenet_read_value'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_onenet_receive_command); this.setOutput(true, null); this.setColour(ONENET_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) { // TODO: Assemble Python into code variable. var code = '_nbiot_read_onenet_data(_nbiot_readings)'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_service_nbiot_cococloud_read'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_title); this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_event_api).appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(COCOCLOUD_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) { var api = block.getFieldValue('t_api'); // TODO: Assemble Python into code variable. var code = '\n'; return code; }; Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = { init: function init() { this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_property); this.appendValueInput("property").setCheck(null); this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_get_data); this.setInputsInline(true); this.setOutput(true, null); this.setColour(COCOCLOUD_BLOCK_COLOR); this.setTooltip(""); this.setHelpUrl(""); } }; Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) { var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. var code = '\n'; // TODO: Change ORDER_NONE to the correct strength. return [code, Blockly.Python.ORDER_NONE]; }; Blockly.Blocks['iot_service_nbiot_cococloud'] = { init: function init() { this.appendDummyInput().appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, { alt: "*", flipRtl: "FALSE" })); this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_title); this.appendDummyInput().appendField(Blockly.Msg.nbiot_cococloud_send_api).appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api"); /*this.appendValueInput("first_input") .setCheck(null) .appendField("Field 0");*/ this.itemCount_ = 1; this.updateShape_(); this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item'])); this.setColour(COCOCLOUD_BLOCK_COLOR); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP); this.setHelpUrl(""); }, mutationToDom: function mutationToDom() { var container = document.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, domToMutation: function domToMutation(xmlElement) { this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); this.updateShape_(); }, decompose: function decompose(workspace) { var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container'); containerBlock.initSvg(); var connection = containerBlock.getInput('STACK').connection; for (var i = 0; i < this.itemCount_; i++) { var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item'); itemBlock.initSvg(); connection.connect(itemBlock.previousConnection); connection = itemBlock.nextConnection; } return containerBlock; }, compose: function compose(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); // Count number of inputs. var connections = []; while (itemBlock) { connections.push(itemBlock.valueConnection_); itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } // Disconnect any children that don't belong. for (var i = 0; i < this.itemCount_; i++) { var connection = this.getInput('ADD' + i).connection.targetConnection; if (connection && connections.indexOf(connection) == -1) { connection.disconnect(); } } this.itemCount_ = connections.length; this.updateShape_(); // Reconnect any child blocks. for (var i = 0; i < this.itemCount_; i++) { Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i); } }, saveConnections: function saveConnections(containerBlock) { var itemBlock = containerBlock.getInputTargetBlock('STACK'); var i = 0; while (itemBlock) { var input = this.getInput('ADD' + i); itemBlock.valueConnection_ = input && input.connection.targetConnection; i++; itemBlock = itemBlock.nextConnection && itemBlock.nextConnection.targetBlock(); } }, updateShape_: function updateShape_() { for (var i = 0; i < this.itemCount_; i++) { if (!this.getInput('ADD' + i)) { var input = this.appendValueInput('ADD' + i); input.appendField(Blockly.Msg.nbiot_cococloud_send_property).appendField(new Blockly.FieldTextInput("Property" + i), "field" + i); //input.appendField("資料 " + (i + 1) + ":"); //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i); // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i) } } while (this.getInput('ADD' + i)) { this.removeInput('ADD' + i); i++; } } }; Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = { init: function init() { this.setColour(ESP32_NBIOT_COLOR); this.appendDummyInput().appendField("Items"); this.appendStatementInput('STACK'); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = { init: function init() { this.setColour(COCOCLOUD_BLOCK_COLOR); this.appendDummyInput().appendField("Field"); this.setPreviousStatement(true); this.setNextStatement(true); this.setTooltip(''); this.contextMenu = false; } }; Blockly.Python['iot_service_nbiot_cococloud'] = function (block) { var api = block.getFieldValue('t_api'); Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true); Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true); Blockly.Python.definitions_['import'] = '' + '_nbiot_cococloud_send_init_state = False\n' + '\n' + 'def _nbiot_cococloud_send(data):\n' + ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' + ' print(_nbiot_cococloud_send_data)\n' + ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.cn/\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' + ' time.sleep_ms(500)\n' + ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' + ' time.sleep_ms(500)\n' + ''; var item_field = '', item_value = ''; var nbiot_cococloud_data = "'{"; for (var n = 0; n < this.itemCount_; n++) { item_field = block.getFieldValue("field" + n); item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || ''; nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",'; } var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'"; var code = '' + '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' + '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' + ''; return code; };