env.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 Ardublockly JavaScript for the Blockly resources and bindings.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Blocks.env');
  10. goog.require('Blockly.Blocks');
  11. Blockly.Blocks.env.HUE = 200;
  12. var colorENV = 200;
  13. Blockly.Blocks['env_init'] = {
  14. init: function() {
  15. this.appendDummyInput()
  16. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/dev/img/blocks/cocomod_blockly_envSensor.png", 140, 40, "15"));
  17. .appendField(new Blockly.FieldImage("./../blockly/media/main-env.png", 50, 40, "15"));
  18. this.appendDummyInput()
  19. .appendField(Blockly.Msg.ENV_SETUP);
  20. // this.appendDummyInput()
  21. // .appendField(Blockly.Msg.ENV_RED_PIN)
  22. // .appendField(new Blockly.FieldNumber(9),"env_red_pin")
  23. // .appendField(Blockly.Msg.ENV_GREEN_PIN)
  24. // .appendField(new Blockly.FieldNumber(10),"env_green_pin")
  25. // .appendField(Blockly.Msg.ENV_BLUE_PIN)
  26. // .appendField(new Blockly.FieldNumber(11),"env_blue_pin")
  27. this.setColour(Blockly.Blocks.env.HUE);
  28. this.setTooltip('');
  29. this.setHelpUrl('');
  30. }
  31. };
  32. // Blockly.Blocks['iot_temp_unit'] = {
  33. // init: function() {
  34. // this.appendDummyInput()
  35. // .appendField("Set temperature unit as ")
  36. // .appendField(new Blockly.FieldDropdown([["Fahrenheit","f"], ["Celsius","c"]]), "temp_unit");
  37. // this.setPreviousStatement(true, null);
  38. // this.setNextStatement(true, null);
  39. // this.setColour(230);
  40. // this.setTooltip("");
  41. // this.setHelpUrl("");
  42. // }
  43. // };
  44. Blockly.Blocks['iot_read_temp'] = {
  45. init: function() {
  46. this.appendDummyInput()
  47. .appendField(Blockly.Msg.ENV_SENSOR_READTEMP)
  48. //.appendField(new Blockly.FieldTextInput("4"),"env_sensor_temp");
  49. .appendField(new Blockly.FieldDropdown(
  50. Blockly.Python.Boards.selected.digitalPins), "env_sensor_temp");
  51. this.appendDummyInput()
  52. .appendField(Blockly.Msg.ENV_SENSOR_TEMPUNIT)
  53. .appendField(new Blockly.FieldDropdown([
  54. [Blockly.Msg.ENV_SENSOR_UNITC, "c"],
  55. [Blockly.Msg.ENV_SENSOR_UNITF, "f"]
  56. ]), "temp_unit");
  57. this.setOutput(true, "Number");
  58. this.setColour(Blockly.Blocks.env.HUE);
  59. this.setTooltip("");
  60. this.setHelpUrl("");
  61. },
  62. getBlockType: function() {
  63. return Blockly.Types.identifyNumber('1.1');
  64. }
  65. };
  66. // Blockly.Blocks['iot_buzzer_alarm'] = {
  67. // init: function() {
  68. // this.appendDummyInput()
  69. // .appendField("Buzzer Alarm -")
  70. // .appendField(new Blockly.FieldNumber(1000, 0, Infinity, 1), "alarm_time")
  71. // .appendField("millisecond");
  72. // this.setPreviousStatement(true, null);
  73. // this.setNextStatement(true, null);
  74. // this.setColour(135);
  75. // this.setTooltip("");
  76. // this.setHelpUrl("");
  77. // }
  78. // };
  79. Blockly.Blocks['iot_read_val'] = {
  80. init: function() {
  81. this.appendDummyInput()
  82. .appendField(Blockly.Msg.ENV_SENSOR_READ)
  83. .appendField(new Blockly.FieldDropdown([
  84. [Blockly.Msg.ENV_SENSOR_SOUND, "mic"],
  85. [Blockly.Msg.ENV_SENSOR_LIGHT, "light"],
  86. [Blockly.Msg.ENV_SENSOR_HUMIDITY, "humid"]
  87. ]), "val_type");
  88. this.setOutput(true, "Number");
  89. this.setColour(Blockly.Blocks.env.HUE);
  90. this.setTooltip("");
  91. this.setHelpUrl("");
  92. },
  93. getBlockType: function() {
  94. var numString;
  95. if (this.getFieldValue('val_type') === "humid") {
  96. numString = '1.1';
  97. } else {
  98. numString = '1';
  99. }
  100. return Blockly.Types.identifyNumber(numString);
  101. }
  102. };
  103. Blockly.Blocks['iot_read_mic'] = {
  104. init: function() {
  105. this.appendDummyInput()
  106. .appendField(Blockly.Msg.ENV_SENSOR_MIC)
  107. //.appendField(new Blockly.FieldTextInput("A2"),"env_sensor_mic");
  108. .appendField(new Blockly.FieldDropdown(
  109. Blockly.Python.Boards.selected.analogPins), 'env_sensor_mic');
  110. this.setOutput(true, "Number");
  111. this.setColour(Blockly.Blocks.env.HUE);
  112. this.setTooltip("");
  113. this.setHelpUrl("");
  114. },
  115. updateFields: function() {
  116. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_mic', 'pwmPins');
  117. },
  118. getBlockType: function() {
  119. var numString = '1';
  120. return Blockly.Types.identifyNumber(numString);
  121. }
  122. };
  123. Blockly.Blocks['iot_read_light'] = {
  124. init: function() {
  125. this.appendDummyInput()
  126. .appendField(Blockly.Msg.ENV_SENSOR_LIGHT)
  127. // .appendField(new Blockly.FieldTextInput("A1"),"env_sensor_light");
  128. .appendField(new Blockly.FieldDropdown(
  129. Blockly.Python.Boards.selected.analogPins), "env_sensor_light");
  130. this.setOutput(true, "Number");
  131. this.setColour(Blockly.Blocks.env.HUE);
  132. this.setTooltip("");
  133. this.setHelpUrl("");
  134. },
  135. updateFields: function() {
  136. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_light', 'pwmPins');
  137. },
  138. getBlockType: function() {
  139. var numString = '1';
  140. return Blockly.Types.identifyNumber(numString);
  141. }
  142. };
  143. Blockly.Blocks['iot_read_humid'] = {
  144. init: function() {
  145. this.appendDummyInput()
  146. .appendField(Blockly.Msg.ENV_SENSOR_HUMID)
  147. //.appendField(new Blockly.FieldTextInput("4"),"env_sensor_humid");
  148. .appendField(new Blockly.FieldDropdown(
  149. Blockly.Python.Boards.selected.digitalPins), "env_sensor_humid");
  150. this.setOutput(true, "Number");
  151. this.setColour(Blockly.Blocks.env.HUE);
  152. this.setTooltip("");
  153. this.setHelpUrl("");
  154. },
  155. updateFields: function() {
  156. Blockly.Python.Boards.refreshBlockFieldDropdown(this, 'env_sensor_humid', 'pwmPins');
  157. },
  158. getBlockType: function() {
  159. var numString = '1.1';
  160. return Blockly.Types.identifyNumber(numString);
  161. }
  162. };
  163. // Blockly.Blocks['iot_threshold_controller'] = {
  164. // init: function() {
  165. // this.appendDummyInput()
  166. // .appendField("When the critical value of ")
  167. // .appendField(new Blockly.FieldDropdown([["UV","uv"], ["Light","light"], ["Temperature","temp"], ["Humidity","humid"]]), "threshold_types")
  168. // .appendField("reaches")
  169. // .appendField(new Blockly.FieldTextInput("60"), "threshold_val");
  170. // this.appendStatementInput("Do")
  171. // .setCheck(null)
  172. // .appendField("Do");
  173. // this.setPreviousStatement(true, null);
  174. // this.setNextStatement(true, null);
  175. // this.setColour(135);
  176. // this.setTooltip("");
  177. // this.setHelpUrl("");
  178. // }
  179. // };
  180. Blockly.Blocks['iot_buzzer'] = {
  181. init: function() {
  182. this.interpolate_(Blockly.Msg.BASIC_SETTONE, [{
  183. "type": "field_dropdown",
  184. "name": "PINSNUM",
  185. "options": [
  186. ["7", "7"]
  187. ]
  188. },
  189. {
  190. "type": "input_value",
  191. "name": "FREQUENCY",
  192. "check": Blockly.Types.NUMBER.checkList
  193. }
  194. ]);
  195. this.setInputsInline(true);
  196. this.setPreviousStatement(true);
  197. this.setNextStatement(true);
  198. this.setColour(Blockly.Blocks.env.HUE);
  199. this.setTooltip(Blockly.Msg.BASIC_TONE_TIP);
  200. this.setHelpUrl('https://www.arduino.cc/en/Reference/tone');
  201. },
  202. onchange: function() {
  203. var freq = Blockly.Python.valueToCode(this, "FREQUENCY", Blockly.Python.ORDER_ATOMIC)
  204. if (freq < 31 || freq > 65535) {
  205. this.setWarningText(Blockly.Msg.BASIC_TONE_WARNING, 'io_tone');
  206. } else {
  207. this.setWarningText(null, 'io_tone');
  208. }
  209. }
  210. };
  211. Blockly.Blocks['iot_nobuzzer'] = {
  212. init: function() {
  213. this.interpolate_(Blockly.Msg.BASIC_NOTONE, [{
  214. "type": "field_dropdown",
  215. "name": "PINSNUM",
  216. "options": [
  217. ["7", "7"]
  218. ]
  219. }]);
  220. this.setInputsInline(true);
  221. this.setPreviousStatement(true);
  222. this.setNextStatement(true);
  223. this.setColour(Blockly.Blocks.env.HUE);
  224. this.setTooltip(Blockly.Msg.BASIC_NOTONE_TIP);
  225. this.setHelpUrl('https://www.arduino.cc/en/Reference/noTone');
  226. }
  227. };
  228. // Blockly.Blocks['iot_setup_rgb'] = {
  229. // init: function() {
  230. // this.appendValueInput('RED')
  231. // .setCheck(Blockly.Types.NUMBER.checkList)
  232. // .setAlign(Blockly.ALIGN_RIGHT)
  233. // .appendField(Blockly.Msg.COLOUR_RGB_TITLE)
  234. // .appendField(Blockly.Msg.COLOUR_RGB_RED);
  235. // this.appendValueInput('GREEN')
  236. // .setCheck(Blockly.Types.NUMBER.checkList)
  237. // .setAlign(Blockly.ALIGN_RIGHT)
  238. // .appendField(Blockly.Msg.COLOUR_RGB_GREEN);
  239. // this.appendValueInput('BLUE')
  240. // .setCheck(Blockly.Types.NUMBER.checkList)
  241. // .setAlign(Blockly.ALIGN_RIGHT)
  242. // .appendField(Blockly.Msg.COLOUR_RGB_BLUE);
  243. // this.setOutput(true, 'Colour');
  244. // this.setTooltip(Blockly.Msg.COLOUR_RGB_TOOLTIP);
  245. // this.setHelpUrl(Blockly.Msg.COLOUR_RGB_HELPURL);
  246. // this.setColour(135);
  247. // }
  248. // };
  249. Blockly.Blocks['iot_set_rgb'] = {
  250. init: function() {
  251. this.appendDummyInput()
  252. .appendField(Blockly.Msg.ENV_SENSOR_SET_RGB);
  253. this.appendValueInput("RED_VAL")
  254. .setCheck(null)
  255. .appendField(Blockly.Msg.ENV_SETRGB_R)
  256. .appendField(new Blockly.FieldDropdown(
  257. Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_r');
  258. this.appendValueInput("GREEN_VAL")
  259. .setCheck(null)
  260. .appendField(Blockly.Msg.ENV_SETRGB_G)
  261. .appendField(new Blockly.FieldDropdown(
  262. Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_g');
  263. this.appendValueInput("BLUE_VAL")
  264. .setCheck(null)
  265. .appendField(Blockly.Msg.ENV_SETRGB_B)
  266. .appendField(new Blockly.FieldDropdown(
  267. Blockly.Python.Boards.selected.digitalPins), 'env_sensor_rgbPin_b');
  268. this.setInputsInline(false);
  269. this.setPreviousStatement(true);
  270. this.setNextStatement(true);
  271. this.setColour(Blockly.Blocks.env.HUE);
  272. this.setTooltip("");
  273. this.setHelpUrl("");
  274. }
  275. };