123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- 'use strict';
- goog.provide('Blockly.Python.mainwifi');
- goog.require('Blockly.Python');
- Blockly.Python['mainwifi_setup'] = function (block) {
- let serial_setup = "Serial.begin(9600);";
- Blockly.Python.addSetup('serial_9600', serial_setup);
- return '';
- };
- Blockly.Python['mainwifi_sendMsg'] = function (block) {
- var checkbit = "#define SOP '<'\n" +
- "#define EOP '>'\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
- var datavar = Blockly.Python.variableDB_.getName(
- block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
- var length = "sizeof(" + datavar + ")/sizeof(" + datavar + "[0])";
- var dataLength = length + "+2";
- var code = "unsigned char DataOut_[" + dataLength + "]\n" +
- "DataOut_[0]= SOP\n" +
- "DataOut_[" + length + "+1]= EOP\n" +
- "for (int i=0; i<" + length + ";i++) {\n" +
- " DataOut_[i+1]=" + datavar + "[i]\n" +
- "}\n" +
- "Serial1.write(DataOut_, " + dataLength + ")\n";
- return code;
- };
- Blockly.Python['mainwifi_receiveMsg'] = function (block) {
- var checkbit = "#define SOP '<'\n" +
- "#define EOP '>'\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
- var MsgReceiveStatus = "bool started = false\n" +
- "bool ended = false\n" +
- "int index_i\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
- var datavar = Blockly.Python.variableDB_.getName(
- block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
- var length = 'sizeof(' + datavar + ')/sizeof(' + datavar + '[0])' || 1;
- var dataIn_function = "int handleDataIn() {\n" +
- " while (Serial1.available() > 0) {\n" +
- " int inChar = Serial1.read()\n" +
- " if(inChar == SOP) {\n" +
- " index_i = 0\n" +
- " started = true\n" +
- " ended = false\n" +
- " } else if(inChar == EOP) {\n" +
- " ended = true\n" +
- " break\n" +
- " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
- " " + datavar + "[index_i] = inChar\n" +
- " index_i++\n" +
- " }\n" +
- " }\n" +
- " if(started && ended) {\n" +
- " return 1\n" +
- " } else {\n" +
- " return 0\n" +
- " }\n" +
- "}\n";
- Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
- var code = "boolean receiveMsgsuccess_ = handleDataIn()\n";
- return code;
- };
- Blockly.Python['mainwifi_receiveMsg_isSuccessed'] = function (block) {
- var code = "receiveMsgsuccess_";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- // easymode
- Blockly.Python['MainEasymode_transfer_send'] = function (block) {
- var dataMain = Blockly.Python.variableDB_.getName(
- block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
- var checkbit = "#define SOP '<'\n" +
- "#define EOP '>'\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
- var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
- var dataLength = length + "+2";
- var code = "unsigned char DataOut_[" + dataLength + "]\n" +
- "DataOut_[0]= SOP\n" +
- "DataOut_[" + length + "+1]= EOP\n" +
- "for (int i=0; i<" + length + ";i++) {\n" +
- " DataOut_[i+1]=" + dataMain + "[i]\n" +
- "}\n" +
- "Serial1.write(DataOut_, " + dataLength + ")\n";
- return code;
- }
- Blockly.Python['MainEasymode_transfer_receive'] = function (block) {
- var dataMain = Blockly.Python.variableDB_.getName(
- block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
- Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
- var checkbit = "#define SOP '<'\n" +
- "#define EOP '>'\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
- var MsgReceiveStatus = "bool started = false\n" +
- "bool ended = false\n" +
- "int index_i\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
- var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
- var dataIn_function = "int handleDataIn() {\n" +
- " while (Serial1.available() > 0) {\n" +
- " int inChar = Serial1.read()\n" +
- " if(inChar == SOP) {\n" +
- " index_i = 0\n" +
- " started = true\n" +
- " ended = false\n" +
- " } else if(inChar == EOP) {\n" +
- " ended = true\n" +
- " break\n" +
- " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
- " " + dataMain + "[index_i] = inChar\n" +
- " index_i++\n" +
- " }\n" +
- " }\n" +
- " if(started && ended) {\n" +
- " return 1\n" +
- " } else {\n" +
- " return 0\n" +
- " }\n" +
- "}\n";
- Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
- var code = "boolean receiveMsgsuccess_ = handleDataIn()\n";
- return code;
- }
- /*************************
- * version 2 *
- * last version *
- *************************/
- Blockly.Python['MainEasymode_transfer2_send_old'] = function (block) {
- var _ocde = "uart = UART(1, 115200)" +
- "uart.init(115200, bits=8, parity=None, stop=1)" +
- 'uart_data_set = str(pa.value()) + "," + str(pb.value())+ "," + str(pl.value())+ "," + str(pm.value())+ "," + str(pr.value())' +
- "uart.write(button_data_set)"
- return Blockly.Python.definitions_.import_camare = "from machine import UART",
- [_ocde, Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['MainEasymode_transfer2_receive_old'] = function (block) {
- var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
- var list_length = block.getFieldValue("LENGTH");
- var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
- for (var i = 1; i < list_length; i++) {
- dataMain_list += ",0";
- }
- dataMain_list += "}\n";
- Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
- Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
- var checkbit = "#define SOP '<'\n" +
- "#define EOP '>'\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
- var MsgReceiveStatus = "bool started = false\n" +
- "bool ended = false\n" +
- "int index_i\n";
- Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
- var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
- var dataIn_function = "int handleDataIn() {\n" +
- " while (Serial1.available() > 0) {\n" +
- " int inChar = Serial1.read()\n" +
- " if(inChar == SOP) {\n" +
- " index_i = 0\n" +
- " started = true\n" +
- " ended = false\n" +
- " } else if(inChar == EOP) {\n" +
- " ended = true\n" +
- " break\n" +
- " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
- " " + dataMain + "[index_i] = inChar\n" +
- " index_i++\n" +
- " }\n" +
- " }\n" +
- " delay(10)\n" +
- " if(started && ended) {\n" +
- " return 1\n" +
- " } else {\n" +
- " return 0\n" +
- " }\n" +
- "}\n";
- Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
- var code = "boolean receiveMsgsuccess_ = handleDataIn()\n" +
- "if (!receiveMsgsuccess_) {\n" +
- " Serial.println(\"Error: Fail to receive data!\")\n" +
- " return\n" +
- "}\n";
- return code;
- }
- Blockly.Python['MainEasymode_transfer2_receive_getValue_old'] = function (block) {
- var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
- var list_length = block.getFieldValue("INDEX");
- var code = "" + dataMain + "[" + list_length + "]";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- /*************************
- * version 3 *
- * current *
- *************************/
- Blockly.Python['MainEasymode_transfer2_send'] = function (block) {
- var item_value = '';
- for (var n = 0; n < this.itemCount_; n++) {
- item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
- if (n != this.itemCount_ - 1) {
- item_value += " + ',' + ";
- }
- }
- var _ocde = "uart = UART(1, 115200) \n" +
- "uart.init(115200, bits=8, parity=None, stop=1) \n" +
- 'uart_data_set = ' + item_value + " \n" +
- "uart.write(uart_data_set) \n";
- return Blockly.Python.definitions_.import_camare = "from machine import UART",
- [_ocde, Blockly.Python.ORDER_ATOMIC];
- }
- /*************************
- * version 3 *
- * current *
- *************************/
- Blockly.Python['AiEasymode_transfer2_send'] = function (block) {
- var item_value = '';
- for (var n = 0; n < this.itemCount_; n++) {
- item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
- if (n != this.itemCount_ - 1) {
- item_value += " + ',' + ";
- }
- }
- var _ocde = "uart = UART(1, 115200) \n" +
- "uart.init(115200, bits=8, parity=None, stop=1) \n" +
- 'uart_data_set = ' + item_value + " \n" +
- "uart.write(uart_data_set) \n";
- return Blockly.Python.definitions_.import_camare = "from machine import UART",
- [_ocde, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['MainEasymode_transfer2_receive'] = function (block) {
- var item_value = '';
- var _len = block.getField('LENGTH').text_;
- //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
- for (var n = 0; n < _len; n++) {
- item_value += "0";
- if (n != _len - 1) {
- item_value += ",";
- }
- }
- var _code = 'uart = UART(1, 115200) \n' +
- 'uart.init(115200, bits=8, parity=None, stop=1) \n' +
- "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
- 'read_data = uart.read() \n' +
- 'if read_data != None: \n' +
- " read_str = read_data.decode('utf-8') \n" +
- 'elif read_data == None: \n' +
- " read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
- return Blockly.Python.definitions_.import_camare = "from machine import UART",
- [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['AiEasymode_transfer2_receive'] = function (block) {
- var item_value = '';
- var _len = block.getField('LENGTH').text_;
- //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
- for (var n = 0; n < _len; n++) {
- item_value += "0";
- if (n != _len - 1) {
- item_value += ",";
- }
- }
- var _code = 'uart = UART(1, 115200) \n' +
- 'uart.init(115200, bits=8, parity=None, stop=1) \n' +
- "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
- 'read_data = uart.read() \n' +
- 'if read_data != None: \n' +
- " read_str = read_data.decode('utf-8') \n" +
- 'elif read_data == None: \n' +
- " read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
- return Blockly.Python.definitions_.import_camare = "from machine import UART",
- [_code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['MainEasymode_transfer2_receive_getValue'] = function (block) {
- var index = parseInt(block.getFieldValue("INDEX")) * 2;
- var code = "read_str[" + index + "] \n";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['AiEasymode_transfer2_receive_getValue'] = function (block) {
- var index = parseInt(block.getFieldValue("INDEX")) * 2;
- var code = "read_str[" + index + "] \n";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
|