text: /**
   * Visual Blocks Language
   *
   * Copyright 2012 Fred Lin.
   * https://github.com/gasolin/BlocklyDuino
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /**
   * @fileoverview Helper functions for generating Arduino blocks.
   * @author gasolin@gmail.com (Fred Lin)
   */
  'use strict';
  
  goog.provide('Blockly.Arduino.arduino_base');
  
  goog.require('Blockly.Arduino');
  
  Blockly.Arduino['arduino_base_inout_buildin_led'] = function(block) {
    var dropdown_stat = block.getFieldValue('STAT');
    Blockly.Arduino.setups_['setup_output_13'] = 'pinMode(13, OUTPUT);';
    var code = 'digitalWrite(13, ' + dropdown_stat + ');\n'
    return code;
  };
  
  Blockly.Arduino['arduino_base_inout_digital_write'] = function(block) {
    var dropdown_pin = block.getFieldValue('PIN');
    var dropdown_stat = block.getFieldValue('STAT');
    Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ', ' + dropdown_stat + ');\n'
    return code;
  };
  
  Blockly.Arduino['arduino_base_inout_digital_read'] = function(block) {
    var dropdown_pin = block.getFieldValue('PIN');
    Blockly.Arduino.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'digitalRead(' + dropdown_pin + ')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  
  Blockly.Arduino['arduino_base_inout_highlow'] = function(block) {
    // Boolean values HIGH and LOW.
    var code = (block.getFieldValue('BOOL') == 'HIGH') ? 'HIGH' : 'LOW';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  
  Blockly.Arduino['arduino_base_inout_analog_write'] = function(block) {
    var dropdown_pin = block.getFieldValue('PIN');
    var value_num = Blockly.Arduino.valueToCode(this, 'PWM', Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.setups_['setup_output'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    var code = 'analogWrite(' + dropdown_pin + ', ' + value_num + ');\n';
    return code;
  };
  
  Blockly.Arduino['arduino_base_inout_analog_read'] = function(block) {
    var dropdown_pin = block.getFieldValue('PIN');
    //Blockly.Arduino.setups_['setup_input_'+dropdown_pin] = 'pinMode('+dropdown_pin+', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  };
  
  Blockly.Arduino['arduino_base_delay'] = function(block) {
    var delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '1000'
    var code = 'delay(' + delay_time + ');\n';
    return code;
  };
  
  Blockly.Arduino['arduino_base_angle'] = function(block) {
    var angle = this.getFieldValue('ANGLE');
    return [angle, Blockly.Arduino.ORDER_ATOMIC];
  };
  
  Blockly.Arduino['arduino_base_date'] = function(block) {
    var angle = this.getFieldValue('DATE');
    return [angle, Blockly.Arduino.ORDER_ATOMIC];
  };
  
  Blockly.Arduino['arduino_base_map'] = function(block) {
    var value_num = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_NONE);
    var value_dmax = Blockly.Arduino.valueToCode(this, 'DMAX', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'map(' + value_num + ', 0, 1024, 0, ' + value_dmax + ')';
    return [code, Blockly.Arduino.ORDER_NONE];
  };
  
  Blockly.Arduino['arduino_base_inout_tone'] = function(block) {
    var dropdown_pin = block.getFieldValue("PIN");
    var value_num = Blockly.Arduino.valueToCode(this, "NUM", Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.setups_['setup_output'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    var code = "tone(" + dropdown_pin + ", " + value_num + ");\n";
    return code;
  };
  
  Blockly.Arduino['arduino_base_inout_notone'] = function(block) {
    var dropdown_pin = block.getFieldValue("PIN");
    Blockly.Arduino.setups_['setup_output'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    var code = "noTone(" + dropdown_pin + ");\n";
    return code;
  };
  
  Blockly.Arduino['arduino_base_serial_print'] = function(block) {
    var content = Blockly.Arduino.valueToCode(this, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || '0'
    //content = content.replace('(','').replace(')','');
  
    Blockly.Arduino.setups_['setup_serial_' + profile.default.serial] = 'Serial.begin(' + profile.default.serial + ');\n';
  
    var code = 'Serial.println(' + content + ');\n';
    return code;
  };