serial.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Blocks for the Arduino serial communication functions.
  7. * The Arduino built in functions syntax can be found at:
  8. * http://arduino.cc/en/Reference/HomePage
  9. *
  10. * TODO: There are more function that can be added:
  11. * http://arduino.cc/en/Reference/Serial
  12. */
  13. 'use strict';
  14. goog.provide('Blockly.Blocks.serial');
  15. goog.require('Blockly.Blocks');
  16. goog.require('Blockly.Types');
  17. /** Common HSV hue for all blocks in this category. */
  18. Blockly.Blocks.serial.HUE = 80;
  19. Blockly.Blocks['serial_setup'] = {
  20. /**
  21. * Block for setting the speed of the serial connection.
  22. * @this Blockly.Block
  23. */
  24. init: function() {
  25. this.setHelpUrl('http://arduino.cc/en/Serial/Begin');
  26. this.setColour(Blockly.Blocks.serial.HUE);
  27. this.appendDummyInput()
  28. .appendField(Blockly.Msg.ARD_SERIAL_SETUP)
  29. .appendField(
  30. new Blockly.FieldDropdown(
  31. Blockly.Python.Boards.selected.serial.concat([
  32. ["serial1", "Serial1"]
  33. ])), 'SERIAL_ID')
  34. .appendField(Blockly.Msg.ARD_SERIAL_SPEED)
  35. .appendField(
  36. new Blockly.FieldDropdown(
  37. [
  38. ["9600", "9600"],
  39. ["115200", "115200"]
  40. ]), 'SPEED')
  41. .appendField(Blockly.Msg.ARD_SERIAL_BPS);
  42. this.setInputsInline(true);
  43. this.setTooltip(Blockly.Msg.ARD_SERIAL_SETUP_TIP);
  44. },
  45. /**
  46. * Returns the serial instance name.
  47. * @return {!string} Serial instance name.
  48. * @this Blockly.Block
  49. */
  50. getSerialSetupInstance: function() {
  51. return this.getFieldValue('SERIAL_ID');
  52. },
  53. /**
  54. * Updates the content of the the serial related fields.
  55. * @this Blockly.Block
  56. */
  57. updateFields: function() {
  58. Blockly.Python.Boards.refreshBlockFieldDropdown(
  59. this, 'SERIAL_ID', 'serial');
  60. Blockly.Python.Boards.refreshBlockFieldDropdown(
  61. this, 'SPEED', 'serialSpeed');
  62. }
  63. };
  64. Blockly.Blocks['serial_print'] = {
  65. /**
  66. * Block for creating a write to serial com function.
  67. * @this Blockly.Block
  68. */
  69. init: function() {
  70. this.setHelpUrl('http://www.arduino.cc/en/Serial/Print');
  71. this.setColour(Blockly.Blocks.serial.HUE);
  72. this.appendDummyInput()
  73. .appendField(new Blockly.FieldDropdown(
  74. Blockly.Python.Boards.selected.serial), 'SERIAL_ID')
  75. .appendField(Blockly.Msg.ARD_SERIAL_PRINT);
  76. this.appendValueInput('CONTENT')
  77. .setCheck(null);
  78. this.appendDummyInput()
  79. .appendField(new Blockly.FieldCheckbox('TRUE'), 'NEW_LINE')
  80. .appendField(Blockly.Msg.ARD_SERIAL_PRINT_NEWLINE);
  81. this.setInputsInline(true);
  82. this.setPreviousStatement(true, null);
  83. this.setNextStatement(true, null);
  84. this.setTooltip(Blockly.Msg.ARD_SERIAL_PRINT_TIP);
  85. },
  86. /**
  87. * Called whenever anything on the workspace changes.
  88. * It checks the instances of serial_setup and attaches a warning to this
  89. * block if not valid data is found.
  90. * @this Blockly.Block
  91. */
  92. // onchange: function() {
  93. // if (!this.workspace) { return; } // Block has been deleted.
  94. // // Get the Serial instance from this block
  95. // var thisInstanceName = this.getFieldValue('SERIAL_ID');
  96. // // Iterate through top level blocks to find setup instance for the serial id
  97. // var blocks = Blockly.mainWorkspace.getTopBlocks();
  98. // var setupInstancePresent = false;
  99. // for (var x = 0; x < blocks.length; x++) {
  100. // var func = blocks[x].getSerialSetupInstance;
  101. // if (func) {
  102. // var setupBlockInstanceName = func.call(blocks[x]);
  103. // if (thisInstanceName == setupBlockInstanceName) {
  104. // setupInstancePresent = true;
  105. // }
  106. // }
  107. // }
  108. // if (!setupInstancePresent) {
  109. // this.setWarningText(Blockly.Msg.ARD_SERIAL_PRINT_WARN, 'serial_setup1');
  110. // } else {
  111. // this.setWarningText(null, 'serial_setup1');
  112. // }
  113. // },
  114. /**
  115. * Updates the content of the the serial related fields.
  116. * @this Blockly.Block
  117. */
  118. updateFields: function() {
  119. Blockly.Python.Boards.refreshBlockFieldDropdown(
  120. this, 'SERIAL_ID', 'serial');
  121. }
  122. };
  123. /**
  124. * Block for UART Communication
  125. * serial write
  126. */
  127. Blockly.Blocks['serial1_write'] = {
  128. /**
  129. * Block for creating a write to serial1 com function.
  130. * @this Blockly.Block
  131. */
  132. init: function() {
  133. this.setHelpUrl('http://www.arduino.cc/en/Serial/Write');
  134. this.setColour(Blockly.Blocks.serial.HUE);
  135. this.appendDummyInput()
  136. .appendField(new Blockly.FieldDropdown(
  137. [
  138. ["serial1", "Serial1"]
  139. ]), 'SERIAL_ID')
  140. .appendField(Blockly.Msg.ARD_SERIAL_WRITE);
  141. this.appendValueInput('CONTENT')
  142. .setCheck(null);
  143. // this.appendDummyInput()
  144. // .appendField(new Blockly.FieldCheckbox('TRUE'), 'NEW_LINE')
  145. // .appendField(Blockly.Msg.ARD_SERIAL_PRINT_NEWLINE);
  146. this.setInputsInline(true);
  147. this.setPreviousStatement(true, null);
  148. this.setNextStatement(true, null);
  149. this.setTooltip(Blockly.Msg.ARD_SERIAL_PRINT_TIP);
  150. },
  151. }
  152. /**
  153. * Block for UART Communication
  154. * serial read
  155. */
  156. Blockly.Blocks['serial1_read'] = {
  157. /**
  158. * Block for creating a write to serial1 com function.
  159. * @this Blockly.Block
  160. */
  161. init: function() {
  162. this.setHelpUrl('http://www.arduino.cc/en/Serial/Read');
  163. this.setColour(Blockly.Blocks.serial.HUE);
  164. this.appendDummyInput()
  165. .appendField(new Blockly.FieldDropdown(
  166. [
  167. ["serial1", "Serial1"]
  168. ]), 'SERIAL_ID')
  169. .appendField(Blockly.Msg.ARD_SERIAL_READ);
  170. // this.appendDummyInput()
  171. // .appendField(new Blockly.FieldCheckbox('TRUE'), 'NEW_LINE')
  172. // .appendField(Blockly.Msg.ARD_SERIAL_PRINT_NEWLINE);
  173. this.setInputsInline(true);
  174. this.setPreviousStatement(true, null);
  175. this.setNextStatement(true, null);
  176. this.setTooltip(Blockly.Msg.ARD_SERIAL_PRINT_TIP);
  177. },
  178. }
  179. /**
  180. * Block for UART Communication
  181. * serial read
  182. */
  183. Blockly.Blocks['serial1_available'] = {
  184. /**
  185. * Block for creating a write to serial1 com function.
  186. * @this Blockly.Block
  187. */
  188. init: function() {
  189. this.setColour(Blockly.Blocks.serial.HUE);
  190. this.appendDummyInput()
  191. .appendField(new Blockly.FieldDropdown(
  192. [
  193. ["serial1", "Serial1"]
  194. ]), 'SERIAL_ID')
  195. .appendField(Blockly.Msg.ARD_SERIAL_AVAILABLE);
  196. this.setOutput(true);
  197. this.setTooltip(Blockly.Msg.ARD_SERIAL_PRINT_TIP);
  198. },
  199. }