env.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Code generator for the test 2 blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.env');
  10. goog.require('Blockly.Python');
  11. Blockly.Python['env_init'] = function(block) {
  12. Blockly.Python.addDeclaration('env_init_', '#include "DHT.h"\n #define DHTTYPE DHT11\n');
  13. Blockly.Python.addSetup('env_init_2', 'Serial.println("DHTxx test!"); \n');
  14. return "";
  15. };
  16. // Blockly.Python['iot_buzzer_alarm'] = function(block) {
  17. // var number_alarm_time = block.getFieldValue('alarm_time');
  18. // Blockly.Python.addDeclaration('buzzer_alarm_decl', "#define PIN_BUZZER 7\n");
  19. // Blockly.Python.addSetup('buzzer_alarm_setup','pinMode(PIN_BUZZER, OUTPUT);\n');
  20. // var code = 'digitalWrite(PIN_BUZZER, HIGH);\ndelay('+number_alarm_time+');\n';
  21. // return code;
  22. // };
  23. // var valuesMap = new Map();
  24. Blockly.Python['iot_read_val'] = function(block) {
  25. var dropdown_val_type = block.getFieldValue('val_type');
  26. var code;
  27. if (dropdown_val_type == 'mic') {
  28. var decl = "#define PIN_MIC A2\n";
  29. Blockly.Python.addDeclaration('iot_mic_decl', decl);
  30. Blockly.Python.addSetup('iot_mic_setup', 'pinMode(PIN_MIC, INPUT);');
  31. code = 'analogRead(PIN_MIC)';
  32. } else if (dropdown_val_type == 'light') {
  33. var decl = "#define PIN_LIGHT A1\n";
  34. Blockly.Python.addDeclaration('iot_light_decl', decl);
  35. Blockly.Python.addSetup('iot_light_setup', 'pinMode(PIN_LIGHT, INPUT);');
  36. code = 'analogRead(PIN_LIGHT)';
  37. } else { //humidity
  38. code = 'dht.readHumidity()';
  39. }
  40. // valuesMap.set(dropdown_val_type, text_val_name);
  41. return [code, Blockly.Python.ORDER_ATOMIC];
  42. };
  43. Blockly.Python['iot_read_mic'] = function(block) {
  44. var code;
  45. var pin_mic = block.getFieldValue('env_sensor_mic');
  46. var decl = "#define PIN_MIC " + pin_mic;
  47. Blockly.Python.addDeclaration('iot_mic_decl', decl);
  48. Blockly.Python.addSetup('iot_mic_setup', 'pinMode(PIN_MIC, INPUT);');
  49. code = 'analogRead(PIN_MIC)';
  50. // valuesMap.set(dropdown_val_type, text_val_name);
  51. return [code, Blockly.Python.ORDER_ATOMIC];
  52. };
  53. Blockly.Python['iot_read_light'] = function(block) {
  54. var code;
  55. var pin_light = block.getFieldValue('env_sensor_light');
  56. var decl = "#define PIN_LIGHT " + pin_light;
  57. Blockly.Python.addDeclaration('iot_light_decl', decl);
  58. Blockly.Python.addSetup('iot_light_setup', 'pinMode(PIN_LIGHT, INPUT);');
  59. code = 'analogRead(PIN_LIGHT)';
  60. // valuesMap.set(dropdown_val_type, text_val_name);
  61. return [code, Blockly.Python.ORDER_ATOMIC];
  62. };
  63. Blockly.Python['iot_read_humid'] = function(block) {
  64. var pin_humid = block.getFieldValue('env_sensor_humid');
  65. var humid_decl = '#define DHTPIN ' + pin_humid + '\n' + 'DHT dht(DHTPIN, DHTTYPE);\n';
  66. Blockly.Python.addDeclaration('humid_temp_init', humid_decl);
  67. var humid_setup = 'dht.begin();\n';
  68. Blockly.Python.addSetup('humid_temp_setup', humid_setup);
  69. var code = 'dht.readHumidity()';
  70. // valuesMap.set(dropdown_val_type, text_val_name);
  71. return [code, Blockly.Python.ORDER_ATOMIC];
  72. };
  73. Blockly.Python['iot_read_temp'] = function(block) {
  74. var pin_temp = block.getFieldValue('env_sensor_temp');
  75. var temp_decl = '#define DHTPIN ' + pin_temp + '\n' + 'DHT dht(DHTPIN, DHTTYPE);\n';
  76. Blockly.Python.addDeclaration('humid_temp_init', temp_decl);
  77. var temp_setup = 'dht.begin();\n';
  78. Blockly.Python.addSetup('humid_temp_setup', temp_setup);
  79. var dropdown_temp_unit = block.getFieldValue('temp_unit');
  80. var code = 'dht.readTemperature(';
  81. if (dropdown_temp_unit == 'c') {
  82. code += ')';
  83. } else { //f
  84. code += 'true)';
  85. }
  86. // valuesMap.set('temp', text_temp_name);
  87. return [code, Blockly.Python.ORDER_ATOMIC];
  88. };
  89. // Blockly.Python['iot_threshold_controller'] = function(block) {
  90. // var dropdown_threshold_types = block.getFieldValue('threshold_types');
  91. // var text_threshold_val = block.getFieldValue('threshold_val');
  92. // var statements_do = Blockly.Python.statementToCode(block, 'Do');
  93. // var variable;
  94. // if (valuesMap.has(dropdown_threshold_types)) {
  95. // variable = valuesMap.get(dropdown_threshold_types);
  96. // }
  97. // var code = 'if ('+variable+' >= '+text_threshold_val+') {\n' +
  98. // statements_do +
  99. // '}\n';
  100. // return code;
  101. // };
  102. Blockly.Python['iot_buzzer'] = function(block) {
  103. var pin = block.getFieldValue('PINSNUM');
  104. var freq = Blockly.Python.valueToCode(block, 'FREQUENCY', Blockly.Python.ORDER_ATOMIC);
  105. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);\n';
  106. Blockly.Python.addSetup('iot_buzzer_' + pin, pinSetupCode, false);
  107. var code = 'tone(' + pin + ',' + freq + ');\n';
  108. return code;
  109. };
  110. Blockly.Python['iot_nobuzzer'] = function(block) {
  111. //var pin = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  112. var pin = block.getFieldValue('PINSNUM');
  113. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);\n';
  114. Blockly.Python.addSetup('iot_buzzer_' + pin, pinSetupCode, false);
  115. var code = 'noTone(' + pin + ');\n';
  116. return code;
  117. };
  118. Blockly.Python['iot_set_rgb'] = function(block) {
  119. var redPin = block.getFieldValue('env_sensor_rgbPin_r');
  120. var greenPin = block.getFieldValue('env_sensor_rgbPin_g');
  121. var bluePin = block.getFieldValue('env_sensor_rgbPin_b');
  122. var pin_rgb = "int redPin = " + redPin + ";\n int greenPin = " + greenPin + ";\n int bluePin = " + bluePin + ";";
  123. Blockly.Python.addDeclaration('pin_rgb', pin_rgb);
  124. var setup_rgb = "pinMode(redPin, OUTPUT);\n" +
  125. "pinMode(greenPin, OUTPUT);\n " +
  126. "pinMode(bluePin, OUTPUT);\n" +
  127. "setColor(0,0,0);\n";
  128. Blockly.Python.addSetup('setup_rgb', setup_rgb);
  129. var setcolor_rgb = 'void setColor(int red, int green, int blue)\n' +
  130. '{\n' +
  131. ' red = 255 - red;\n' +
  132. ' green = 255 - green;\n' +
  133. ' blue = 255 - blue;\n' +
  134. ' analogWrite(redPin, red);\n' +
  135. ' analogWrite(greenPin, green);\n' +
  136. ' analogWrite(bluePin, blue);\n' +
  137. '}\n';
  138. Blockly.Python.addFunction('setcolor_rgb', setcolor_rgb);
  139. var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
  140. var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
  141. var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
  142. var code = 'setColor(' + r + ',' + g + ',' + b + ');';
  143. return code;
  144. }