#include "lcd.h"
#include "stdlib.h"
#include "font.h" 
#include "usart.h"
#include "delay.h"	 
   
					 
//画笔颜色,背景颜色
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;  
u16 DeviceCode;	 

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

//写寄存器
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{	
	LCD_WR_REG(LCD_Reg);  
	LCD_WR_DATA(LCD_RegValue);	    		 
}	   
//读寄存器
u16 LCD_ReadReg(u8 LCD_Reg)
{										   
	u16 t;
	LCD_WR_REG(LCD_Reg);  //写入要读的寄存器号  
	GPIOB->CRL=0X88888888; //PB0-7  上拉输入
	GPIOB->CRH=0X88888888; //PB8-15 上拉输入
	GPIOB->ODR=0XFFFF;    //全部输出高
#if LCD_FAST_IO==1 //快速IO
	LCD_RS_SET;
	LCD_CS_CLR;
	//读取数据(读寄存器时,并不需要读2次)
	LCD_RD_CLR;
	delay_us(5);//FOR 8989,延时5us					   
	LCD_RD_SET;
	t=DATAIN;  
	LCD_CS_SET; 
#else
	LCD_RS=1;
	LCD_CS=0;
	//读取数据(读寄存器时,并不需要读2次)
	LCD_RD=0;					   
	LCD_RD=1;
	t=DATAIN;  
	LCD_CS=1; 
#endif  
	GPIOB->CRL=0X33333333; //PB0-7  上拉输出
	GPIOB->CRH=0X33333333; //PB8-15 上拉输出
	GPIOB->ODR=0XFFFF;    //全部输出高
	return t;  
}  

//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
	if(DeviceCode==0x164)LCD_WR_REG(R34);
	else LCD_WR_REG(0x2c);//开始写入GRAM
}	 
//LCD写GRAM
void LCD_WriteRAM(u16 RGB_Code)
{							    
	LCD_WR_DATA(RGB_Code);//写十六位GRAM
}	 


void LCD_Window_Set(u8 x1,u8 y1,u8 x2,u8 y2)
{	
  if(DeviceCode==0x164)
	{
		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); 
	}else
	{
		LCD_WR_REG(0x2a); 
		LCD_WR_DATA(x1>>8); 
		LCD_WR_DATA(x1&0XFF);	
		
		LCD_WR_REG(0x2b); 
		LCD_WR_DATA(y1>>8); 
		LCD_WR_DATA(y1&0XFF);
	}	
}
//画点
//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_SetCursor(x,y);//设置光标位置 
	if(DeviceCode==0x164)LCD_WR_REG(R34);//开始写入GRAM
	else LCD_WR_REG(0x2c);//开始写入GRAM
	LCD_WR_DATA(POINT_COLOR); 
} 	 
//初始化lcd
//该初始化函数可以初始化各种ILI93XX液晶,但是其他函数是基于ILI9320的!!!
//在其他型号的驱动芯片上没有测试! 
void LCD_Init(void)
{  
     										 
	GPIO_InitTypeDef GPIO_InitStructure;

 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE); 
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_14;	   //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_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_14);
	//GPIO_ResetBits(GPIOD,GPIO_Pin_3);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
	GPIO_Init(GPIOE, &GPIO_InitStructure); //GPIOB 
	GPIO_SetBits(GPIOE,GPIO_Pin_All);
 
//	delay_ms(50); // delay 50 ms 
//	LCD_WriteReg(0x0000,0x0001);
//	delay_ms(50); // delay 50 ms 
//	DeviceCode = LCD_ReadReg(0x0000);   
//	printf(" LCD ID:%x\n",DeviceCode); //打印LCD ID  

//if(DeviceCode==0x164)
//{
	/*Power up sequence*/
	DeviceCode = 0x164;
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x001A);
LCD_WR_REG(0x0012);
LCD_WR_DATA(0x3121);
LCD_WR_REG(0x0013);
LCD_WR_DATA(0x006C);
LCD_WR_REG(0x0014);
LCD_WR_DATA(0x4249);
LCD_WR_REG(0x0010);
LCD_WR_DATA(0x0800);
delay_ms(10);
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x011A);
delay_ms(10);
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x031A);
delay_ms(10);
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x071A);
delay_ms(10);
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x0F1A);
delay_ms(20);
LCD_WR_REG(0x0011);
LCD_WR_DATA(0x0F3A);
delay_ms(30);
     /*Initialization set sequence*/
LCD_WR_REG(0x0001);
LCD_WR_DATA(0x011C);
LCD_WR_REG(0x0002);
LCD_WR_DATA(0x0100);
LCD_WR_REG(0x0003);
LCD_WR_DATA(0x1030);
LCD_WR_REG(0x0007);
LCD_WR_DATA(0x0000);
LCD_WR_REG(0x0008);
LCD_WR_DATA(0x0808);
LCD_WR_REG(0x000B);
LCD_WR_DATA(0x1100);
LCD_WR_REG(0x000C);
LCD_WR_DATA(0x0000);
LCD_WR_REG(0x000F);
LCD_WR_DATA(0x1401);
LCD_WR_REG(0x0015);
LCD_WR_DATA(0x0000);

LCD_WR_REG(0x0038);
LCD_WR_DATA(0x00DB);
LCD_WR_REG(0x0039);
LCD_WR_DATA(0x0000);
LCD_WR_REG(0x0050);
LCD_WR_DATA(0x0001);//Gamma
LCD_WR_REG(0x0051);
LCD_WR_DATA(0x020B);
LCD_WR_REG(0x0052);
LCD_WR_DATA(0x0805);
LCD_WR_REG(0x0053);
LCD_WR_DATA(0x0404);
LCD_WR_REG(0x0054);
LCD_WR_DATA(0x0C0C);
LCD_WR_REG(0x0055);
LCD_WR_DATA(0x000C);
LCD_WR_REG(0x0056);
LCD_WR_DATA(0x0101);
LCD_WR_REG(0x0057);
LCD_WR_DATA(0x0400);
LCD_WR_REG(0x0058);
LCD_WR_DATA(0x1108);
LCD_WR_REG(0x0059);
LCD_WR_DATA(0x050C);

LCD_WR_REG(0x0036);
LCD_WR_DATA(0x00AF);//H end
LCD_WR_REG(0x0037);
LCD_WR_DATA(0x0000);//H start
LCD_WR_REG(0x0038);
LCD_WR_DATA(0x00DB);//V end
LCD_WR_REG(0x0039);
LCD_WR_DATA(0x0000);//V start

LCD_WR_REG(0x000F);
LCD_WR_DATA(0x0B01);
LCD_WR_REG(0x0007);
LCD_WR_DATA(0x0016);
delay_ms(2);
LCD_WR_REG(0x0007);
LCD_WR_DATA(0x0017);
LCD_WR_REG(0x0022);
delay_ms(10);
//}else
//{
//LCD_WR_REG(0x01);              //NT3916  NT35601
//    delay_ms(5);
//    LCD_WR_REG(0x11);
//    delay_ms(150);

//    LCD_WR_REG(0xDE);  LCD_WR_DATA(0xAA);
//                       LCD_WR_DATA(0x55);
//                       LCD_WR_DATA(0x15);
//    LCD_WR_REG(0xC2);  LCD_WR_DATA(0x00);
//                       LCD_WR_DATA(0x04);
//    LCD_WR_REG(0xC0);  LCD_WR_DATA(0x03);
//                       LCD_WR_DATA(0x02);
//    LCD_WR_REG(0xC1);  LCD_WR_DATA(0x07);
//    LCD_WR_REG(0xC3);  LCD_WR_DATA(0x00);
//                       LCD_WR_DATA(0x07);
//    LCD_WR_REG(0xC4);  LCD_WR_DATA(0x00);
//                       LCD_WR_DATA(0x04);
//    LCD_WR_REG(0xC5);  LCD_WR_DATA(0x4c);
//                       LCD_WR_DATA(0x5B);
//    LCD_WR_REG(0xC7);  LCD_WR_DATA(0x9F);
//    LCD_WR_REG(0xB1);  LCD_WR_DATA(0x0C);
//                       LCD_WR_DATA(0x04);
//    LCD_WR_REG(0xB4);  LCD_WR_DATA(0x00);
//    LCD_WR_REG(0xF3);  LCD_WR_DATA(0x02);
//    LCD_WR_REG(0xF9);  LCD_WR_DATA(0x08);
//                       LCD_WR_DATA(0x19);
//                          
//    LCD_WR_REG(0xE0);  LCD_WR_DATA(0x20);
//                       LCD_WR_DATA(0x63);
//                       LCD_WR_DATA(0x71);
//                       LCD_WR_DATA(0x00);
//                       LCD_WR_DATA(0x80);
//                       LCD_WR_DATA(0x05);
//                       LCD_WR_DATA(0x06);
//                       LCD_WR_DATA(0x94);
//    LCD_WR_REG(0xE1);  LCD_WR_DATA(0x56);
//		LCD_WR_REG(0x33);
//                       LCD_WR_DATA(0x76);
//                       LCD_WR_DATA(0x06);
//                       LCD_WR_DATA(0xE9);
//                       LCD_WR_DATA(0x04);
//                       LCD_WR_DATA(0x00);
//                       LCD_WR_DATA(0x1F);                      

//    LCD_WR_REG(0x36);  LCD_WR_DATA(0x48);
//    LCD_WR_REG(0x3A);  LCD_WR_DATA(0x05);
//    LCD_WR_REG(0x2C);
//    LCD_WR_REG(0x29);
//}
		
	LCD_LED=1;//点亮背光	 
	LCD_Clear(WHITE);
}  		  
  
//清屏函数
//Color:要清屏的填充色
void LCD_Clear(u16 Color)
{
	u32 index=0;      
	LCD_Window_Set(0,0,LCD_W-1,LCD_H-1); //LCD_SetCursor(0x00,0x0000);//设置光标位置 
	LCD_WriteRAM_Prepare();     //开始写入GRAM	 	  
	for(index=0;index<38720;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 212
#define MAX_CHAR_POSY 168 
#else     
#define MAX_CHAR_POSX 168
#define MAX_CHAR_POSY 212
#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++;
    }  
}

void LCD_WR_DATA(u16 data)
{
	LCD_RS_SET;
	LCD_CS_CLR;
	DATAOUT(data<<8);
	LCD_LE_SET;
	LCD_LE_CLR;
	DATAOUT(data);
	LCD_WR_CLR;
	LCD_WR_SET;
	LCD_CS_SET;
}
