mainwifi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict';
  2. goog.provide('Blockly.Python.mainwifi');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['mainwifi_setup'] = function(block) {
  5. let serial_setup = "Serial.begin(9600);";
  6. Blockly.Python.addSetup('serial_9600', serial_setup);
  7. return '';
  8. };
  9. Blockly.Python['mainwifi_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. "Serial1.write(DataOut_, " + dataLength + ");\n";
  24. return code;
  25. };
  26. Blockly.Python['mainwifi_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 (Serial1.available() > 0) {\n" +
  39. " int inChar = Serial1.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['mainwifi_receiveMsg_isSuccessed'] = function(block) {
  63. var code = "receiveMsgsuccess_";
  64. return [code, Blockly.Python.ORDER_ATOMIC];
  65. };
  66. // easymode
  67. Blockly.Python['MainEasymode_transfer_send'] = function(block) {
  68. var dataMain = Blockly.Python.variableDB_.getName(
  69. block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
  70. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  71. var checkbit = "#define SOP '<'\n" +
  72. "#define EOP '>'\n";
  73. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  74. var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
  75. var dataLength = length + "+2";
  76. var code = "unsigned char DataOut_[" + dataLength + "];\n" +
  77. "DataOut_[0]= SOP;\n" +
  78. "DataOut_[" + length + "+1]= EOP;\n" +
  79. "for (int i=0; i<" + length + ";i++) {\n" +
  80. " DataOut_[i+1]=" + dataMain + "[i];\n" +
  81. "}\n" +
  82. "Serial1.write(DataOut_, " + dataLength + ");\n";
  83. return code;
  84. }
  85. Blockly.Python['MainEasymode_transfer_receive'] = function(block) {
  86. var dataMain = Blockly.Python.variableDB_.getName(
  87. block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
  88. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  89. var checkbit = "#define SOP '<'\n" +
  90. "#define EOP '>'\n";
  91. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  92. var MsgReceiveStatus = "bool started = false;\n" +
  93. "bool ended = false;\n" +
  94. "int index_i;\n";
  95. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  96. var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  97. var dataIn_function = "int handleDataIn() {\n" +
  98. " while (Serial1.available() > 0) {\n" +
  99. " int inChar = Serial1.read();\n" +
  100. " if(inChar == SOP) {\n" +
  101. " index_i = 0;\n" +
  102. " started = true;\n" +
  103. " ended = false;\n" +
  104. " } else if(inChar == EOP) {\n" +
  105. " ended = true;\n" +
  106. " break;\n" +
  107. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  108. " " + dataMain + "[index_i] = inChar;\n" +
  109. " index_i++;\n" +
  110. " }\n" +
  111. " }\n" +
  112. " if(started && ended) {\n" +
  113. " return 1;\n" +
  114. " } else {\n" +
  115. " return 0;\n" +
  116. " }\n" +
  117. "}\n";
  118. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  119. var code = "boolean receiveMsgsuccess_ = handleDataIn();\n";
  120. return code;
  121. }
  122. /*************************
  123. * version 2 *
  124. * last version *
  125. *************************/
  126. Blockly.Python['MainEasymode_transfer2_send_old'] = function(block) {
  127. var dataMain = block.getFieldValue('DATAMAIN');
  128. var dataMain_list = "int " + dataMain + "[" + this.itemCount_ + "] = {0";
  129. for (var i = 1; i < this.itemCount_; i++) {
  130. dataMain_list += ",0";
  131. }
  132. dataMain_list += "};\n";
  133. Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  134. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  135. var checkbit = "#define SOP '<'\n" +
  136. "#define EOP '>'\n";
  137. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  138. var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
  139. var dataLength = length + "+2";
  140. var code = "";
  141. var item_value = '';
  142. for (var n = 0; n < this.itemCount_; n++) {
  143. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  144. Blockly.Python.ORDER_NONE) || '0';
  145. code += dataMain + "[" + n + "] = " + item_value + ";\n";
  146. }
  147. code += "unsigned char DataOut_[" + dataLength + "];\n" +
  148. "DataOut_[0]= SOP;\n" +
  149. "DataOut_[" + length + "+1]= EOP;\n" +
  150. "for (int i=0; i<" + length + ";i++) {\n" +
  151. " DataOut_[i+1]=" + dataMain + "[i];\n" +
  152. "}\n" +
  153. "Serial1.write(DataOut_, " + dataLength + ");\n";
  154. return code;
  155. }
  156. Blockly.Python['MainEasymode_transfer2_receive_old'] = function(block) {
  157. var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
  158. var list_length = block.getFieldValue("LENGTH");
  159. var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
  160. for (var i = 1; i < list_length; i++) {
  161. dataMain_list += ",0";
  162. }
  163. dataMain_list += "};\n";
  164. Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  165. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  166. var checkbit = "#define SOP '<'\n" +
  167. "#define EOP '>'\n";
  168. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  169. var MsgReceiveStatus = "bool started = false;\n" +
  170. "bool ended = false;\n" +
  171. "int index_i;\n";
  172. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  173. var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  174. var dataIn_function = "int handleDataIn() {\n" +
  175. " while (Serial1.available() > 0) {\n" +
  176. " int inChar = Serial1.read();\n" +
  177. " if(inChar == SOP) {\n" +
  178. " index_i = 0;\n" +
  179. " started = true;\n" +
  180. " ended = false;\n" +
  181. " } else if(inChar == EOP) {\n" +
  182. " ended = true;\n" +
  183. " break;\n" +
  184. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  185. " " + dataMain + "[index_i] = inChar;\n" +
  186. " index_i++;\n" +
  187. " }\n" +
  188. " }\n" +
  189. " delay(10);\n" +
  190. " if(started && ended) {\n" +
  191. " return 1;\n" +
  192. " } else {\n" +
  193. " return 0;\n" +
  194. " }\n" +
  195. "}\n";
  196. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  197. var code = "boolean receiveMsgsuccess_ = handleDataIn();\n" +
  198. "if (!receiveMsgsuccess_) {\n" +
  199. " Serial.println(\"Error: Fail to receive data!\");\n" +
  200. " return;\n" +
  201. "}\n";
  202. return code;
  203. }
  204. Blockly.Python['MainEasymode_transfer2_receive_getValue_old'] = function(block) {
  205. var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
  206. var list_length = block.getFieldValue("INDEX");
  207. var code = "" + dataMain + "[" + list_length + "]";
  208. return [code, Blockly.Python.ORDER_ATOMIC];
  209. }
  210. /*************************
  211. * version 3 *
  212. * current *
  213. *************************/
  214. Blockly.Python['MainEasymode_transfer2_send'] = function(block) {
  215. Blockly.Python.addDeclaration("Serial_sendState", "int serialSendState = 0;");
  216. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  217. var serial_SendData = 'void serialSendData() {\n'+
  218. ' int serialDataLength = serialDataPackage().length() + 1;\n'+
  219. ' char serialDataBuf[serialDataLength];\n'+
  220. ' serialDataPackage().toCharArray(serialDataBuf, serialDataLength);\n'+
  221. ' if ( serialSendState == 0 ) {\n'+
  222. ' Serial1.write(serialDataBuf);\n'+
  223. ' }\n'+
  224. ' if ( (millis() / 100) % 4 == 3 && serialSendState == 0) {\n'+
  225. // Send data set to WiFi Module.\n'
  226. ' Serial1.write(serialDataBuf);\n'+
  227. ' serialSendState = 1;\n'+
  228. ' } else if ( (millis() / 100) % 4 == 1) {\n'+
  229. ' serialSendState = 0;\n'+
  230. ' }\n'+
  231. '}\n';
  232. Blockly.Python.addFunction("SerialSendData",serial_SendData);
  233. var item_value = ''
  234. var serial_DataPackage = 'String serialDataPackage() { \n'+
  235. ' String data = "";\n'+
  236. ' data += "SOF";\n' // Set up a "SOF" initializer for data validation usage.
  237. for (var n = 0; n < this.itemCount_; n++) {
  238. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  239. Blockly.Python.ORDER_NONE) || '0';
  240. serial_DataPackage += ' data += "|";\n data += '+ item_value + ';\n'
  241. }
  242. serial_DataPackage += ' data += "|";\n data += "\\r\\n";\n '+
  243. ' return data;\n}\n';
  244. Blockly.Python.addFunction("SerialDataPackage",serial_DataPackage);
  245. var code = "";
  246. code += "serialSendData();\n";
  247. return code;
  248. }
  249. Blockly.Python['MainEasymode_transfer2_receive'] = function(block) {
  250. Blockly.Python.addSetup('serial1_trans', "Serial1.begin(9600);\n Serial1.setTimeout(20);");
  251. Blockly.Python.addDeclaration("serialDataReceived", 'String serialDataReceived;\n');
  252. var serial_DataFilter = 'int serialDataFilter() {\n'+
  253. ' if (Serial1.available() > 0) {\n'+
  254. ' serialDataReceived = Serial1.readStringUntil("\\r\\n");\n'+
  255. ' }\n'+
  256. ' if (getSerialDataValue(serialDataReceived, \'|\', 0) == "SOF") {\n'+
  257. ' return 1;\n'+
  258. ' } else {\n'+
  259. ' return 0;\n'+
  260. ' }\n'+
  261. '}\n'
  262. Blockly.Python.addFunction("serialDataFilter", serial_DataFilter);
  263. var serial_getDataValue = 'String getSerialDataValue(String data, char separator, int index)\n'+
  264. '{\n'+
  265. ' int found = 0;\n'+
  266. ' int strIndex[] = {0, -1};\n'+
  267. ' int maxIndex = data.length() - 1;\n'+
  268. ' for (int i = 0; i <= maxIndex && found <= index; i++) {\n'+
  269. ' if (data.charAt(i) == separator || i == maxIndex) {\n'+
  270. ' found++;\n'+
  271. ' strIndex[0] = strIndex[1] + 1;\n'+
  272. ' strIndex[1] = (i == maxIndex) ? i + 1 : i;\n'+
  273. ' }\n'+
  274. ' }\n'+
  275. ' return found > index ? data.substring(strIndex[0], strIndex[1]) : "";\n'+
  276. '}\n'
  277. Blockly.Python.addFunction("getSerialDataValue", serial_getDataValue);
  278. var code = 'boolean serialReceiveCheck_ = serialDataFilter();\n'+
  279. 'if (!serialReceiveCheck_) {\n'+
  280. ' Serial.println("Error receiving data from WiFi Module.");\n'+
  281. ' return;\n'+
  282. ' }\n'
  283. return code;
  284. }
  285. Blockly.Python['MainEasymode_transfer2_receive_getValue'] = function(block) {
  286. var index = parseInt(block.getFieldValue("INDEX")) +1;
  287. var type = block.getFieldValue("TYPE")
  288. var code = "getSerialDataValue(serialDataReceived, '|', "+ index +")"+ type;
  289. return [code, Blockly.Python.ORDER_ATOMIC];
  290. }