#include "lcd.h"
#include <SPI.h>

SPIClass * vspi = NULL;




/******************************************************************************
      函数说明：LCD写入命令
      入口数据：dat 写入的命令
      返回值：  无
******************************************************************************/
void LCD_WR_REG(uint8_t dat)
{
  digitalWrite(LCD_DS,0);//写命令
  vspi->transfer(dat);
  digitalWrite(LCD_DS,1);//写数据
}

/******************************************************************************
      函数说明：设置起始和结束地址
      入口数据：x1,x2 设置列的起始和结束地址
                y1,y2 设置行的起始和结束地址
      返回值：  无
******************************************************************************/
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{

#if USE_HORIZONTAL==0
    LCD_WR_REG(0x2a);//列地址设置
    vspi->transfer16(x1);
    vspi->transfer16(x2);
    LCD_WR_REG(0x2b);//行地址设置
    vspi->transfer16(y1);
    vspi->transfer16(y2);
    LCD_WR_REG(0x2c);//储存器写
    #elif USE_HORIZONTAL==1
    LCD_WR_REG(0x2a);//列地址设置
    vspi->transfer16(x1);
    vspi->transfer16(x2);
    LCD_WR_REG(0x2b);//行地址设置
    vspi->transfer16(y1+80);
    vspi->transfer16(y2+80);
    LCD_WR_REG(0x2c);//储存器写
    #elif USE_HORIZONTAL==2
    LCD_WR_REG(0x2a);//列地址设置
    vspi->transfer16(x1);
    vspi->transfer16(x2);
    LCD_WR_REG(0x2b);//行地址设置
    vspi->transfer16(y1);
    vspi->transfer16(y2);
    LCD_WR_REG(0x2c);//储存器写
    #elif USE_HORIZONTAL==3
    LCD_WR_REG(0x2a);//列地址设置
    vspi->transfer16(x1+80);
    vspi->transfer16(x2+80);
    LCD_WR_REG(0x2b);//行地址设置
    vspi->transfer16(y1);
    vspi->transfer16(y2);
    LCD_WR_REG(0x2c);//储存器写

#endif


}


void LCD_fillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)
{
  LCD_Address_Set(x,y,width+x-1,height+y-1);
  for(uint16_t i=0;i<height;i++)
  {
    for (uint16_t j=0;j<width;j++)
    {
        vspi->transfer16(color);  
        //delay(100);      
    }
  }

}


/******************************************************************************
      函数说明：LCD清屏函数
      入口数据：无
      返回值：  无
******************************************************************************/
void LCD_Clear(uint16_t Color)
{
  uint16_t i,j;   
  LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
  for(i=0;i<LCD_W;i++)
  {
    for (j=0;j<LCD_H;j++)
    {
        vspi->transfer16(Color);        
    }
  }
}

void LCD_Clear_Rand()
{
  uint16_t i,j;   
  LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
    for(i=0;i<LCD_W;i++)
   {
    for (j=0;j<LCD_H;j++)
      {
          vspi->transfer16(rand());         
      }

    }
}

unsigned int rgb555torgb565(unsigned int c)
{
    c=c&0x7fff;
    unsigned int r=c>>10;
    c=c&0x03ff;
    unsigned int g=c>>5;
    c=c&0x001f;
    unsigned int b=c;

    unsigned int output=0;

    g=g*2;
    output= (r<<11)|(g<<5)|(b);
    return output;

}

void lcd_init()
{

  pinMode(LCD_SDA, OUTPUT);
  pinMode(LCD_SCL, OUTPUT);
  pinMode(LCD_CS, OUTPUT);
  pinMode(LCD_DS, OUTPUT);

  digitalWrite(LCD_SDA,0);
  digitalWrite(LCD_SCL,0);
  digitalWrite(LCD_CS,0);
  digitalWrite(LCD_DS,0);
  
  vspi = new SPIClass(VSPI);
  vspi->begin(LCD_SCL, 0, LCD_SDA, LCD_CS);
  vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  pinMode(vspi->pinSS(), OUTPUT); //HSPI SS
  
  
  digitalWrite(vspi->pinSS(), LOW); //pull SS slow to prep other end for transfer



  
  //************* Start Initial Sequence **********//
//delay(120); 
//LCD_WR_REG(0x01);
//上电后硬件会自己复位
delay(220);
LCD_WR_REG(0x36);
if(USE_HORIZONTAL==0)vspi->transfer(0x00);
else if(USE_HORIZONTAL==1)vspi->transfer(0xC0);
else if(USE_HORIZONTAL==2)vspi->transfer(0x70);
else vspi->transfer(0xA0);

LCD_WR_REG(0x3A); 
vspi->transfer(0x05);

LCD_WR_REG(0xB2);
vspi->transfer(0x0C);
vspi->transfer(0x0C);
vspi->transfer(0x00);
vspi->transfer(0x33);
vspi->transfer(0x33); 

LCD_WR_REG(0xB7); 
vspi->transfer(0x35);  

LCD_WR_REG(0xBB);
vspi->transfer(0x19);

LCD_WR_REG(0xC0);
vspi->transfer(0x2C);

LCD_WR_REG(0xC2);
vspi->transfer(0x01);

LCD_WR_REG(0xC3);
vspi->transfer(0x12);   

LCD_WR_REG(0xC4);
vspi->transfer(0x20);  

LCD_WR_REG(0xC6); 
vspi->transfer(0x0F);    

LCD_WR_REG(0xD0); 
vspi->transfer(0xA4);
vspi->transfer(0xA1);

LCD_WR_REG(0xE0);
vspi->transfer(0xD0);
vspi->transfer(0x04);
vspi->transfer(0x0D);
vspi->transfer(0x11);
vspi->transfer(0x13);
vspi->transfer(0x2B);
vspi->transfer(0x3F);
vspi->transfer(0x54);
vspi->transfer(0x4C);
vspi->transfer(0x18);
vspi->transfer(0x0D);
vspi->transfer(0x0B);
vspi->transfer(0x1F);
vspi->transfer(0x23);

LCD_WR_REG(0xE1);
vspi->transfer(0xD0);
vspi->transfer(0x04);
vspi->transfer(0x0C);
vspi->transfer(0x11);
vspi->transfer(0x13);
vspi->transfer(0x2C);
vspi->transfer(0x3F);
vspi->transfer(0x44);
vspi->transfer(0x51);
vspi->transfer(0x2F);
vspi->transfer(0x1F);
vspi->transfer(0x1F);
vspi->transfer(0x20);
vspi->transfer(0x23);

LCD_WR_REG(0x21); 

LCD_WR_REG(0x11); 
delay(120); 

LCD_WR_REG(0x29); 
}
