io.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 Arduino Digital and Analogue input and output
  7. * functions. The Arduino function syntax can be found at
  8. * http://arduino.cc/en/Reference/HomePage
  9. *
  10. * TODO: maybe change this to a "PIN" BlocklyType
  11. */
  12. 'use strict';
  13. goog.provide('Blockly.Blocks.io');
  14. goog.require('Blockly.Blocks');
  15. goog.require('Blockly.Types');
  16. /** Common HSV hue for all blocks in this category. */
  17. Blockly.Blocks.io.HUE = 20;
  18. Blockly.Blocks['io_digitalwrite'] = {
  19. /**
  20. * Block for creating a 'set pin' to a state.
  21. * @this Blockly.Block
  22. */
  23. init: function() {
  24. this.setHelpUrl('http://arduino.cc/en/Reference/DigitalWrite');
  25. this.setColour(Blockly.Blocks.io.HUE);
  26. this.appendValueInput('PIN')
  27. .setCheck(Blockly.Types.NUMBER.output)
  28. .appendField(Blockly.Msg.ARD_DIGITALWRITE);
  29. this.appendValueInput('STATE')
  30. //.appendField(Blockly.Msg.ARD_DIGITALWRITE)
  31. //.appendField(new Blockly.FieldTextInput("1"), "PIN")
  32. // .appendField(new Blockly.FieldDropdown(
  33. // Blockly.Python.Boards.selected.digitalPins), 'PIN')
  34. .appendField(Blockly.Msg.ARD_WRITE_TO)
  35. .setCheck(Blockly.Types.BOOLEAN.checkList);
  36. this.setInputsInline(true);
  37. this.setPreviousStatement(true, null);
  38. this.setNextStatement(true, null);
  39. this.setTooltip(Blockly.Msg.ARD_DIGITALWRITE_TIP);
  40. },
  41. /**
  42. * Updates the content of the the pin related fields.
  43. * @this Blockly.Block
  44. */
  45. updateFields: function() {
  46. Blockly.Python.Boards.refreshBlockFieldDropdown(
  47. this, 'PIN', 'digitalPins');
  48. }
  49. };
  50. Blockly.Blocks['io_digitalread'] = {
  51. /**
  52. * Block for creating a 'read pin'.
  53. * @this Blockly.Block
  54. */
  55. init: function() {
  56. this.setHelpUrl('http://arduino.cc/en/Reference/DigitalRead');
  57. this.setColour(Blockly.Blocks.io.HUE);
  58. // this.appendDummyInput()
  59. this.appendValueInput('PIN')
  60. .setCheck(Blockly.Types.NUMBER.output)
  61. .appendField(Blockly.Msg.ARD_DIGITALREAD);
  62. // .appendField(new Blockly.FieldDropdown(
  63. // Blockly.Python.Boards.selected.digitalPins), 'PIN');
  64. // .appendField(new Blockly.FieldTextInput("1"), "PIN");
  65. this.setInputsInline(true);
  66. this.setOutput(true, Blockly.Types.BOOLEAN.output);
  67. this.setTooltip(Blockly.Msg.ARD_DIGITALREAD_TIP);
  68. },
  69. /** @return {!string} The type of return value for the block, an integer. */
  70. getBlockType: function() {
  71. return Blockly.Types.BOOLEAN;
  72. },
  73. /**
  74. * Updates the content of the the pin related fields.
  75. * @this Blockly.Block
  76. */
  77. updateFields: function() {
  78. Blockly.Python.Boards.refreshBlockFieldDropdown(
  79. this, 'PIN', 'digitalPins');
  80. }
  81. };
  82. Blockly.Blocks['io_builtin_led'] = {
  83. /**
  84. * Block for setting built-in LED to a state.
  85. * @this Blockly.Block
  86. */
  87. init: function() {
  88. this.setHelpUrl('http://arduino.cc/en/Reference/DigitalWrite');
  89. this.setColour(Blockly.Blocks.io.HUE);
  90. this.appendValueInput('STATE')
  91. .appendField(Blockly.Msg.ARD_BUILTIN_LED)
  92. .appendField(new Blockly.FieldDropdown(
  93. Blockly.Python.Boards.selected.builtinLed), 'BUILT_IN_LED')
  94. .appendField('to')
  95. .setCheck(Blockly.Types.BOOLEAN.checkList);
  96. this.setInputsInline(false);
  97. this.setPreviousStatement(true, null);
  98. this.setNextStatement(true, null);
  99. this.setTooltip(Blockly.Msg.ARD_BUILTIN_LED_TIP);
  100. },
  101. /**
  102. * Updates the content of the the pin related fields.
  103. * @this Blockly.Block
  104. */
  105. updateFields: function() {
  106. Blockly.Python.Boards.refreshBlockFieldDropdown(
  107. this, 'BUILT_IN_LED', 'builtinLed');
  108. },
  109. /** @return {!string} The type of input value for the block, an integer. */
  110. getBlockType: function() {
  111. return Blockly.Types.BOOLEAN;
  112. },
  113. };
  114. Blockly.Blocks['io_analogwrite'] = {
  115. /**
  116. * Block for creating a 'set pin' to an analogue value.
  117. * @this Blockly.Block
  118. */
  119. init: function() {
  120. this.setHelpUrl('http://arduino.cc/en/Reference/AnalogWrite');
  121. this.setColour(Blockly.Blocks.io.HUE);
  122. this.appendValueInput('PIN')
  123. .setCheck([Blockly.Types.NUMBER.output, Blockly.Types.TEXT.output])
  124. .appendField(Blockly.Msg.ARD_ANALOGWRITE);
  125. this.appendValueInput('ANALOGNUM')
  126. // .appendField(new Blockly.FieldDropdown(
  127. // Blockly.Python.Boards.selected.pwmPins), 'PIN')
  128. // .appendField(new Blockly.FieldNumber(1, 0, 13, 1), "PIN")
  129. .appendField(Blockly.Msg.ARD_WRITE_TO)
  130. .setCheck(Blockly.Types.NUMBER.output);
  131. this.setInputsInline(true);
  132. this.setPreviousStatement(true, null);
  133. this.setNextStatement(true, null);
  134. this.setTooltip(Blockly.Msg.ARD_ANALOGWRITE_TIP);
  135. },
  136. /**
  137. * Updates the content of the the pin related fields.
  138. * @this Blockly.Block
  139. */
  140. updateFields: function() {
  141. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'PIN', 'pwmPins');
  142. },
  143. /** @return {!string} The type of input value for the block, an integer. */
  144. getBlockType: function() {
  145. return Blockly.Types.NUMBER;
  146. },
  147. };
  148. Blockly.Blocks['io_analogread'] = {
  149. /**
  150. * Block for reading an analogue input.
  151. * @this Blockly.Block
  152. */
  153. init: function() {
  154. this.setHelpUrl('http://arduino.cc/en/Reference/AnalogRead');
  155. this.setColour(Blockly.Blocks.io.HUE);
  156. // this.appendDummyInput()
  157. this.appendValueInput('PIN')
  158. .setCheck(Blockly.Types.NUMBER.output)
  159. .appendField(Blockly.Msg.ARD_ANALOGREAD)
  160. // .appendField(new Blockly.FieldDropdown(
  161. // Blockly.Python.Boards.selected.analogPins), 'PIN');
  162. // .appendField(new Blockly.FieldTextInput("A0"), "PIN");
  163. this.setInputsInline(true);
  164. this.setOutput(true, Blockly.Types.NUMBER.output);
  165. this.setTooltip(Blockly.Msg.ARD_ANALOGREAD_TIP);
  166. },
  167. /** @return {!string} The type of return value for the block, an integer. */
  168. getBlockType: function() {
  169. return Blockly.Types.NUMBER;
  170. },
  171. /**
  172. * Updates the content of the the pin related fields.
  173. * @this Blockly.Block
  174. */
  175. updateFields: function() {
  176. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'PIN', 'analogPins');
  177. }
  178. };
  179. Blockly.Blocks['io_pwmwrite'] = {
  180. /**
  181. * Block for creating a 'set pin' to an analogue value.
  182. * @this Blockly.Block
  183. */
  184. init: function() {
  185. this.setHelpUrl('http://arduino.cc/en/Reference/AnalogWrite');
  186. this.setColour(Blockly.Blocks.io.HUE);
  187. this.appendValueInput('PIN')
  188. .setCheck([Blockly.Types.NUMBER.output, Blockly.Types.TEXT.output])
  189. .appendField(Blockly.Msg.ARD_PWMWRITE);
  190. this.appendValueInput('PWMVALUE')
  191. // .appendField(new Blockly.FieldDropdown(
  192. // Blockly.Python.Boards.selected.pwmPins), 'PIN')
  193. // .appendField(new Blockly.FieldNumber(1, 0, 13, 1), "PIN")
  194. .appendField(Blockly.Msg.ARD_WRITE_TO)
  195. .setCheck(Blockly.Types.NUMBER.output);
  196. this.setInputsInline(true);
  197. this.setPreviousStatement(true, null);
  198. this.setNextStatement(true, null);
  199. this.setTooltip(Blockly.Msg.ARD_ANALOGWRITE_TIP);
  200. },
  201. /**
  202. * Updates the content of the the pin related fields.
  203. * @this Blockly.Block
  204. */
  205. updateFields: function() {
  206. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'PIN', 'pwmPins');
  207. },
  208. /** @return {!string} The type of input value for the block, an integer. */
  209. getBlockType: function() {
  210. return Blockly.Types.NUMBER;
  211. },
  212. };
  213. Blockly.Blocks['io_highlow'] = {
  214. /**
  215. * Block for creating a pin state.
  216. * @this Blockly.Block
  217. */
  218. init: function() {
  219. this.setHelpUrl('http://arduino.cc/en/Reference/Constants');
  220. this.setColour(Blockly.Blocks.io.HUE);
  221. this.appendDummyInput()
  222. .appendField(
  223. new Blockly.FieldDropdown([
  224. [Blockly.Msg.ARD_HIGH, 'HIGH'],
  225. [Blockly.Msg.ARD_LOW, 'LOW']
  226. ]),
  227. 'STATE');
  228. this.setOutput(true, Blockly.Types.BOOLEAN.output);
  229. this.setTooltip(Blockly.Msg.ARD_HIGHLOW_TIP);
  230. },
  231. /** @return {!string} The type of return value for the block, an integer. */
  232. getBlockType: function() {
  233. return Blockly.Types.BOOLEAN;
  234. }
  235. };
  236. Blockly.Blocks['io_pulsein'] = {
  237. init: function() {
  238. this.appendDummyInput()
  239. .appendField(Blockly.Msg.ARD_PULSEREAD);
  240. this.appendValueInput("PULSETYPE")
  241. .setCheck(Blockly.Types.BOOLEAN.check);
  242. this.appendDummyInput()
  243. .appendField(Blockly.Msg.ARD_PULSEON)
  244. .appendField(new Blockly.FieldDropdown(
  245. Blockly.Python.Boards.selected.digitalPins), "PULSEPIN");
  246. this.setOutput(true);
  247. this.setInputsInline(true);
  248. this.setColour(Blockly.Blocks.io.HUE);
  249. this.setTooltip(Blockly.Msg.ARD_PULSE_TIP);
  250. this.setHelpUrl('https://www.arduino.cc/en/Reference/PulseIn');
  251. },
  252. /** @return {!string} The type of input value for the block, an integer. */
  253. getBlockType: function() {
  254. return Blockly.Types.NUMBER;
  255. }
  256. };
  257. Blockly.Blocks['io_pulsetimeout'] = {
  258. init: function() {
  259. this.appendDummyInput()
  260. .appendField(Blockly.Msg.ARD_PULSEREAD);
  261. this.appendValueInput("PULSETYPE")
  262. .setCheck(Blockly.Types.BOOLEAN.check);
  263. this.appendDummyInput()
  264. .appendField(Blockly.Msg.ARD_PULSEON)
  265. .appendField(new Blockly.FieldDropdown(
  266. Blockly.Python.Boards.selected.digitalPins), "PULSEPIN");
  267. this.appendDummyInput()
  268. .appendField(Blockly.Msg.ARD_PULSETIMEOUT);
  269. this.appendValueInput('TIMEOUT')
  270. .setCheck(Blockly.Types.NUMBER.output);
  271. this.appendDummyInput()
  272. .appendField(Blockly.Msg.ARD_PULSETIMEOUT_MS);
  273. this.setOutput(true);
  274. this.setInputsInline(true);
  275. this.setColour(Blockly.Blocks.io.HUE);
  276. this.setTooltip(Blockly.Msg.ARD_PULSETIMEOUT_TIP);
  277. this.setHelpUrl('https://www.arduino.cc/en/Reference/PulseIn');
  278. },
  279. /** @return {!string} The type of input value for the block, an integer. */
  280. getBlockType: function() {
  281. return Blockly.Types.NUMBER;
  282. }
  283. };
  284. Blockly.Blocks['io_dropdown_digital'] = {
  285. /**
  286. * Block for creating a digtial pin selector.
  287. * @this Blockly.Block
  288. */
  289. init: function() {
  290. this.appendDummyInput()
  291. .appendField(new Blockly.FieldDropdown(
  292. Blockly.Python.Boards.selected.digitalPins), 'SELECTPIN');
  293. this.setOutput(true, Blockly.Types.NUMBER.output);
  294. this.setColour(Blockly.Blocks.io.HUE);
  295. },
  296. /** @return {!string} The type of return value for the block, an integer. */
  297. getBlockType: function() {
  298. return Blockly.Types.NUMBER;
  299. }
  300. };
  301. Blockly.Blocks['io_dropdown_analog'] = {
  302. /**
  303. * Block for creating a analog pin selector.
  304. * @this Blockly.Block
  305. */
  306. init: function() {
  307. this.appendDummyInput()
  308. .appendField(new Blockly.FieldDropdown(
  309. Blockly.Python.Boards.selected.analogPins), 'SELECTPIN');
  310. this.setOutput(true, Blockly.Types.NUMBER.output);
  311. this.setColour(Blockly.Blocks.io.HUE);
  312. },
  313. /** @return {!string} The type of return value for the block, an integer. */
  314. getBlockType: function() {
  315. return Blockly.Types.NUMBER;
  316. }
  317. };
  318. Blockly.Blocks['io_dropdown_pwm'] = {
  319. /**
  320. * Block for creating a analog pin selector.
  321. * @this Blockly.Block
  322. */
  323. init: function() {
  324. this.appendDummyInput()
  325. .appendField(new Blockly.FieldDropdown(
  326. Blockly.Python.Boards.selected.pwmPins), 'SELECTPIN');
  327. this.setOutput(true, Blockly.Types.NUMBER.output);
  328. this.setColour(Blockly.Blocks.io.HUE);
  329. },
  330. /** @return {!string} The type of return value for the block, an integer. */
  331. getBlockType: function() {
  332. return Blockly.Types.NUMBER;
  333. }
  334. };