123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /**
- * @license Licensed under the Apache License, Version 2.0 (the "License"):
- * http://www.apache.org/licenses/LICENSE-2.0
- */
- /**
- * @fileoverview Code generator for the test 2 blocks.
- */
- 'use strict';
- goog.provide('Blockly.Python.env');
- goog.require('Blockly.Python');
- Blockly.Python['env_init'] = function(block) {
- Blockly.Python.addDeclaration('env_init_', '#include "DHT.h"\n #define DHTTYPE DHT11\n');
- Blockly.Python.addSetup('env_init_2', 'Serial.println("DHTxx test!"); \n');
- return "";
- };
- // Blockly.Python['iot_buzzer_alarm'] = function(block) {
- // var number_alarm_time = block.getFieldValue('alarm_time');
- // Blockly.Python.addDeclaration('buzzer_alarm_decl', "#define PIN_BUZZER 7\n");
- // Blockly.Python.addSetup('buzzer_alarm_setup','pinMode(PIN_BUZZER, OUTPUT);\n');
- // var code = 'digitalWrite(PIN_BUZZER, HIGH);\ndelay('+number_alarm_time+');\n';
- // return code;
- // };
- // var valuesMap = new Map();
- Blockly.Python['iot_read_val'] = function(block) {
- var dropdown_val_type = block.getFieldValue('val_type');
- var code;
- if (dropdown_val_type == 'mic') {
- var decl = "#define PIN_MIC A2\n";
- Blockly.Python.addDeclaration('iot_mic_decl', decl);
- Blockly.Python.addSetup('iot_mic_setup', 'pinMode(PIN_MIC, INPUT);');
- code = 'analogRead(PIN_MIC)';
- } else if (dropdown_val_type == 'light') {
- var decl = "#define PIN_LIGHT A1\n";
- Blockly.Python.addDeclaration('iot_light_decl', decl);
- Blockly.Python.addSetup('iot_light_setup', 'pinMode(PIN_LIGHT, INPUT);');
- code = 'analogRead(PIN_LIGHT)';
- } else { //humidity
- code = 'dht.readHumidity()';
- }
- // valuesMap.set(dropdown_val_type, text_val_name);
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['iot_read_mic'] = function(block) {
- var code;
- var pin_mic = block.getFieldValue('env_sensor_mic');
- var decl = "#define PIN_MIC " + pin_mic;
- Blockly.Python.addDeclaration('iot_mic_decl', decl);
- Blockly.Python.addSetup('iot_mic_setup', 'pinMode(PIN_MIC, INPUT);');
- code = 'analogRead(PIN_MIC)';
- // valuesMap.set(dropdown_val_type, text_val_name);
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['iot_read_light'] = function(block) {
- var code;
- var pin_light = block.getFieldValue('env_sensor_light');
- var decl = "#define PIN_LIGHT " + pin_light;
- Blockly.Python.addDeclaration('iot_light_decl', decl);
- Blockly.Python.addSetup('iot_light_setup', 'pinMode(PIN_LIGHT, INPUT);');
- code = 'analogRead(PIN_LIGHT)';
- // valuesMap.set(dropdown_val_type, text_val_name);
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['iot_read_humid'] = function(block) {
- var pin_humid = block.getFieldValue('env_sensor_humid');
- var humid_decl = '#define DHTPIN ' + pin_humid + '\n' + 'DHT dht(DHTPIN, DHTTYPE);\n';
- Blockly.Python.addDeclaration('humid_temp_init', humid_decl);
- var humid_setup = 'dht.begin();\n';
- Blockly.Python.addSetup('humid_temp_setup', humid_setup);
- var code = 'dht.readHumidity()';
- // valuesMap.set(dropdown_val_type, text_val_name);
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- Blockly.Python['iot_read_temp'] = function(block) {
- var pin_temp = block.getFieldValue('env_sensor_temp');
- var temp_decl = '#define DHTPIN ' + pin_temp + '\n' + 'DHT dht(DHTPIN, DHTTYPE);\n';
- Blockly.Python.addDeclaration('humid_temp_init', temp_decl);
- var temp_setup = 'dht.begin();\n';
- Blockly.Python.addSetup('humid_temp_setup', temp_setup);
- var dropdown_temp_unit = block.getFieldValue('temp_unit');
- var code = 'dht.readTemperature(';
- if (dropdown_temp_unit == 'c') {
- code += ')';
- } else { //f
- code += 'true)';
- }
- // valuesMap.set('temp', text_temp_name);
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- // Blockly.Python['iot_threshold_controller'] = function(block) {
- // var dropdown_threshold_types = block.getFieldValue('threshold_types');
- // var text_threshold_val = block.getFieldValue('threshold_val');
- // var statements_do = Blockly.Python.statementToCode(block, 'Do');
- // var variable;
- // if (valuesMap.has(dropdown_threshold_types)) {
- // variable = valuesMap.get(dropdown_threshold_types);
- // }
- // var code = 'if ('+variable+' >= '+text_threshold_val+') {\n' +
- // statements_do +
- // '}\n';
- // return code;
- // };
- Blockly.Python['iot_buzzer'] = function(block) {
- var pin = block.getFieldValue('PINSNUM');
- var freq = Blockly.Python.valueToCode(block, 'FREQUENCY', Blockly.Python.ORDER_ATOMIC);
- var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);\n';
- Blockly.Python.addSetup('iot_buzzer_' + pin, pinSetupCode, false);
- var code = 'tone(' + pin + ',' + freq + ');\n';
- return code;
- };
- Blockly.Python['iot_nobuzzer'] = function(block) {
- //var pin = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
- var pin = block.getFieldValue('PINSNUM');
- var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);\n';
- Blockly.Python.addSetup('iot_buzzer_' + pin, pinSetupCode, false);
- var code = 'noTone(' + pin + ');\n';
- return code;
- };
- Blockly.Python['iot_set_rgb'] = function(block) {
- var redPin = block.getFieldValue('env_sensor_rgbPin_r');
- var greenPin = block.getFieldValue('env_sensor_rgbPin_g');
- var bluePin = block.getFieldValue('env_sensor_rgbPin_b');
- var pin_rgb = "int redPin = " + redPin + ";\n int greenPin = " + greenPin + ";\n int bluePin = " + bluePin + ";";
- Blockly.Python.addDeclaration('pin_rgb', pin_rgb);
- var setup_rgb = "pinMode(redPin, OUTPUT);\n" +
- "pinMode(greenPin, OUTPUT);\n " +
- "pinMode(bluePin, OUTPUT);\n" +
- "setColor(0,0,0);\n";
- Blockly.Python.addSetup('setup_rgb', setup_rgb);
- var setcolor_rgb = 'void setColor(int red, int green, int blue)\n' +
- '{\n' +
- ' red = 255 - red;\n' +
- ' green = 255 - green;\n' +
- ' blue = 255 - blue;\n' +
- ' analogWrite(redPin, red);\n' +
- ' analogWrite(greenPin, green);\n' +
- ' analogWrite(bluePin, blue);\n' +
- '}\n';
- Blockly.Python.addFunction('setcolor_rgb', setcolor_rgb);
- var r = Blockly.Python.valueToCode(block, 'RED_VAL', Blockly.Python.ORDER_ATOMIC);
- var g = Blockly.Python.valueToCode(block, 'GREEN_VAL', Blockly.Python.ORDER_ATOMIC);
- var b = Blockly.Python.valueToCode(block, 'BLUE_VAL', Blockly.Python.ORDER_ATOMIC);
- var code = 'setColor(' + r + ',' + g + ',' + b + ');';
- return code;
- }
|