bluetooth.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. "use strict";
  2. goog.provide("Blockly.Blocks.bluetooth");
  3. goog.require("Blockly.Blocks");
  4. goog.require("Blockly.Types");
  5. Blockly.Blocks.bluetooth.HUE = 230;
  6. Blockly.Blocks["bluetooth_setup"] = {
  7. init: function() {
  8. this.appendDummyInput()
  9. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_bluetooth.png", 140, 40, "0"));
  10. .appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth.png",50,40,"0"));
  11. this.appendDummyInput()
  12. .appendField(Blockly.Msg.BLUETOOTH_SETUP)
  13. .appendField(
  14. new Blockly.FieldDropdown([
  15. [Blockly.Msg.BLUETOOTH_SETUP_OPT1, "string"],
  16. [Blockly.Msg.BLUETOOTH_SETUP_OPT2, "number"],
  17. [Blockly.Msg.BLUETOOTH_SETUP_OPT3, "array"]
  18. ]),
  19. "OPTIONS"
  20. );
  21. this.appendDummyInput("VAR")
  22. .appendField(Blockly.Msg.BLUETOOTH_SETUP_DATA_SET)
  23. .appendField(new Blockly.FieldVariable("command"), "CMDVAR")
  24. .appendField(Blockly.Msg.BLUETOOTH_SETUP_TO);
  25. this.appendDummyInput("BAUDSETTING")
  26. .appendField(Blockly.Msg.BLUETOOTH_SETUP_SET)
  27. .appendField(
  28. new Blockly.FieldDropdown([["38400", "38400"], ["9600", "9600"]]),
  29. "BAUD"
  30. );
  31. this.appendStatementInput("RECEIVED_DO")
  32. .setCheck(null)
  33. .appendField(Blockly.Msg.BLUETOOTH_SETUP_DO);
  34. this.getVarType();
  35. this.setColour(Blockly.Blocks.bluetooth.HUE);
  36. this.setTooltip("");
  37. this.setHelpUrl("");
  38. },
  39. getVarType: function(varName) {
  40. let vartype = Blockly.Types.UNDEF;
  41. if (this && this.getFieldValue("OPTIONS") === "string") {
  42. vartype = Blockly.Types.TEXT;
  43. } else if (this && this.getFieldValue("OPTIONS") === "number") {
  44. vartype = Blockly.Types.NUMBER;
  45. } else if (this && this.getFieldValue("OPTIONS") === "array") {
  46. vartype = Blockly.Types.ARRAY;
  47. }
  48. return vartype;
  49. },
  50. onchange: function() {
  51. if (this && this.getFieldValue("OPTIONS") === "array") {
  52. if (!this.getInput("VARLENGTH")) {
  53. this.appendDummyInput("VARLENGTH")
  54. .appendField(Blockly.Msg.BLUETOOTH_SETUP_LIST_SETTING)
  55. .appendField(
  56. new Blockly.FieldDropdown([
  57. ["1", "1"],
  58. ["2", "2"],
  59. ["3", "3"],
  60. ["4", "4"],
  61. ["5", "5"],
  62. ["6", "6"],
  63. ["7", "7"],
  64. ["8", "8"],
  65. ["9", "9"],
  66. ["10", "10"]
  67. ]),
  68. "LENGTH"
  69. );
  70. this.moveInputBefore("VARLENGTH", "BAUDSETTING");
  71. }
  72. } else {
  73. if (this.getInput("VARLENGTH")) this.removeInput("VARLENGTH");
  74. }
  75. }
  76. };
  77. Blockly.Blocks["bluetooth_text_getCommand"] = {
  78. init: function() {
  79. this.appendDummyInput()
  80. .appendField(Blockly.Msg.BLUETOOTH_TEXT_RECEIVED)
  81. .appendField(new Blockly.FieldVariable("command"), "VAR");
  82. this.setColour(Blockly.Blocks.bluetooth.HUE);
  83. this.setOutput(true);
  84. this.setTooltip("");
  85. this.setHelpUrl("");
  86. },
  87. customContextMenu: function(options) {
  88. var option = { enabled: true };
  89. var name = this.getFieldValue("VAR");
  90. option.text = this.contextMenuMsg_.replace("%1", name);
  91. var xmlField = goog.dom.createDom("field", null, name);
  92. xmlField.setAttribute("name", "VAR");
  93. var xmlBlock = goog.dom.createDom("block", null, xmlField);
  94. xmlBlock.setAttribute("type", this.contextMenuType_);
  95. option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
  96. options.push(option);
  97. },
  98. getBlockType: function() {
  99. return Blockly.Types.TEXT;
  100. },
  101. getVarType: function(varName) {
  102. return Blockly.Types.TEXT;
  103. }
  104. };
  105. Blockly.Blocks["bluetooth_number_getCommand"] = {
  106. init: function() {
  107. this.appendDummyInput()
  108. .appendField(Blockly.Msg.BLUETOOTH_NUMBER_RECEIVED)
  109. .appendField(new Blockly.FieldVariable("command"), "VAR");
  110. this.setColour(Blockly.Blocks.bluetooth.HUE);
  111. this.setOutput(true);
  112. this.setTooltip("");
  113. this.setHelpUrl("");
  114. },
  115. customContextMenu: function(options) {
  116. var option = { enabled: true };
  117. var name = this.getFieldValue("VAR");
  118. option.text = this.contextMenuMsg_.replace("%1", name);
  119. var xmlField = goog.dom.createDom("field", null, name);
  120. xmlField.setAttribute("name", "VAR");
  121. var xmlBlock = goog.dom.createDom("block", null, xmlField);
  122. xmlBlock.setAttribute("type", this.contextMenuType_);
  123. option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
  124. options.push(option);
  125. },
  126. getBlockType: function() {
  127. return Blockly.Types.NUMBER;
  128. },
  129. getVarType: function(varName) {
  130. return Blockly.Types.NUMBER;
  131. }
  132. };
  133. Blockly.Blocks["bluetooth_list_getCommand"] = {
  134. init: function() {
  135. this.appendDummyInput()
  136. .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED)
  137. .appendField(new Blockly.FieldVariable("command"), "VAR")
  138. .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED1)
  139. .appendField(
  140. new Blockly.FieldDropdown([
  141. ["0", "0"],
  142. ["1", "1"],
  143. ["2", "2"],
  144. ["3", "3"],
  145. ["4", "4"],
  146. ["5", "5"],
  147. ["6", "6"],
  148. ["7", "7"],
  149. ["8", "8"],
  150. ["9", "9"],
  151. ]),
  152. "INDEX"
  153. )
  154. .appendField(Blockly.Msg.BLUETOOTH_LIST_RECEIVED2);
  155. this.setColour(Blockly.Blocks.bluetooth.HUE);
  156. this.setOutput(true);
  157. this.setTooltip("");
  158. this.setHelpUrl("");
  159. },
  160. customContextMenu: function(options) {
  161. var option = { enabled: true };
  162. var name = this.getFieldValue("VAR");
  163. option.text = this.contextMenuMsg_.replace("%1", name);
  164. var xmlField = goog.dom.createDom("field", null, name);
  165. xmlField.setAttribute("name", "VAR");
  166. var xmlBlock = goog.dom.createDom("block", null, xmlField);
  167. xmlBlock.setAttribute("type", this.contextMenuType_);
  168. option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
  169. options.push(option);
  170. },
  171. getBlockType: function() {
  172. return Blockly.Types.NUMBER;
  173. },
  174. getVarType: function(varName) {
  175. return Blockly.Types.NUMBER;
  176. }
  177. };
  178. /*bluetooth intercommunication*/
  179. Blockly.Blocks["bluetooth_intercomms_send"] = {
  180. init: function() {
  181. this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
  182. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/main-bluetooth_comm.png", 50, 40, "0"));
  183. this.appendDummyInput()
  184. .appendField("New "+Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
  185. this.appendDummyInput()
  186. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  187. .appendField(new Blockly.FieldDropdown([
  188. ["9600","9600"],
  189. ["38400","38400"]]),"BAUD")
  190. this.appendDummyInput()
  191. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SENDMSG)
  192. this.appendValueInput("ADD0")
  193. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA)
  194. .setAlign(Blockly.ALIGN_RIGHT)
  195. // this.setColour(215);
  196. this.setColour(145);
  197. this.setTooltip("");
  198. this.setHelpUrl("");
  199. this.setPreviousStatement(true);
  200. this.setNextStatement(true);
  201. this.itemCount_ = 1;
  202. this.updateShape_();
  203. this.setMutator(new Blockly.Mutator(['bluetooth_intercomms_create_with_item']));
  204. },
  205. mutationToDom: function() {
  206. var container = document.createElement('mutation');
  207. container.setAttribute('items', this.itemCount_);
  208. return container;
  209. },
  210. /**
  211. * Parse XML to restore the inputs.
  212. * @param {!Element} xmlElement XML storage element.
  213. * @this Blockly.Block
  214. */
  215. domToMutation: function(xmlElement) {
  216. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  217. this.updateShape_();
  218. },
  219. /**
  220. * Populate the mutator's dialog with this block's components.
  221. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  222. * @return {!Blockly.Block} Root block in mutator.
  223. * @this Blockly.Block
  224. */
  225. decompose: function(workspace) {
  226. var containerBlock = workspace.newBlock('bluetooth_intercomms_create_with_container');
  227. containerBlock.initSvg();
  228. var connection = containerBlock.getInput('STACK').connection;
  229. for (var i = 0; i < this.itemCount_; i++) {
  230. var itemBlock = workspace.newBlock('bluetooth_intercomms_create_with_item');
  231. itemBlock.initSvg();
  232. connection.connect(itemBlock.previousConnection);
  233. connection = itemBlock.nextConnection;
  234. }
  235. return containerBlock;
  236. },
  237. /**
  238. * Reconfigure this block based on the mutator dialog's components.
  239. * @param {!Blockly.Block} containerBlock Root block in mutator.
  240. * @this Blockly.Block
  241. */
  242. compose: function(containerBlock) {
  243. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  244. // Count number of inputs.
  245. var connections = [];
  246. while (itemBlock) {
  247. connections.push(itemBlock.valueConnection_);
  248. itemBlock = itemBlock.nextConnection &&
  249. itemBlock.nextConnection.targetBlock();
  250. }
  251. // Disconnect any children that don't belong.
  252. for (var i = 0; i < this.itemCount_; i++) {
  253. var connection = this.getInput('ADD' + i).connection.targetConnection;
  254. if (connection && connections.indexOf(connection) == -1) {
  255. connection.disconnect();
  256. }
  257. }
  258. this.itemCount_ = connections.length;
  259. this.updateShape_();
  260. // Reconnect any child blocks.
  261. for (var i = 0; i < this.itemCount_; i++) {
  262. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  263. }
  264. },
  265. /**
  266. * Store pointers to any connected child blocks.
  267. * @param {!Blockly.Block} containerBlock Root block in mutator.
  268. * @this Blockly.Block
  269. */
  270. saveConnections: function(containerBlock) {
  271. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  272. var i = 0;
  273. while (itemBlock) {
  274. var input = this.getInput('ADD' + i);
  275. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  276. i++;
  277. itemBlock = itemBlock.nextConnection &&
  278. itemBlock.nextConnection.targetBlock();
  279. }
  280. },
  281. /**
  282. * Modify this block to have the correct number of inputs.
  283. * @private
  284. * @this Blockly.Block
  285. */
  286. updateShape_: function() {
  287. // Add new inputs.
  288. for (var i = 0; i < this.itemCount_; i++) {
  289. if (!this.getInput('ADD' + i)) {
  290. var input = this.appendValueInput('ADD' + i)
  291. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA + i)
  292. .setAlign(Blockly.ALIGN_RIGHT)
  293. }
  294. }
  295. while (this.getInput('ADD' + i)) {
  296. this.removeInput('ADD' + i);
  297. i++;
  298. }
  299. },
  300. };
  301. Blockly.Blocks['bluetooth_intercomms_create_with_container'] = {
  302. /**
  303. * Mutator block for list container.
  304. * @this Blockly.Block
  305. */
  306. init: function() {
  307. this.setColour(215);
  308. this.appendDummyInput()
  309. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  310. this.appendStatementInput('STACK');
  311. this.setTooltip('');
  312. this.contextMenu = false;
  313. }
  314. };
  315. Blockly.Blocks['bluetooth_intercomms_create_with_item'] = {
  316. /**
  317. * Mutator bolck for adding items.
  318. * @this Blockly.Block
  319. */
  320. init: function() {
  321. this.setColour(215);
  322. this.appendDummyInput()
  323. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  324. this.setPreviousStatement(true);
  325. this.setNextStatement(true);
  326. this.setTooltip('');
  327. this.contextMenu = false;
  328. }
  329. };
  330. /*receive message from bluetooth to bluetooth */
  331. Blockly.Blocks["bluetooth_intercomms_receive"] = {
  332. init: function() {
  333. this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
  334. this.appendDummyInput()
  335. .appendField("New "+Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
  336. this.appendDummyInput()
  337. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  338. .appendField(new Blockly.FieldDropdown([
  339. ["9600","9600"],
  340. ["38400","38400"]]),"BAUD")
  341. this.appendDummyInput()
  342. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_TIMEOUT)
  343. .appendField(new Blockly.FieldTextInput("50"),"TIMEOUT")
  344. this.appendDummyInput()
  345. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVEMSG)
  346. this.appendDummyInput()
  347. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_LINE3)
  348. .appendField(new Blockly.FieldDropdown([
  349. ["1", "1"],
  350. ["2", "2"],
  351. ["3", "3"],
  352. ["4", "4"],
  353. ["5", "5"],
  354. ["6", "6"],
  355. ["7", "7"],
  356. ["8", "8"],
  357. ["9", "9"],
  358. ["10", "10"],
  359. ["11", "11"],
  360. ["12", "12"],
  361. ]), "LENGTH");
  362. // this.setColour("215");
  363. this.setColour(145);
  364. this.setPreviousStatement(true, null);
  365. this.setNextStatement(true, null);
  366. // this.setOutput(true,Blockly.Types.BOOLEAN.output);
  367. this.setTooltip("");
  368. this.setHelpUrl("");
  369. },
  370. getVarType: function(varName) {
  371. return Blockly.Types.ARRAY;
  372. },
  373. getBlockType: function() {
  374. var type = this.getFieldValue("TYPE");
  375. switch (type) {
  376. case "" :
  377. return Blockly.Types.TEXT;
  378. case ".toInt()":
  379. return Blockly.Types.NUMBER;
  380. case ".toFloat()":
  381. return Blockly.Types.LARGE_NUMBER;
  382. }
  383. return Blockly.Types.NUMBER;
  384. },
  385. };
  386. /*check if sucessfully receive message from bluetooth to bluetooth */
  387. Blockly.Blocks["bluetooth_intercomms_receive_getData"] = {
  388. init: function() {
  389. this.appendDummyInput()
  390. // .appendField(" " + Blockly.Msg.MAINEASYMODE_TRANSFER_RECEIVE_GET)
  391. // .appendField(new Blockly.FieldDropdown([
  392. // ["rxData", "rxData"]
  393. // ]), "DATAMAIN")
  394. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2)
  395. .appendField(new Blockly.FieldDropdown([
  396. ["0", "0"],
  397. ["1", "1"],
  398. ["2", "2"],
  399. ["3", "3"],
  400. ["4", "4"],
  401. ["5", "5"],
  402. ["6", "6"],
  403. ["7", "7"],
  404. ["8", "8"],
  405. ["9", "9"],
  406. ["10", "10"],
  407. ["11", "11"],
  408. ]), "INDEX")
  409. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE3)
  410. .appendField(new Blockly.FieldDropdown([
  411. ["String", ""],
  412. ["Number", ".toInt()"],
  413. ["Float", ".toFloat()"],
  414. ]), "TYPE")
  415. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE4)
  416. // this.setColour("215");
  417. this.setColour(145);
  418. this.setOutput(true)
  419. // this.setInputsInline(true);
  420. this.setTooltip("");
  421. this.setHelpUrl("");
  422. },
  423. getBlockType: function() {
  424. var type = this.getFieldValue("TYPE");
  425. switch (type) {
  426. case "" :
  427. return Blockly.Types.TEXT;
  428. case ".toInt()":
  429. return Blockly.Types.NUMBER;
  430. case ".toFloat()":
  431. return Blockly.Types.LARGE_NUMBER;
  432. }
  433. return Blockly.Types.NUMBER;
  434. },
  435. getOutputType: function() {
  436. var type = this.getFieldValue("TYPE");
  437. switch (type) {
  438. case "" :
  439. return Blockly.Types.TEXT.output
  440. case ".toInt()":
  441. return Blockly.Types.NUMBER.output;
  442. case ".toFloat()":
  443. return Blockly.Types.LARGE_NUMBER.output;
  444. }
  445. return Blockly.Types.NUMBER.output;
  446. }
  447. };
  448. /************************************************* */
  449. /*bluetooth intercommunication*/
  450. Blockly.Blocks["bluetooth_intercomms_send_old"] = {
  451. init: function() {
  452. this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
  453. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/main-bluetooth_comm.png", 50, 40, "0"));
  454. this.appendDummyInput()
  455. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
  456. this.appendDummyInput()
  457. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  458. .appendField(new Blockly.FieldDropdown([
  459. ["38400","38400"],
  460. ["9600","9600"]]),"BAUD")
  461. this.appendDummyInput()
  462. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SENDMSG)
  463. this.appendValueInput("ADD0")
  464. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA)
  465. .setAlign(Blockly.ALIGN_RIGHT)
  466. this.setColour(215);
  467. this.setTooltip("");
  468. this.setHelpUrl("");
  469. this.setPreviousStatement(true);
  470. this.setNextStatement(true);
  471. this.itemCount_ = 1;
  472. this.updateShape_();
  473. this.setMutator(new Blockly.Mutator(['bluetooth_intercomms_create_with_item']));
  474. },
  475. mutationToDom: function() {
  476. var container = document.createElement('mutation');
  477. container.setAttribute('items', this.itemCount_);
  478. return container;
  479. },
  480. /**
  481. * Parse XML to restore the inputs.
  482. * @param {!Element} xmlElement XML storage element.
  483. * @this Blockly.Block
  484. */
  485. domToMutation: function(xmlElement) {
  486. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  487. this.updateShape_();
  488. },
  489. /**
  490. * Populate the mutator's dialog with this block's components.
  491. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  492. * @return {!Blockly.Block} Root block in mutator.
  493. * @this Blockly.Block
  494. */
  495. decompose: function(workspace) {
  496. var containerBlock = workspace.newBlock('bluetooth_intercomms_create_with_container');
  497. containerBlock.initSvg();
  498. var connection = containerBlock.getInput('STACK').connection;
  499. for (var i = 0; i < this.itemCount_; i++) {
  500. var itemBlock = workspace.newBlock('bluetooth_intercomms_create_with_item');
  501. itemBlock.initSvg();
  502. connection.connect(itemBlock.previousConnection);
  503. connection = itemBlock.nextConnection;
  504. }
  505. return containerBlock;
  506. },
  507. /**
  508. * Reconfigure this block based on the mutator dialog's components.
  509. * @param {!Blockly.Block} containerBlock Root block in mutator.
  510. * @this Blockly.Block
  511. */
  512. compose: function(containerBlock) {
  513. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  514. // Count number of inputs.
  515. var connections = [];
  516. while (itemBlock) {
  517. connections.push(itemBlock.valueConnection_);
  518. itemBlock = itemBlock.nextConnection &&
  519. itemBlock.nextConnection.targetBlock();
  520. }
  521. // Disconnect any children that don't belong.
  522. for (var i = 0; i < this.itemCount_; i++) {
  523. var connection = this.getInput('ADD' + i).connection.targetConnection;
  524. if (connection && connections.indexOf(connection) == -1) {
  525. connection.disconnect();
  526. }
  527. }
  528. this.itemCount_ = connections.length;
  529. this.updateShape_();
  530. // Reconnect any child blocks.
  531. for (var i = 0; i < this.itemCount_; i++) {
  532. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  533. }
  534. },
  535. /**
  536. * Store pointers to any connected child blocks.
  537. * @param {!Blockly.Block} containerBlock Root block in mutator.
  538. * @this Blockly.Block
  539. */
  540. saveConnections: function(containerBlock) {
  541. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  542. var i = 0;
  543. while (itemBlock) {
  544. var input = this.getInput('ADD' + i);
  545. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  546. i++;
  547. itemBlock = itemBlock.nextConnection &&
  548. itemBlock.nextConnection.targetBlock();
  549. }
  550. },
  551. /**
  552. * Modify this block to have the correct number of inputs.
  553. * @private
  554. * @this Blockly.Block
  555. */
  556. updateShape_: function() {
  557. // Add new inputs.
  558. for (var i = 0; i < this.itemCount_; i++) {
  559. if (!this.getInput('ADD' + i)) {
  560. var input = this.appendValueInput('ADD' + i)
  561. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_DATA + i)
  562. .setAlign(Blockly.ALIGN_RIGHT)
  563. }
  564. }
  565. while (this.getInput('ADD' + i)) {
  566. this.removeInput('ADD' + i);
  567. i++;
  568. }
  569. },
  570. };
  571. Blockly.Blocks['bluetooth_intercomms_create_with_container'] = {
  572. /**
  573. * Mutator block for list container.
  574. * @this Blockly.Block
  575. */
  576. init: function() {
  577. this.setColour(215);
  578. this.appendDummyInput()
  579. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  580. this.appendStatementInput('STACK');
  581. this.setTooltip('');
  582. this.contextMenu = false;
  583. }
  584. };
  585. Blockly.Blocks['bluetooth_intercomms_create_with_item'] = {
  586. /**
  587. * Mutator bolck for adding items.
  588. * @this Blockly.Block
  589. */
  590. init: function() {
  591. this.setColour(215);
  592. this.appendDummyInput()
  593. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  594. this.setPreviousStatement(true);
  595. this.setNextStatement(true);
  596. this.setTooltip('');
  597. this.contextMenu = false;
  598. }
  599. };
  600. /*receive message from bluetooth to bluetooth */
  601. Blockly.Blocks["bluetooth_intercomms_receive_old"] = {
  602. init: function() {
  603. this.appendDummyInput().appendField(new Blockly.FieldImage("./../blockly/media/main-bluetooth_comm.png",50,40,"0"));
  604. this.appendDummyInput()
  605. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_SETUP);
  606. this.appendDummyInput()
  607. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  608. .appendField(new Blockly.FieldDropdown([
  609. ["38400","38400"],
  610. ["9600","9600"]]),"BAUD")
  611. // this.appendDummyInput()
  612. // .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_TIMEOUT)
  613. // .appendField(new Blockly.FieldTextInput("50"),"TIMEOUT")
  614. this.appendDummyInput()
  615. .appendField(Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVEMSG)
  616. this.appendDummyInput()
  617. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_LINE3)
  618. .appendField(new Blockly.FieldDropdown([
  619. ["1", "1"],
  620. ["2", "2"],
  621. ["3", "3"],
  622. ["4", "4"],
  623. ["5", "5"],
  624. ["6", "6"],
  625. ["7", "7"],
  626. ["8", "8"],
  627. ["9", "9"],
  628. ["10", "10"],
  629. ["11", "11"],
  630. ["12", "12"],
  631. ]), "LENGTH");
  632. this.setColour("215");
  633. this.setPreviousStatement(true, null);
  634. this.setNextStatement(true, null);
  635. // this.setOutput(true,Blockly.Types.BOOLEAN.output);
  636. this.setTooltip("");
  637. this.setHelpUrl("");
  638. },
  639. getVarType: function(varName) {
  640. return Blockly.Types.ARRAY;
  641. }
  642. };
  643. /*check if sucessfully receive message from bluetooth to bluetooth */
  644. Blockly.Blocks["bluetooth_intercomms_receive_getData_old"] = {
  645. init: function() {
  646. this.appendDummyInput()
  647. // .appendField(" " + Blockly.Msg.MAINEASYMODE_TRANSFER_RECEIVE_GET)
  648. // .appendField(new Blockly.FieldDropdown([
  649. // ["rxData", "rxData"]
  650. // ]), "DATAMAIN")
  651. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2)
  652. .appendField(new Blockly.FieldDropdown([
  653. ["0", "0"],
  654. ["1", "1"],
  655. ["2", "2"],
  656. ["3", "3"],
  657. ["4", "4"],
  658. ["5", "5"],
  659. ["6", "6"],
  660. ["7", "7"],
  661. ["8", "8"],
  662. ["9", "9"],
  663. ["10", "10"],
  664. ["11", "11"],
  665. ]), "INDEX")
  666. .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE2_2)
  667. // .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE3)
  668. // .appendField(new Blockly.FieldDropdown([
  669. // ["String", ""],
  670. // ["Number", ".toInt()"],
  671. // ["Float", ".toFloat()"],
  672. // ]), "TYPE")
  673. // .appendField(" " + Blockly.Msg.BLUETOOTH_INTERCOMMS_RECEIVE_GET_LINE4)
  674. this.setColour("215");
  675. this.setOutput(true,Blockly.Types.NUMBER.output)
  676. // this.setInputsInline(true);
  677. this.setTooltip("");
  678. this.setHelpUrl("");
  679. },
  680. getBlockType: function() {
  681. // var type = this.getFieldValue("TYPE");
  682. // switch (type) {
  683. // case "" :
  684. // return Blockly.Types.TEXT;
  685. // case ".toInt()":
  686. // return Blockly.Types.NUMBER;
  687. // case ".toFloat()":
  688. // return Blockly.Types.LARGE_NUMBER;
  689. // }
  690. return Blockly.Types.NUMBER;
  691. },
  692. // getOutputType: function() {
  693. // var type = this.getFieldValue("TYPE");
  694. // switch (type) {
  695. // case "" :
  696. // return Blockly.Types.TEXT.output
  697. // case ".toInt()":
  698. // return Blockly.Types.NUMBER.output;
  699. // case ".toFloat()":
  700. // return Blockly.Types.LARGE_NUMBER.output;
  701. // }
  702. // return Blockly.Types.NUMBER.output;
  703. // }
  704. };
  705. /************************************************* */
  706. /*
  707. *AT Mode Block
  708. *Slave setting
  709. */
  710. Blockly.Blocks["bluetooth_at_slave"] = {
  711. init:function() {
  712. this.appendDummyInput()
  713. .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
  714. this.appendDummyInput()
  715. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SLAVE)
  716. this.appendDummyInput()
  717. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  718. .appendField(new Blockly.FieldDropdown([
  719. ["38400","38400"],
  720. ["9600","9600"]]),"BAUD")
  721. this.setColour("#5d67a0")
  722. this.setTooltip("");
  723. }
  724. }
  725. /*
  726. *AT Mode Block
  727. *master setting
  728. */
  729. Blockly.Blocks["bluetooth_at_master"] = {
  730. init:function() {
  731. this.appendDummyInput()
  732. .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
  733. this.appendDummyInput()
  734. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_MASTER)
  735. this.appendDummyInput()
  736. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_BIND_ADDR)
  737. .appendField(new Blockly.FieldTextInput("98d3:81:fd6a96"),"ADDR")
  738. this.appendDummyInput()
  739. .appendField(Blockly.Msg.BLUETOOTH_PAIRING_SETTING_BAUDRATE)
  740. .appendField(new Blockly.FieldDropdown([
  741. ["38400","38400"],
  742. ["9600","9600"]]),"BAUD")
  743. this.setColour("#5d67a0")
  744. this.setTooltip("");
  745. }
  746. }
  747. /*
  748. AT Mode code
  749. */
  750. Blockly.Blocks['bluetooth_at_interaction'] = {
  751. init: function() {
  752. this.appendDummyInput()
  753. .appendField(new Blockly.FieldImage("./../blockly/media/at_mode.png",120,40,"0"));
  754. this.appendDummyInput()
  755. .appendField(Blockly.Msg.BLUETOOTH_INTERACTION_TITLE);
  756. this.appendDummyInput()
  757. .appendField(Blockly.Msg.BLUETOOTH_INTERACTION_SET_CONTENT)
  758. .appendField(new Blockly.FieldDropdown([["9600","9600"], ["38400","38400"], ["115200","115200"]]), "bluetooth_at_interaction_baud");
  759. this.setColour("#5d67a0");
  760. this.setTooltip("");
  761. this.setHelpUrl("");
  762. }
  763. };