/**
  *****************************************************************************
  * 						lcdʾؿ
  *****************************************************************************
  *
  * @File   : lcd.c
  * @By     : Sun
  * @Version: V1.0
  * @Date   : 2021 / 11 / 04
  * @Shop	: https://xiaomeige.taobao.com/
  *
  *****************************************************************************
**/
#include "lcd.h"
#include "font.h"

/******** XY귽 ********

        	X(0~800)
|
|
|
|

Y(0~480)

*********************************/

XAxiVdma     Vdma;
DisplayCtrl  DispCtrl;

u8 *frame = (u8*)FrameBuffer_Addr;

/***************ɫͱɫ***************/
u32 POINT_COLOR=LCD_RED;	//ɫĬΪɫ
u32 BACK_COLOR =LCD_WHITE; 	//ֱɫĬΪɫ


/**
  *****************************************************************************
  * @: RGBLCDʼ
  *****************************************************************************
**/
void LCD_Init()
{
	//VDMA
	run_vdma_frame_buffer(&Vdma, VDMA_ID, RGB_LCD.width, RGB_LCD.height,
							FrameBuffer_Addr,0, 0,ONLY_READ);

	memset(frame,0xFF,800*480*BYTES_PIXEL);
    //ʼDisplay controller
	DisplayInitialize(&DispCtrl, DISP_VTC_ID, DYNCLK_BASEADDR);

    //VideoMode
	DisplaySetMode(&DispCtrl, &RGB_LCD);
	DisplayStart(&DispCtrl);
}

/**
  *****************************************************************************
  * @: RGBLCDˢ£ͼݸı󣬱ˢ²ʾͼ
  * @ע⣺	ʹñˢͼҪʱ䣬ڻͼκʹñһˢȫͼ
  * 		ҪʱƵˢ£ᵼͼر
  *****************************************************************************
**/
void LCD_Refresh()
{
	//ˢData Cacheͼ
	Xil_DCacheFlushRange((UINTPTR) FrameBuffer_Addr, 800*480*BYTES_PIXEL);
}

/**
  *****************************************************************************
  * @: 
  *
  * @: x,y:
  * 		 POINT_COLOR:˵ɫ
  *****************************************************************************
**/
void LCD_DrawPoint(u16 x,u16 y,u32 color)
{
	//x޷
	if(x>800)
		x=800;
	//y޷
	if(y>480)
		y=480;
	frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y] = color&0x0000ff;
	frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y + 1] = (color>>8)&0x0000ff;

	//LCD_Refresh();//ˢData Cacheͼ
}


/**
  *****************************************************************************
  * @: ָָɫ
  *
  * @: (x1,y1),(x2,y2):ζԽ,СΪ:(x2-x1+1)*(y2-y1+1)
  * 		  color:Ҫɫ
  *****************************************************************************
**/
void LCD_Color_Fill(u16 x1,u16 y1,u16 x2,u16 y2,u32 color)
{
	u16 x,y,temp;

	if(y1>y2)
	{
		temp=y1;
		y1=y2;
		y2=temp;
	}

	if(x1>x2)
	{
		temp=x1;
		x1=x2;
		x2=temp;
	}

	for(y=y1;y<=y2;y++)
	{
		for(x=x1;x<=x2;x++)
		{
			frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y] = color&0x0000ff;
			frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y + 1] = (color>>8)&0x0000ff;
		}
	}

	//LCD_Refresh();//ˢData Cacheͼ
}

/**
  *****************************************************************************
  * @: 
  *
  * @: color:Ҫɫ
  *****************************************************************************
**/
void LCD_Clear(u32 color)
{
	u16 x,y;
	for(y=0;y<480;y++)
	{
		for(x=0;x<800;x++)
		{
			frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y] = color&0x0000ff;
			frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y + 1] = (color>>8)&0x0000ff;
		}
	}

	LCD_Refresh();//ˢData Cacheͼ
}
/*******************************************************
 *@:POINT_COLORָɫֱ
 *@:uint16_t x1   : һx
 *			uint16_t y1   : һy
 *			uint16_t x2   : ڶx
 *			uint16_t y2   : ڶy
 *			uint16_t color: ߵɫ
 *ʹ˵ :ʹֻҪ㶼LCDʾΧڣҪλ
 *@ֵ  :0:, 1:ʧ
 *******************************************************/
uint8_t LCD_DrawWaveLine(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;

	if(y1 >= 480 - 40 - 1)
		y1 = 480 - 40 - 1;

	if(y1 <= 40)
		y1 = 40;

	if(y2 >= 480 - 40 - 1)
		y2 = 480 - 40 - 1;

	if(y2 <= 40)
		y2 = 40;

	delta_x = x2 - x1; //
	delta_y = y2 - y1;
	uRow = x1;
	uCol = y1;
	if (delta_x > 0)
		incx = 1; //xΪ-1
	else if (delta_x == 0)
		incx = 0; //ֱ
	else {
		incx = -1; //xΪ1
	}

	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_Fast_DrawPoint(uRow, uCol, POINT_COLOR); //

		//x޷
		if(uRow>800)
			uRow=800;
		//y޷
		if(uCol>480)
			uCol=480;
		frame[BYTES_PIXEL*uRow + 800*BYTES_PIXEL*uCol] = POINT_COLOR&0x0000ff;
		frame[BYTES_PIXEL*uRow + 800*BYTES_PIXEL*uCol + 1] = (POINT_COLOR>>8)&0x0000ff;


		xerr += delta_x;
		yerr += delta_y;
		if (xerr > distance) {
			xerr -= distance;
			uRow += incx;
		}
		if (yerr > distance) {
			yerr -= distance;
			uCol += incy;
		}
	}
	//ˢCache
	//Xil_DCacheFlush();

	return 0;
}
/**
  *****************************************************************************
  * @: 
  *
  * @: x1,y1:
  * 		 x2,y2:յ
  *****************************************************************************
**/
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2,u32 color)
{
	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++ )    //
	{
		frame[BYTES_PIXEL*uRow + 800*BYTES_PIXEL*uCol] = color&0x0000ff;
		frame[BYTES_PIXEL*uRow + 800*BYTES_PIXEL*uCol + 1] = (color>>8)&0x0000ff;

		xerr+=delta_x ;
		yerr+=delta_y ;
		if(xerr>distance)
		{
			xerr-=distance;
			uRow+=incx;
		}
		if(yerr>distance)
		{
			yerr-=distance;
			uCol+=incy;
		}
	}

	//ˢCache
	//Xil_DCacheFlush();
}

/**
  *****************************************************************************
  * @: 
  *
  * @: (x1,y1),(x2,y2):εĶԽ
  *****************************************************************************
**/
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2, u32 color)
{
    LCD_DrawLine(x1, y1, x2, y1, color);
    LCD_DrawLine(x1, y1, x1, y2, color);
    LCD_DrawLine(x1, y2, x2, y2, color);
    LCD_DrawLine(x2, y1, x2, y2, color);
}

/**
  *****************************************************************************
  * @: ָλʾһַ
  *
  * @:	x,y:ʼ
  * 		 	num:Ҫʾַ:" "--->"~"
  * 		 	size:С 12/16/24/32
  * 			mode:ӷʽ(1)Ƿǵӷʽ(0)
  *
  * @Sample:	LCD_ShowChar(200, 50,'A',16,1);//(200,50)ʾַ'A'
  *****************************************************************************
**/
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{
    u8 temp,t1,t;
    u16 y0=y;
    u8 csize=(size/8+((size%8)?1:0))*(size/2);   //õһַӦռֽ
    num=num-' ';//õƫƺֵASCIIֿǴӿոʼȡģ-' 'ǶӦַֿ⣩
    for(t=0;t<csize;t++) {
        if(size==12)
            temp=asc2_1206[num][t];  //1206
        else if(size==16)
            temp=asc2_1608[num][t];  //1608
        else if(size==24)
            temp=asc2_2412[num][t];  //2412
        else if(size==32)
            temp=asc2_3216[num][t];  //3216
        else return;                 //ûеֿ
        for(t1=0;t1<8;t1++) {
            if(temp&0x80)
			{
            	frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y] = POINT_COLOR&0x0000ff;
            	frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y + 1] = (POINT_COLOR>>8)&0x0000ff;
			}
            else if(mode==0)
            {
				frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y] = BACK_COLOR&0x0000ff;
				frame[BYTES_PIXEL*x + 800*BYTES_PIXEL*y + 1] = (BACK_COLOR>>8)&0x0000ff;
            }
            temp<<=1;
            y++;
            if(y>=480)
                return;     //
            if((y-y0)==size) {
                y=y0;
                x++;
                if(x>=800)
                    return;  //
                break;
            }
        }
    }

    //ˢCache
    //Xil_DCacheFlush();
}

/**
  *****************************************************************************
  * @: ʾַ
  *
  * @: x,y:
  * 		 width,height:С
  * 		 size:С
  * 		 *p:ַʼַ
  * 		 mode:ӷʽ(1)Ƿǵӷʽ(0)
  *****************************************************************************
**/
void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 mode,u8 *p)
{
    u8 x0=x;
    width+=x;
    height+=y;
    while((*p<='~')&&(*p>=' ')) {	//жǲǷǷַ!
        if(x>=width) {
            x=x0;
            y+=size;
        }
        if(y>=height)
            break;//˳
        LCD_ShowChar(x,y,*p,size,mode);
        x+=size/2;
        p++;
    }

}

/**
  *****************************************************************************
  * @: m^n
  *
  * @: m,n
  *
  * @ֵ   : m^nη.
  *****************************************************************************
**/
u32 LCD_Pow(u8 m,u8 n)
{
    u32 result=1;
    while(n--)
        result*=m;
    return result;
}

/**
  *****************************************************************************
  * @: ʾ,λΪ0,ʾ
  *
  * @: 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/LCD_Pow(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);
    }
}


/**
  *****************************************************************************
  * @: ʾ,λΪ0,ʾ0
  *
  * @: x,y:
  * 		 num:ֵ(0~999999999);
  * 		 len:(Ҫʾλ)
  * 		 size:С
  * 		 mode[7]:0,0;1,0.
  * 		 mode[1]:0,num=0ʾ0;1,num=0ʾ
  * 		 [6:2]:
  * 		 mode[0]:0,ǵʾ;1,ʾ.
  *****************************************************************************
**/
void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode)
{
    u8 t,temp;
    u8 enshow=0;
    for(t=0;t<len;t++)
    {
       temp=(num/LCD_Pow(10,len-t-1))%10;
       if(enshow==0&&t<(len-1))
       {
            if(temp==0)
            {
                 if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);
                 else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);
                 continue;
            }else enshow=1;
       }
       if((mode&0X02)&&(!num))
    	   LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);
       else
    	   LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01);
    }
}

/**
  *****************************************************************************
  * @: ʾͼƬ
  *
  * @: x,y:
  * 		 pic:ͼƬָ(ͼƬ뱣ͷϢ)
  *****************************************************************************
**/
void LCD_DisplayPic(u16 x, u16 y,const u8 *Pic)
{
	u32 width,height;
	u32 x_pos, y_pos;
	u32 Cnt = 0;

	width = (Pic[2]<<8) + Pic[3];//ȡͼƬ
	height = (Pic[4]<<8) + Pic[5];//ȡͼƬ߶

	for(y_pos=y;y_pos<y+height;y_pos++)
	{
		for(x_pos=x;x_pos<x+width;x_pos++)
		{
			frame[BYTES_PIXEL*x_pos + 800*BYTES_PIXEL*y_pos] =  Pic[Cnt + 8 + 1];
			frame[BYTES_PIXEL*x_pos + 800*BYTES_PIXEL*y_pos + 1] = Pic[Cnt + 8];
			Cnt+=BYTES_PIXEL;
		}
	}

	//ˢCache
	LCD_Refresh();

}

