#include "main.h"
#include "internal_flash.h"
#include "lcd_base_display.h"
#include "my_font.h"

#define BIT8(n)          ((unsigned char)((unsigned char)1<<n) )	//n(0~7)
#define isBit8(dat, n)   ((dat & BIT8(n)) ? 1: 0)	//0~7

#define Hzk_16_data  (const unsigned char *)((unsigned int)0x80000000)//no use
#define Hzk_24_data  (const unsigned char *)(FLASH_USER_FONT_24X24)

const unsigned char *text_buf;

extern const unsigned char asc2_1608[95][16];
extern const unsigned char asc2_2412[95][36];

void gui_put_pixel(unsigned short x, unsigned short y, unsigned int offset, unsigned int color)
{
	LCD_Fast_DrawPoint(x,y,color);
}

unsigned int get_chinese_offset_16(const unsigned char *p)
{
	unsigned int d;
	
	d=32UL*( ((*p)-0xa1)*94 + ((*(p+1))-0xa1) );
	
	return d;
}

void gui_write_CN16(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, const unsigned char *p)
{
	const unsigned char* pbuf;
	unsigned short ix, iy;
	unsigned int address;
	unsigned int offset = 0;
   
	offset = get_chinese_offset_16( p);
	
	text_buf = Hzk_16_data;
	pbuf = &text_buf[offset];			//______________________________fill location
  
   for(iy=0; iy<16; iy++)
   {
      for(ix=0; ix<8; ix++)
      {
         if( isBit8(*pbuf, (7-ix))==0 )
         {
            gui_put_pixel(x+ix, y, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x+ix, y, 0, CharColor);	//______________________________put pixel here
         }
      }
      
      pbuf++;
      for(ix=0; ix<8; ix++)//?1????8?
      {
         if( isBit8(*pbuf, (7-ix))==0 )
         {
            gui_put_pixel(x+ix+8, y, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x+ix+8, y, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
      y++;
   }
}

void gui_write_EN1608(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor,unsigned char num)
{
	const unsigned char* pbuf;
	unsigned short ix, iy;
	unsigned int offset = 0;
	
 	//num=num-' ';//得到偏移后的值（ASCII字库是从空格开始取模，所以-' '就是对应字符的字库）
	offset = (num-' ');
	//text_buf = &asc2_1608[0][0];
	pbuf = &asc2_1608[offset][0];
	
	 for(ix=0; ix<8; ix++)
   {
      for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
			
			for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+8+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+8+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
      x++;
   }
}

void gui_write_hex16(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, unsigned char hex)
{
	if(((hex&0xF0)>>4)>9)
		gui_write_EN1608(x,y,CharColor,BackColor,(((hex&0xF0)>>4) - 0x0A +'A'));
	else
		gui_write_EN1608(x,y,CharColor,BackColor,(((hex&0xF0)>>4)+'0'));
	
	if((hex&0x0F)>9)
		gui_write_EN1608((x+8),y,CharColor,BackColor,((hex&0x0F) - 0x0A +'A'));
	else
		gui_write_EN1608((x+8),y,CharColor,BackColor,((hex&0x0F)+'0'));
}

void gui_write_num16(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, unsigned char num)
{
	gui_write_EN1608(x,y,CharColor,BackColor,(num+'0'));
}

void gui_write_num(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, unsigned short num, unsigned char bit)
{
	if((bit > 4)||(0==bit))return;
	LCD_Fill(x,y,(x+(bit<<3)-1),(y+8-1),BackColor);
	if(4 == bit)
	{
		gui_write_num16(x,y,CharColor,BackColor,(num/1000%10));
		gui_write_num16((x+8),y,CharColor,BackColor,(num/100%10));
		gui_write_num16((x+16),y,CharColor,BackColor,(num/10%10));
		gui_write_num16((x+24),y,CharColor,BackColor,(num%10));
	}
	else if(3 == bit)
	{
		gui_write_num16((x),y,CharColor,BackColor,(num/100%10));
		gui_write_num16((x+8),y,CharColor,BackColor,(num/10%10));
		gui_write_num16((x+16),y,CharColor,BackColor,(num%10));
	}
	else if(2 == bit)
	{
		gui_write_num16(x,y,CharColor,BackColor,(num/10%10));
		gui_write_num16((x+8),y,CharColor,BackColor,(num%10));
	}
	else if(1 == bit)
	{
		gui_write_num16(x,y,CharColor,BackColor,(num%10));
	}
}

void gui_write_string16(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, const unsigned char* p)
{
 unsigned char dat;
   
   for(; *p!=0; p++)
   {
      if(( (unsigned char)*p&0x80)==0)//??
      {
         if(*p=='\r')//??
         {
            //x=START_X;
            continue;
         }
         else if(*p=='\n')//??
         {
            //y+=24;
            //if(y > RECT_YMAX-24)
            //   y=START_Y;
            continue;
         }
         else if(*p=='\1')         //?????.?
            dat='~'-' '+1;
         else if(*p=='\2')    //?????.?
            dat='~'-' '+2;
         else if(*p=='\3')    //?????.?
            dat='~'-' '+3;
         else if(*p=='\4')    //?????.?
            dat='~'-' '+4;
//         else if(*p=='\\')    //?????
//         {
//            p++;
//            if(*p=='U')         //?????.?
//               dat='~'-' '+1;
//            else if(*p=='D')    //?????.?
//               dat='~'-' '+2;
//            else if(*p=='L')    //?????.?
//               dat='~'-' '+3;
//            else// if(*p=='R')    //?????.?
//               dat='~'-' '+4;
//         }
         else //??????
            dat=*p-0x20;  
         
				 dat=*p; 
         gui_write_EN1608(x, y, CharColor, BackColor, dat);
         x+=8;
      }
      else//??
      {
         gui_write_CN16(x, y, CharColor, BackColor, (unsigned char*)p);
         x+=16;
         p++;
      }
   }
}

unsigned int get_chinese_offset_24(const unsigned char *p)
{
	unsigned int d;
	
	d=72UL*( ((*p)-15-0xa1)*94 + ((*(p+1))-0xa1) );
	
	return d;
}

void gui_write_CN24(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, const unsigned char *p)
{
	const unsigned char* pbuf;
	unsigned short ix, iy;
	unsigned int address;
	unsigned int offset = 0;
   
	offset = get_chinese_offset_24( p);
	
	text_buf = Hzk_24_data;
	pbuf = &text_buf[offset];			//______________________________fill location
  
   for(ix=0; ix<24; ix++)
   {
      for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
      
      pbuf++;
      for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+iy+8, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+iy+8, 0, CharColor);	//______________________________put pixel here
         }
      }
			
			pbuf++;
      for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+iy+16, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+iy+16, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
      x++;
   }
}

void gui_write_EN2412(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor,unsigned char num)
{
	const unsigned char* pbuf;
	unsigned short ix, iy;
	unsigned int offset = 0;
	
 	//num=num-' ';//得到偏移后的值（ASCII字库是从空格开始取模，所以-' '就是对应字符的字库）
	offset = (num-' ');
	pbuf = &asc2_2412[offset][0];
	
	 for(ix=0; ix<12; ix++)
   {
      for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
			
			for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+8+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+8+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
			
			pbuf++;
			
			for(iy=0; iy<8; iy++)
      {
         if( isBit8(*pbuf, (7-iy))==0 )
         {
            gui_put_pixel(x, y+16+iy, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x, y+16+iy, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
      x++;
   }
}

void gui_write_EN2417(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor,unsigned char num)
{
	const unsigned char* pbuf;
	unsigned short ix, iy;
	unsigned int offset = 0;
	
 	//num=num-' ';//得到偏移后的值（ASCII字库是从空格开始取模，所以-' '就是对应字符的字库）
	offset = (num-' ')*72;
	text_buf = ascii_17x24_table;
	pbuf = &text_buf[offset];
	
	 for(iy=0; iy<24; iy++)
   {
      for(ix=0; ix<8; ix++)
      {
         if( isBit8(*pbuf, (7-ix))==0 )
         {
            gui_put_pixel(x+ix, y, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x+ix, y, 0, CharColor);	//______________________________put pixel here
         }
      }
      
      pbuf++;
      for(ix=0; ix<8; ix++)//?1????8?
      {
         if( isBit8(*pbuf, (7-ix))==0 )
         {
            gui_put_pixel(x+ix+8, y, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x+ix+8, y, 0, CharColor);	//______________________________put pixel here
         }
      }
			
			pbuf++;
      for(ix=0; ix<8; ix++)//?1????8?
      {
         if( isBit8(*pbuf, (7-ix))==0 )
         {
            gui_put_pixel(x+ix+16, y, 0, BackColor);		//______________________________put pixel here
         }
         else
         {
            gui_put_pixel(x+ix+16, y, 0, CharColor);	//______________________________put pixel here
         }
      }
			
      pbuf++;
      y++;
   }
}

void gui_write_string24(unsigned short x, unsigned short y, unsigned int CharColor, unsigned int BackColor, const unsigned char* p)
{
 unsigned char dat;
   
   for(; *p!=0; p++)
   {
      if(( (unsigned char)*p&0x80)==0)//??
      {
         if(*p=='\r')//??
         {
            //x=START_X;
            continue;
         }
         else if(*p=='\n')//??
         {
            //y+=24;
            //if(y > RECT_YMAX-24)
            //   y=START_Y;
            continue;
         }
         else if(*p=='\1')         //?????.?
            dat='~'-' '+1;
         else if(*p=='\2')    //?????.?
            dat='~'-' '+2;
         else if(*p=='\3')    //?????.?
            dat='~'-' '+3;
         else if(*p=='\4')    //?????.?
            dat='~'-' '+4;
//         else if(*p=='\\')    //?????
//         {
//            p++;
//            if(*p=='U')         //?????.?
//               dat='~'-' '+1;
//            else if(*p=='D')    //?????.?
//               dat='~'-' '+2;
//            else if(*p=='L')    //?????.?
//               dat='~'-' '+3;
//            else// if(*p=='R')    //?????.?
//               dat='~'-' '+4;
//         }
         else //??????
            dat=*p-0x20;  
         
				 dat=*p;
				 gui_write_EN2412(x, y, CharColor, BackColor, dat);
				 x+=12;
         //gui_write_EN2417(x, y, CharColor, BackColor, dat);
         //x+=17;
      }
			
      else//??
      {
         gui_write_CN24(x, y, CharColor, BackColor, (unsigned char*)p);
         x+=24;
         p++;
      }
   }
}
