#include "lcd.h"
#include "stdlib.h"
#include "font.h" 
#include "usart.h"
#include "delay.h"	
#include "picture.h"


u16 DeviceCode;		   
					 
//画笔颜色,背景颜色
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;   

#if LCD_FAST_IO==1 //快速IO
//写寄存器函数
void LCD_WR_REG(u16 data)
{ 
	LCD_RS_CLR;//写地址 
 	LCD_CS_CLR; 
	DATAOUT((data&0xff00)>>8); 
	LCD_WR_CLR; 
	LCD_WR_SET; 
	DATAOUT((data&0x00ff)); 
	LCD_WR_CLR; 
	LCD_WR_SET; 
 	LCD_CS_SET;   
}
#else//正常IO
//写寄存器函数
void LCD_WR_REG(u16 data)
{ 
	LCD_RS=0;//写地址  
 	LCD_CS=0; 
	DATAOUT((data&0xff00)>>8); 
	LCD_WR=0; 
	LCD_WR=1; 
	DATAOUT((data&0x00ff)); 
	LCD_WR=0; 
	LCD_WR=1; 
 	LCD_CS=1;   
} 	
#endif	

//写寄存器
void LCD_WriteReg(u16 LCD_Reg, u16 LCD_RegValue)
{	
	LCD_WR_REG(LCD_Reg);  
	LCD_WR_DATA(LCD_RegValue);	    		 
}	   


//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
	LCD_WR_REG(R34);
}	 
  

void LCD_Window_Set(u8 x1,u8 y1,u8 x2,u8 y2)
{	
  LCD_WR_REG(0x0037);LCD_WR_DATA(x1); // windows address 
	LCD_WR_REG(0x0036);LCD_WR_DATA(x2);
	LCD_WR_REG(0x0039);LCD_WR_DATA(y1);    
	LCD_WR_REG(0x0038);LCD_WR_DATA(y2);	
	LCD_WR_REG(0x0020);  //HS
	LCD_WR_DATA(x1); 		 
	LCD_WR_REG(0x0021);  //VS
	LCD_WR_DATA(y1); 	 				 
}

//画点
//x:0~239
//y:0~319
//POINT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y)
{
	LCD_Window_Set(x,y,x+1,y+1);
	LCD_WR_REG(R34);//开始写入GRAM
	LCD_WR_DATA(POINT_COLOR);  
} 	 

void LCD_Point(u16 x,u16 y,u16 color)
{
	LCD_Window_Set(x,y,x+1,y+1);
	LCD_WR_REG(R34);//开始写入GRAM
	LCD_WR_DATA(color);  
} 

//初始化lcd
//该初始化函数可以初始化各种ILI93XX液晶,但是其他函数是基于ILI9320的!!!
//在其他型号的驱动芯片上没有测试! 
void LCD_Init(void)
{ 	
 	GPIO_InitTypeDef GPIO_InitStructure;

 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); 
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;	   //GPIO_Pin_10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOD, &GPIO_InitStructure); //GPIOC	
	GPIO_SetBits(GPIOD,GPIO_Pin_All);



	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;	   //GPIO_Pin_10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIOC	
	GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOC	
	GPIO_Init(GPIOA, &GPIO_InitStructure); //GPIOC	
	GPIO_SetBits(GPIOD,GPIO_Pin_All);

	/*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;	//  
	GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB 
	GPIO_SetBits(GPIOB,GPIO_Pin_All); */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;	//  
	GPIO_Init(GPIOE, &GPIO_InitStructure); //GPIOB 
	GPIO_SetBits(GPIOE,GPIO_Pin_All);  
	GPIO_ResetBits(GPIOE,GPIO_Pin_All);  

		 //*ST7775R INIT
    LCD_WriteReg(0x0001, 0x011C); // set SS and NL bit
    LCD_WriteReg(0x0002, 0x0100); // set 1 line inversion
    LCD_WriteReg(0x0003, 0x1030); // set GRAM write direction and BGR=1,??16BIT 65K?MDT1=0,MDT0=0
    LCD_WriteReg(0x0008, 0x0808); // set BP and FP
    LCD_WriteReg(0x000C, 0x0000); // RGB interface setting R0Ch=0x0110 for RGB 18Bit and R0Ch=0111for

    LCD_WriteReg(0x000F, 0x0e01); // Set frame rate
   
//*************Power On sequence ****************//
    delay_ms(50); //     Delay 50ms
    LCD_WriteReg(0x0010, 0x0A00); // Set SAP,DSTB,STB
    LCD_WriteReg(0x0011, 0x1038); // Set APON,PON,AON,VCI1EN,VC
    delay_ms(50); //     Delay 50ms
    LCD_WriteReg(0x00ff, 0x0003);    //
    LCD_WriteReg(0x00b0, 0x1411);    //Set VCOM 1d
    LCD_WriteReg(0x00b1, 0x0202);    //GVCL/GVDD voltage setting
    LCD_WriteReg(0x00b2, 0x0313);  //VCL voltage setting

//------------------------ Set GRAM area  set window --------------------------------//
    LCD_WriteReg (0x0030, 0x0000);//???????
    LCD_WriteReg (0x0031, 0x00db);
    LCD_WriteReg (0x0032, 0x0000);
    LCD_WriteReg (0x0033, 0x0000);//??????
    LCD_WriteReg (0x0034, 0x00db);
    LCD_WriteReg (0x0035, 0x0000);//R34H,R35H???????
    LCD_WriteReg (0x0036, 0x00AF);
    LCD_WriteReg (0x0037, 0x0000);
    LCD_WriteReg (0x0038, 0x00DB);
    LCD_WriteReg (0x0039, 0x0000);
    delay_ms(20);
   
    LCD_WriteReg(0x00ff, 0x0003);
//    WriteRegister(0x00b0, 0x1d01);
// ----------- Adjust the Gamma Curve ----------//
     LCD_WriteReg(0x0050, 0x0000);
     LCD_WriteReg(0x0051, 0x0300);
     LCD_WriteReg(0x0052, 0x0103);
     LCD_WriteReg(0x0053, 0x2011);
     LCD_WriteReg(0x0054, 0x0703);
     LCD_WriteReg(0x0055, 0x0000);
     LCD_WriteReg(0x0056, 0x0400);
     LCD_WriteReg(0x0057, 0x0107);
     LCD_WriteReg(0x0058, 0x2011);
     LCD_WriteReg(0x0059, 0x0703);
     delay_ms(50); //     Delay 50ms
     LCD_WriteReg(0x0020, 0x0000); // Set GRAM Address
     LCD_WriteReg(0x0021, 0x0000); // Set GRAM Address
     LCD_WriteReg(0x0007, 0x1017);
		  
	LCD_LED=1;//点亮背光	 
	LCD_Clear(RED);
}  		  
  
//清屏函数
//Color:要清屏的填充色
void LCD_Clear(u16 Color)
{
	u32 index=0; 
	
	LCD_Window_Set(0,0,LCD_W-1,LCD_H-1); 
	LCD_WriteRAM_Prepare();     //开始写入GRAM	 	  
	for(index=0;index<38720;index++)
	{
		LCD_WR_DATA(Color);    
	}
}  

void rect(u16 x0,u16 y0, u16 w , u16 h ,u16 Color)
{
	u32 index=w*h; 
	
	LCD_Window_Set(x0,y0,x0+w-1,y0+h-1); 
	LCD_WriteRAM_Prepare();     //开始写入GRAM	 	  
	/*for(index=0;index<38720;index++)
	{
		LCD_WR_DATA(Color);    
	}*/
	while(index--)
	{
		LCD_WR_DATA(Color);
	}
} 

void Paint()
{
	u16*p;
	u16 i;
	LCD_Window_Set(0,0,170-1,220-1); 
	LCD_WriteRAM_Prepare();     //开始写入GRAM	 
	p = (u16*)&gImage_ok;
	i = 170*220;
	while(i)
	{
		LCD_WR_DATA(*p);
		p++;
		i--;
	}
}

void CheckColor(u16 Color,u8 x1,u8 y1, u8 x2, u8 y2)
{
	u32 index=0; 
	u32 number;
	LCD_Window_Set(x1,y1,x2,y2); 
	LCD_WriteRAM_Prepare();     //开始写入GRAM	
	number = (x2-x1)*(y2-y1);
	for(index=0;index<number;index++)
	{
		LCD_WR_DATA(Color);    
	}
}


//画线
//x1,y1:起点坐标
//x2,y2:终点坐标  
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
{
	u16 t; 
	int xerr=0,yerr=0,delta_x,delta_y,distance; 
	int incx,incy,uRow,uCol; 

	delta_x=x2-x1; //计算坐标增量 
	delta_y=y2-y1; 
	uRow=x1; 
	uCol=y1; 
	if(delta_x>0)incx=1; //设置单步方向 
	else if(delta_x==0)incx=0;//垂直线 
	else {incx=-1;delta_x=-delta_x;} 
	if(delta_y>0)incy=1; 
	else if(delta_y==0)incy=0;//水平线 
	else{incy=-1;delta_y=-delta_y;} 
	if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
	else distance=delta_y; 
	for(t=0;t<=distance+1;t++ )//画线输出 
	{  
		LCD_DrawPoint(uRow,uCol);//画点 
		xerr+=delta_x ; 
		yerr+=delta_y ; 
		if(xerr>distance) 
		{ 
			xerr-=distance; 
			uRow+=incx; 
		} 
		if(yerr>distance) 
		{ 
			yerr-=distance; 
			uCol+=incy; 
		} 
	}  
}    
//画矩形
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
{
	LCD_DrawLine(x1,y1,x2,y1);
	LCD_DrawLine(x1,y1,x1,y2);
	LCD_DrawLine(x1,y2,x2,y2);
	LCD_DrawLine(x2,y1,x2,y2);
}
//在指定位置画一个指定大小的圆
//(x,y):中心点
//r    :半径
void Draw_Circle(u16 x0,u16 y0,u8 r)
{
	int a,b;
	int di;
	a=0;b=r;	  
	di=3-(r<<1);             //判断下个点位置的标志
	while(a<=b)
	{
		LCD_DrawPoint(x0-b,y0-a);             //3           
		LCD_DrawPoint(x0+b,y0-a);             //0           
		LCD_DrawPoint(x0-a,y0+b);             //1       
		LCD_DrawPoint(x0-b,y0-a);             //7           
		LCD_DrawPoint(x0-a,y0-b);             //2             
		LCD_DrawPoint(x0+b,y0+a);             //4               
		LCD_DrawPoint(x0+a,y0-b);             //5
		LCD_DrawPoint(x0+a,y0+b);             //6 
		LCD_DrawPoint(x0-b,y0+a);             
		a++;
		//使用Bresenham算法画圆     
		if(di<0)di +=4*a+6;	  
		else
		{
			di+=10+4*(a-b);   
			b--;
		} 
		LCD_DrawPoint(x0+a,y0+b);
	}
} 
//在指定位置显示一个字符
//x:0~234
//y:0~308
//num:要显示的字符:" "--->"~"
//size:字体大小 12/16
//mode:叠加方式(1)还是非叠加方式(0)
//在指定位置显示一个字符
//x:0~234
//y:0~308
//num:要显示的字符:" "--->"~"
//size:字体大小 12/16
//mode:叠加方式(1)还是非叠加方式(0)
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{  
#if USE_HORIZONTAL==1
#define MAX_CHAR_POSX 312
#define MAX_CHAR_POSY 232 
#else     
#define MAX_CHAR_POSX 232
#define MAX_CHAR_POSY 312
#endif 
    u8 temp;
    u8 pos,t;
	u16 x0=x;
	u16 colortemp=POINT_COLOR;      
    if(x>MAX_CHAR_POSX||y>MAX_CHAR_POSY)return;	    
	//设置窗口		   
	num=num-' ';//得到偏移后的值
	if(!mode) //非叠加方式
	{
		for(pos=0;pos<size;pos++)
		{
			if(size==12)temp=asc2_1206[num][pos];//调用1206字体
			else temp=asc2_1608[num][pos];		 //调用1608字体
			for(t=0;t<size/2;t++)
		    {                 
		        if(temp&0x01)POINT_COLOR=colortemp;
				else POINT_COLOR=BACK_COLOR;
				LCD_DrawPoint(x,y);	
				temp>>=1; 
				x++;
		    }
			x=x0;
			y++;
		}	
	}else//叠加方式
	{
		for(pos=0;pos<size;pos++)
		{
			if(size==12)temp=asc2_1206[num][pos];//调用1206字体
			else temp=asc2_1608[num][pos];		 //调用1608字体
			for(t=0;t<size/2;t++)
		    {                 
		        if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//画一个点     
		        temp>>=1; 
		    }
		}
	}
	POINT_COLOR=colortemp;	    	   	 	  
}   
//m^n函数
u32 mypow(u8 m,u8 n)
{
	u32 result=1;	 
	while(n--)result*=m;    
	return result;
}			 
//显示2个数字
//x,y :起点坐标	 
//len :数字的位数
//size:字体大小
//color:颜色
//num:数值(0~4294967295);	 
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
{         	
	u8 t,temp;
	u8 enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/mypow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
				continue;
			}else enshow=1; 
		 	 
		}
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0); 
	}
} 
//显示2个数字
//x,y:起点坐标
//size:字体大小
//mode:模式	0,填充模式;1,叠加模式
//num:数值(0~99);	 
void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len,u8 size,u8 mode)
{         	
	u8 t,temp;						   
	for(t=0;t<len;t++)
	{
		temp=(num/mypow(10,len-t-1))%10;
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode); 
	}
} 
//显示字符串
//x,y:起点坐标  
//*p:字符串起始地址
//用16字体
void LCD_ShowString(u16 x,u16 y,const u8 *p)
{         
    while(*p!='\0')
    {       
        if(x>MAX_CHAR_POSX){x=0;y+=16;}
        if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear(WHITE);}
        LCD_ShowChar(x,y,*p,16,0);
        x+=8;
        p++;
    }  
}






























