'use strict';
goog.provide('Blockly.Arduino.ywrobotmodule');
goog.require('Blockly.Arduino');


Blockly.Arduino.ymath_number = function() {
  // Numeric value.
  var code = (this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.ytext = function() {
  // Text value.
    //var code = 'String('+Blockly.Arduino.quote_(this.getFieldValue('TEXT'))+')';
  var code =  Blockly.Arduino.quote_(this.getFieldValue('yTEXT')) ;
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.stext =Blockly.Arduino.ytext

Blockly.Arduino.yprocedures_defreturn = function() {
  // Define a procedure with a return value.
  var funcName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('NAME'),
      Blockly.Procedures.NAME_TYPE);
  var branch = Blockly.Arduino.statementToCode(this, 'STACK');
  if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
    branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
        '\'' + this.id + '\'') + branch;
  }
  var returnValue = Blockly.Arduino.valueToCode(this, 'RETURN',
      Blockly.Arduino.ORDER_NONE) || '';
  var type=this.getFieldValue('TYPE');
  if (returnValue) {
    returnValue = '  return ' + returnValue + ';\n';
  }
  var returnType = returnValue ? type : 'void';
  var args = [];
  for (var x = 0; x < this.arguments_.length; x++) {
    args[x] = this.argumentstype_[x]+ ' '+ Blockly.Arduino.variableDB_.getName(this.arguments_[x],
        Blockly.Variables.NAME_TYPE);
  }
  var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
      branch + returnValue + '}\n';
  code = Blockly.Arduino.scrub_(this, code);
  Blockly.Arduino.definitions_[funcName] = code;
  return null;
};

Blockly.Arduino.yprocedures_defnoreturn = Blockly.Arduino.yprocedures_defreturn;

//取模工具显示数据部分
Blockly.Arduino.ytool_modulus_show = function() {
  var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),
    Blockly.Variables.NAME_TYPE);
  var X = this.getFieldValue('x');
  var checkbox_save_hz = this.getFieldValue('save_hz') == 'TRUE';
  var value_input = Blockly.Arduino.valueToCode(this, 'input_data', Blockly.Arduino.ORDER_ATOMIC);

  var X_1 = 0;
  for(var i of value_input)
  {
    if(i == ',')
      X_1++;
  }
  X_1++;

  this.setFieldValue(X_1,"x");
  
  if(checkbox_save_hz)
    Blockly.Arduino.definitions_['var_declare'+varName] = 'static const unsigned char PROGMEM '+varName+'['+X_1+'] = '+ '{' + value_input + '};';
  else
    Blockly.Arduino.definitions_['var_declare'+varName] = 'unsigned char '+varName+'['+X_1+'] = '+ '{' + value_input + '};';
  var code = '';
  return code;
};

//取模工具设置部分
var bitArr=new Array();
for(var i=0;i<8;i++)bitArr[i]=(0x80>>i);//初始化位数组
var canvas=document.createElement("canvas");//创建canvas
var ctx=canvas.getContext("2d");//获得内容描述句柄

Blockly.Arduino.ytool_modulus = function() {
  var dropdown_bitmap_formats = this.getFieldValue('bitmap_formats');
  var dropdown_modulus_way = this.getFieldValue('modulus_way');
  var dropdown_modulus_direction = this.getFieldValue('modulus_direction');
  var dropdown_hz_sharp = this.getFieldValue('hz_sharp');
  var text_hz_line_height = this.getFieldValue('hz_line_height');
  var dropdown_hz_up_down = this.getFieldValue('hz_up_down');
  var text_hz_up_down_data = this.getFieldValue('hz_up_down_data');
  var dropdown_hz_left_right = this.getFieldValue('hz_left_right');
  var text_hz_left_right_data = this.getFieldValue('hz_left_right_data');
  var text_bitmap_width = this.getFieldValue('bitmap_width');
  var text_bitmap_height = this.getFieldValue('bitmap_height');
  var angle_bitmap_rotate = 0;
  //  var checkbox_show_hz = this.getFieldValue('show_hz') == 'TRUE';
  var checkbox_show_hz = 'TRUE';
  var text_input_data = this.getFieldValue('input_data');
  var dropdown_hz_variant = 'normal';
  var dropdown_hz_style = 'normal';
  var dropdown_hz_thickness = 'normal';
  var fontSize_width=myAtoi(text_bitmap_width);
  var fontSize_height=myAtoi(text_bitmap_height);
    var bs=Math.ceil(fontSize_width/8);//每行占字节数

    var move_x = 0;
    var move_y = 0;
    if(dropdown_hz_up_down == "hz_down")
    {
      move_y = myAtoi(text_hz_up_down_data);
    }
    else
    {
      move_y = myAtoi("-"+text_hz_up_down_data);
    }

    if(dropdown_hz_left_right == "hz_right")
    {
      move_x = myAtoi(text_hz_left_right_data);
    }
    else
    {
      move_x = myAtoi("-"+text_hz_left_right_data);
    }
    canvas.width=fontSize_width;
    canvas.height=fontSize_height;
    ctx.font = dropdown_hz_style + ' ' + dropdown_hz_variant + ' ' + dropdown_hz_thickness + ' ' + text_hz_line_height + 'px ' + dropdown_hz_sharp;
    ctx.textAlign="left";
    ctx.textBaseline="top";

    var c = text_input_data;

    ctx.fillStyle="#000000";
    ctx.fillRect(0,0,fontSize_width,fontSize_height);//涂背景
    ctx.fillStyle="#ffffff";
    ctx.translate(fontSize_width/2,fontSize_height/2);
    ctx.rotate(Math.PI/180*(angle_bitmap_rotate-0));
    ctx.fillText(c,move_x-fontSize_width/2,move_y-fontSize_height/2);//写字
    //ctx.drawImage(img,0,0,100,100);//写字

    var data=ctx.getImageData(0,0,fontSize_width,fontSize_height).data;//获取图像
    var zm=new Array(bs*fontSize_height);
    for(var i=0;i<zm.length;i++)zm[i]=0;//初始化字模数组
    for(var i=0;i<fontSize_height;i++)//读像素值组成字模数组
      for(var j=0;j<fontSize_width;j++)
        if(data[i*fontSize_width*4+j*4])zm[parseInt(j/8)+i*bs]+=bitArr[j%8];
    var outStr="";//将字模数组转化为十六进制形式
    for(var i=0;i<zm.length-1;i++)outStr+=toHex(zm[i])+",";
      outStr+=toHex(zm[i]);

    var zm1=new Array(bs*fontSize_height);
    var outstr1 = "";
    for(var i in zm)zm1[i] = zm[i].toString(2);
      for(var i in zm1)
      {
        var str = "";
        for(var j = 0;j<8-zm1[i].length;j++)str+="0";
          zm1[i] = str + zm1[i];
      }
      for(var i in zm1)outstr1+=zm1[i];

        var HZ_image = "";
      var num_hz = 0;
      for(var i = 0;i<fontSize_width;i++)
      {
        HZ_image+="--";
        if(i == (fontSize_width - 1))HZ_image+="\n|";
      }

      for(var data_hz of outstr1)
      {
        num_hz++;
        if(num_hz == outstr1.length)
        {
          HZ_image+="|\n";
        }
        else if(num_hz%(bs*8) < fontSize_width && num_hz%(bs*8) > 0)
        {
          if(data_hz == "0")HZ_image+="  ";
          else if(data_hz == "1")HZ_image+="0 ";
        } 
        else if(num_hz%(bs*8) == 0)
        {
          HZ_image+="|\n|";
        }
      }
      for(var i = 0;i<fontSize_width;i++)
      {
        HZ_image+="--";
      }
      HZ_image = "/*" + "\n" + HZ_image + "\n" + "*/";
      
      var hz_sharp = "";
      switch(dropdown_hz_sharp)
      {
        case "STHeiti":
        hz_sharp = "华文黑体";
        break;
        case "STKaiti":
        hz_sharp = "华文楷体";
        break;
        case "STXihei":
        hz_sharp = "华文细黑";
        break;
        case "STSong":
        hz_sharp = "华文宋体";
        break;
        case "STZhongsong":
        hz_sharp = "华文中宋";
        break;
        case "STFangsong":
        hz_sharp = "华文仿宋";
        break;
        case "STCaiyun":
        hz_sharp = "华文彩云";
        break;
        case "STHupo":
        hz_sharp = "华文琥珀";
        break;
        case "STLiti":
        hz_sharp = "华文隶书";
        break;
        case "STXingkai":
        hz_sharp = "华文行楷";
        break;
        case "STXinwei":
        hz_sharp = "华文新魏";
        break;
        case "simHei":
        hz_sharp = "黑体";
        break;
        case "simSun":
        hz_sharp = "宋体";
        break;
        case "NSimSun":
        hz_sharp = "新宋体";
        break;
        case "FangSong":
        hz_sharp = "仿宋";
        break;
        case "KaiTi":
        hz_sharp = "楷体";
        break;
        case "FangSong_GB2312":
        hz_sharp = "仿宋_GB2312";
        break;
        case "KaiTi_GB2312":
        hz_sharp = "楷体_GB2312";
        break;
        case "LiSu":
        hz_sharp = "隶书";
        break;
        case "YouYuan":
        hz_sharp = "幼圆";
        break;
        case "PMingLiU":
        hz_sharp = "新细明体";
        break;
        case "MingLiU":
        hz_sharp = "细明体";
        break;
        case "DFKai-SB":
        hz_sharp = "标楷体";
        break;
        case "Microsoft JhengHei":
        hz_sharp = "微软正黑体";
        break;
        case "Microsoft YaHei":
        hz_sharp = "微软雅黑体";
        break;
        default:
        hz_sharp = dropdown_hz_sharp;
        break;
      }
      hz_sharp = "字体：" + hz_sharp + "  字号：" + text_hz_line_height + "px" + "  显示文字：" + text_input_data + '\n' + HZ_image;

      var modulus_array = new Array();
      for(var i = 0;i < fontSize_height; i++)
      {
        modulus_array[i] = new Array();
        for(var j = 0;j < bs*8;j++)
        {
          modulus_array[i][j] = "";
        }
      }

      for(var i = 1;i <= fontSize_height; i++)
      {
        for(var j = 1;j <= bs*8;j++)
        {
          modulus_array[i-1][j-1] = outstr1.charAt((i-1)*bs*8 + j - 1);
        }
      }
    //取模方式
    //逐列式 - 1,逐行式 - 2,列行式 - 3,行列式 - 4

    //取模走向
    //顺向(高位在前) - 1,逆向(低位在前) - 2
    var bit_num = fontSize_height*bs;
    var modulus_data = "";
    var array_x = 0;
    var array_y = 0;
    var modulus_y = Math.ceil(fontSize_height/8);
    var modulus_x = Math.ceil(fontSize_width/8);
    
    //if(dropdown_modulus_direction == '1')
    //{
    //逐列式 - 1
    if(dropdown_modulus_way == '1')
    {
      bit_num = modulus_y*fontSize_width;
      for(var j = 1;j <= bit_num;j++)
      {
        for(var i = 1;i <= 8;i++)
        {
          if(j%modulus_y == 0)
            array_y = (modulus_y-1)*8 + i - 1;
          else
            array_y = (j%modulus_y-1)*8 + i - 1;
          
          array_x = Math.ceil(j/modulus_y) - 1;
          if(array_x > (fontSize_width - 1))
            break;
          if(array_y > (fontSize_height - 1))
          {
            if(dropdown_bitmap_formats == '1')
              modulus_data+="0";
            else
              modulus_data+="1";
            continue;
          }

          //modulus_data+=modulus_array[array_y][array_x];
          if(dropdown_bitmap_formats == '1')
            modulus_data+=modulus_array[array_y][array_x];
          else
          {
            if(modulus_array[array_y][array_x] == "0")
              modulus_data+="1";
            else
              modulus_data+="0";
          }
        }
        modulus_data+=",";
      }
    }
    //逐行式 - 2
    else if(dropdown_modulus_way == '2')
    {
      bit_num = modulus_x*fontSize_height;
      for(var j = 1;j <= bit_num;j++)
      {
        for(var i = 1;i <= 8;i++)
        {
          if(j%modulus_x == 0)
            array_x = (modulus_x-1)*8 + i - 1;
          else
            array_x = (j%modulus_x-1)*8 + i - 1;
          array_y = Math.ceil(j/modulus_x) - 1;

          //modulus_data+=modulus_array[array_y][array_x];
          if(dropdown_bitmap_formats == '1')
            modulus_data+=modulus_array[array_y][array_x];
          else
          {
            if(modulus_array[array_y][array_x] == "0")
              modulus_data+="1";
            else
              modulus_data+="0";
          }
        }
        modulus_data+=",";
      }
    }
    //列行式 - 3
    else if(dropdown_modulus_way == '3')
    {
      bit_num = modulus_y*fontSize_width;
      for(var j = 1;j <= bit_num;j++)
      {
        for(var i = 1;i <= 8;i++)
        {
          if(j%(modulus_x*8) == 0)
            array_x = modulus_x*8 - 1;
          else
            array_x = j%(modulus_x*8) - 1;
          array_y = (Math.ceil(j/(modulus_x*8)) - 1)*8 + i - 1;
          if(array_x > (fontSize_width - 1))
            break;
          if(array_y > (fontSize_height - 1))
          {
            if(dropdown_bitmap_formats == '1')
              modulus_data+="0";
            else
              modulus_data+="1";
            continue;
          }

          //modulus_data+=modulus_array[array_y][array_x];
          if(dropdown_bitmap_formats == '1')
            modulus_data+=modulus_array[array_y][array_x];
          else
          {
            if(modulus_array[array_y][array_x] == "0")
              modulus_data+="1";
            else
              modulus_data+="0";
          }
        }
        modulus_data+=",";
      }
    }
    //行列式 - 4
    else if(dropdown_modulus_way == '4')
    {
      bit_num = modulus_x*fontSize_height;
      for(var j = 1;j <= bit_num;j++)
      {
        for(var i = 1;i <= 8;i++)
        {
          if(j%fontSize_height == 0)
            array_y = fontSize_height - 1;
          else
            array_y = j%fontSize_height - 1;
          array_x = (Math.ceil(j/fontSize_height) - 1)*8 + i - 1;

          //modulus_data+=modulus_array[array_y][array_x];
          if(dropdown_bitmap_formats == '1')
            modulus_data+=modulus_array[array_y][array_x];
          else
          {
            if(modulus_array[array_y][array_x] == "0")
              modulus_data+="1";
            else
              modulus_data+="0";
          }
        }
        modulus_data+=",";
      }
    }
    //}
    var now_data = "";
    var end_data = "";
    if(dropdown_modulus_direction == 2)
    {
      for(var i of modulus_data)
      {
        if(i == ",")
        {
          end_data+=now_data;
          end_data+=",";
          now_data = "";
        }
        else
          now_data = i + now_data;
      }
      modulus_data = end_data;
    }

    now_data = "";
    end_data = "0x";
    for(var i of modulus_data)
    {
      if(i == ",")
      {
        end_data+=",0x";
        continue;
      }
      now_data+=i;
      if(now_data.length == 4)
      {
        end_data+=string_Bin_to_Hex(now_data);
        now_data = "";
      }
    }
    modulus_data = end_data;
    modulus_data = modulus_data.substring(0,modulus_data.length-3);
    
    if(checkbox_show_hz)
      Blockly.Arduino.definitions_['var_declare_tool_modulus_data_' + dropdown_hz_sharp + '_' + text_hz_line_height + 'px' + encodeUnicode(text_input_data)] = '//' + hz_sharp;
    
    var code = modulus_data;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
  };

//

Blockly.Arduino.yprocedures_callnoreturn = function() {
  // Call a procedure with no return value.
  var funcName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('NAME'),
      Blockly.Procedures.NAME_TYPE);
  var args = [];
  for (var x = 0; x < this.arguments_.length; x++) {
    args[x] = Blockly.Arduino.valueToCode(this, 'ARG' + x,
        Blockly.Arduino.ORDER_NONE) || 'null';
  }
  var code = funcName + '(' + args.join(', ') + ');\n';
  return code;
};



Blockly.Arduino.ywrobotmodule_robot_pins_digital = function() {
  var code = this.getFieldValue('PIN');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_robot_pins_motor = function() {
  var code = this.getFieldValue('ID');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_LCD1602_Print = function() {
  var varName = this.getFieldValue('VAR');
  var str1 = Blockly.Arduino.valueToCode(this, 'yTEXT', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
  var str2 = Blockly.Arduino.valueToCode(this, 'yTEXT2', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
  var device = Blockly.Arduino.valueToCode(this, 'device', Blockly.Arduino.ORDER_ATOMIC) || '0x27';
  Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
  Blockly.Arduino.definitions_['include_LiquidCrystal_I2C'] = '#include <LiquidCrystal_I2C.h>';
  Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_'+varName] = 'LiquidCrystal_I2C '+varName+'('+device+',16,2);';
  if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
      Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
      Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
  }
  var code = varName+'.setCursor(0, 0);\n'
  code+=varName+'.print('+str1+');\n';
  code+=varName+'.setCursor(0, 1);\n';
  code+=varName+'.print('+str2+');\n';  
  return code;
};
Blockly.Arduino.ywrobotmodule_LCD1602_Print2 = function() {
  var varName = this.getFieldValue('VAR');
  var str = Blockly.Arduino.valueToCode(this, 'yTEXT', Blockly.Arduino.ORDER_ATOMIC) || 'String(\"\")';
  var row = Blockly.Arduino.valueToCode(this, 'row', Blockly.Arduino.ORDER_ATOMIC) || '1';
  var column = Blockly.Arduino.valueToCode(this, 'column', Blockly.Arduino.ORDER_ATOMIC) || '1';
  if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
      Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
      Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
  }
  var code = varName+'.setCursor('+column+'-1, '+row+'-1);\n'
  code+=varName+'.print('+str+');\n';
  return code;
};
Blockly.Arduino.ywrobotmodule_LCD1602_Power = function() {
  var varName = this.getFieldValue('VAR');
  var dropdown_stat = this.getFieldValue('STAT');
  if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
      Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
      Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
  }
  var code = varName+'.'+dropdown_stat+'();\n'
  return code;
};

Blockly.Arduino.ywrobotmodule_display_rgb_init = function () {
  var dropdown_rgbpin = this.getFieldValue('PIN');
  var type = this.getFieldValue('TYPE');
  var value_ledcount = Blockly.Arduino.valueToCode(this, 'LEDCOUNT', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
  Blockly.Arduino.definitions_['var_declare_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '= Adafruit_NeoPixel(' + value_ledcount + ',' + dropdown_rgbpin + ','+type+' + NEO_KHZ800);';
  Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
  return '';
};
Blockly.Arduino.ywrobotmodule_display_rgb_Brightness = function () {
  var dropdown_rgbpin = this.getFieldValue('PIN');
  var Brightness = Blockly.Arduino.valueToCode(this, 'Brightness', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
  Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
  var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + Brightness + ');';
  return code;
};

Blockly.Arduino.ywrobotmodule_display_rgb=function(){
  var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var value_led = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
  var value_rvalue = Blockly.Arduino.valueToCode(this, 'RVALUE', Blockly.Arduino.ORDER_ATOMIC);
  var value_gvalue = Blockly.Arduino.valueToCode(this, 'GVALUE', Blockly.Arduino.ORDER_ATOMIC);
  var value_bvalue = Blockly.Arduino.valueToCode(this, 'BVALUE', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
   Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
   if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
      Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(4);';
      Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
      Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
  }
  
  var code = 'rgb_display_'+dropdown_rgbpin+'.setPixelColor('+value_led+'-1, '+value_rvalue+','+value_gvalue+','+value_bvalue+');\n';
  code+='rgb_display_'+dropdown_rgbpin+'.show();\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_display_rgb2=function(){
  var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var value_led = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
  var colour_rgb_led_color = this.getFieldValue('RGB_LED_COLOR');
  var color = goog.color.hexToRgb(colour_rgb_led_color);
  Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
    Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
   if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
      Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(4);';
      Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
      Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
  }
  var code = 'rgb_display_'+dropdown_rgbpin+'.setPixelColor('+value_led+'-1, '+color+');\n';
  code+='rgb_display_'+dropdown_rgbpin+'.show();\n';
  return code;
};


Blockly.Arduino.ywrobotmodule_display_rgb_rainbow1 = function () {
  var dropdown_rgbpin = this.getFieldValue('PIN');
  var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();\n';
  var funcName2 = 'Wheel';
  var code2 = 'uint32_t Wheel(byte WheelPos) {\n';
  code2 += 'if(WheelPos < 85) \n{\nreturn rgb_display_' + dropdown_rgbpin + '.Color(WheelPos * 3, 255 - WheelPos * 3, 0);\n} \n';
  code2 += 'else if(WheelPos < 170) \n{\nWheelPos -= 85; \nreturn rgb_display_' + dropdown_rgbpin + '.Color(255 - WheelPos * 3, 0, WheelPos * 3);\n}\n ';
  code2 += 'else\n {\nWheelPos -= 170;\nreturn rgb_display_' + dropdown_rgbpin + '.Color(0, WheelPos * 3, 255 - WheelPos * 3);\n}\n';
  code2 += '}\n';
  Blockly.Arduino.definitions_[funcName2] = code2;
  var funcName3 = 'rainbow';
  var code3 = 'void rainbow(uint8_t wait) {\n uint16_t i, j;\n';
  code3 += 'for(j=0; j<256; j++) {\n';
  code3 += 'for(i=0; i<rgb_display_' + dropdown_rgbpin + '.numPixels(); i++)\n {\n';
  code3 += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel((i+j) & 255));\n}\n';
  code3 += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
  code3 += 'delay(wait);\n}\n}\n';
  Blockly.Arduino.definitions_[funcName3] = code3;
  var code = 'rainbow(' + wait_time + ');\n'
  return code;
};

Blockly.Arduino.ywrobotmodule_servo_move = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);
  //value_degree = value_degree.replace('(','').replace(')','')
  var delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '0'
  //delay_time = delay_time.replace('(','').replace(')','');
  
  Blockly.Arduino.definitions_['include_Servo'] = '#include <Servo.h>';
  Blockly.Arduino.definitions_['var_servo'+dropdown_pin] = 'Servo servo_'+dropdown_pin+';';
  Blockly.Arduino.setups_['setup_servo_'+dropdown_pin] = 'servo_'+dropdown_pin+'.attach('+dropdown_pin+');';
  
  var code = 'servo_'+dropdown_pin+'.write('+value_degree+');\n'+'delay(' + delay_time + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_servo_writeMicroseconds = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);  
  Blockly.Arduino.definitions_['include_Servo'] = '#include <Servo.h>';
  Blockly.Arduino.definitions_['var_servo'+dropdown_pin] = 'Servo servo_'+dropdown_pin+';';
  Blockly.Arduino.setups_['setup_servo_'+dropdown_pin] = 'servo_'+dropdown_pin+'.attach('+dropdown_pin+');'; 
  var code = 'servo_'+dropdown_pin+'.writeMicroseconds('+value_degree+');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_servo_read_degrees = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  
  Blockly.Arduino.definitions_['include_Servo'] = '#include <Servo.h>';
  Blockly.Arduino.definitions_['var_servo'+dropdown_pin] = 'Servo servo_'+dropdown_pin+';';
  Blockly.Arduino.setups_['setup_servo_'+dropdown_pin] = 'servo_'+dropdown_pin+'.attach('+dropdown_pin+');';
  
  var code = 'servo_'+dropdown_pin+'.read()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//数字量传感器
Blockly.Arduino.ywrobotmodule_anjian = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var code = "";
  if(window.isNaN(dropdown_pin)){
	var funcName='mixly_digitalRead';
	var code2='int'+ ' ' + funcName + '(uint8_t pin) {\n' 
	+ '  pinMode(pin, INPUT);\n'
	+ '  return digitalRead(pin);\n'
	+ '}\n';
    Blockly.Arduino.definitions_[funcName] = code2;
	code = 'mixly_digitalRead('+dropdown_pin+')';
  }else{
    if(Blockly.Arduino.setups_['setup_output_'+dropdown_pin]){
	   //存在pinMode已设为output则不再设为input
	}else{
       Blockly.Arduino.setups_['setup_input_'+dropdown_pin] = 'pinMode('+dropdown_pin+', INPUT);';
	}
	code = 'digitalRead('+dropdown_pin+')';
  }
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_chumo = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_pengzhuang = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_zhendong = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_ganhuang = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_qingxie = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_rentihong = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_huoer = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_bizhang = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_xunji = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_caoxingjishu = Blockly.Arduino.ywrobotmodule_anjian;
Blockly.Arduino.ywrobotmodule_yaoganK = Blockly.Arduino.ywrobotmodule_anjian;


//模拟量传感器
Blockly.Arduino.ywrobotmodule_dianweiqi = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var code = 'analogRead('+dropdown_pin+')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_tuizi = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_huoyan = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_liangdu = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_shengyin = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_turang = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_shuiwei = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_mq2 = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_dianya = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_huidu = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_yaoganX = Blockly.Arduino.ywrobotmodule_dianweiqi;
Blockly.Arduino.ywrobotmodule_yaoganY = Blockly.Arduino.ywrobotmodule_dianweiqi;


Blockly.Arduino.ywrobotmodule_LM35 = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var code = 'analogRead('+dropdown_pin+')*0.488';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.ywrobotmodule_robot_hc_sr04_trig = function() {
  var code = this.getFieldValue('PIN1');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_robot_hc_sr04_echo = function() {
  var code = this.getFieldValue('PIN2');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_hc_sr04 = function() {
    var dropdown_pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    Blockly.Arduino.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', INPUT);\n';
    var funcName = 'checkdistance_' + dropdown_pin1 + '_' + dropdown_pin2;
    var code = 'float' + ' ' + funcName + '() {\n'
	+ '  digitalWrite(' + dropdown_pin1 + ', LOW);\n' + '  delayMicroseconds(2);\n'
	+ '  digitalWrite(' + dropdown_pin1 + ', HIGH);\n' + '  delayMicroseconds(10);\n'
	+ '  digitalWrite(' + dropdown_pin1 + ', LOW);\n'
	+ '  float distance = pulseIn(' + dropdown_pin2 + ', HIGH) / 58.00;\n'
	+ '  delay(10);\n' +'  return distance;\n'
	+ '}\n';
    Blockly.Arduino.definitions_[funcName] = code;
    return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
}


//数字量执行器


Blockly.Arduino.ywrobotmodule_LED = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_stat = this.getFieldValue('STAT');
  var code = "";
  if(window.isNaN(dropdown_pin)){
    code = code+'pinMode('+dropdown_pin+', OUTPUT);\n';
  }else{
    Blockly.Arduino.setups_['setup_output_'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
  }
  code += 'digitalWrite('+dropdown_pin+','+dropdown_stat+');\n'
  return code;
};



Blockly.Arduino.ywrobotmodule_MLED = function () {
    var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
    //var dropdown_stat = this.getFieldValue('STAT');
    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 = 'analogWrite(' + dropdown_pin + ',' + value_num + ');\n';
    return code;
};

Blockly.Arduino.ywrobotmodule_mshirenyu = Blockly.Arduino.ywrobotmodule_MLED;
Blockly.Arduino.ywrobotmodule_fengshan = Blockly.Arduino.ywrobotmodule_MLED;
Blockly.Arduino.ywrobotmodule_mzhendongmada = Blockly.Arduino.ywrobotmodule_MLED;

Blockly.Arduino.ywrobotmodule_ybuzzer = Blockly.Arduino.ywrobotmodule_LED;
Blockly.Arduino.ywrobotmodule_shirenyu = Blockly.Arduino.ywrobotmodule_LED;
Blockly.Arduino.ywrobotmodule_zhendongmada = Blockly.Arduino.ywrobotmodule_LED;
Blockly.Arduino.ywrobotmodule_jidianqi = Blockly.Arduino.ywrobotmodule_LED;

Blockly.Arduino.ywrobotmodule_fengshan9110 = function () {
  var PIN1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var PIN2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var code = 'setMotor(' + PIN1 + ', ' + PIN2 + ',' + speed + ');\n';
  Blockly.Arduino.setups_['setup_output_' + PIN1 + PIN2 + '_S'] = 'pinMode(' + PIN1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + PIN1 + PIN2 + '_D'] = 'pinMode(' + PIN2 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + PIN1 + PIN2 + '_S_W'] = 'digitalWrite(' + PIN1 + ', LOW);';
  Blockly.Arduino.setups_['setup_output_' + PIN1 + PIN2 + '_D_W'] = 'digitalWrite(' + PIN2 + ', LOW);';
  var funcName = 'setMotor';
  var code2 = ' void ' + funcName + '(int speedpin,int dirpin, int speed)\n '
  + '  {\n'
  + 'if (speed == 0)\n'
  + '{\n'
  + 'digitalWrite(speedpin, LOW);\n'
  + '} \n'
  + 'else if (speed > 0)\n'
  + '{\n'
  + 'digitalWrite(dirpin, LOW);\n'
  + 'analogWrite(speedpin, speed);\n'
  + '} \n'
  + 'else \n'
  + '{\n'
  +'digitalWrite(dirpin, HIGH);\n'
  +'analogWrite(speedpin, speed);\n'
  + '}\n'
  + '}\n';
  Blockly.Arduino.definitions_[funcName] = code2;
  return code;
};

Blockly.Arduino.ywrobotmodule_quancai = function () {
    var dropdown_pinR = Blockly.Arduino.valueToCode(this, 'PINR', Blockly.Arduino.ORDER_ATOMIC);
    //var dropdown_stat = this.getFieldValue('STAT');
    var value_numR = Blockly.Arduino.valueToCode(this, 'NUMR', Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.setups_['setup_output'+dropdown_pinR] = 'pinMode('+dropdown_pinR+', OUTPUT);';
    var codeR = 'analogWrite(' + dropdown_pinR + ',' + value_numR + ');\n';
	var dropdown_pinG = Blockly.Arduino.valueToCode(this, 'PING', Blockly.Arduino.ORDER_ATOMIC);
    //var dropdown_stat = this.getFieldValue('STAT');
    var value_numG = Blockly.Arduino.valueToCode(this, 'NUMG', Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.setups_['setup_output'+dropdown_pinG] = 'pinMode('+dropdown_pinG+', OUTPUT);';
    var codeG = 'analogWrite(' + dropdown_pinG + ',' + value_numG + ');\n';
	var dropdown_pinB = Blockly.Arduino.valueToCode(this, 'PINB', Blockly.Arduino.ORDER_ATOMIC);
    //var dropdown_stat = this.getFieldValue('STAT');
    var value_numB = Blockly.Arduino.valueToCode(this, 'NUMB', Blockly.Arduino.ORDER_ATOMIC);
    //Blockly.Arduino.setups_['setup_output'+dropdown_pinB] = 'pinMode('+dropdown_pinB+', OUTPUT);';
    var codeB = 'analogWrite(' + dropdown_pinB + ',' + value_numB + ');\n';
    var code = codeR;
	code += codeG;
    code += codeB;
	return code;
};

Blockly.Arduino.ywrobotmodule_quancai2 = Blockly.Arduino.ywrobotmodule_quancai;

Blockly.Arduino.ywrobotmodule_xinhaodeng = function () {
	var dropdown_pinR = Blockly.Arduino.valueToCode(this, 'PINR',Blockly.Arduino.ORDER_ATOMIC);
	var dropdown_pinY = Blockly.Arduino.valueToCode(this, 'PINY',Blockly.Arduino.ORDER_ATOMIC);
	var dropdown_pinG = Blockly.Arduino.valueToCode(this, 'PING',Blockly.Arduino.ORDER_ATOMIC);
	var dropdown_statR = this.getFieldValue('STATR');
	var dropdown_statY = this.getFieldValue('STATY');
	var dropdown_statG = this.getFieldValue('STATG');
	Blockly.Arduino.setups_['setup_output_' + dropdown_pinR] = 'pinMode(' + dropdown_pinR + ', OUTPUT);\n'
	Blockly.Arduino.setups_['setup_output_' + dropdown_pinY] = 'pinMode(' + dropdown_pinY + ', OUTPUT);\n'
	Blockly.Arduino.setups_['setup_output_' + dropdown_pinG] = 'pinMode(' + dropdown_pinG + ', OUTPUT);\n'
    var code = 'digitalWrite(' + dropdown_pinR + ',' + dropdown_statR + ');\n'
	code += 'digitalWrite(' + dropdown_pinY + ',' + dropdown_statY + ');\n'
	code += 'digitalWrite(' + dropdown_pinG + ',' + dropdown_statG + ');\n'
    return code;
}; 

//总线

Blockly.Arduino.ywrobotmodule_dht11 = function() {
	var sensor_type = this.getFieldValue('TYPE');
	var dropdown_pin = this.getFieldValue('PIN');
    var what = this.getFieldValue('WHAT');
    Blockly.Arduino.definitions_['include_DHT'] = '#include <DHT.h>';
	Blockly.Arduino.definitions_['var_declare_dht' + dropdown_pin] = 'DHT dht'+dropdown_pin+'('+dropdown_pin+', '+sensor_type+');'
	Blockly.Arduino.setups_['DHT_SETUP'+dropdown_pin] = ' dht'+dropdown_pin+'.begin();';
	var code;
	if(what=="temperature")
		code= 'dht'+dropdown_pin+'.readTemperature()'
	else
		code= 'dht'+dropdown_pin+'.readHumidity()'
	return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.ywrobotmodule_ds18B20 = function () {
    var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
    var unit = this.getFieldValue('UNIT');
    Blockly.Arduino.definitions_['include_OneWire'] = '#include <OneWire.h>';
    Blockly.Arduino.definitions_['include_DallasTemperature'] = '#include <DallasTemperature.h>';
    Blockly.Arduino.definitions_['var_OneWire_oneWire_' + dropdown_pin] = 'OneWire oneWire_' + dropdown_pin + '(' + dropdown_pin + ');';
    Blockly.Arduino.definitions_['var_DallasTemperature_sensors_' + dropdown_pin] = 'DallasTemperature sensors_' + dropdown_pin + '(&oneWire_' + dropdown_pin + ');';
    Blockly.Arduino.definitions_['var_DeviceAddress_insideThermometer'] = 'DeviceAddress insideThermometer;';
    Blockly.Arduino.setups_['setup_sensors_' + dropdown_pin + '_getAddress'] = 'sensors_' + dropdown_pin + '.getAddress(insideThermometer, 0);';
    Blockly.Arduino.setups_['setup_sensors_' + dropdown_pin + '_setResolution'] = 'sensors_' + dropdown_pin + '.setResolution(insideThermometer, 9);';
    var funcName = 'ds18b20_' + dropdown_pin + '_getTemp';
    var code = 'float' + ' ' + funcName + '(int w) {\n'
	+ '  sensors_' + dropdown_pin + '.requestTemperatures();\n'
	+ '  if(w==0) {\nreturn sensors_' + dropdown_pin + '.getTempC(insideThermometer);\n}\n'
	+ '  else {\nreturn sensors_' + dropdown_pin + '.getTempF(insideThermometer);\n}\n'
	+ '}\n';
    Blockly.Arduino.definitions_[funcName] = code;
    return ['ds18b20_' + dropdown_pin + '_getTemp(' + unit + ')', Blockly.Arduino.ORDER_ATOMIC];
}

Blockly.Arduino.ywrobotmodule_ADXL345 = function() {
  Blockly.Arduino.definitions_['include_ADXL345'] = '#include <ADXL345.h>';
  Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
  Blockly.Arduino.definitions_['include_I2Cdev'] = '#include <I2Cdev.h>';
  Blockly.Arduino.definitions_['var_ADXL345'] = 'ADXL345 accel;';
  Blockly.Arduino.setups_['setup_ADXL345-A'] = 'Wire.begin();';
  Blockly.Arduino.setups_['setup_ADXL345-B'] = 'accel.initialize();';
  var dropdown_type = this.getFieldValue('ADXL345_PIN');
  var code = '';
  if (dropdown_type == "xa") code += 'accel.X_angle()';
  if (dropdown_type == "ya") code += 'accel.Y_angle()';
  if (dropdown_type == "x") code += 'accel.getAccelerationX()';
  if (dropdown_type == "y") code += 'accel.getAccelerationY()';
  if (dropdown_type == "z") code += 'accel.getAccelerationZ()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.ywrobotmodule_DS1307_init = function () {
  var SDA = Blockly.Arduino.valueToCode(this, 'SDA', Blockly.Arduino.ORDER_ATOMIC);
  var SCL = Blockly.Arduino.valueToCode(this, 'SCL', Blockly.Arduino.ORDER_ATOMIC);
  var RTCType = this.getFieldValue('RTCType');
  Blockly.Arduino.definitions_['include_'+RTCType] = '#include <'+RTCType+'.h>';
  //Blockly.Arduino.definitions_['var_declare_RtcDateTime_dt'] = 'const RtcDateTime dt;';
  if(SDA!="SDA"||SCL!="SCL"){
    Blockly.Arduino.definitions_['include_Wire'] = '#include <SoftwareWire.h>';
    Blockly.Arduino.definitions_['var_declare_SoftwareWire'] = 'SoftwareWire myWire('+SDA+','+SCL+');';
    Blockly.Arduino.definitions_['var_declare_'+RTCType] = RTCType+'<SoftwareWire> Rtc(myWire);';
  }  
  else{
    Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
    Blockly.Arduino.definitions_['var_declare_'+RTCType] = RTCType+'<TwoWire> Rtc(Wire);';
  }
  Blockly.Arduino.setups_['setup_Rtc.Begin'] = 'Rtc.Begin();' ;
  return "";
}


Blockly.Arduino.ywrobotmodule_RTC_get_time = function () {
  var timeType = this.getFieldValue('TIME_TYPE');
  var code = 'Rtc.GetDateTime().' + timeType +'()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}


Blockly.Arduino.ywrobotmodule_RTC_date = function () {
 var year = Blockly.Arduino.valueToCode(this, "year", Blockly.Arduino.ORDER_ATOMIC);
 var month = Blockly.Arduino.valueToCode(this, "month", Blockly.Arduino.ORDER_ATOMIC);
 var day = Blockly.Arduino.valueToCode(this, "day", Blockly.Arduino.ORDER_ATOMIC);

 switch(month)
 {
  case '1':
  month = 'Jan';
  break;
  case '2':
  month = 'Feb';
  break;
  case '3':
  month = 'Mar';
  break;
  case '4':
  month = 'Apr';
  break;
  case '5':
  month = 'May';
  break;
  case '6':
  month = 'Jun';
  break;
  case '7':
  month = 'Jul';
  break;
  case '8':
  month = 'Aug';
  break;
  case '9':
  month = 'Sep';
  break;
  case '10':
  month = 'Oct';
  break;
  case '11':
  month = 'Nov';
  break;
  case '12':
  month = 'Dec';
  break;
  default:
  month = 'Jan';
}
if(day.length == 1)
  day = '0'+day;
var code = '"'+month  + '/'+ day + '/'+ year +'"';
return [code, Blockly.Arduino.ORDER_ATOMIC];
}

Blockly.Arduino.ywrobotmodule_RTC_time = function () {
 var hour = Blockly.Arduino.valueToCode(this, "hour", Blockly.Arduino.ORDER_ATOMIC);
 var minute = Blockly.Arduino.valueToCode(this, "minute", Blockly.Arduino.ORDER_ATOMIC);
 var second = Blockly.Arduino.valueToCode(this, "second", Blockly.Arduino.ORDER_ATOMIC);
 if(hour.length == 1)
  hour = '0'+hour;
if(minute.length == 1)
  minute = '0'+minute;
if(second.length == 1)
  second = '0'+second;
var code = '"'+ hour + ':'+ minute + ':'+ second +'"';
return [code, Blockly.Arduino.ORDER_ATOMIC];
}

Blockly.Arduino.ywrobotmodule_get_system_date_time = function() {
  var dropdown_type = this.getFieldValue('type');
  var code = '__'+dropdown_type+'__';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_RTC_set_time = function() {
  var value_date = Blockly.Arduino.valueToCode(this, 'date', Blockly.Arduino.ORDER_ATOMIC);
  var value_time = Blockly.Arduino.valueToCode(this, 'time', Blockly.Arduino.ORDER_ATOMIC);
  var code = '';
  code = 'Rtc.SetDateTime(RtcDateTime('+value_date+', '+value_time+'));\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_RTC_set_date = function () {
  var RTCName = "myRTC";
  var code = RTCName + '.setDate('+ year + ','+ month + ','+ day +');\n';
  code += RTCName + '.setDOW('+ year + ','+ month + ','+ day +');\n';
  return code;
}








Blockly.Arduino.ywrobotmodule_encoder_write = function() {
  var Encoder_NO= this.getFieldValue('Encoder_NO');
  var value= Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_encoder_write_' + Encoder_NO + value] ='encoder_'+Encoder_NO+'.write('+value+');';
  return "";
};

Blockly.Arduino.ywrobotmodule_encoder_read = function() {
  var Encoder_NO= this.getFieldValue('Encoder_NO');
  var code = 'encoder_' +Encoder_NO+ '.read()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_encoder_init = function() {
  var OTB = this.getFieldValue('OTB');
  var OTA = this.getFieldValue('OTA');
  var Encoder_NO= this.getFieldValue('Encoder_NO');
  Blockly.Arduino.definitions_['include_Encoder'] ='#include <Encoder.h>\n';
  Blockly.Arduino.definitions_['var_declare_EncoderDY'] = 'Encoder encoder_'+Encoder_NO+'('+OTB+','+OTA+');\n ';
  var code='';
  return code;
};

Blockly.Arduino.ywrobotmodule_store_sd_init = function() {
	var pin_cs = Blockly.Arduino.valueToCode(this, 'PIN_CS', Blockly.Arduino.ORDER_ATOMIC);
	Blockly.Arduino.definitions_['include_SD'] = '#include <SD.h>';
	Blockly.Arduino.definitions_['include_SPI'] = '#include <SPI.h>';
	Blockly.Arduino.setups_['setup_sd_write_begin'] = 'SD.begin('+pin_cs+');';
	var code = '';
	return code;
};

Blockly.Arduino.ywrobotmodule_store_sd_write = function() {
	var file = Blockly.Arduino.quote_(this.getFieldValue('FILE'));
	//file=file.replace(/String/,"");
	var data = Blockly.Arduino.valueToCode(this, 'DATA', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
	//data=data.replace(/String/,"");
	var newline = Blockly.Arduino.valueToCode(this, 'NEWLINE', Blockly.Arduino.ORDER_ATOMIC) || 'false';
	Blockly.Arduino.definitions_['var_declare_File_datafile'] = 'File datafile;';
	var code='datafile = SD.open('+file+', FILE_WRITE);\n';
	code+='if(datafile){\n';
	code+='	datafile.print('+data+');\n';
	if(newline=='true'){
		code+='	datafile.println("");\n';
	}
	code+='	datafile.close();\n';
	code+='}\n';
	return code;
}
Blockly.Arduino.ywrobotmodule_sd_exist = function() {
	var text_FileName = this.getFieldValue('FileName');
	var code = 'SD.exists("'+text_FileName+'")';
	return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_sd_read = function() {
  var text_FileName = this.getFieldValue('FileName');
  const serial_select='Serial';
  var content = Blockly.Arduino.valueToCode(this, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || profile.default.serial;
  Blockly.Arduino.definitions_['var_declare_File_datafile'] = 'File datafile;';
  Blockly.Arduino.setups_['setup_serial_' + serial_select + profile.default.serial] = serial_select + '.begin(' + content + ');';
  var code ='datafile = SD.open("'+text_FileName+'");\n'
  code+= 'while (datafile.available()) {\n'
  code+='Serial.write(datafile.read( ));\n'
  code+='}\n'
  code+='datafile.close();\n'  
  return code;
};

Blockly.Arduino.ywrobotmodule_sd_DelFile = function() {
	var text_FileName = this.getFieldValue('FileName');
	var code = 'SD.remove("'+text_FileName+'");';
	return code;
};


//红外接收

Blockly.Arduino.ywrobotmodule_ir_recv = function () {
    var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    Blockly.Arduino.definitions_['var_declare' + variable] = 'long ' + variable + ';';
    var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
    var branch = Blockly.Arduino.statementToCode(this, 'DO');
    var branch2 = Blockly.Arduino.statementToCode(this, 'DO2');
    var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),
       Blockly.Variables.NAME_TYPE);
    Blockly.Arduino.definitions_['include_IRremote'] = '#include <IRremote.h>\n';
    //Blockly.Arduino.definitions_['var_declare'+varName] = 'long '+varName+';\n';
    Blockly.Arduino.definitions_['var_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\ndecode_results results_' + dropdown_pin + ';\n';
    Blockly.Arduino.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();';
    var code = "if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
    code += '  ' + variable + '=results_' + dropdown_pin + '.value;\n';
    code += '  String type="UNKNOWN";\n';
    ////////////////////////////////////////////////////////////////
    code += '  String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};\n';
    code += '  if(results_' + dropdown_pin + '.decode_type>=1&&results_' + dropdown_pin + '.decode_type<=13){\n';
    code += '    type=typelist[results_' + dropdown_pin + '.decode_type];\n'
    code += '  }\n';
    code += '  Serial.print("IR TYPE:"+type+"  ");\n';
    code += branch;
    code += '  irrecv_' + dropdown_pin + '.resume();\n'
    code += '} else {\n';
    code += branch2;
    code += '}\n';
    return code;
};
Blockly.Arduino.ywrobotmodule_ir_recv2 = Blockly.Arduino.ywrobotmodule_ir_recv;

Blockly.Arduino.ywrobotmodule_robot_tone_notes = function() {
  var code = this.getFieldValue('STAT');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.ywrobotmodule_robot_tone = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var fre = Blockly.Arduino.valueToCode(this, 'FREQUENCY', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var dur = Blockly.Arduino.valueToCode(this, 'DURATION', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  Blockly.Arduino.definitions_['newtone0'] = 'float period;\nfloat pulse;';
  Blockly.Arduino.definitions_['newtone1'] = 'void newtone(int tonePin, int frequency, int duration)';
  Blockly.Arduino.definitions_['newtone2'] = '{\nfloat period = 1000000.0 /frequency;';
  Blockly.Arduino.definitions_['newtone3'] = 'float pulse = period / 2.0;';
  Blockly.Arduino.definitions_['newtone4'] = 'for (int i=1; i<=((duration * 1000.0)/period);i=i+1)';
  Blockly.Arduino.definitions_['newtone5'] = ' {\npinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,HIGH);';
  Blockly.Arduino.definitions_['newtone6'] = 'delayMicroseconds(pulse);';
  Blockly.Arduino.definitions_['newtone7'] = 'pinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,LOW);';
  Blockly.Arduino.definitions_['newtone8'] = ' delayMicroseconds(pulse);\n}\n}\n';
  var code = "newtone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
  return code;
};


Blockly.Arduino.ywrobotmodule_robot_newNoTone = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var Delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  var code = "digitalWrite(" + dropdown_pin + ", LOW);\n";
  code += 'delay(' + Delay_time + ');';
  return code;
};

Blockly.Arduino.ywrobotmodule_speaker_tone = Blockly.Arduino.ywrobotmodule_robot_tone;
Blockly.Arduino.ywrobotmodule_speaker_Notone = Blockly.Arduino.ywrobotmodule_robot_newNoTone;

Blockly.Arduino.ywrobotvoice_robot_pins_digital = function() {
  var code = this.getFieldValue('PIN');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotvoice_robot_pins_digital = function() {
  var code = this.getFieldValue('BUSY');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotvoice_pins_voice = function() {
  var code = this.getFieldValue('VOICE');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.controls_whileUntil = function () {
    // Do while/until loop.
    var argument0 = Blockly.Arduino.valueToCode(this, 'BOOL',
        Blockly.Arduino.ORDER_NONE) || 'false';
    var branch = Blockly.Arduino.statementToCode(this, 'DO');
    if (Blockly.Arduino.INFINITE_LOOP_TRAP) {
        branch = Blockly.Arduino.INFINITE_LOOP_TRAP.replace(/%1/g,
            '\'' + this.id + '\'') + branch;
    }
    if (this.getFieldValue('MODE') == 'UNTIL') {
        if (!argument0.match(/^\w+$/)) {
            argument0 = '(' + argument0 + ')';
        }
        argument0 = '!' + argument0;
    }
    return 'while (' + argument0 + ') {\n' + branch + '}\n';
};

Blockly.Arduino.ywrobotvoice_voice = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
  var busy = Blockly.Arduino.valueToCode(this, 'BUSY',Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_voice =  Blockly.Arduino.valueToCode(this, 'VOICE',Blockly.Arduino.ORDER_ATOMIC);
  var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ASSIGNMENT) || '0'; 
   Blockly.Arduino.setups_['setup_output_sda'] = 'pinMode('+dropdown_pin+', OUTPUT);\n';
   Blockly.Arduino.setups_['setup_input_sda'] = 'pinMode('+busy+', INPUT);';
   var code =  'send_data('+dropdown_voice+'); //volume control 0xE0-E7;\n';
       code += 'delay('+wait_time +');\n'
   
   var funcName = 'voice_out';
   
   var code2 =  'void send_data(int addr)\n ';
       code2 += '{int i;digitalWrite('+dropdown_pin+' , LOW);\n';
       code2 += 'delay(3); //>2ms\n';
       code2 += 'for(i=0;i<8;i++)\n';
       code2 += '{digitalWrite('+dropdown_pin+ ', HIGH);\n';
       code2 += 'if(addr&1){delayMicroseconds(2400); //>2400us\n';
       code2 += '          digitalWrite('+dropdown_pin+', LOW);\n';
       code2 += '          delayMicroseconds(800);} //>800us\n';
       code2 += 'else{ delayMicroseconds(800); //>800us\n';
       code2 += '      digitalWrite('+dropdown_pin+ ' , LOW);\n';
       code2 += '      delayMicroseconds(2400);} //>2400us\n';
       code2 += '      addr>>=1;} //地址值右移一位\n';
       code2 += '      digitalWrite('+dropdown_pin+', HIGH);\n';
	   code2 += 'while(digitalRead(' + busy + ') == 0);}\n';
	  
	   
   Blockly.Arduino.definitions_['funcName'] = code2;   
   
  return code;
};


Blockly.Arduino.ywrobotmodule_bujindianji = function () {
  var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  var dropdown_pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin3 = Blockly.Arduino.valueToCode(this, 'PIN3', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin4 = Blockly.Arduino.valueToCode(this, 'PIN4', Blockly.Arduino.ORDER_ATOMIC);
  var steps = Blockly.Arduino.valueToCode(this, 'steps', Blockly.Arduino.ORDER_ATOMIC);
  var speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_Stepper'] = '#include <Stepper.h>';
  Blockly.Arduino.definitions_['var_declare_stepper' + varName] = 'Stepper ' + varName + '(' + steps + ',' + dropdown_pin1 + ',' + dropdown_pin2 + ',' + dropdown_pin3 + ',' + dropdown_pin4 + ');';
  Blockly.Arduino.setups_['setup_stepper' + varName] = varName + '.setSpeed(' + speed + ');';
  return '';
};

Blockly.Arduino.ywrobotmodule_bujindianjibushu = function () {
  var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  var step = Blockly.Arduino.valueToCode(this, 'step', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_Stepper'] = '#include <Stepper.h>';
  return varName + '.step(' + step + ');\n';
};

//显示器

Blockly.Arduino.ywrobotmodule_LCD1602_init = function() {
  var varName = this.getFieldValue('VAR');
  var TYPE = this.getFieldValue('TYPE');
  var SCL = this.getFieldValue('SCL');
  var SDA = this.getFieldValue('SDA');
  var device = Blockly.Arduino.valueToCode(this, 'device', Blockly.Arduino.ORDER_ATOMIC) || '0x27';  
  if(SDA=="SDA"&&SCL=="SCL")
  {
    Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
    Blockly.Arduino.definitions_['include_LiquidCrystal_I2C'] = '#include <LiquidCrystal_I2C.h>';
    Blockly.Arduino.definitions_['var_declare_LiquidCrystal_I2C_'+varName] = 'LiquidCrystal_I2C '+varName+'('+device+','+TYPE+');';
  }
  else
  {
    Blockly.Arduino.definitions_['include_SoftI2CMaster'] = '#include <SoftI2CMaster.h>';
    Blockly.Arduino.definitions_['include_LiquidCrystal_SoftI2C'] = '#include <LiquidCrystal_SoftI2C.h>';
    Blockly.Arduino.definitions_['var_declare_LiquidCrystal_SoftI2C_' + varName] = 'LiquidCrystal_SoftI2C ' + varName + '(' + device + ',' + TYPE + ',' + SCL + ',' + SDA + ');';
  }
  Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
  Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';    
  return '';
};

Blockly.Arduino.ywrobotmodule_LCD1602_init3 = function () {
  var varName = this.getFieldValue('VAR');
  var TYPE = this.getFieldValue('TYPE');
  var dropdown_pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin3 = Blockly.Arduino.valueToCode(this, 'PIN3', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin4 = Blockly.Arduino.valueToCode(this, 'PIN4', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin5 = Blockly.Arduino.valueToCode(this, 'PIN5', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_pin6 = Blockly.Arduino.valueToCode(this, 'PIN6', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_LiquidCrystal'] = '#include <LiquidCrystal.h>';
  Blockly.Arduino.definitions_['var_declare_LiquidCrystal' + varName] = 'LiquidCrystal ' + varName + '(' + dropdown_pin1 + ',' + dropdown_pin2 + ',' + dropdown_pin3 + ',' + dropdown_pin4 + ',' + dropdown_pin5 + ',' + dropdown_pin6 + ');';
  Blockly.Arduino.setups_['setup_lcd_begin_' + varName] = varName + '.begin('+TYPE+');';

  return '';
};


var tm1637_DIO;
var tm1637_CLK;

Blockly.Arduino.ywrobotmodule_TM1637_init = function () {
 tm1637_CLK = this.getFieldValue('CLK');
 tm1637_DIO = this.getFieldValue('DIO');
 Blockly.Arduino.definitions_['include_SevenSegmentTM1637'] = '#include <SevenSegmentTM1637.h>';
 Blockly.Arduino.definitions_['var_declare_SevenSegmentTM1637'] = 'SevenSegmentTM1637  display(' + tm1637_CLK + ',' + tm1637_DIO + ');';
 Blockly.Arduino.setups_['setup_ display.begin()'] = ' display.begin();';
 return '';
};

Blockly.Arduino.ywrobotmodule_TM1637_displyPrint = function () {
    //var Speed = Blockly.Arduino.valueToCode(this, 'Speed', Blockly.Arduino.ORDER_ATOMIC);
    var aVALUE = Blockly.Arduino.valueToCode(this, 'VALUE', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'display.print(' + aVALUE + ');' + '\n';
    return code;
  };
    
  Blockly.Arduino.ywrobotmodule_TM1637_dian = function () {
    var _WEI_ =this.getFieldValue("_WEI_");
    var code ='display.printRaw(B10000000,' + _WEI_ + ');' + '\n';
    return  code;
  };


  Blockly.Arduino.ywrobotmodule_TM1637_displayTime = function () {
    Blockly.Arduino.definitions_['include_SevenSegmentExtended'] = '#include <SevenSegmentExtended.h>';
    Blockly.Arduino.definitions_['var_declare_SevenSegmentTM1637'] = 'SevenSegmentExtended  display(' + tm1637_CLK + ',' + tm1637_DIO + ');';
    var hour = Blockly.Arduino.valueToCode(this, 'hour', Blockly.Arduino.ORDER_ATOMIC);
    var minute = Blockly.Arduino.valueToCode(this, 'minute', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_stat = Blockly.Arduino.valueToCode(this, 'STAT', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'display.printTime(' + hour + ',' + minute +','+dropdown_stat+');\n';
    return code;
  };

  Blockly.Arduino.ywrobotmodule_TM1637_clearDisplay = function () {
    var code = 'display.clear();\n';
    return code;
  };

  Blockly.Arduino.ywrobotmodule_TM1637_Brightness = function () {
    var BRIGHTNESS = Blockly.Arduino.valueToCode(this, 'Brightness', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'display.setBacklight(' + BRIGHTNESS + ');\n';
    return code;
  };
  
//OLE	D


Blockly.Arduino.ywrobotmodule_oled_init = function() {
  var OLED_TYPE = this.getFieldValue('OLED_TYPE');
  var ROTATION = this.getFieldValue('ROTATION');
  var SDA = this.getFieldValue('SDA');
  var SCL = this.getFieldValue('SCL');
  //var board_type=JSFuncs.getPlatform();
  var board_type ="ESP8266";
  Blockly.Arduino.definitions_['include_U8g2lib'] = '#include <U8g2lib.h>';
  if(board_type.match(RegExp(/AVR/)))
  {
    if(SDA=="SDA"&&SCL=="SCL")
      Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_'+OLED_TYPE+'_F_HW_I2C u8g2('+ROTATION+', U8X8_PIN_NONE);';
    else
      Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_'+OLED_TYPE+'_F_SW_I2C u8g2('+ROTATION+',  '+SCL+', '+SDA+', U8X8_PIN_NONE);';
  }
  else
  {
    if(SDA=="SDA"&&SCL=="SCL")
      Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_'+OLED_TYPE+'_1_HW_I2C u8g2('+ROTATION+', U8X8_PIN_NONE);';
    else
      Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_'+OLED_TYPE+'_1_SW_I2C u8g2('+ROTATION+',  '+SCL+', '+SDA+', U8X8_PIN_NONE);';
  }  
  Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
  Blockly.Arduino.setups_["setup_u8g2"] ='u8g2.begin();';
  var code = '';
  return code;
};

Blockly.Arduino.ywrobotmodule_u8g2_spi_init = function() {
  var U8G2_TYPE_SPI = this.getFieldValue('U8G2_TYPE_SPI');
  var ROTATION = this.getFieldValue('ROTATION');
  var CS = this.getFieldValue('CS');
  var DC = this.getFieldValue('DC');
  var RST = this.getFieldValue('RST');
  Blockly.Arduino.definitions_['include_U8g2lib'] = '#include <U8g2lib.h>';
  Blockly.Arduino.definitions_['include_SPI'] = '#include <SPI.h>';
  Blockly.Arduino.setups_["setup_u8g2"] ='u8g2.begin();';
  Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_'+U8G2_TYPE_SPI+'_1_4W_HW_SPI u8g2('+ROTATION+', '+CS+', '+DC+', '+RST+');';
  var code = '';
  return code;
};

Blockly.Arduino.ywrobotmodule_u8g2_LCD12864_spi_init = function() {
  var ROTATION = this.getFieldValue('ROTATION');
  
  var DC = this.getFieldValue('DC');
  //var RST = this.getFieldValue('RST');
  Blockly.Arduino.definitions_['include_U8g2lib'] = '#include <U8g2lib.h>';
  Blockly.Arduino.definitions_['include_SPI'] = '#include <SPI.h>';
  Blockly.Arduino.setups_["setup_u8g2"] ='u8g2.begin();';
  Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_ST7920_128X64_1_HW_SPI u8g2('+ROTATION+', '+DC+', U8X8_PIN_NONE);';
  var code = '';
  return code;
};

Blockly.Arduino.ywrobotmodule_u8g2_LCD12864_8080_init = function() {
  var ROTATION = this.getFieldValue('ROTATION');
  var DB0 = this.getFieldValue('DB0');
  var DB1 = this.getFieldValue('DB1');
  var DB2 = this.getFieldValue('DB2');
  var DB3 = this.getFieldValue('DB3');
  var DB4 = this.getFieldValue('DB4');
  var DB5 = this.getFieldValue('DB5');
  var DB6 = this.getFieldValue('DB6');
  var DB7 = this.getFieldValue('DB7');
  var ENABLE = this.getFieldValue('ENABLE');
  var DC = this.getFieldValue('DC');
  //var RST = this.getFieldValue('RST');
  Blockly.Arduino.definitions_['include_U8g2lib'] = '#include <U8g2lib.h>';
  Blockly.Arduino.setups_["setup_u8g2"] ='u8g2.begin();';
  Blockly.Arduino.definitions_['var_declare_U8G2'] ='U8G2_ST7920_128X64_1_8080 u8g2('+ROTATION+', '+DB0+', '+DB1+', '+DB2+', '+DB3+', '+DB4+', '+DB5+', '+DB6+', '+DB7+', '+ENABLE+', U8X8_PIN_NONE, '+DC+');';
  var code = '';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_clear = function() {
  var code="u8g2.clearDisplay();\n";
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_face = function() {
 var POS_x = Blockly.Arduino.valueToCode(this, 'POS_X') || '0';
 var POS_y = Blockly.Arduino.valueToCode(this, 'POS_Y') || '0';
 var FACE_IMAGE = this.getFieldValue('FACE_IMAGE');
 var pos=FACE_IMAGE.indexOf(',');
 var varName="FACE_"+FACE_IMAGE.substring(0,pos);
 FACE_IMAGE=FACE_IMAGE.substring(pos+1,FACE_IMAGE.length);
 // YANG use PROGMEM to save the RAM space
 //Blockly.Arduino.definitions_['var_declare' + varName] = 'static unsigned char ' + varName + '[]={' + FACE_IMAGE + ' };\n';
 //var code="u8g2.drawXBM("+POS_x+","+POS_y+",89,64,"+varName+");\n";
 Blockly.Arduino.definitions_['var_declare' + varName] = 'const static unsigned char ' + varName + '[] PROGMEM ={' + FACE_IMAGE + ' };\n';
 var code="u8g2.drawXBMP("+POS_x+","+POS_y+",89,64,"+varName+");\n";
 return code;
};

Blockly.Arduino.ywrobotmodule_oled_icons = function() {
 var POS_x = Blockly.Arduino.valueToCode(this, 'POS_X') || '0';
 var POS_y = Blockly.Arduino.valueToCode(this, 'POS_Y') || '0';
 var ICON_SIZE = this.getFieldValue('ICON_SIZE');
 var ICON_IMAGE = this.getFieldValue('ICON_IMAGE');
 var code = "u8g2.setFont(u8g2_font_open_iconic_all_"+ICON_SIZE+"x_t);\n"
 +"u8g2.drawGlyph("+POS_x+","+POS_y+"+"+ICON_SIZE+"*8,"+ICON_IMAGE+");\n";
 return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawPixel = function() {
  var pos_x = Blockly.Arduino.valueToCode(this, 'POS_X') || '0';
  var pos_y = Blockly.Arduino.valueToCode(this, 'POS_Y') || '0';
  var code = "";
  code += 'u8g2.drawPixel(' + pos_x + ',';
  code += pos_y + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_page = function() {
  var branch = Blockly.Arduino.statementToCode(this, 'DO');
  //branch = branch.replace(/(^\s*)|(\s*$)/g, ""); 
  if (branch) {
    var code = "u8g2.firstPage();"
    +"\ndo"
    +"\n{"
    +"\n"+branch 
    +  "}while(u8g2.nextPage());\n";
    return code;
  }
};

Blockly.Arduino.ywrobotmodule_oled_showBitmap = function() {
  var start_x = Blockly.Arduino.valueToCode(this, 'START_X') || '0';
  var start_y = Blockly.Arduino.valueToCode(this, 'START_Y') || '0';
  var width = Blockly.Arduino.valueToCode(this, 'WIDTH') || '0';
  var height = Blockly.Arduino.valueToCode(this, 'HEIGHT') || '0';
  var data_name = Blockly.Arduino.valueToCode(this, 'bitmap_name', Blockly.Arduino.ORDER_ATOMIC);
  data_name = data_name.replace(/\"/g, ""); 
  var code = "";
  //YANG use PROGMEM to save the RAM space
  //code = 'u8g2.drawXBM(' + start_x + ', ';
  code = 'u8g2.drawXBMP(' + start_x + ', ';
  code += start_y + ', ';
  code += width + ', ';
  code += height + ', ' + data_name + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_define_bitmap_data = function() {
  var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  var text = this.getFieldValue('TEXT');
  //YANG use PROGMEM to save the RAM space
  //Blockly.Arduino.definitions_['var_declare' + varName] = 'static unsigned char ' + varName + '[]={' + text + ' };\n';
  Blockly.Arduino.definitions_['var_declare' + varName] = 'const static unsigned char ' + varName + '[] PROGMEM ={' + text + ' };\n';
  return '';
};

Blockly.Arduino.ywrobotmodule_oled_drawLine = function() {
  var start_x = Blockly.Arduino.valueToCode(this, 'START_X') || '0';
  var start_y = Blockly.Arduino.valueToCode(this, 'START_Y') || '0';
  var end_x = Blockly.Arduino.valueToCode(this, 'END_X') || '0';
  var end_y = Blockly.Arduino.valueToCode(this, 'END_Y') || '0';
  var code = "";
  code = 'u8g2.drawLine(' + start_x + ',';
  code += start_y + ',';
  code += end_x + ',';
  code += end_y + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_draw_Str_Line = function() {
  var start_x = Blockly.Arduino.valueToCode(this, 'START_X') || '0';
  var start_y = Blockly.Arduino.valueToCode(this, 'START_Y') || '0';
  var length = Blockly.Arduino.valueToCode(this, 'LENGTH') || '0';
  var TYPE = this.getFieldValue('TYPE');
  var code = "";
  code = "u8g2.draw" + TYPE + "Line(" + start_x + ',';
  code += start_y + ',';
  code += length + ');\n'; 
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawTriangle = function() {
  var D0_x = Blockly.Arduino.valueToCode(this, 'D0_X') || '0';
  var D0_y = Blockly.Arduino.valueToCode(this, 'D0_Y') || '0';
  var D1_x = Blockly.Arduino.valueToCode(this, 'D1_X') || '0';
  var D1_y = Blockly.Arduino.valueToCode(this, 'D1_Y') || '0';
  var D2_x = Blockly.Arduino.valueToCode(this, 'D2_X') || '0';
  var D2_y = Blockly.Arduino.valueToCode(this, 'D2_Y') || '0';
  var code = "";
  code = 'u8g2.drawTriangle(' + D0_x + ',';
  code += D0_y + ',';
  code += D1_x + ',';
  code += D1_y + ',';
  code += D2_x + ',';
  code += D2_y + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawFrame = function() {
  var D0_x = Blockly.Arduino.valueToCode(this, 'D0_X') || '0';
  var D0_y = Blockly.Arduino.valueToCode(this, 'D0_Y') || '0';
  var Width = Blockly.Arduino.valueToCode(this, 'WIDTH') || '0';
  var Height = Blockly.Arduino.valueToCode(this, 'HEIGHT') || '0';
  var type = this.getFieldValue('TYPE');
  var code = "";
  code = 'u8g2.'+type+'(' + D0_x + ',';
  code += D0_y + ',';
  code += Width + ',';
  code += Height + ');\n';
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawRFrame = function() {
  var D0_x = Blockly.Arduino.valueToCode(this, 'D0_X') || '0';
  var D0_y = Blockly.Arduino.valueToCode(this, 'D0_Y') || '0';
  var Width = Blockly.Arduino.valueToCode(this, 'WIDTH') || '0';
  var Height = Blockly.Arduino.valueToCode(this, 'HEIGHT') || '0';
  var Rauius = Blockly.Arduino.valueToCode(this, 'RADIUS') || '0';
  var type = this.getFieldValue('TYPE');
  var code = "";
  code = 'u8g2.'+type+'(' + D0_x + ',';
  code += D0_y + ',';
  code += Width + ',';
  code += Height + ',';
  code += Rauius + ');\n'; 
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawCircle = function() {
  var D0_x = Blockly.Arduino.valueToCode(this, 'D0_X') || '0';
  var D0_y = Blockly.Arduino.valueToCode(this, 'D0_Y') || '0';
  var Rauius = Blockly.Arduino.valueToCode(this, 'RADIUS') || '0';
  var type = this.getFieldValue('TYPE');
  var opt = this.getFieldValue('OPT');
  var code = "";
  code = 'u8g2.'+type+'(' + D0_x + ',';
  code += D0_y + ',';
  code += Rauius + "," + opt + "); \n";
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_drawEllipse = function() {
  var D0_x = Blockly.Arduino.valueToCode(this, 'D0_X') || '0';
  var D0_y = Blockly.Arduino.valueToCode(this, 'D0_Y') || '0';
  var Rauius_X = Blockly.Arduino.valueToCode(this, 'RADIUS_X') || '0';
  var Rauius_Y = Blockly.Arduino.valueToCode(this, 'RADIUS_Y') || '0';
  var type = this.getFieldValue('TYPE');
  var opt = this.getFieldValue('OPT');
  var code = "";
  code = 'u8g2.'+type+'(' + D0_x + ',';
  code += D0_y + ',';
  code += Rauius_X + "," ;
  code += Rauius_Y + "," + opt + "); \n";
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_print = function() {
  var POS_x = Blockly.Arduino.valueToCode(this, 'POS_X') || '0';
  var POS_y = Blockly.Arduino.valueToCode(this, 'POS_Y') || '0';
  var TEXT = Blockly.Arduino.valueToCode(this, 'TEXT') || '0';
  Blockly.Arduino.setups_["setup_enableUTF8Print"] ='u8g2.enableUTF8Print();\n';
  var code = "";
  code = 'u8g2.setCursor(' + POS_x + ',';
  code += POS_y + "); \n";
  code += "u8g2.print(" + TEXT + "); \n";
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_set_EN_Font = function() {
  var FONT_NAME = this.getFieldValue('FONT_NAME');
  var FONT_SIZE = this.getFieldValue('FONT_SIZE');
  var FONT_STYLE = this.getFieldValue('FONT_STYLE');
  var code = "u8g2.setFont(u8g2_font_"+FONT_NAME+FONT_STYLE+FONT_SIZE+"_tf);\nu8g2.setFontPosTop();\n";
  return code;
};

Blockly.Arduino.ywrobotmodule_oled_set_CN_Font = function() {
  var FONT_NAME = this.getFieldValue('FONT_NAME');
  var FONT_SIZE = this.getFieldValue('FONT_SIZE');
  var code = "u8g2.setFont(u8g2_font_"+FONT_SIZE+FONT_NAME+");\nu8g2.setFontPosTop();\n";
  return code;
};
//OLED背光亮度
Blockly.Arduino.ywrobotmodule_u8g2_setContrast = function() {
  var Contrast= Blockly.Arduino.valueToCode(this, 'Contrast', Blockly.Arduino.ORDER_ATOMIC);
  var code='u8g2.setContrast(' +Contrast+ ');\n';
  return code;
};

//返回UTF8字符串宽度
Blockly.Arduino.ywrobotmodule_get_utf8_width = function() {
    var str= Blockly.Arduino.valueToCode(this, 'str', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['getutf8width'] ='int getUTF8Width(String str) {\n  const char *string_variable = str.c_str();\n  return u8g2.getUTF8Width(string_variable);\n}';
    var code='getUTF8Width(String('+str+'))';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.ywrobotmodule_group_lcd_init=Blockly.Arduino.ywrobotmodule_group_lcd_init2;  

//点阵


  //Max7219点阵初始化
  Blockly.Arduino.ywrobotmodule_MAX7219_init = function() {
   var pin_cs = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
   //var matrixName = this.getFieldValue('matrixName');
   var matrixName = "myMatrix";
   var hDisplays = Blockly.Arduino.valueToCode(this, 'hDisplays', Blockly.Arduino.ORDER_ATOMIC);
   var vDisplays = Blockly.Arduino.valueToCode(this, 'vDisplays', Blockly.Arduino.ORDER_ATOMIC);
   Blockly.Arduino.definitions_['include_SPI'] = '#include <SPI.h>';
   Blockly.Arduino.definitions_['include_Adafruit_GFX'] = '#include <Adafruit_GFX.h>';
   Blockly.Arduino.definitions_['include_Max72xxPanel'] = '#include <Max72xxPanel.h>';
   Blockly.Arduino.definitions_['var_declare_Max72xxPanel'] = 'Max72xxPanel '+matrixName+' = Max72xxPanel('+pin_cs+','+hDisplays+','+ vDisplays+');';
   var code = '';
   return code;
 };

 //点阵屏画点
 Blockly.Arduino.ywrobotmodule_Matrix_DrawPixel = function() {
  var matrixType = this.getFieldValue('TYPE');
  var pos_x = Blockly.Arduino.valueToCode(this, 'XVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
  var pos_y = Blockly.Arduino.valueToCode(this, 'YVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
  //var matrixName = this.getFieldValue('matrixName');
  var matrixName = "myMatrix";
  var dropdown_type = Blockly.Arduino.valueToCode(this, 'STAT', Blockly.Arduino.ORDER_ATOMIC);
  if(matrixType=="HT16K33")
  {
    var code = matrixName + '.drawPixel('+pos_x+',7-'+pos_y+','+dropdown_type+');\n'
  }
  else
  {
    var code =matrixName+ '.drawPixel('+pos_x+','+pos_y+','+dropdown_type+');\n'
  }
  code+= matrixName +'.write();\n';
  return code;
};

//点阵屏滚动显示文本
Blockly.Arduino.ywrobotmodule_Matrix_TEXT = function() {
  var matrixType = this.getFieldValue('TYPE');
  var matrixName = "myMatrix";
  var textString = Blockly.Arduino.valueToCode(this, 'TEXT', Blockly.Arduino.ORDER_ASSIGNMENT);
  var speed = Blockly.Arduino.valueToCode(this, 'Speed', Blockly.Arduino.ORDER_ATOMIC);
  var code = matrixName + '.scrollMessage('+textString+','+speed+');\n'
  return code;
};

//点阵屏显示文本
Blockly.Arduino.ywrobotmodule_Matrix_print = function() {
  var matrixType = this.getFieldValue('TYPE');
  var matrixName = "myMatrix";
  var textString = Blockly.Arduino.valueToCode(this, 'TEXT', Blockly.Arduino.ORDER_ASSIGNMENT);
  var code=matrixName+'.setCursor(0, 0);\n'
  code += matrixName + '.print('+textString+');\n'
  code += matrixName + '.write();\n'
  return code;
};

//点阵屏显示_显示图案
Blockly.Arduino.ywrobotmodule_Matrix_DisplayChar = function() {
  var matrixType = this.getFieldValue('TYPE');
  //var matrixName = this.getFieldValue('matrixName');
  var matrixName = "myMatrix";
  var NO = Blockly.Arduino.valueToCode(this, 'NO', Blockly.Arduino.ORDER_ATOMIC);
  var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'LEDArray', Blockly.Arduino.ORDER_ASSIGNMENT);
  Blockly.Arduino.definitions_['var_declare_LEDArray'] = 'uint8_t  LEDArray[8];';
  var code='';
  code+= 'memcpy_P (&LEDArray, &'+ dotMatrixArray + ', 8);\n';
  code+='for(int index_i=0; index_i<8; index_i++)\n';
  code+='{\n'
//code+='  LEDArray[index_i]='+dotMatrixArray+'[index_i];\n';
code+='  for(int index_j='+(NO)+'*8; index_j<'+ (NO)+'*8+8; index_j++)\n'
  //code+='  for(int index_j=7; index_j>=0; index_j--)\n'
  code+='  {\n'
  code+='    if((LEDArray[index_i]&0x01)>0)\n';
  if(matrixType=="HT16K33")
  {
   code+='      '+  matrixName +'.drawPixel(index_j, index_i,1);\n';
   code+='else\n'+  matrixName +'.drawPixel(index_j, index_i,0);\n';
 }
 else
 {
  code+='     '+  matrixName +'.drawPixel(index_j, 7-index_i,1);\n';
  code+='else\n'+  matrixName +'.drawPixel(index_j, 7-index_i,0);\n';
}
code+='     LEDArray[index_i] = LEDArray[index_i]>>1;\n';
code+='  }  \n'
code+='}\n'
code+= matrixName+'.write();\n'
return code;
};

//点阵屏显示_点阵数组
Blockly.Arduino.ywrobotmodule_Matrix_LedArray = function() {
  var varName = this.getFieldValue('VAR');
  var a = new Array();
  for (var i = 1; i < 9; i++) {
    a[i] = new Array();
    for (var j = 1; j < 9; j++) {
      a[i][9-j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
    }
  }
  var code = '{';
  for (var i = 1; i < 9; i++) {
    var tmp = ""
    for (var j = 1; j < 9; j++) {
      tmp += a[i][j];
    }
    tmp = (parseInt(tmp, 2)).toString(16)
    if (tmp.length == 1) tmp = "0" + tmp;
    code += '0x' + tmp + ((i != 8) ? ',' : '');
  }
  code += '};';
  //Blockly.Arduino.definitions_[varName] = "uint8_t " + varName + "[8]=" + code;
  Blockly.Arduino.definitions_[varName] = "const uint8_t " + varName + "[8] PROGMEM =" + code;
  return [varName, Blockly.Arduino.ORDER_ATOMIC];
};

//点阵设置亮度
Blockly.Arduino.ywrobotmodule_Matrix_Brightness = function () {
 var matrixType = this.getFieldValue('TYPE');
 //var matrixName = this.getFieldValue('matrixName');
 var matrixName = "myMatrix";
 var BRIGHTNESS = Blockly.Arduino.valueToCode(this, 'Brightness', Blockly.Arduino.ORDER_ATOMIC);
 if(matrixType=="HT16K33")
 {
  var code = matrixName +'.setBrightness(' + BRIGHTNESS + ');\n';
}
else
{
  var code = matrixName +'.setIntensity(' + BRIGHTNESS + ');\n';
}
return code;
};

//点阵 全亮/全灭/关闭/开启
Blockly.Arduino.ywrobotmodule_Matrix_fillScreen = function() {
  var matrixType = this.getFieldValue('TYPE');
  //var matrixName = this.getFieldValue('matrixName');
  var matrixName = "myMatrix";
  var FILLSCREEN_TYPE = this.getFieldValue('FILLSCREEN_TYPE');
  var code = matrixName+'.'+FILLSCREEN_TYPE+';\n'
  code+= matrixName+'.write();\n';
  return code;
};

//点阵屏旋转
Blockly.Arduino.ywrobotmodule_Max7219_Rotation = function() {
  //var matrixName = this.getFieldValue('matrixName');
  var matrixName = "myMatrix";
  var dropdown_type = this.getFieldValue('Rotation_TYPE');
  var NO = Blockly.Arduino.valueToCode(this, 'NO', Blockly.Arduino.ORDER_ATOMIC);
  var code = matrixName + '.setRotation('+NO+','+dropdown_type+');\n'
  return code;
};

//点阵屏位置
Blockly.Arduino.ywrobotmodule_Max7219_setPosition = function() {
  //var matrixName = this.getFieldValue('matrixName');
  var matrixName = "myMatrix";
  var NO = Blockly.Arduino.valueToCode(this, 'NO', Blockly.Arduino.ORDER_ATOMIC);
  var X = Blockly.Arduino.valueToCode(this, 'X', Blockly.Arduino.ORDER_ATOMIC);
  var Y = Blockly.Arduino.valueToCode(this, 'Y', Blockly.Arduino.ORDER_ATOMIC);
  var code = matrixName + '.setPosition('+NO+','+X+','+Y+');\n'
  return code;
};

//点阵屏旋转
Blockly.Arduino.ywrobotmodule_HT16K33_Rotation = function() {
 //var matrixName = this.getFieldValue('matrixName');
 var matrixName = "myMatrix";
 var dropdown_type = this.getFieldValue('Rotation_TYPE');
 var code = matrixName + '.setRotation(4-'+dropdown_type+');\n'
 return code;
};

//点阵屏 图案数组
Blockly.Arduino.ywrobotmodule_LedArray = function() {
  var varName = this.getFieldValue('VAR');
  var a = new Array();
  for (var i = 1; i < 9; i++) {
    a[i] = new Array();
    for (var j = 1; j < 9; j++) {
      a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
    }
  }
  var code = '{';
  for (var i = 1; i < 9; i++) {
    var tmp = ""
    for (var j = 1; j < 9; j++) {
      tmp += a[i][j];
    }
    tmp = (parseInt(tmp, 2)).toString(16)
    if (tmp.length == 1) tmp = "0" + tmp;
    code += '0x' + tmp + ((i != 8) ? ',' : '');
  }
  code += '};\n';
  Blockly.Arduino.definitions_[varName] = "byte " + varName + "[]=" + code;
  return [varName, Blockly.Arduino.ORDER_ATOMIC];
};

//点阵屏预设图案
Blockly.Arduino.ywrobotmodule_Matrix_img = function() {
  var dropdown_img_ = this.getFieldValue('img_');
  var code = '"' + dropdown_img_ + '"';
  code = '{';
  for (var i = 0; i < 15; i += 2) {
    code += '0x' + dropdown_img_.substr(i, 2) + ((i != 14) ? ',' : '');
  }
  code += '};\n';
  Blockly.Arduino.definitions_['matrix_img_' + dropdown_img_] = "const uint8_t " + 'matrix_img_' + dropdown_img_ + "[8] PROGMEM=" + code;
  return ['matrix_img_' + dropdown_img_, Blockly.Arduino.ORDER_ATOMIC];
};

