wificomms.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. goog.provide('Blockly.Python.wificomms');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['wificomms_setup'] = function(block) {
  5. let serial_setup = "Serial.begin(9600);";
  6. Blockly.Python.addSetup('serial_9600', serial_setup);
  7. return '';
  8. };
  9. Blockly.Python['wificomms_sendMsg'] = function(block) {
  10. var checkbit = "#define SOP '<'\n" +
  11. "#define EOP '>'\n";
  12. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  13. var datavar = Blockly.Python.variableDB_.getName(
  14. block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
  15. var length = "sizeof(" + datavar + ")/sizeof(" + datavar + "[0])";
  16. var dataLength = length + "+2";
  17. var code = "unsigned char DataOut_[" + dataLength + "];\n" +
  18. "DataOut_[0]= SOP;\n" +
  19. "DataOut_[" + length + "+1]= EOP;\n" +
  20. "for (int i=0; i<" + length + ";i++) {\n" +
  21. " DataOut_[i+1]=" + datavar + "[i];\n" +
  22. "}\n" +
  23. "Serial.write(DataOut_, " + dataLength + ");\n";
  24. return code;
  25. };
  26. Blockly.Python['wificomms_receiveMsg'] = function(block) {
  27. var checkbit = "#define SOP '<'\n" +
  28. "#define EOP '>'\n";
  29. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  30. var MsgReceiveStatus = "bool started = false;\n" +
  31. "bool ended = false;\n" +
  32. "int index_i;\n";
  33. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  34. var datavar = Blockly.Python.variableDB_.getName(
  35. block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
  36. var length = 'sizeof(' + datavar + ')/sizeof(' + datavar + '[0])' || 1;
  37. var dataIn_function = "int handleDataIn() {\n" +
  38. " while (Serial.available() > 0) {\n" +
  39. " int inChar = Serial.read();\n" +
  40. " if(inChar == SOP) {\n" +
  41. " index_i = 0;\n" +
  42. " started = true;\n" +
  43. " ended = false;\n" +
  44. " } else if(inChar == EOP) {\n" +
  45. " ended = true;\n" +
  46. " break;\n" +
  47. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  48. " " + datavar + "[index_i] = inChar;\n" +
  49. " index_i++;\n" +
  50. " }\n" +
  51. " }\n" +
  52. " if(started && ended) {\n" +
  53. " return 1;\n" +
  54. " } else {\n" +
  55. " return 0;\n" +
  56. " }\n" +
  57. "}\n";
  58. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  59. var code = "boolean receiveMsgsuccess_ = handleDataIn();\n";
  60. return code;
  61. };
  62. Blockly.Python['wificomms_receiveMsg_isSuccessed'] = function(block) {
  63. var code = "receiveMsgsuccess_";
  64. return [code, Blockly.Python.ORDER_ATOMIC];
  65. }
  66. /*************************
  67. * version 2 *
  68. * last ve *
  69. *************************/
  70. Blockly.Python['wifiTransfer_send_old'] = function(block) {
  71. let serial_setup = "Serial.begin(9600);";
  72. Blockly.Python.addSetup('serial_9600', serial_setup);
  73. var dataMain = block.getFieldValue('DATAMAIN');
  74. var dataMain_list = "int " + dataMain + "[" + this.itemCount_ + "] = {0";
  75. for (var i = 1; i < this.itemCount_; i++) {
  76. dataMain_list += ",0";
  77. }
  78. dataMain_list += "};\n";
  79. Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  80. var checkbit = "#define SOP '<'\n" +
  81. "#define EOP '>'\n";
  82. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  83. var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
  84. var dataLength = length + "+2";
  85. var code = "";
  86. var item_value = '';
  87. for (var n = 0; n < this.itemCount_; n++) {
  88. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  89. Blockly.Python.ORDER_NONE) || '0';
  90. code += dataMain + "[" + n + "] = " + item_value + ";\n";
  91. }
  92. code += "unsigned char DataOut_[" + dataLength + "];\n" +
  93. "DataOut_[0]= SOP;\n" +
  94. "DataOut_[" + length + "+1]= EOP;\n" +
  95. "for (int i=0; i<" + length + ";i++) {\n" +
  96. " DataOut_[i+1]=" + dataMain + "[i];\n" +
  97. "}\n" +
  98. "Serial.write(DataOut_, " + dataLength + ");\n";
  99. return code;
  100. }
  101. Blockly.Python['wifiTransfer_receive_old'] = function(block) {
  102. let serial_setup = "Serial.begin(9600);";
  103. Blockly.Python.addSetup('serial_9600', serial_setup);
  104. var dataMain = block.getFieldValue('DATAMAIN') || '_wifi_transfer';
  105. var list_length = block.getFieldValue("LENGTH");
  106. var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
  107. for (var i = 1; i < list_length; i++) {
  108. dataMain_list += ",0";
  109. }
  110. dataMain_list += "};\n";
  111. Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  112. var checkbit = "#define SOP '<'\n" +
  113. "#define EOP '>'\n";
  114. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  115. var MsgReceiveStatus = "bool started = false;\n" +
  116. "bool ended = false;\n" +
  117. "int index_i;\n";
  118. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  119. var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  120. var dataIn_function = "int handleDataIn() {\n" +
  121. " while (Serial.available() > 0) {\n" +
  122. " int inChar = Serial.read();\n" +
  123. " if(inChar == SOP) {\n" +
  124. " index_i = 0;\n" +
  125. " started = true;\n" +
  126. " ended = false;\n" +
  127. " } else if(inChar == EOP) {\n" +
  128. " ended = true;\n" +
  129. " break;\n" +
  130. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  131. " " + dataMain + "[index_i] = inChar;\n" +
  132. " index_i++;\n" +
  133. " }\n" +
  134. " }\n" +
  135. " delay(10);\n" +
  136. " if(started && ended) {\n" +
  137. " return 1;\n" +
  138. " } else {\n" +
  139. " return 0;\n" +
  140. " }\n" +
  141. "}\n";
  142. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  143. var code = "boolean receiveMsgsuccess_ = handleDataIn();\n" +
  144. "if (!receiveMsgsuccess_) {\n" +
  145. " Serial.println(\"Error: Fail to receive data!\");\n" +
  146. " return;\n" +
  147. "}\n";
  148. return code;
  149. }
  150. Blockly.Python['wifiTransfer_receive_getValue_old'] = function(block) {
  151. var dataMain = block.getFieldValue('DATAMAIN') || '_wifi_transfer';
  152. var list_length = block.getFieldValue("INDEX");
  153. var code = "" + dataMain + "[" + list_length + "]";
  154. return [code, Blockly.Python.ORDER_ATOMIC];
  155. }
  156. /*************************
  157. * version 3 *
  158. * current *
  159. *************************/
  160. Blockly.Python['wifiTransfer_send'] = function(block) {
  161. Blockly.Python.addDeclaration("Serial_sendState", "int serialSendState = 0;");
  162. Blockly.Python.addSetup('serial', "Serial.begin(9600);");
  163. var serial_SendData = 'void serialSendData() {\n'+
  164. ' int serialDataLength = serialDataPackage().length() + 1;\n'+
  165. ' char serialDataBuf[serialDataLength];\n'+
  166. ' serialDataPackage().toCharArray(serialDataBuf, serialDataLength);\n'+
  167. ' if ( serialSendState == 0 ) {\n'+
  168. ' Serial.write(serialDataBuf);\n'+
  169. ' }\n'+
  170. ' if ( (millis() / 100) % 4 == 3 && serialSendState == 0) {\n'+
  171. // Send data set to WiFi Module.\n
  172. ' Serial.write(serialDataBuf);\n'+
  173. ' serialSendState = 1;\n'+
  174. ' } else if ( (millis() / 100) % 4 == 1) {\n'+
  175. ' serialSendState = 0;\n'+
  176. ' }\n'+
  177. '}\n';
  178. Blockly.Python.addFunction("SerialSendData",serial_SendData);
  179. var item_value = ''
  180. var serial_DataPackage = 'String serialDataPackage() { \n'+
  181. ' String data = "";\n'+
  182. ' data += "SOF";\n' // Set up a "SOF" initializer for data validation usage.
  183. for (var n = 0; n < this.itemCount_; n++) {
  184. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  185. Blockly.Python.ORDER_NONE) || '0';
  186. // item_value = item_value.replace(/\"/g,'');
  187. serial_DataPackage += ' data += "|";\n data += '+ item_value + ';\n'
  188. }
  189. serial_DataPackage += ' data += "|";\n data += "\\r\\n";\n '+
  190. ' return data;\n}\n';
  191. Blockly.Python.addFunction("SerialDataPackage",serial_DataPackage);
  192. var code = "";
  193. code += " serialSendData();\n";
  194. return code;
  195. }
  196. Blockly.Python['wifiTransfer_receive'] = function(block) {
  197. Blockly.Python.addSetup('serial_trans', "Serial.begin(9600);\n Serial.setTimeout(20);");
  198. Blockly.Python.addDeclaration("serialDataReceived", 'String serialDataReceived;\n');
  199. var serial_DataFilter = 'int serialDataFilter() {\n'+
  200. ' if (Serial.available() > 0) {\n'+
  201. ' serialDataReceived = Serial.readStringUntil(\'\\r\\n\');\n'+
  202. ' }\n'+
  203. ' if (getSerialDataValue(serialDataReceived, \'|\', 0) == "SOF") {\n'+
  204. ' return 1;\n'+
  205. ' } else {\n'+
  206. ' return 0;\n'+
  207. ' }\n'+
  208. '}\n'
  209. Blockly.Python.addFunction("serialDataFilter", serial_DataFilter);
  210. var serial_getDataValue = 'String getSerialDataValue(String data, char separator, int index)\n'+
  211. '{\n'+
  212. ' int found = 0;\n'+
  213. ' int strIndex[] = {0, -1};\n'+
  214. ' int maxIndex = data.length() - 1;\n'+
  215. ' for (int i = 0; i <= maxIndex && found <= index; i++) {\n'+
  216. ' if (data.charAt(i) == separator || i == maxIndex) {\n'+
  217. ' found++;\n'+
  218. ' strIndex[0] = strIndex[1] + 1;\n'+
  219. ' strIndex[1] = (i == maxIndex) ? i + 1 : i;\n'+
  220. ' }\n'+
  221. ' }\n'+
  222. ' return found > index ? data.substring(strIndex[0], strIndex[1]) : "";\n'+
  223. '}\n'
  224. Blockly.Python.addFunction("getSerialDataValue", serial_getDataValue);
  225. var code = 'boolean serialReceiveCheck_ = serialDataFilter();\n'+
  226. ' if (!serialReceiveCheck_) {\n'+
  227. ' Serial.println("Error receiving data from Main Module.");\n'+
  228. ' return;\n'+
  229. ' }\n'
  230. return code;
  231. }
  232. Blockly.Python['wifiTransfer_receive_getValue'] = function(block) {
  233. var index = parseInt(block.getFieldValue("INDEX")) +1;
  234. var type = block.getFieldValue("TYPE")
  235. var code = "getSerialDataValue(serialDataReceived, '|', "+ index +")"+ type;
  236. return [code, Blockly.Python.ORDER_ATOMIC];
  237. }