mainwifi.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 _ocde = "uart = UART(1, 115200)" +
  128. "uart.init(115200, bits=8, parity=None, stop=1)" +
  129. 'uart_data_set = str(pa.value()) + "," + str(pb.value())+ "," + str(pl.value())+ "," + str(pm.value())+ "," + str(pr.value())' +
  130. "uart.write(button_data_set)"
  131. return Blockly.Python.definitions_.import_camare = "from machine import UART",
  132. [_ocde, Blockly.Python.ORDER_ATOMIC]
  133. }
  134. Blockly.Python['MainEasymode_transfer2_receive_old'] = function (block) {
  135. var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
  136. var list_length = block.getFieldValue("LENGTH");
  137. var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
  138. for (var i = 1; i < list_length; i++) {
  139. dataMain_list += ",0";
  140. }
  141. dataMain_list += "}\n";
  142. Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  143. Blockly.Python.addSetup('serial1', "Serial1.begin(9600);");
  144. var checkbit = "#define SOP '<'\n" +
  145. "#define EOP '>'\n";
  146. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  147. var MsgReceiveStatus = "bool started = false\n" +
  148. "bool ended = false\n" +
  149. "int index_i\n";
  150. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  151. var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  152. var dataIn_function = "int handleDataIn() {\n" +
  153. " while (Serial1.available() > 0) {\n" +
  154. " int inChar = Serial1.read()\n" +
  155. " if(inChar == SOP) {\n" +
  156. " index_i = 0\n" +
  157. " started = true\n" +
  158. " ended = false\n" +
  159. " } else if(inChar == EOP) {\n" +
  160. " ended = true\n" +
  161. " break\n" +
  162. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  163. " " + dataMain + "[index_i] = inChar\n" +
  164. " index_i++\n" +
  165. " }\n" +
  166. " }\n" +
  167. " delay(10)\n" +
  168. " if(started && ended) {\n" +
  169. " return 1\n" +
  170. " } else {\n" +
  171. " return 0\n" +
  172. " }\n" +
  173. "}\n";
  174. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  175. var code = "boolean receiveMsgsuccess_ = handleDataIn()\n" +
  176. "if (!receiveMsgsuccess_) {\n" +
  177. " Serial.println(\"Error: Fail to receive data!\")\n" +
  178. " return\n" +
  179. "}\n";
  180. return code;
  181. }
  182. Blockly.Python['MainEasymode_transfer2_receive_getValue_old'] = function (block) {
  183. var dataMain = block.getFieldValue('DATAMAIN') || '_main_transfer';
  184. var list_length = block.getFieldValue("INDEX");
  185. var code = "" + dataMain + "[" + list_length + "]";
  186. return [code, Blockly.Python.ORDER_ATOMIC];
  187. }
  188. /*************************
  189. * version 3 *
  190. * current *
  191. *************************/
  192. Blockly.Python['MainEasymode_transfer2_send'] = function (block) {
  193. var item_value = '';
  194. for (var n = 0; n < this.itemCount_; n++) {
  195. item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
  196. if (n != this.itemCount_ - 1) {
  197. item_value += " + ',' + ";
  198. }
  199. }
  200. var _ocde = "uart = UART(1, 115200) \n" +
  201. "uart.init(115200, bits=8, parity=None, stop=1) \n" +
  202. 'uart_data_set = ' + item_value + " \n" +
  203. "uart.write(uart_data_set) \n";
  204. return Blockly.Python.definitions_.import_camare = "from machine import UART",
  205. [_ocde, Blockly.Python.ORDER_ATOMIC];
  206. }
  207. /*************************
  208. * version 3 *
  209. * current *
  210. *************************/
  211. Blockly.Python['AiEasymode_transfer2_send'] = function (block) {
  212. var item_value = '';
  213. for (var n = 0; n < this.itemCount_; n++) {
  214. item_value += "str(" + (Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '0') + ")";
  215. if (n != this.itemCount_ - 1) {
  216. item_value += " + ',' + ";
  217. }
  218. }
  219. var _ocde = "uart = UART(1, 115200) \n" +
  220. "uart.init(115200, bits=8, parity=None, stop=1) \n" +
  221. 'uart_data_set = ' + item_value + " \n" +
  222. "uart.write(uart_data_set) \n";
  223. return Blockly.Python.definitions_.import_camare = "from machine import UART",
  224. [_ocde, Blockly.Python.ORDER_ATOMIC];
  225. }
  226. Blockly.Python['MainEasymode_transfer2_receive'] = function (block) {
  227. var item_value = '';
  228. var _len = block.getField('LENGTH').text_;
  229. //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
  230. for (var n = 0; n < _len; n++) {
  231. item_value += "0";
  232. if (n != _len - 1) {
  233. item_value += ",";
  234. }
  235. }
  236. var _code = 'uart = UART(1, 115200) \n' +
  237. 'uart.init(115200, bits=8, parity=None, stop=1) \n' +
  238. "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
  239. 'read_data = uart.read() \n' +
  240. 'if read_data != None: \n' +
  241. " read_str = read_data.decode('utf-8') \n" +
  242. 'elif read_data == None: \n' +
  243. " read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
  244. return Blockly.Python.definitions_.import_camare = "from machine import UART",
  245. [_code, Blockly.Python.ORDER_ATOMIC];
  246. }
  247. Blockly.Python['AiEasymode_transfer2_receive'] = function (block) {
  248. var item_value = '';
  249. var _len = block.getField('LENGTH').text_;
  250. //var _len = Blockly.Python.valueToCode(this, 'LENGTH', Blockly.Python.ORDER_NONE);
  251. for (var n = 0; n < _len; n++) {
  252. item_value += "0";
  253. if (n != _len - 1) {
  254. item_value += ",";
  255. }
  256. }
  257. var _code = 'uart = UART(1, 115200) \n' +
  258. 'uart.init(115200, bits=8, parity=None, stop=1) \n' +
  259. "read_data = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n" +
  260. 'read_data = uart.read() \n' +
  261. 'if read_data != None: \n' +
  262. " read_str = read_data.decode('utf-8') \n" +
  263. 'elif read_data == None: \n' +
  264. " read_str = '" + item_value + "' # 取决于用户在积木中选择了有多少个数据 \n";
  265. return Blockly.Python.definitions_.import_camare = "from machine import UART",
  266. [_code, Blockly.Python.ORDER_ATOMIC];
  267. }
  268. Blockly.Python['MainEasymode_transfer2_receive_getValue'] = function (block) {
  269. var index = parseInt(block.getFieldValue("INDEX")) * 2;
  270. var code = "read_str[" + index + "] \n";
  271. return [code, Blockly.Python.ORDER_ATOMIC];
  272. }
  273. Blockly.Python['AiEasymode_transfer2_receive_getValue'] = function (block) {
  274. var index = parseInt(block.getFieldValue("INDEX")) * 2;
  275. var code = "read_str[" + index + "] \n";
  276. return [code, Blockly.Python.ORDER_ATOMIC];
  277. }