goog.provide('Blockly.Arduino.EPD_152X296');
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 +''
                                    + ');';
  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+');';
  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);';
  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+' );';
  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;
};

