123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- '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 dataMain = block.getFieldValue('DATAMAIN');
- var dataMain_list = "int " + dataMain + "[" + this.itemCount_ + "] = {0";
- for (var i = 1; i < this.itemCount_; 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 length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
- var dataLength = length + "+2";
- var code = "";
- var item_value = '';
- for (var n = 0; n < this.itemCount_; n++) {
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
- Blockly.Python.ORDER_NONE) || '0';
- code += dataMain + "[" + n + "] = " + item_value + ";\n";
- }
- 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_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) {
- Blockly.Python.addDeclaration("Serial_sendState", "int serialSendState = 0;");
- Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
- var serial_SendData = 'void serialSendData() {\n'+
- ' int serialDataLength = serialDataPackage().length() + 1;\n'+
- ' char serialDataBuf[serialDataLength];\n'+
- ' serialDataPackage().toCharArray(serialDataBuf, serialDataLength);\n'+
- ' if ( serialSendState == 0 ) {\n'+
- ' Serial1.write(serialDataBuf);\n'+
- ' }\n'+
- ' if ( (millis() / 100) % 4 == 3 && serialSendState == 0) {\n'+
- // Send data set to WiFi Module.\n'
- ' Serial1.write(serialDataBuf);\n'+
- ' serialSendState = 1;\n'+
- ' } else if ( (millis() / 100) % 4 == 1) {\n'+
- ' serialSendState = 0;\n'+
- ' }\n'+
- '}\n';
- Blockly.Python.addFunction("SerialSendData",serial_SendData);
- var item_value = ''
- var serial_DataPackage = 'String serialDataPackage() { \n'+
- ' String data = "";\n'+
- ' data += "SOF";\n' // Set up a "SOF" initializer for data validation usage.
- for (var n = 0; n < this.itemCount_; n++) {
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
- Blockly.Python.ORDER_NONE) || '0';
- serial_DataPackage += ' data += "|";\n data += '+ item_value + ';\n'
- }
- serial_DataPackage += ' data += "|";\n data += "\\r\\n";\n '+
- ' return data;\n}\n';
- Blockly.Python.addFunction("SerialDataPackage",serial_DataPackage);
- var code = "";
- code += "serialSendData();\n";
- return code;
- }
- Blockly.Python['MainEasymode_transfer2_receive'] = function(block) {
- Blockly.Python.addSetup('serial1_trans', "Serial1.begin(9600);\n Serial1.setTimeout(20);");
- Blockly.Python.addDeclaration("serialDataReceived", 'String serialDataReceived;\n');
- var serial_DataFilter = 'int serialDataFilter() {\n'+
- ' if (Serial1.available() > 0) {\n'+
- ' serialDataReceived = Serial1.readStringUntil("\\r\\n");\n'+
- ' }\n'+
- ' if (getSerialDataValue(serialDataReceived, \'|\', 0) == "SOF") {\n'+
- ' return 1;\n'+
- ' } else {\n'+
- ' return 0;\n'+
- ' }\n'+
- '}\n'
- Blockly.Python.addFunction("serialDataFilter", serial_DataFilter);
- var serial_getDataValue = 'String getSerialDataValue(String data, char separator, int index)\n'+
- '{\n'+
- ' int found = 0;\n'+
- ' int strIndex[] = {0, -1};\n'+
- ' int maxIndex = data.length() - 1;\n'+
- ' for (int i = 0; i <= maxIndex && found <= index; i++) {\n'+
- ' if (data.charAt(i) == separator || i == maxIndex) {\n'+
- ' found++;\n'+
- ' strIndex[0] = strIndex[1] + 1;\n'+
- ' strIndex[1] = (i == maxIndex) ? i + 1 : i;\n'+
- ' }\n'+
- ' }\n'+
- ' return found > index ? data.substring(strIndex[0], strIndex[1]) : "";\n'+
- '}\n'
- Blockly.Python.addFunction("getSerialDataValue", serial_getDataValue);
- var code = 'boolean serialReceiveCheck_ = serialDataFilter();\n'+
- 'if (!serialReceiveCheck_) {\n'+
- ' Serial.println("Error receiving data from WiFi Module.");\n'+
- ' return;\n'+
- ' }\n'
- return code;
- }
- Blockly.Python['MainEasymode_transfer2_receive_getValue'] = function(block) {
- var index = parseInt(block.getFieldValue("INDEX")) +1;
- var type = block.getFieldValue("TYPE")
- var code = "getSerialDataValue(serialDataReceived, '|', "+ index +")"+ type;
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
|