
#include "lcd_func.h"
#include "lcd_drv.h"

#include "flash_drv.h"
#include "rtp_drv.h"

#include "eport_drv.h"
#include "wav_dac.h"
#include "wav_pit.h"
#include "ccm_drv.h" 

#include "uart_drv.h"
#include "pwm_drv.h"


#include "delay.h"
#include "sys.h"
#include "font.h"


//----------------------------------------------------------------------2D??-------------------------------------------------------------------
/*??*/
//x,y:??
//color:??
void LT268_TFT_DrawPoint(UINT16 x,UINT16 y,UINT16 color)
{
	LT268_TFT_SetWindows(x,y,1,1);
	LCD_RS_Data;
	LCD_CS_RESET;
	
//	TFT_Data_Tran(((UINT8*)&color),2);
	TFT_Data_Tran2(color,1);

	LCD_CS_SET;	
}

//x1,y1:??1
//x2,y2:??2
//color:??
void LT268_TFT_DrawLine(UINT16 x1,UINT16 y1,UINT16 x2,UINT16 y2,UINT16 color)
{
	UINT16 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++ )//????
	{
		LT268_TFT_DrawPoint(uRow,uCol,color);//??
		xerr+=delta_x ;
		yerr+=delta_y ;
		if(xerr>distance)
		{
			xerr-=distance;
			uRow+=incx;
		}
		if(yerr>distance)
		{
			yerr-=distance;
			uCol+=incy;
		}
	}
}

void _draw_circle_8(int xc, int yc, int x, int y, UINT16 color)
{
	LT268_TFT_DrawPoint(xc + x, yc + y, color);
	LT268_TFT_DrawPoint(xc - x, yc + y, color);
	LT268_TFT_DrawPoint(xc + x, yc - y, color);
	LT268_TFT_DrawPoint(xc - x, yc - y, color);
	LT268_TFT_DrawPoint(xc + y, yc + x, color);
	LT268_TFT_DrawPoint(xc - y, yc + x, color);
	LT268_TFT_DrawPoint(xc + y, yc - x, color);
	LT268_TFT_DrawPoint(xc - y, yc - x, color);
}

/*?????*/
//xc,yc:??
//r:???
//color:??
void LT268_TFT_DrawCircle(UINT16 xc, UINT16 yc, UINT16 r, UINT16 color)
{
	int x = 0, y = r, d;
	d = 3 - 2 * r;

	while (x <= y) {
		_draw_circle_8(xc, yc, x, y, color);
		if (d < 0) {
			d = d + 4 * x + 6;
		} else {
			d = d + 4 * (x - y) + 10;
			y--;
		}
		x++;
	}
}

/*????????*/
//x,y:??
//Width:??
//High:??
//color:??
void LT268_TFT_FillColor(UINT16 x,UINT16 y,UINT16 Width,UINT16 High,UINT16 color)
{
//	UINT32 i=0;

	LT268_TFT_SetWindows(x,y,Width,High);
	//LCD_RD_SET;
	LCD_RS_Data;
	LCD_CS_RESET;

	TFT_Data_Tran2(color,Width*High);
	
	LCD_CS_SET;
}


void LT268_TFT_DrawPoint_Buffer(UINT16 x,UINT16 y,UINT16 color,UINT16 canvas_w)
{
	if(y*2*canvas_w+2*x+1 < MAX_PIXELSIZE - 1)
	{
		m_PngData[y*2*canvas_w+2*x+1] = color>>8;
		m_PngData[y*2*canvas_w+2*x] = color;
	}
}

/*????????*/
//x,y:??
//Width:??
//High:??
//color:??
void LT268_TFT_FillColor_Buffer(UINT16 x,UINT16 y,UINT16 Width,UINT16 High,UINT16 color,UINT16 canvas_w)
{
	UINT32 j,k;
	
//	if(x==0&&y==0&&Width==canvas_w)
//	{
//		for(j=0;j<Width*High;j++)
//			p16_m_PngData[j] = color;
//	}
//	else
//	{
		for(j=0;j<High;j++)
		{
			for(k=0;k<Width;k++)
			{
				p16_m_PngData[(y+j)*canvas_w+x+k] = color;
			}
		}
//	}
	
}

/*?????*/
//x1,y1:??1
//x2,y2:??2
//color:??
void LT268_TFT_DrawSquare_Fill(UINT16 x1,UINT16 y1,UINT16 x2,UINT16 y2,UINT16 color)
{
	UINT16 temp=0;
	if(y1>y2)
	{
		temp=y1;
		y1=y2;
		y2=temp;
	}
	if(x1>x2)
	{
		temp=x1;
		x1=x2;
		x2=temp;
	}
	LT268_TFT_FillColor(x1,y1,x2-x1+1,y2-y1+1,color);
}

void LT_ReadFlash_DMA(UINT8* pBuffer,UINT32 ReadAddr,UINT16 NumByteToRead)
{
	/*
	if(NumByteToRead==0) return;

	if(Flash_Type!=2)		
	{
		//NorFlash_DMA_Read_Init(2,8,NumByteToRead);				
		NorFlash_DMA_Read(pBuffer,ReadAddr,NumByteToRead);//双线DMA
		NorFlash_DMA_Wait();
	}
	else
	{
		UINT16 page,endpage,column,Fdata,datalen,i;
			
		page   = (ReadAddr/2048);						//起始（页地址）
		endpage= ((ReadAddr+NumByteToRead-1)/2048);		//终止（页地址）
		column = (ReadAddr%2048);						//(列地址)
		Fdata = 2048-column;
		//printf("page endpage column Fdata %d %d %d %d \r\n",page,endpage,column,Fdata);

		if(page==endpage)  
		{
			NandFlash_DMA_Read(page, column, pBuffer, NumByteToRead);			
			NandFlash_DMA_Wait();
		}
		else
		{
			if(column!=0)
			{
				NandFlash_DMA_Read(page++, column , pBuffer ,Fdata);
				NandFlash_DMA_Wait();
			}
			else 	Fdata=0; 
			datalen=NumByteToRead-Fdata;
				
			for(i=0;datalen>2048;i++)  
			{
				datalen-=2048;
				NandFlash_DMA_Read(page++, 0 , &pBuffer[Fdata+i*2048] ,2048);
				NandFlash_DMA_Wait();
			}
			if(datalen!=0)
			{
				NandFlash_DMA_Read(endpage, 0 , &pBuffer[Fdata+i*2048] ,datalen); 
				NandFlash_DMA_Wait();
			}
		}
	DelayUS(1);
	}
	*/
}



UINT8 LT268_pic_to_buffer                
(
UINT16 buff_x, UINT16 buff_y, UINT16 buff_w, UINT8 *m_Data,    
UINT16 pic_x,  UINT16 pic_y,  UINT16 pic_w,  UINT32 pic_addr,
UINT16 Width,  UINT16 High,   UINT8  Cflag 
)
{
	UINT16 i,j;
	UINT32 Pic_addr=0;
	UINT16 count,count_1;
	
	if((Width*High*2)>MAX_PIXELSIZE)  return 0;	
	
	count=Width*2/DMA_LEN2;
	count_1=(Width*2)%DMA_LEN2;
	
	if(Cflag==0)
	{
		for(j=0;j<High;j++)
		{
			Pic_addr=pic_addr+pic_x*2+(pic_y+j)*pic_w*2;
			for(i=0;i<count;i++)
			{
				LT_ReadFlash_DMA(&m_PngData[(buff_x+buff_y*buff_w+j*buff_w+DMA_LEN2*i)*2] ,Pic_addr+i*DMA_LEN2, DMA_LEN2);
			}
			if(count_1!=0)
			{
				LT_ReadFlash_DMA(&m_PngData[(buff_x+buff_y*buff_w+j*buff_w+DMA_LEN2*i)*2] ,Pic_addr+i*DMA_LEN2, count_1);
			}
		}
	}
	
	return 0xff;
}

/************************将缓存区中的数据显示到屏幕 ***************************************/
UINT8 LT268_buffer_to_TFT      
(
UINT16 buff_x, UINT16 buff_y, UINT16 buff_w, UINT8* m_Data,
UINT16 TFT_x,  UINT16 TFT_y,  UINT16 Width,  UINT16 High
)
{	
	
	UINT32 j;
	
	LT268_TFT_SetWindows(TFT_x,TFT_y,Width,High);
	LCD_RS_Data;
	LCD_CS_RESET;
	
	if((buff_x==0)&&(buff_w==Width))
	{
		TFT_Data_Tran(&m_PngData[buff_y*buff_w*2],Width*High*2);
	}
	else
	{
		for(j=0;j<High;j++)
		{
			TFT_Data_Tran(&m_PngData[buff_x*2+buff_y*buff_w*2+j*buff_w*2],Width*2);
		}		
	}
		
	LCD_CS_SET;
	
	return 1;

}

//向TFT连续写入数据（RGB565）
void TFT_Data_Tran(UINT8* Tx_Addr, UINT32 length)
{
#if LCD_INTERFACE	==	LT268_SPI
	u32 i,temp;
	
	if(length==0) return;
	for(i=0;i<length;i+=2)
	{
		temp=Tx_Addr[i];
		Tx_Addr[i]=Tx_Addr[i+1];
		Tx_Addr[i+1]=temp;
	}
	TFTSPI->SPIDMACR |= SPISR_TXDMAE_MASK|SPISR_RXDMAE_MASK;	//使能SPI TX RX的DMA
	
	TFTspi_dma_control->DMA_CONFIG = 1;
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_SADDR = (UINT32)Tx_Addr;
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_DADDR = (UINT32)&TFTSPI->SPIDR;
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CTRL = SIEC|DNOCHG|M2P_DMA|SWIDTH_B|DWIDTH_B;	//8bit
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CTRL_HIGH = length;    								 				//最大4095
	//TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CTRL = SIEC|DNOCHG|M2P_DMA|SWIDTH_HW|DWIDTH_HW; //16bit
	//TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CTRL_HIGH = length/2;     											//最大4095
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CFG = (HS_SEL_SRC_SOFT);
	TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CFG_HIGH = TFTSPI_DST_TX;
	TFTspi_dma_control->DMA_CHEN |= CHANNEL_WRITE_ENABLE(TFTSPI_CHNUM)|CHANNEL_ENABLE(TFTSPI_CHNUM);

	while(TFTspi_dma_control->DMA_CHEN & CHANNEL_STAT(TFTSPI_CHNUM))  {  ; }	
	TFTspi_dma_control->DMA_CHEN &=~(CHANNEL_WRITE_ENABLE(TFTSPI_CHNUM)|CHANNEL_ENABLE(TFTSPI_CHNUM));
	//TFTspi_dma_channel[TFTSPI_CHNUM]->DMA_CFG &= ~(1<<8);
	
	TFTSPI->SPIDMACR &= (~(SPISR_TXDMAE_MASK|SPISR_RXDMAE_MASK));
#endif
	
#if LCD_INTERFACE == LT268_PPI
	UINT32  m=0;
	UINT8 valHigh,valLow;
	
	valHigh = LCD_WR_rPIN_H; 
	valLow  = LCD_WR_rPIN_L;
	
	for(m=1;m<=length/2;m++)
	{
#if LCD_DATABIT == LT268_8DATABIT
			LCD_data_pin_L = Tx_Addr[2*m-1];
			LCD_WR_wPIN = valLow; 			  
			LCD_WR_wPIN = valHigh;	
			LCD_data_pin_L = Tx_Addr[2*m-2];
			LCD_WR_wPIN = valLow; 			   
			LCD_WR_wPIN = valHigh;	
#endif
#if LCD_DATABIT == LT268_16DATABIT
			LCD_data_pin_H = Tx_Addr[2*m-1];
			LCD_data_pin_L = Tx_Addr[2*m-2];
			LCD_WR_wPIN = valLow; 			   
			LCD_WR_wPIN = valHigh;	
#endif
	}
#endif
}

//向TFT连续写入一种颜色（RGB565）
void TFT_Data_Tran2(UINT16 Tx_Addr, UINT32 length)
{
#if LCD_INTERFACE ==	LT268_SPI
	u32 i;
	
	for(i=0;i<length;i++)
	{
		LCD_SPI_ReadWriteByte(Tx_Addr>>8);
	  LCD_SPI_ReadWriteByte(Tx_Addr);
	}
#endif
	
#if LCD_INTERFACE == LT268_PPI
	UINT32  m=0;
	UINT8 valHigh,valLow;
	
	valHigh = LCD_WR_rPIN_H; 
	valLow  = LCD_WR_rPIN_L;

	for(m=0;m<length;m++)
	{
#if LCD_DATABIT == LT268_8DATABIT
			LCD_data_pin_L = Tx_Addr>>8;
			LCD_WR_wPIN = valLow; 			  
			LCD_WR_wPIN = valHigh;	
			LCD_data_pin_L = Tx_Addr;
			LCD_WR_wPIN = valLow; 			   
			LCD_WR_wPIN = valHigh;	
#endif
#if LCD_DATABIT == LT268_16DATABIT
			LCD_data_pin_H =  Tx_Addr>>8;
			LCD_data_pin_L =  Tx_Addr;
			LCD_WR_wPIN = valLow; 			   
			LCD_WR_wPIN = valHigh;	
#endif
	}
#endif
}

//---------------DMA----------------------------------------------------------
/*
* 函数名：LT_ReadFlash_DMA
* 功能  ：从外挂的FLASH中读取数据
* 输入  ：pBuffer:数据存储区
*         ReadAddr:开始读入的地址(24bit)
*         要读入的字节数(最大4096)
* 输出  ：无
* 作者  ：LEVETOP
*/


//--spi screen
/*
DMA_CHANNEL_REG *TFTspi_dma_channel[DMAC_CHNUM] = {(DMA_CHANNEL_REG *)(TFTSPI_DMA_BASE_ADDR        ),(DMA_CHANNEL_REG *)(TFTSPI_DMA_BASE_ADDR + 0x58 ),\
													  (DMA_CHANNEL_REG *)(TFTSPI_DMA_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(TFTSPI_DMA_BASE_ADDR + 0x108)};//global struct variable for for Channel registers					  

DMA_CONTROL_REG *TFTspi_dma_control = (DMA_CONTROL_REG*)(TFTSPI_DMA_BASE_ADDR+0x2C0);//global struct variable for for DMAC registers

//--exflash
DMA_CHANNEL_REG *exFlash_dma_channel[DMAC_CHNUM] = {(DMA_CHANNEL_REG *)(exFlash_DMA_BASE_ADDR        ),(DMA_CHANNEL_REG *)(exFlash_DMA_BASE_ADDR + 0x58 ),\
													  (DMA_CHANNEL_REG *)(exFlash_DMA_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(exFlash_DMA_BASE_ADDR + 0x108)};//global struct variable for for Channel registers					  

DMA_CONTROL_REG *exFlash_dma_control = (DMA_CONTROL_REG*)(exFlash_DMA_BASE_ADDR+0x2C0);//global struct variable for for DMAC registers

//--COMuart
DMA_CHANNEL_REG *WAV_dac_dma_channel[DMAC_CHNUM] = {(DMA_CHANNEL_REG *)(COMuart_DMA_BASE_ADDR        ),(DMA_CHANNEL_REG *)(COMuart_DMA_BASE_ADDR + 0x58 ),\
													  (DMA_CHANNEL_REG *)(COMuart_DMA_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(COMuart_DMA_BASE_ADDR + 0x108)};//global struct variable for for Channel registers					  

DMA_CONTROL_REG *WAV_dac_dma_control = (DMA_CONTROL_REG*)(COMuart_DMA_BASE_ADDR+0x2C0);//global struct variable for for DMAC registers

//--WAVdac
DMA_CHANNEL_REG *WAVdac_dma_channel[DMAC_CHNUM] = {(DMA_CHANNEL_REG *)(WAVdac_DMA_BASE_ADDR        ),(DMA_CHANNEL_REG *)(WAVdac_DMA_BASE_ADDR + 0x58 ),\
													  (DMA_CHANNEL_REG *)(WAVdac_DMA_BASE_ADDR + 0xB0 ),(DMA_CHANNEL_REG *)(WAVdac_DMA_BASE_ADDR + 0x108)};//global struct variable for for Channel registers					  

DMA_CONTROL_REG *WAVdac_dma_control = (DMA_CONTROL_REG*)(WAVdac_DMA_BASE_ADDR+0x2C0);//global struct variable for for DMAC registers
*/

void Display_ASCII(UINT16 x,UINT16 y,UINT8 size,UINT8 transparency,UINT16 fgColor,UINT16 bgColor,UINT8 num)
{
	UINT8 temp,t1,t;
	UINT16 y0=y,x0=x;
	UINT16 ii,jj,kk;
	UINT8 hbyte=(size/16+((size/2%8)?1:0));		    //??????????
	//UINT8 csize=hbyte*size;							//???????????????????

 	num=num-' ';//???????(ASCII??????????,??-' '?????????)
	t=0;
	
	for(ii=0;ii<size;ii++)
	{
		for(jj=0;jj<hbyte;jj++)
		{
			if (size==16) temp=Font_0816[num][t++];	//??1608??
			else 
				if (size==24) temp=Font_1224[num][t++];
				else return;	
			
			for(kk=0;kk<8;kk++)
			{
				if(temp&0x80)
				{					
					//if(transparency==OPAQUE||transparency==TRANSPARENCY)
						LT268_TFT_DrawPoint(x,y,fgColor);
				}
				else
				{					
					if(transparency!=TRANSPARENCY) //if(transparency==0||transparency==2)
						LT268_TFT_DrawPoint(x,y,bgColor);
				}
				x++;
				temp<<=1;
				if(x>=(x0+size/2))
				{
					x=x0;
					y++;
					break;
				}
			}
		}
	}
}


//????Ascll???
//x,y :?????
//size:????
//mode: 0:?????  1:????
//FontColor:????
//BackGroundColor:????
//str:???
void Display_Text(UINT16 x,UINT16 y,UINT8 font,UINT8 transparency,UINT16 fgColor,UINT16 bgColor,char *str)
{
//	UINT8 x0=x;

	u16 size;
	if (font==0) size=16;
	else size=24;
	while((*str<='~')&&(*str>=' '))//?????????!
	{
		if(x+size/2>LCD_XSIZE_TFT){x=0;y+=size;}
		if(y+size>LCD_YSIZE_TFT)break;//??
		Display_ASCII(x,y,size,transparency,fgColor,bgColor,*str);
		x+=size/2;
		str++;
    }
}



void Screen_Clear(UINT16 color)
{
	LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,color);
}

void Clear_Screen(UINT16 color)
{
	u16 x,y,pixelData;
	u32 i,bmpSize;
	unsigned const char *bmpPtr;
	
	bmpSize=LCD_XSIZE_TFT*LCD_YSIZE_TFT;
	printf("LCD_X=%d LCD_Y=%d ",LCD_XSIZE_TFT,LCD_YSIZE_TFT);
	x=y=0;
	for (i=0;i<bmpSize;i++)
	{
		LT268_TFT_DrawPoint(x,y,grYELLOW);
		x++;
		if(x>=LCD_XSIZE_TFT)
		{
			x=0;
			y++;
		}
	}
	printf("LCD_X=%d LCD_Y=%d ",LCD_XSIZE_TFT,LCD_YSIZE_TFT);
}

void Display_16BitBMP(UINT16 x,UINT16 y,unsigned const char *bmp,UINT16 transColor)
{
	UINT32 i,bmpSize,sizeX,sizeY;
	UINT16 locX,pixelData;
	unsigned const char *bmpPtr;	
	locX=x; 
	x=0;	
	
	sizeX=*(u16 *)(bmp+2);
	sizeY=*(u16 *)(bmp+4);
	
	bmpSize=sizeX*sizeY;
	bmpPtr=bmp+8;
	
	INTX_DISABLE();
	for (i=0;i<bmpSize;i++)
	{
		pixelData=*(u16 *)bmpPtr;
		bmpPtr+=2;
		
		if (pixelData!=transColor)
			LT268_TFT_DrawPoint(locX+x,y,pixelData);
		
		x++;
		if(x>=sizeX)
		{
			x=0;
			y++;
		}
	}
	INTX_ENABLE();
}

