goog.provide('Blockly.Arduino.HELLO_STEM');
goog.require('Blockly.Arduino');


Blockly.Arduino.forBlock.EPD_Pin_Init = function() {
  var value_SCK = Blockly.Arduino.valueToCode(this, 'SCK', Blockly.Arduino.ORDER_ATOMIC);
  var value_MOSI = Blockly.Arduino.valueToCode(this, 'MOSI', Blockly.Arduino.ORDER_ATOMIC);
  var value_CS = Blockly.Arduino.valueToCode(this, 'CS', Blockly.Arduino.ORDER_ATOMIC);
  var value_RST = Blockly.Arduino.valueToCode(this, 'RST', Blockly.Arduino.ORDER_ATOMIC);
  var value_DC = Blockly.Arduino.valueToCode(this, 'DC', Blockly.Arduino.ORDER_ATOMIC);
  var value_BUSY = Blockly.Arduino.valueToCode(this, 'BUSY', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_'+'DEV_Config.h'] = '#include "DEV_Config.h"';
  Blockly.Arduino.setups_['NAME'] = 'EPD_SetPin('+value_SCK+','
                                    + ''+value_MOSI+','
                                    + ''+value_CS+','
                                    + ''+value_RST+','
                                    + ''+value_DC +','
                                    + ''+value_BUSY +''
                                    + ');\r\n';
  var code = '';
  return code;
};

Blockly.Arduino.forBlock.EPD_Init = function() {
  var dropdown_Rotate_value = this.getFieldValue('Rotate_value');
  var dropdown_Color_value = this.getFieldValue('Color_value');
  Blockly.Arduino.definitions_['include_'+'DEV_Config.h'] = '#include "DEV_Config.h"';
  Blockly.Arduino.definitions_['include_'+'EPD.h'] = '#include "EPD.h"';
  Blockly.Arduino.definitions_['include_'+'GUI_Paint.h'] = '#include "GUI_Paint.h"';
  Blockly.Arduino.definitions_['include_'+'stdlib.h'] = '#include <stdlib.h>';
  Blockly.Arduino.setups_['DEV_Module_Init'] = 'DEV_Module_Init();';
  Blockly.Arduino.setups_['EPD_2IN66_Init'] = 'EPD_2IN66_Init();';
  Blockly.Arduino.setups_['DEV_Delay_ms'] = 'DEV_Delay_ms(500);';
  Blockly.Arduino.setups_['BlackImage'] = 'UBYTE *BlackImage;';
  Blockly.Arduino.setups_['Imagesize'] = 'UWORD Imagesize = ((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1)) * EPD_2IN66_HEIGHT;';
  Blockly.Arduino.setups_['BlackImage_Init'] = 'if((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {\n'
                                               + '      printf("Failed to apply for black memory...\\r\\n");\n'
                                               + '      while(1);\n'
                                               + '    }';
  Blockly.Arduino.setups_['NewImage'] = 'Paint_NewImage(BlackImage, EPD_2IN66_WIDTH, EPD_2IN66_HEIGHT, '+dropdown_Rotate_value+', '+dropdown_Color_value+');';
  Blockly.Arduino.setups_['Arr_clear'] = 'Paint_Clear('+dropdown_Color_value+');\r\n';
  var code = '';
  return code;
};

Blockly.Arduino.forBlock.EPD_Display = function() {
  var statements_function = Blockly.Arduino.statementToCode(this, 'function');
  var code = statements_function
             + 'EPD_2IN66_Display(Paint.Image);\r\n';
  return code;
};


Blockly.Arduino.forBlock.Paint_DrawNum = function() {
  var value_Num_x = Blockly.Arduino.valueToCode(this, 'Num_x', Blockly.Arduino.ORDER_ATOMIC);
  var value_Num_y = Blockly.Arduino.valueToCode(this, 'Num_y', Blockly.Arduino.ORDER_ATOMIC);
  var value_Num_Value = Blockly.Arduino.valueToCode(this, 'Num_Value', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_Num_word_size = this.getFieldValue('Num_word_size');
  var dropdown_Num_color1 = this.getFieldValue('Num_color1');
  var dropdown_Num_color2 = this.getFieldValue('Num_color2');
  var code = 'Paint_DrawNum('+value_Num_x +', '+value_Num_y +', '+value_Num_Value +', '+dropdown_Num_word_size +','+dropdown_Num_color1 +','+dropdown_Num_color2+' );\r\n';
  return code;
};


Blockly.Arduino.forBlock.Paint_DrawString_EN = function() {
  var value_String_x = Blockly.Arduino.valueToCode(this, 'String_x', Blockly.Arduino.ORDER_ATOMIC);
  var value_String_y = Blockly.Arduino.valueToCode(this, 'String_y', Blockly.Arduino.ORDER_ATOMIC);
  var value_String_Value = Blockly.Arduino.valueToCode(this, 'String_Value', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_String_word_size = this.getFieldValue('String_word_size');
  var dropdown_String_color1 = this.getFieldValue('String_color1');
  var dropdown_String_color2 = this.getFieldValue('String_color2');
  var code = 'Paint_DrawString_EN('+value_String_x +', '+value_String_y +', '+value_String_Value +', '+dropdown_String_word_size +','+dropdown_String_color1 +','+dropdown_String_color2+' );\r\n';
  return code;
};


Blockly.Arduino.forBlock.Paint_DrawImage = function() {
  var value_Image_x = Blockly.Arduino.valueToCode(this, 'Image_x', Blockly.Arduino.ORDER_ATOMIC);
  var value_Image_y = Blockly.Arduino.valueToCode(this, 'Image_y', Blockly.Arduino.ORDER_ATOMIC);
  var value_Image_Width = Blockly.Arduino.valueToCode(this, 'Image_Width', Blockly.Arduino.ORDER_ATOMIC);
  var value_Image_height = Blockly.Arduino.valueToCode(this, 'Image_height', Blockly.Arduino.ORDER_ATOMIC);
  var value_Image_value = Blockly.Arduino.valueToCode(this, 'Image_value', Blockly.Arduino.ORDER_ATOMIC);
  value_Image_value = value_Image_value.replace(/\"/g, "");
  var code = "";
  code = 'Paint_DrawImage('+value_Image_value +','+value_Image_x +','+value_Image_y +','+value_Image_Width +','+value_Image_height +');\r\n';
  return code;
};



Blockly.Arduino.forBlock.Paint_Clear = function() {
  var dropdown_Clear_Color = this.getFieldValue('Clear_Color');
  var code = 'Paint_Clear('+dropdown_Clear_Color +');\r\n';
  return code;
};


Blockly.Arduino.forBlock.EPD_2IN66_Partial_Display = function() {
  var statements_Partial_Display = Blockly.Arduino.statementToCode(this, 'Partial_Display');
  var code = statements_Partial_Display
			 +'EPD_2IN66_Partial_Display(Paint.Image);\r\n';
  return code;
};

Blockly.Arduino.forBlock.Hello_STEM_GPS = function() {
  var value_Tx_PIN = Blockly.Arduino.valueToCode(this, 'Tx_PIN', Blockly.Arduino.ORDER_ATOMIC);
  var value_Rx_PIN = Blockly.Arduino.valueToCode(this, 'Rx_PIN', Blockly.Arduino.ORDER_ATOMIC);
  var value_Bave = Blockly.Arduino.valueToCode(this, 'Bave', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['define_'+'TinyGPS++'] = '#include <TinyGPS++.h>';
  Blockly.Arduino.definitions_['include_'+'TinyGPSPlus'] = '\nTinyGPSPlus gps;';
  Blockly.Arduino.definitions_['include_'+'Hardware'] = '\nHardwareSerial gps_ss(2);';
  Blockly.Arduino.setups_['GPS_INIT'] = 'gps_ss.begin('+value_Bave +', SERIAL_8N1, '+value_Tx_PIN +', '+value_Rx_PIN +');  ';
  var code = '';
  return code;
};


Blockly.Arduino.forBlock.GPS_GET_DATA = function() {
  var code = 'gps_ss.available()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock.GPS_DATA_Proc = function() {
  var code = 'gps.encode(gps_ss.read())';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.forBlock.GPS_DATA_SUCC = function() {
  var dropdown_xuanze = this.getFieldValue('xuanze');
  var code = 'gps.'+dropdown_xuanze +'.isValid()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock.GPS_Specific_DATA = function() {
  var dropdown_SP_DATA = this.getFieldValue('SP_DATA');
  var code = 'gps.'+dropdown_SP_DATA +'';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


Blockly.Arduino.forBlock.SU03T_Init = function () {
    var rx = Blockly.Arduino.valueToCode(this, "Rx_PIN", Blockly.Arduino.ORDER_ATOMIC) || '16';
    var tx = Blockly.Arduino.valueToCode(this, "Tx_PIN", Blockly.Arduino.ORDER_ATOMIC) || '17';


    Blockly.Arduino.definitions_['su03t_all_code'] = `
volatile int yuyin;

#define mySerial Serial2

#define UART_SEND_MAX      32
#define UART_MSG_HEAD_LEN  2
#define UART_MSG_FOOT_LEN  2
#define U_MSG_bozhensgshu      1
#define U_MSG_boxiaoshu      2
#define U_MSG_bobao1      3
#define U_MSG_bobao2      4
#define U_MSG_bobao3      5
#define U_MSG_bobao4      6
#define U_MSG_bobao5      7
#define U_MSG_bobao6      8
#define U_MSG_bobao7      9
#define U_MSG_bobao8      10
#define U_MSG_bobao9      11
#define U_MSG_bobao10      12
#define U_MSG_bobao11      13
#define U_MSG_bobao12      14
#define U_MSG_bobao13      15
#define U_MSG_bobao14      16
#define U_MSG_bobao15      17
#define U_MSG_bobao16      18
#define U_MSG_bobao17      19
#define U_MSG_bobao18      20
#define U_MSG_bobao19      21
#define U_MSG_bobao20      22
#define U_MSG_bobao21      23
#define U_MSG_bobao22      24
#define U_MSG_bobao23      25
#define U_MSG_bobao24      26
#define U_MSG_bobao25      27
#define U_MSG_bobao26      28
#define U_MSG_bobao27      29
#define U_MSG_bobao28      30
#define U_MSG_bobao29      31
#define U_MSG_bobao30      32
#define U_MSG_bobao31      33
#define U_MSG_bobao32      34
#define U_MSG_bobao33      35
#define U_MSG_bobao34      36

typedef union {
  double d_double;
  int d_int;
  unsigned char d_ucs[8];
  char d_char;
  unsigned char d_uchar;
  unsigned long d_long;
  short d_short;
  float d_float;
} uart_param_t;


void _uart_send_impl(unsigned char* buff, int len) {
  for (int i = 0; i < len; i++) {
    mySerial.write(*buff++);
  }
}

const unsigned char g_uart_send_head[] = { 0xaa, 0x55 };
const unsigned char g_uart_send_foot[] = { 0x55, 0xaa };

void _int16_to_int32(uart_param_t* param) {
  if (sizeof(int) >= 4) return;
  unsigned long value = param->d_long;
  unsigned long sign = (value >> 15) & 1;
  unsigned long v = value;
  if (sign) v = 0xFFFF0000 | value;
  uart_param_t p;
  p.d_long = v;
  param->d_ucs[0] = p.d_ucs[0];
  param->d_ucs[1] = p.d_ucs[1];
  param->d_ucs[2] = p.d_ucs[2];
  param->d_ucs[3] = p.d_ucs[3];
}

void _float_to_double(uart_param_t* param) {
  if (sizeof(double) >= 8) return;
  unsigned long value = param->d_long;
  unsigned long sign = value >> 31;
  unsigned long M = value & 0x007FFFFF;
  unsigned long e = ((value >> 23) & 0xFF) - 127 + 1023;
  uart_param_t p1;
  p1.d_long = ((sign & 1) << 31) | ((e & 0x7FF) << 20) | (M >> 3);
  param->d_ucs[0] = 0;
  param->d_ucs[1] = 0;
  param->d_ucs[2] = 0;
  param->d_ucs[3] = 0;
  param->d_ucs[4] = p1.d_ucs[0];
  param->d_ucs[5] = p1.d_ucs[1];
  param->d_ucs[6] = p1.d_ucs[2];
  param->d_ucs[7] = p1.d_ucs[3];
}

void _uart_bobao1() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao1;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao2() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao2;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao3() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao3;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao4() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao4;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao5() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao5;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao6() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao6;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao7() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao7;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao8() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao8;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao22() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao22;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao23() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao23;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao24() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao24;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao25() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao25;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao26() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao26;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao27() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao27;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}


void _uart_bobao28() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao28;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao29() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao29;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao30() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao30;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao32() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao32;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao33() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao33;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

void _uart_bobao34() {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bobao34;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 3] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 5);
}

`;

    Blockly.Arduino.setups_['su03t_setup'] =
        'Serial.begin(9600);\n' +
        '\tmySerial.begin(9600, SERIAL_8N1, ' + rx + ', ' + tx + ');\n' +
        '\tyuyin = 0;';

var code = `
if (mySerial.available() > 0) {
  yuyin = mySerial.read();
  Serial.println(yuyin, HEX);
}
`;
    return code;
};




Blockly.Arduino.forBlock.SU03T_SHIBIE = function () {
  var FIELD_ZHILING = this.getFieldValue("ZHILING");
  var code = 'yuyin == ' + FIELD_ZHILING;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};



Blockly.Arduino.forBlock.SU03T_ClearShiBie = function () {
    return "yuyin = 0;\n";
};

Blockly.Arduino.forBlock.SU03T_BOBAOXIAOSHU = function () {
    var VALUE_INPUT_NAME = Blockly.Arduino.valueToCode(this, "NAME", Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['_uart_boxiaoshu'] = `
void _uart_boxiaoshu(float xiaoshu) {
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_boxiaoshu;
  param.d_double = xiaoshu;
  _float_to_double(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  buff[7] = param.d_ucs[4];
  buff[8] = param.d_ucs[5];
  buff[9] = param.d_ucs[6];
  buff[10] = param.d_ucs[7];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
    buff[i + 11] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 13);
}
`;

    var code = `_uart_boxiaoshu(${VALUE_INPUT_NAME});\n`;
    return code;
};


Blockly.Arduino.forBlock.SU03T_BOBAOZHENGSHU = function () {
    var VALUE_INPUT_BOBAOZHENGSHU = Blockly.Arduino.valueToCode(this, "BOBAOZHENGSHU", Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['_uart_bozhensgshu'] = `
void _uart_bozhensgshu(int zhengshu) {
  uart_param_t param;
    int i = 0;
    unsigned char buff[UART_SEND_MAX] = {0};
    for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
        buff[i + 0] = g_uart_send_head[i];
    }
    buff[2] = U_MSG_bozhensgshu;
    param.d_int = zhengshu;
    _int16_to_int32(&param);
    buff[3] = param.d_ucs[0];
    buff[4] = param.d_ucs[1];
    buff[5] = 0;
    buff[6] = 0;
    for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
        buff[i + 7] = g_uart_send_foot[i];
    }
    _uart_send_impl(buff, 9);
}
`;
    var code = `_uart_bozhensgshu(${VALUE_INPUT_BOBAOZHENGSHU});\n`;
    return code;
};

Blockly.Arduino.forBlock.SU03T_BOBAO = function () {
    var FIELD_NRIRONG = this.getFieldValue("NRIRONG");
    return "_uart_bobao" + FIELD_NRIRONG + "();\n";
};


Blockly.Arduino.forBlock.HS_F26_Voice = function() {
  var value_F26_PIN = Blockly.Arduino.valueToCode(this, 'F26_PIN', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_Voice = this.getFieldValue('Voice');
  var value_Voice_Delay = Blockly.Arduino.valueToCode(this, 'Voice_Delay', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['hs_f26_send_data'] = 
`void send_data(int addr, int pin) {
  int i;
  digitalWrite(pin, LOW);
  delay(3);
  for (i = 0; i < 8; i++) {
    digitalWrite(pin, HIGH);
    if (addr & 1) {
      delayMicroseconds(2400);
      digitalWrite(pin, LOW);
      delayMicroseconds(800);
    } else {
      delayMicroseconds(800);
      digitalWrite(pin, LOW);
      delayMicroseconds(2400);
    }
    addr >>= 1;
  }
  digitalWrite(pin, HIGH);
}`;

  Blockly.Arduino.setups_['setup_hs_f26_pin_' + value_F26_PIN] = 
`  pinMode(${value_F26_PIN}, OUTPUT);`;

  var code = `send_data(${dropdown_Voice}, ${value_F26_PIN});\n`;
  code += `delay(${value_Voice_Delay});\n`;
  
  return code;
};


Blockly.Arduino.forBlock.S23_Init = function() {
  var value_S23_PIN = Blockly.Arduino.valueToCode(this, 'S23_PIN', Blockly.Arduino.ORDER_ATOMIC) || '2';
  var statements_Have_signal = Blockly.Arduino.statementToCode(this, 'Have_signal');
  var statements_NO_signal = Blockly.Arduino.statementToCode(this, 'NO_signal');

  Blockly.Arduino.definitions_['define_IR_PROTOCOL_TYPE'] = 
    '#include <IRremote.h>\n\n' +
    'const String IR_PROTOCOL_TYPE[] = {\n' +
    '  "UNKNOWN",\n' +
    '  "PULSE_DISTANCE",\n' +
    '  "PULSE_WIDTH",\n' +
    '  "DENON",\n' +
    '  "DISH",\n' +
    '  "JVC",\n' +
    '  "LG",\n' +
    '  "LG2",\n' +
    '  "NEC",\n' +
    '  "PANASONIC",\n' +
    '  "KASEIKYO",\n' +
    '  "KASEIKYO_JVC",\n' +
    '  "KASEIKYO_DENON",\n' +
    '  "KASEIKYO_SHARP",\n' +
    '  "KASEIKYO_MITSUBISHI",\n' +
    '  "RC5",\n' +
    '  "RC6",\n' +
    '  "SAMSUNG",\n' +
    '  "SHARP",\n' +
    '  "SONY",\n' +
    '  "ONKYO",\n' +
    '  "APPLE",\n' +
    '  "BOSEWAVE",\n' +
    '  "LEGO_PF",\n' +
    '  "MAGIQUEST",\n' +
    '  "WHYNTER"\n' +
    '};';

  var irName = 'irrecv_' + value_S23_PIN;
  Blockly.Arduino.definitions_['include_' + irName] = 'IRrecv ' + irName + '(' + value_S23_PIN + ');';

  Blockly.Arduino.setups_['setup_' + irName] = irName + '.enableIRIn();';

  var code = 'if (' + irName + '.decode()) {\n' +
             '  struct IRData *pIrData = &' + irName + '.decodedIRData;\n' +
             '  long ir_item = pIrData->decodedRawData;\n' +
             '  String irProtocol = IR_PROTOCOL_TYPE[pIrData->protocol];\n' +
             '  Serial.print("IR TYPE:" + irProtocol + "\\tVALUE:");\n' +
             '  Serial.println(ir_item, HEX);\n' +
             statements_Have_signal + 
             '  ' + irName + '.resume();\n' +
             '} else {\n' +
             statements_NO_signal +
             '}\n';

  return code;
};

Blockly.Arduino.forBlock.F25_Init = function() {
  // 获取积木块的输入值
  var value_PIN_1 = Blockly.Arduino.valueToCode(this, 'PIN_1', Blockly.Arduino.ORDER_ATOMIC);
  var value_PIN_2 = Blockly.Arduino.valueToCode(this, 'PIN_2', Blockly.Arduino.ORDER_ATOMIC);
  var value_PIN_3 = Blockly.Arduino.valueToCode(this, 'PIN_3', Blockly.Arduino.ORDER_ATOMIC);
  var value_PIN_4 = Blockly.Arduino.valueToCode(this, 'PIN_4', Blockly.Arduino.ORDER_ATOMIC);
  var value_Step_count = Blockly.Arduino.valueToCode(this, 'Step_count', Blockly.Arduino.ORDER_ATOMIC);
  var value_Rotational_speed = Blockly.Arduino.valueToCode(this, 'Rotational_speed', Blockly.Arduino.ORDER_ATOMIC);

  Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>';

  Blockly.Arduino.definitions_['var_mystepper'] = 
    'Stepper mystepper(' + value_Step_count + ', ' + 
    value_PIN_1 + ', ' + value_PIN_2 + ', ' + 
    value_PIN_3 + ', ' + value_PIN_4 + ');';

  Blockly.Arduino.setups_['setup_mystepper'] = 
    '  mystepper.setSpeed(' + value_Rotational_speed + ');';

  var code = '';
  return code;
};

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


Blockly.Arduino.forBlock.F25_Set = function() {
  var value_Move_Step = Blockly.Arduino.valueToCode(this, 'Move_Step', Blockly.Arduino.ORDER_ATOMIC);
  var code = `mystepper.step(${value_Move_Step});\n`;
  return code;
};


Blockly.Arduino.forBlock.VTX316_Init = function () {
    var value_Tx_Pin = Blockly.Arduino.valueToCode(this, 'Tx_Pin', Blockly.Arduino.ORDER_ATOMIC) || '17';
    var value_Rx_Pin = Blockly.Arduino.valueToCode(this, 'Rx_Pin', Blockly.Arduino.ORDER_ATOMIC) || '16';
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    Blockly.Arduino.setups_['VTX316_Init'] = 'VTX316_Init(' + value_Rx_Pin + ',' + value_Tx_Pin + ');\n';
    var code = '';
    return code;
};

Blockly.Arduino.forBlock.VTX316_YUYINBOBAO = function () {
    var value_YUYINBOBAO = Blockly.Arduino.valueToCode(this, 'YUYINBOBAO', Blockly.Arduino.ORDER_ATOMIC) || '""';
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_BOBAO(' + value_YUYINBOBAO + ');\n';
    return code;
};

Blockly.Arduino.forBlock.VTX316_YINLIANG = function () {
    var number_YINLIANG = this.getFieldValue('YINLIANG');
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_YIN_LIANG(' + number_YINLIANG + ');\n';
    return code;
};

Blockly.Arduino.forBlock.VTX316_ZANTING = function () {
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_ZANTING();\n';
    return code;
};

Blockly.Arduino.forBlock.VTX316_HUIFU = function () {
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_HUIFU();\n';
    return code;
};

Blockly.Arduino.forBlock.TTX_YINXIAO = function () {
    var dropdown_YINXIAO = this.getFieldValue('YINXIAO');
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_BOBAO(' + '"' + dropdown_YINXIAO + '"' + ');\n';
    return code;
};

Blockly.Arduino.forBlock.TTX_FAYANREN = function () {
    var dropdown_FAYANREN = this.getFieldValue('FAYANREN');
    Blockly.Arduino.definitions_['VTX316'] = '#include "VTX316.h"';
    var code = 'Voice_FAYANREN(' + dropdown_FAYANREN + ');\n';
    return code;
};


Blockly.Arduino.forBlock.HC164D_init = function (_, generator) {
    var text_SMG_name = this.getFieldValue('SMG_name');
    var value_CLK_Pin = generator.valueToCode(this, 'CLK_Pin', generator.ORDER_ATOMIC);
    var value_DIO_Pin = generator.valueToCode(this, 'DIO_Pin', generator.ORDER_ATOMIC);
    generator.definitions_['HC164D_CLK_Init' + text_SMG_name] = 'unsigned char ' + text_SMG_name + '_CLK_Pin=' + value_CLK_Pin + ';';
    generator.definitions_['HC164D_DIO_Init' + text_SMG_name] = 'unsigned char ' + text_SMG_name + '_DIO_Pin=' + value_DIO_Pin + ';';
    generator.definitions_['HC164D_init' + text_SMG_name] = 'char HC164D_mylist[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};';
    generator.setups_['HC164D_CLK_Mode_Init' + text_SMG_name] = 'pinMode(' + text_SMG_name + '_CLK_Pin, OUTPUT);';
    generator.setups_['HC164D_DIO_Mode_Init' + text_SMG_name] = 'pinMode(' + text_SMG_name + '_DIO_Pin, OUTPUT);';
    var code = '';
    return code;
};

Blockly.Arduino.forBlock.HC164D_display = function (_, generator) {
    var text_SMG_name = this.getFieldValue('SMG_name');
    var value_value = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = 'shiftOut(' + text_SMG_name + '_DIO_Pin, ' + text_SMG_name + '_CLK_Pin, MSBFIRST, HC164D_mylist[' + value_value + ']);'
        + 'delay(1000);\n'
    return code;
};

Blockly.Arduino.forBlock.SMG_value = function (_, generator) {
    var dropdown_SMG_value = this.getFieldValue('SMG_value');
    var code = dropdown_SMG_value;
    return [code, generator.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock.HC595A_init = function (_, generator) {
    var text_SMG_name = this.getFieldValue('SMG_name');
    var value_RCK_Pin = generator.valueToCode(this, 'RCK_Pin', generator.ORDER_ATOMIC);
    var value_SCK_Pin = generator.valueToCode(this, 'SCK_Pin', generator.ORDER_ATOMIC);
    var value_DS_Pin = generator.valueToCode(this, 'DS_Pin', generator.ORDER_ATOMIC);
    generator.definitions_['HC595A_RCK_Init' + text_SMG_name] = 'unsigned char ' + text_SMG_name + '_RCK_Pin=' + value_RCK_Pin + ';';
    generator.definitions_['HC595A_SCK_Init' + text_SMG_name] = 'unsigned char ' + text_SMG_name + '_SCK_Pin=' + value_SCK_Pin + ';';
    generator.definitions_['HC595A_DS_Init' + text_SMG_name] = 'unsigned char ' + text_SMG_name + '_DS_Pin=' + value_DS_Pin + ';';
    generator.definitions_['HC595A_init' + text_SMG_name] = 'char HC595A_mylist[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};';

    generator.setups_['HC595A_RCK_Mode_Init' + text_SMG_name] = 'pinMode(' + text_SMG_name + '_RCK_Pin, OUTPUT);';
    generator.setups_['HC595A_SCK_Mode_Init' + text_SMG_name] = 'pinMode(' + text_SMG_name + '_SCK_Pin, OUTPUT);';
    generator.setups_['HC595A_DS_Mode_Init' + text_SMG_name] = 'pinMode(' + text_SMG_name + '_DS_Pin, OUTPUT);';
    var code = '';
    return code;
};

Blockly.Arduino.forBlock.HC595A_display = function (_, generator) { 
    var text_SMG_name = this.getFieldValue('SMG_name');
    var value_value = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = 'digitalWrite(' + text_SMG_name + '_RCK_Pin,LOW);\n'
        + 'shiftOut(' + text_SMG_name + '_DS_Pin, ' + text_SMG_name + '_SCK_Pin, MSBFIRST, ~HC595A_mylist[' + value_value + ']);\n'
        + 'digitalWrite(' + text_SMG_name + '_RCK_Pin,HIGH);\n'
    return code;
};


Blockly.Arduino.forBlock.yuyingmokuai = function (_, generator) {
    var value_Pin = generator.valueToCode(this, 'Pin', generator.ORDER_ATOMIC);
    var dropdown_Entry = this.getFieldValue('Entry');
    var value_Delay = generator.valueToCode(this, 'Delay', generator.ORDER_ATOMIC);
    generator.definitions_['Define'] = 'void send_data(int addr) {\nint i;\ndigitalWrite(' + value_Pin + ', LOW);\ndelay(3); //>2ms\nfor (i = 0; i < 8; i++) {\ndigitalWrite(' + value_Pin + ', HIGH);\nif (addr & 1) {\ndelayMicroseconds(2400); //>2400us\ndigitalWrite(' + value_Pin + ', LOW);\ndelayMicroseconds(800);\n} //>800us\nelse {\ndelayMicroseconds(800); //>800us\ndigitalWrite(' + value_Pin + ', LOW);\ndelayMicroseconds(2400);\n} //>2400us\naddr >>= 1;\n}\ndigitalWrite(' + value_Pin + ', HIGH);\n}\n';
    generator.setups_['Init+' + value_Pin + ''] = 'pinMode(' + value_Pin + ', OUTPUT);\n';
    var code = 'send_data(' + dropdown_Entry + '); //volume control 0xE0-E7;\ndelay(' + value_Delay + ');\n';
    return code;
};


Blockly.Arduino.forBlock.TDS_Init = function () {
    var VALUE_INPUT_PIN = Blockly.Arduino.valueToCode(this, "PIN", Blockly.Arduino.ORDER_ATOMIC) || '34';
    Blockly.Arduino.definitions_['include_GravityTDS'] = '#include "GravityTDS.h"';
    Blockly.Arduino.definitions_['tds_global'] = 'const int TdsSensorPin = ' + VALUE_INPUT_PIN + ';\n'
        + 'GravityTDS gravityTds;\n';
    Blockly.Arduino.setups_['tds_setup'] = 'gravityTds.setPin(TdsSensorPin);\n'
        + '\tgravityTds.setAref(3.3);\n'
        + '\tgravityTds.setAdcRange(4096);\n'
        + '\tgravityTds.begin();';
    return "";
};

Blockly.Arduino.forBlock.TDS_Get = function () {
    Blockly.Arduino.definitions_['tds_get_value'] = 'float getTdsValue() {\n'
        + '  gravityTds.update();\n'
        + '  return gravityTds.getTdsValue();\n'
        + '}';
    return ["getTdsValue()", Blockly.Arduino.ORDER_ATOMIC];
};
