bluetooth.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. 'use strict';
  2. goog.provide('Blockly.Python.bluetooth');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['bluetooth_setup'] = function(block) {
  5. let baudNum = block.getFieldValue('BAUD');
  6. Blockly.Python.addSetup('bluetooth_serial_begin', 'Serial1.begin(' + baudNum + ');', false);
  7. let receivedDo = Blockly.Python.statementToCode(block, 'RECEIVED_DO');
  8. let option = block.getFieldValue('OPTIONS');
  9. var cmdvar = Blockly.Python.variableDB_.getName(
  10. block.getFieldValue('CMDVAR'), Blockly.Variables.NAME_TYPE);
  11. let code = "";
  12. if (option === "string") {
  13. code = "while (Serial1.available()) {\n" +
  14. " delay(10);\n" +
  15. " char c = Serial1.read();\n" +
  16. " " + cmdvar + " += c;\n" +
  17. "}\n" + receivedDo +
  18. "\n" + cmdvar + " = \"\";\n";
  19. } else if (option === "number") {
  20. code = "if (Serial1.available() > 0) {\n" +
  21. " " + cmdvar + " = Serial1.read();\n" +
  22. "}\n" + receivedDo;
  23. } else if (option === "array") {
  24. var length = block.getFieldValue("LENGTH");
  25. var argument0 = '{';
  26. for (var i = 0; i < length - 1; i++) argument0 += '0,';
  27. argument0 += "0}"
  28. var decl = 'int ' + cmdvar + '[' + length + '] = ' + argument0 + ';\n';
  29. Blockly.Python.addDeclaration('bluetooth_list', decl);
  30. code = "int n = " + 'sizeof(' + cmdvar + ')/sizeof(' + cmdvar + '[0]);\n' +
  31. "while (Serial1.available() < n) { }\n" +
  32. "for (int b_n = 0; b_n < n; b_n++) {\n" +
  33. " " + cmdvar + "[b_n] = Serial1.read();\n" +
  34. "}\n" + receivedDo;
  35. }
  36. return code;
  37. };
  38. Blockly.Python['bluetooth_text_getCommand'] = function(block) {
  39. var code = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  40. Blockly.Variables.NAME_TYPE);
  41. return [code, Blockly.Python.ORDER_ATOMIC];
  42. };
  43. Blockly.Python['bluetooth_number_getCommand'] = function(block) {
  44. var code = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  45. Blockly.Variables.NAME_TYPE);
  46. return [code, Blockly.Python.ORDER_ATOMIC];
  47. };
  48. Blockly.Python['bluetooth_list_getCommand'] = function(block) {
  49. var variab = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  50. Blockly.Variables.NAME_TYPE);
  51. var index = block.getFieldValue("INDEX");
  52. var code = variab + '[' + index + ']';
  53. return [code, Blockly.Python.ORDER_ATOMIC];
  54. };
  55. Blockly.Python['bluetooth_intercomms_send'] = function(block) {
  56. var baud = block.getFieldValue("BAUD");
  57. Blockly.Python.addDeclaration("BT_Serial_sendState", "int BTserialSendState = 0;");
  58. Blockly.Python.addSetup('serial1', "Serial1.begin(" + baud + ");");
  59. var BT_Serial_SendData = 'void BTserialSendData() {\n' +
  60. ' int BTserialDataLength = BTserialDataPackage().length() + 1;\n' +
  61. ' char BTserialDataBuf[BTserialDataLength];\n' +
  62. ' BTserialDataPackage().toCharArray(BTserialDataBuf, BTserialDataLength);\n' +
  63. ' if ( BTserialSendState == 0 ) {\n' +
  64. ' Serial1.write(BTserialDataBuf);\n' +
  65. ' }\n' +
  66. ' if ( (millis() / 100) % 4 == 3 && BTserialSendState == 0) {\n' +
  67. // Send data set to WiFi Module.\n'
  68. ' Serial1.write(BTserialDataBuf);\n' +
  69. ' BTserialSendState = 1;\n' +
  70. ' } else if ( (millis() / 100) % 4 == 1) {\n' +
  71. ' BTserialSendState = 0;\n' +
  72. ' }\n' +
  73. '}\n';
  74. Blockly.Python.addFunction("BTSerialSendData", BT_Serial_SendData);
  75. var item_value = ''
  76. var BTserial_DataPackage = 'String BTserialDataPackage() { \n' +
  77. ' String data = "";\n' +
  78. ' data += "SOF";\n' // Set up a "SOF" initializer for data validation usage.
  79. for (var n = 0; n < this.itemCount_; n++) {
  80. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  81. Blockly.Python.ORDER_NONE) || '0';
  82. BTserial_DataPackage += ' data += "|";\n data += ' + item_value + ';\n'
  83. }
  84. BTserial_DataPackage += ' data += "|";\n data += "\\r\\n";\n ' +
  85. ' return data;\n}\n';
  86. Blockly.Python.addFunction("BTSerialDataPackage", BTserial_DataPackage);
  87. var code = "";
  88. code += "BTserialSendData();\n";
  89. return code;
  90. };
  91. Blockly.Python['bluetooth_intercomms_receive'] = function(block) {
  92. var baud = block.getFieldValue("BAUD");
  93. var timeout = block.getFieldValue("TIMEOUT");
  94. Blockly.Python.addSetup('serial1_trans', "Serial1.begin(" + baud + ");\n Serial1.setTimeout(" + timeout + ");");
  95. Blockly.Python.addDeclaration("BTserialDataReceived", 'String BTserialDataReceived;\n');
  96. var BTserial_DataFilter = 'int BTserialDataFilter() {\n' +
  97. ' if (Serial1.available() > 0) {\n' +
  98. ' BTserialDataReceived = Serial1.readStringUntil("\\r\\n");\n' +
  99. ' }\n' +
  100. ' if (BTgetSerialDataValue(BTserialDataReceived, \'|\', 0) == "SOF") {\n' +
  101. ' return 1;\n' +
  102. ' } else {\n' +
  103. ' return 0;\n' +
  104. ' }\n' +
  105. '}\n'
  106. Blockly.Python.addFunction("BTserialDataFilter", BTserial_DataFilter);
  107. var BTserial_getDataValue = 'String BTgetSerialDataValue(String data, char separator, int index)\n' +
  108. '{\n' +
  109. ' int found = 0;\n' +
  110. ' int strIndex[] = {0, -1};\n' +
  111. ' int maxIndex = data.length() - 1;\n' +
  112. ' for (int i = 0; i <= maxIndex && found <= index; i++) {\n' +
  113. ' if (data.charAt(i) == separator || i == maxIndex) {\n' +
  114. ' found++;\n' +
  115. ' strIndex[0] = strIndex[1] + 1;\n' +
  116. ' strIndex[1] = (i == maxIndex) ? i + 1 : i;\n' +
  117. ' }\n' +
  118. ' }\n' +
  119. ' return found > index ? data.substring(strIndex[0], strIndex[1]) : "";\n' +
  120. '}\n'
  121. Blockly.Python.addFunction("BTgetSerialDataValue", BTserial_getDataValue);
  122. var code = 'boolean BTserialReceiveCheck_ = BTserialDataFilter();\n' +
  123. 'if (!BTserialReceiveCheck_) {\n' +
  124. ' Serial.println("Error receiving data from Bluetooth Module.");\n' +
  125. ' return;\n' +
  126. ' }\n'
  127. return code;
  128. };
  129. Blockly.Python['bluetooth_intercomms_receive_getData'] = function(block) {
  130. var index = parseInt(block.getFieldValue("INDEX")) + 1;
  131. var type = block.getFieldValue("TYPE")
  132. var code = "BTgetSerialDataValue(BTserialDataReceived, '|', " + index + ")" + type;
  133. return [code, Blockly.Python.ORDER_ATOMIC];
  134. };
  135. /******************************** */
  136. Blockly.Python['bluetooth_intercomms_send_old'] = function(block) {
  137. var checkbit = "#define SOP '<'\n" +
  138. "#define EOP '>'\n";
  139. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  140. var baud = block.getFieldValue("BAUD")
  141. Blockly.Python.addSetup('mainwifi_setup', "Serial1.begin(" + baud + ");");
  142. // var datavar = Blockly.Python.variableDB_.getName(
  143. // block.getFieldValue('dataVar'), Blockly.Variables.NAME_TYPE);
  144. var length = block.itemCount_;
  145. var dataLength = length + "+2";
  146. var code = "unsigned char DataOut_[" + dataLength + "];\n" +
  147. "DataOut_[0] = SOP;\n"
  148. for (let i = 1; i < length + 1; i++) {
  149. var data_item = Blockly.Python.valueToCode(block, "ADD" + (i - 1), Blockly.Python.ORDER_ATOMIC) || 0;
  150. code += "DataOut_[" + i + "] = " + data_item + ";\n"
  151. }
  152. code += "DataOut_[" + length + "+1] = EOP;\n" +
  153. "Serial1.write(DataOut_, " + dataLength + ");\n" +
  154. "delay(50);\n";
  155. return code;
  156. };
  157. Blockly.Python['bluetooth_intercomms_receive_old'] = function(block) {
  158. var checkbit = "#define SOP '<'\n" +
  159. "#define EOP '>'\n";
  160. Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  161. var length = block.getFieldValue("LENGTH");
  162. var baud = block.getFieldValue("BAUD")
  163. Blockly.Python.addSetup('mainwifi_setup', "Serial1.begin(" + baud + ");");
  164. var MsgReceiveStatus = "bool started = false;\n" +
  165. "bool ended = false;\n" +
  166. "int index_i;\n";
  167. Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  168. Blockly.Python.addDeclaration("BT_interCommms_receive", "int BTIC_receive[" + length + "] = {0};");
  169. var datavar = "BTIC_receive"
  170. var dataIn_function = "int handleDataIn() {\n" +
  171. " while (Serial1.available() > 0) {\n" +
  172. " int inChar = Serial1.read();\n" +
  173. " if(inChar == SOP) {\n" +
  174. " index_i = 0;\n" +
  175. " started = true;\n" +
  176. " ended = false;\n" +
  177. " } else if(inChar == EOP) {\n" +
  178. " ended = true;\n" +
  179. " break;\n" +
  180. " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  181. " " + datavar + "[index_i] = inChar;\n" +
  182. " index_i++;\n" +
  183. " }\n" +
  184. " }\n" +
  185. " if(started && ended) {\n" +
  186. " return 1;\n" +
  187. " } else {\n" +
  188. " return 0;\n" +
  189. " }\n" +
  190. "}\n";
  191. Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  192. var code = "boolean receiveMsgsuccess_ = handleDataIn();\n";
  193. return code;
  194. };
  195. Blockly.Python['bluetooth_intercomms_receive_getData_old'] = function(block) {
  196. var index = block.getFieldValue("INDEX");
  197. var code = "BTIC_receive[" + index + "]";
  198. return [code, Blockly.Python.ORDER_ATOMIC];
  199. };
  200. /******************************** */
  201. Blockly.Python['bluetooth_at_slave'] = function(block) {
  202. var baud = block.getFieldValue("BAUD")
  203. var BT_at_slave_decl = 'int bluetoothATModeserialSendState = 0;\n' +
  204. 'char c = \' \';\n';
  205. Blockly.Python.addDeclaration("BT_at_slave_decl", BT_at_slave_decl);
  206. var BT_at_slave_setup = ' Serial.begin(' + baud + ');\n' +
  207. ' Serial1.begin(38400);\n'
  208. Blockly.Python.addSetup("BT_at_slave_setup", BT_at_slave_setup);
  209. var BT_at_slave_func = 'void bluetoothATModeInteractive() {\n' +
  210. ' while (Serial1.available() > 0) {\n' +
  211. ' c = Serial1.read();\n' +
  212. ' Serial.write(c);\n' +
  213. ' }\n' +
  214. ' while (Serial.available() > 0) {\n' +
  215. ' c = Serial.read();\n' +
  216. ' Serial1.write(c);\n' +
  217. ' }\n' +
  218. '}\n'
  219. Blockly.Python.addFunction("BT_at_slave_func", BT_at_slave_func);
  220. var code = ' bluetoothATModeInteractive();\n' +
  221. ' // Slave\n' +
  222. ' if ( (millis() / 1000) % 50 == 5 && bluetoothATModeserialSendState == 0) {\n' +
  223. ' Serial.println(" ____ _ _ _ _ ");\n' +
  224. ' Serial.println(" | __ ) | | _ _ ___ | |_ ___ ___ | |_ | |__ ");\n' +
  225. ' Serial.println(" | _ \\ | | | | | | / _ \\ | __| / _ \\ / _ \\ | __| | \'_ \\ ");\n' +
  226. ' Serial.println(" | |_) | | | | |_| | | __/ | |_ | (_) | | (_) | | |_ | | | |");\n' +
  227. ' Serial.println(" |____/ |_| \\__,_| \\___| \\__| \\___/ \\___/ \\__| |_| |_|");\n' +
  228. ' Serial.println("");\n' +
  229. ' Serial.println(" ____ _ ");\n' +
  230. ' Serial.println(" / ___| | | __ _ __ __ ___ ");\n' +
  231. ' Serial.println(" \\___ \\ | | / _` | \\ \\ / / / _ ");\n' +
  232. ' Serial.println(" ___) | | | | (_| | \\ V / | __/");\n' +
  233. ' Serial.println(" |____/ |_| \\__,_| \\_/ \\___|");\n' +
  234. ' Serial.println("");\n' +
  235. ' Serial.println("");\n' +
  236. ' Serial.println("1. Testing communication:");\n' +
  237. ' Serial.print("- ");\n' +
  238. ' Serial1.write("AT\\r\\n");\n' +
  239. ' bluetoothATModeserialSendState = 1;\n' +
  240. ' } else if ( (millis() / 1000) % 50 == 6) {\n' +
  241. ' bluetoothATModeserialSendState = 0;\n' +
  242. ' }\n' +
  243. ' if ( (millis() / 1000) % 50 == 7 && bluetoothATModeserialSendState == 0) {\n' +
  244. ' Serial.println("");\n' +
  245. ' Serial.println("2. Reset previous configuration:");\n' +
  246. ' Serial.print("- ");\n' +
  247. ' Serial1.write("AT+ORGL\\r\\n");\n' +
  248. ' bluetoothATModeserialSendState = 1;\n' +
  249. ' } else if ( (millis() / 1000) % 50 == 8) {\n' +
  250. ' bluetoothATModeserialSendState = 0;\n' +
  251. ' }\n' +
  252. ' if ( (millis() / 1000) % 50 == 10 && bluetoothATModeserialSendState == 0) {\n' +
  253. ' Serial.println("");\n' +
  254. ' Serial.println("3. Set up UART configuration: AT+UART=' + baud + ',0,0");\n' +
  255. ' Serial.print("- ");\n' +
  256. ' Serial1.write("AT+UART=' + baud + ',0,0\\r\\n");\n' +
  257. ' bluetoothATModeserialSendState = 1;\n' +
  258. ' } else if ( (millis() / 1000) % 50 == 11) {\n' +
  259. ' bluetoothATModeserialSendState = 0;\n' +
  260. ' }\n' +
  261. ' if ( (millis() / 1000) % 50 == 13 && bluetoothATModeserialSendState == 0) {\n' +
  262. ' Serial.println("");\n' +
  263. ' Serial.println("4. Set up pairing mode, prevent outside connection: AT+CMODE=0");\n' +
  264. ' Serial.print("- ");\n' +
  265. ' Serial1.write("AT+CMODE=0\\r\\n");\n' +
  266. ' bluetoothATModeserialSendState = 1;\n' +
  267. ' } else if ( (millis() / 1000) % 50 == 14) {\n' +
  268. ' bluetoothATModeserialSendState = 0;\n' +
  269. ' }\n' +
  270. ' if ( (millis() / 1000) % 50 == 16 && bluetoothATModeserialSendState == 0) {\n' +
  271. ' Serial.println("");\n' +
  272. ' Serial.println("5. Set up pairing mode, set as Slave Mode: AT+ROLE=0");\n' +
  273. ' Serial.print("- ");\n' +
  274. ' Serial1.write("AT+ROLE=0\\r\\n");\n' +
  275. ' bluetoothATModeserialSendState = 1;\n' +
  276. ' } else if ( (millis() / 1000) % 50 == 17) {\n' +
  277. ' bluetoothATModeserialSendState = 0;\n' +
  278. ' }\n' +
  279. ' if ( (millis() / 1000) % 50 == 18 && bluetoothATModeserialSendState == 0) {\n' +
  280. ' Serial.println("");\n' +
  281. ' Serial.println("6. Getting MAC address:");\n' +
  282. ' Serial.print("- ");\n' +
  283. ' Serial1.write("AT+ADDR\\r\\n");\n' +
  284. ' bluetoothATModeserialSendState = 1;\n' +
  285. ' } else if ( (millis() / 1000) % 50 == 19) {\n' +
  286. ' bluetoothATModeserialSendState = 0;\n' +
  287. ' }\n' +
  288. ' if ( (millis() / 1000) % 50 == 21 && bluetoothATModeserialSendState == 0) {\n' +
  289. ' Serial.println("");\n' +
  290. ' Serial.println("7. Done, reset bluetooth module to normal state.");\n' +
  291. ' Serial.print("- ");\n' +
  292. ' Serial1.write("AT+RESET\\r\\n");\n' +
  293. ' bluetoothATModeserialSendState = 1;\n' +
  294. ' } else if ( (millis() / 1000) % 50 == 22) {\n' +
  295. ' bluetoothATModeserialSendState = 0;\n' +
  296. ' }\n'
  297. return code;
  298. }
  299. Blockly.Python['bluetooth_at_master'] = function(block) {
  300. var baud = block.getFieldValue("BAUD");
  301. var addr = block.getFieldValue("ADDR").replace(/:/g, ",");
  302. var BT_at_master_decl = 'int bluetoothATModeserialSendState = 0;\n' +
  303. 'char c = \' \';\n'
  304. Blockly.Python.addDeclaration("BT_at_master_decl", BT_at_master_decl);
  305. var BT_at_master_setup = ' Serial.begin(' + baud + ');\n' +
  306. ' Serial1.begin(38400);\n';
  307. Blockly.Python.addSetup("BT_at_master_setup", BT_at_master_setup);
  308. var BT_at_master_fun = 'void bluetoothATModeInteractive() {\n' +
  309. ' while (Serial1.available() > 0) {\n' +
  310. ' c = Serial1.read();\n' +
  311. ' Serial.write(c);\n' +
  312. ' }\n' +
  313. ' while (Serial.available() > 0) {\n' +
  314. ' c = Serial.read();\n' +
  315. ' Serial1.write(c);\n' +
  316. ' }\n' +
  317. '}\n';
  318. Blockly.Python.addFunction("BT_at_master_fun", BT_at_master_fun)
  319. var code = ' bluetoothATModeInteractive();\n' +
  320. ' // Master\n' +
  321. ' if ( (millis() / 1000) % 50 == 5 && bluetoothATModeserialSendState == 0) {\n' +
  322. ' Serial.println(" ____ _ _ _ _ ");\n' +
  323. ' Serial.println(" | __ ) | | _ _ ___ | |_ ___ ___ | |_ | |__ ");\n' +
  324. ' Serial.println(" | _ \\ | | | | | | / _ \\ | __| / _ \\ / _ \\ | __| | \'_ \\ ");\n' +
  325. ' Serial.println(" | |_) | | | | |_| | | __/ | |_ | (_) | | (_) | | |_ | | | |");\n' +
  326. ' Serial.println(" |____/ |_| \\__,_| \\___| \\__| \\___/ \\___/ \\__| |_| |_|");\n' +
  327. ' Serial.println("");\n' +
  328. ' Serial.println(" __ __ _ ");\n' +
  329. ' Serial.println(" | \\/ | __ _ ___ | |_ ___ _ __ ");\n' +
  330. ' Serial.println(" | |\\/| | / _` | / __| | __| / _ \\ | \'__|");\n' +
  331. ' Serial.println(" | | | | | (_| | \\__ \\ | |_ | __/ | | ");\n' +
  332. ' Serial.println(" |_| |_| \\__,_| |___/ \\__| \\___| |_| ");\n' +
  333. ' Serial.println("");\n' +
  334. ' Serial.println("");\n' +
  335. ' Serial.println("1. Testing communication:");\n' +
  336. ' Serial.print("- ");\n' +
  337. ' Serial1.write("AT\\r\\n");\n' +
  338. ' bluetoothATModeserialSendState = 1;\n' +
  339. ' } else if ( (millis() / 1000) % 50 == 6) {\n' +
  340. ' bluetoothATModeserialSendState = 0;\n' +
  341. ' }\n' +
  342. ' if ( (millis() / 1000) % 50 == 7 && bluetoothATModeserialSendState == 0) {\n' +
  343. ' Serial.println("");\n' +
  344. ' Serial.println("2. Reset previous configuration:");\n' +
  345. ' Serial.print("- ");\n' +
  346. ' Serial1.write("AT+ORGL\\r\\n");\n' +
  347. ' bluetoothATModeserialSendState = 1;\n' +
  348. ' } else if ( (millis() / 1000) % 50 == 8) {\n' +
  349. ' bluetoothATModeserialSendState = 0;\n' +
  350. ' }\n' +
  351. ' if ( (millis() / 1000) % 50 == 10 && bluetoothATModeserialSendState == 0) {\n' +
  352. ' Serial.println("");\n' +
  353. ' Serial.println("3. Set up UART configuration: AT+UART=' + baud + ',0,0");\n' +
  354. ' Serial.print("- ");\n' +
  355. ' Serial1.write("AT+UART=' + baud + ',0,0\\r\\n");\n' +
  356. ' bluetoothATModeserialSendState = 1;\n' +
  357. ' } else if ( (millis() / 1000) % 50 == 11) {\n' +
  358. ' bluetoothATModeserialSendState = 0;\n' +
  359. ' }\n' +
  360. ' if ( (millis() / 1000) % 50 == 13 && bluetoothATModeserialSendState == 0) {\n' +
  361. ' Serial.println("");\n' +
  362. ' Serial.println("4. Set up pairing mode, prevent outside connection: AT+CMODE=0");\n' +
  363. ' Serial.print("- ");\n' +
  364. ' Serial1.write("AT+CMODE=0\\r\\n");\n' +
  365. ' bluetoothATModeserialSendState = 1;\n' +
  366. ' } else if ( (millis() / 1000) % 50 == 14) {\n' +
  367. ' bluetoothATModeserialSendState = 0;\n' +
  368. ' }\n' +
  369. ' if ( (millis() / 1000) % 50 == 16 && bluetoothATModeserialSendState == 0) {\n' +
  370. ' Serial.println("");\n' +
  371. ' Serial.println("5. Set up pairing mode, set as Master Mode: AT+ROLE=1");\n' +
  372. ' Serial.print("- ");\n' +
  373. ' Serial1.write("AT+ROLE=1\\r\\n");\n' +
  374. ' bluetoothATModeserialSendState = 1;\n' +
  375. ' } else if ( (millis() / 1000) % 50 == 17) {\n' +
  376. ' bluetoothATModeserialSendState = 0;\n' +
  377. ' }\n' +
  378. ' if ( (millis() / 1000) % 50 == 18 && bluetoothATModeserialSendState == 0) {\n' +
  379. ' Serial.println("");\n' +
  380. ' Serial.println("6. Binding slave device:");\n' +
  381. ' Serial.print("- ");\n' +
  382. ' Serial1.write("AT+BIND=' + addr + '\\r\\n");\n' +
  383. ' bluetoothATModeserialSendState = 1;\n' +
  384. ' } else if ( (millis() / 1000) % 50 == 19) {\n' +
  385. ' bluetoothATModeserialSendState = 0;\n' +
  386. ' }\n' +
  387. ' if ( (millis() / 1000) % 50 == 21 && bluetoothATModeserialSendState == 0) {\n' +
  388. ' Serial.println("");\n' +
  389. ' Serial.println("7. Done, reset bluetooth module to normal state.");\n' +
  390. ' Serial.print("- ");\n' +
  391. ' Serial1.write("AT+RESET\\r\\n");\n' +
  392. ' bluetoothATModeserialSendState = 1;\n' +
  393. ' } else if ( (millis() / 1000) % 50 == 22) {\n' +
  394. ' bluetoothATModeserialSendState = 0;\n' +
  395. ' }\n'
  396. return code;
  397. }
  398. Blockly.Python['bluetooth_at_interaction'] = function(block) {
  399. var dropdown_name = block.getFieldValue('bluetooth_at_interaction_baud');
  400. Blockly.Python.addDeclaration('bluetooth_at_interaction_declare', 'char c = \' \';\n');
  401. Blockly.Python.addSetup('bluetooth_at_interaction_setup', '' +
  402. 'Serial.begin(' + dropdown_name + ');\n' +
  403. 'Serial1.begin(' + dropdown_name + ');\n' +
  404. '');
  405. Blockly.Python.addFunction('bluetooth_at_interaction_function', '' +
  406. 'void _bluetoothATmode() {\n' +
  407. ' if (Serial1.available())\n' +
  408. ' Serial.write(Serial1.read());\n' +
  409. ' if (Serial.available())\n' +
  410. ' Serial1.write(Serial.read());\n' +
  411. '}\n' +
  412. '');
  413. var code = '_bluetoothATmode();\n';
  414. return code;
  415. };