⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.cpp

📁 C++BUILDER 下的串口驱动模块源码
💻 CPP
字号:
#include "lcd.h"
#include "Unit1.h"
#define IS_ABS(a) ((a>0)?a:-a)
extern const unsigned char hzk12[];
extern const unsigned char ascii6x12[];

const unsigned char gImage_hhh7[49] = {// 7*7
0XFF,0XFF,0X00,0X00,0X00,0XFF,0XFF,
0XFF,0X00,0X00,0X00,0X00,0X00,0XFF,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XFF,0X00,0X00,0X00,0X00,0X00,0XFF,
0XFF,0XFF,0X00,0X00,0X00,0XFF,0XFF,};

const unsigned char gImage_hhh4[16] = { // 4*4
0XFF,0X00,0X00,0XFF,
0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,
0XFF,0X00,0X00,0XFF,
};
extern PACKAGE TForm1 *Form1;

void Change_RGB(uint32 color_num,uint8 *R,uint8 *G,uint8 *B)
{
		*R=(uint8)((color_num>>16)&0xff);
		*G=(uint8)((color_num>>8)&0xff);
		*B=(uint8)(color_num&0xff);

}
void lcd_layer_setpixel(int x,int y,int color)
{
	uint8 R,G,B;
	void *tt=Form1->Image1->Canvas->Handle;
	Change_RGB(color,&R,&G,&B);
	::SetPixelV(tt,x,y,RGB(R,G,B));
}

void line_hang(uint32 x1,uint32 y1,uint32 x2,uint32 color)
{
	uint32 i;
	for(i=x1;i<x2;i++)
	{
	  lcd_layer_setpixel(i,y1,color);
	}
}

void line_lie(uint32 x1,uint32 y1,uint32 y2,uint32 color)
{
	uint32 i;
	for(i=y1;i<y2;i++)
	{
		lcd_layer_setpixel(x1,i,color);
	}
}

void busybox_all(uint32 x1,uint32 y1, uint32 w,uint32 h,uint32 color)
{
	 uint32 i;
	 uint32 j;
	 for(j=y1;j<y1+h;j++)
		{
		 for(i=x1;i<x1+w;i++)
			{
			 lcd_layer_setpixel(i,j,color);
			}		
		}
}

void busybox_allxy(uint32 x1,uint32 y1, uint32 x2,uint32 y2,uint32 color)
{
	 uint32 i;
	 uint32 j;
	 for(j=y1;j<y2;j++)
		{
		 for(i=x1;i<x2;i++)
			{
			 lcd_layer_setpixel(i,j,color);
			}		
		}
}

void busybox(uint32 x1,uint32 y1,uint32 x2,uint32 y2,uint32 color)
{
	line_hang(x1,y1,x2,color);
	line_hang(x1,y2,x2,color);
	line_lie(x1,y1,y2,color);
	line_lie(x2,y1,y2,color);
}


void Lcd_DspHz(uint32 x0, uint32 y0, uint8 *s, uint8 type,uint32 color)
{
 uint32 i,j,yy,xx;
 uint8 qm,m;
 int32 n=0;
 int32 ulOffset;
 uint8 ss1;
 uint8 ss2;
 while((*s)!=0)
 {
	ss1=*(s);
	ss2=*(s+1);
	if(ss1<0xb0)			//英文
	{
		//此处添加英文打点函数
		ulOffset = (int32)(ss1-0x20) * 12;
 		for(i=0;i<12;i++)
 		{
			ss1=ascii6x12[ulOffset +i];
			for(j=0;j<8;j++)
 			{
				if(ss1&0x80)
 				{
					lcd_layer_setpixel(x0+n+j ,i+y0,color);
				}
				ss1=ss1<<1;
 			}
 		}
		s=s+1;
		n=n+6;
		continue;
	}
	else					//中文
	{
	 	ulOffset = (int32)((ss1-0xb0) * 94 +(ss2-0xa1)) * 24;
		//printf("------------------------------------------------------%d\n",ulOffset);
		for(i=0;i<24;i++)
		{
			qm=hzk12[ulOffset + i];
			m=0x80;
			xx=x0+n+(i%0x02)*8;
			yy=y0+(i/2);
			for(j=0;j<8;j++)
			{
				if(m&qm)
				{
					lcd_layer_setpixel(xx+j,yy,color);
				}
				m=m>>1;
			}
			
		}
		n=n+12;
	}
	s=s+2;
	}
 }

void LCD_DrawPoint(uint32 x,uint32 y,uint32 color)
{

	lcd_layer_setpixel(x,y,color);
	return;
}

void LCD_DrawBrush(uint32 x,uint32 y,uint32 color,int8 type)
{
	uint8 i,j;
	//printf("x=%d,y=%d\n",x,y);
	if(type==3)
	{
		for(i=0;i<7;i++)
		{
			for(j=0;j<7;j++)
			{
				if(gImage_hhh7[i*7+j]==0x00)
				{
					LCD_DrawPoint(x+i,y+j,color);
				}
			}
		}
	}
	else if(type==2)
	{
		for(i=0;i<4;i++)
		{
			for(j=0;j<4;j++)
			{
				if(gImage_hhh4[i*4+j]==0x00)
				{
					LCD_DrawPoint(x+i,y+j,color);
				}
			}
		}
	}
	else
	{
		LCD_DrawPoint(x,y,color);
	}
}
void LCD_DrawBrushLine(uint32 x1, uint32 y1, uint32 x2, uint32 y2,uint32 color,int8 type)
{
	uint32 x, y, t;
	int32 c;
	//printf("x1=%d,y1=%d\n",x1,y1);
	//printf("x2=%d,y2=%d\n",x2,y2);
	if((x1 == x2) && (y1 == y2))
	{
		LCD_DrawBrush(x1,y1,color,type);
	}
	else if(IS_ABS((int32)(y2-y1)) > IS_ABS((int32)(x2-x1)))
	{
		if(y1 > y2)
		{
			t = y1;
			y1 = y2; y2 = t;
			t = x1;
			x1 = x2; x2 = t;
		}
		c=(int32)(x2 - x1);
		for(y = y1; y <= y2; y=y+type)
		{
			x = x1+ c*(int32)(y - y1)/(int32)(y2 - y1) ;
			LCD_DrawBrush(x,y,color,type);
		}
		for(;y<=y2;y++)
		{
			x = c*(int32)(y - y1)/(int32)(y2 - y1) + x1;
			LCD_DrawBrush(x,y,color,type);
		}
	}
	else
	{
		if(x1 > x2)
		{
			t = y1;
			y1 = y2;y2 = t;
			t = x1; 
			x1 = x2; x2 = t;
		}
		c=(int32)(y2 - y1);
		for(x = x1; x <= x2; x=x+type)
		{
			y = c* (int32)(x - x1)/(int32)(x2 - x1) + y1;
			LCD_DrawBrush(x,y,color,type);
		}
		for(;x <= x2;x++)
		{
			y = c* (int32)(x - x1)/(int32)(x2 - x1) + y1;
			LCD_DrawBrush(x,y,color,type);
		}
	}
}
/*
void paint_bmp_bg(lcd_layer *p,uint16 x,uint16 y,uint16 w,uint16 h,const uint8 *bgp)
{
	uint16 i,j,k;
	uint32 m,n=0;
	uint16 src_w=0,src_h=0;
	uint8 co_tran_type=0;	//透明标志
	uint8 pic_paint_type=0;	//填充方式
	uint16 tran_co=0;
	uint16 left=0;
	uint16 mid=0;
	uint16 right=0;
	
	uint16 top=0;
	uint16 center=0;
	uint16 bottom=0;
	
	uint16 *data;
	uint32 co;
	co_tran_type=bgp[0];	
	pic_paint_type=bgp[1];
	data=(uint16 *)(&bgp[20]);
	tran_co=*(uint16 *)(&bgp[2]);
	src_w=*(uint16 *)(&bgp[4]);
	src_h=*(uint16 *)(&bgp[6]);
	
	left=*(uint16 *)(&bgp[8]);
	mid=*(uint16 *)(&bgp[10]);
	right=*(uint16 *)(&bgp[12]);
	
	top=*(uint16 *)(&bgp[14]);
	center=*(uint16 *)(&bgp[16]);
	bottom=*(uint16 *)(&bgp[18]);

	//printf("top=%d,center=%d,bottom=%d\n",top,center,bottom);
	//printf("w=%d,h=%d,data=%x,tran_co=%x,pic_paint_type=%d\n",src_w,src_h,data[0],tran_co,pic_paint_type);
	//图片字模直接贴图,横向取模
	if(pic_paint_type==0)
	{
		h=IS_MIN(src_h,h);
		w=IS_MIN(src_w,w);
		//printf("w=%d,h=%d,data=%x,tran_co=%x,pic_paint_type=%d\n",w,h,data[0],tran_co,pic_paint_type);
		for(i=0,m=0;i<h;i++,m=m+src_w)
		{
			for(j=0,n=0;j<w;j++,n++)
			{
				if(co_tran_type==1)
				{
					if(data[m+n]==tran_co)
					{
						continue;
					}
				}
				lcd_layer_setpixel16(p, (x+j), (y+i), data[m+n]);
			}
			//printf("m+n=%d\n",(m+n));
		}
		//printf("m+n=%d\n",(m+n));
		return;
	}
//对图片进行横纵中心位拉伸显示
	else if(pic_paint_type==1)
	{
		uint8 R,B,G,Alpha=100;
		n=0;
		m=0;
		k=0;
		for(j=0;j<w;j++)
		{
			for(i=0;i<h;i++,n++)
			{
			//纵向拉伸
				if((i>top)&&(i<(h-bottom)))
				{
					k++;
					if(k==center)
					{
						n=n-center;
						k=0;
					}
					//printf("i=%d,j=%d,k=%d,m=%d,n=%d\n",i,j,k,m,n);
				}				
				if(co_tran_type==1)
				{
					if(data[n]==tran_co)
					{
						continue;
					}
				}
				else if(co_tran_type==2)
				{
					Alpha=tran_co;
					co=lcd_layer_getpixel16(p, (x+j), (y+i));
					R =(((co>>11)&0x1f)*(100-Alpha)+((data[n]>>11)&0x1f)*Alpha)/100;
					G =(((co>>5)&0x3f)*(100-Alpha)+((data[n]>>5)&0x3f)*Alpha)/100;
					B =((co&0x1f)*(100-Alpha)+(data[n]&0x1f)*Alpha)/100;
  					co=((R&0x1f)<<11)+((G&0x3f)<<5)+(B&0x1f);
					lcd_layer_setpixel16(p, (x+j), (y+i),co);
				}
				//lcd_layer_setpixel16(p, (x+j), (y+i),co data[n]);
			}
			if((j>=left)&&(j<(w-right-1)))
			{
			//横向拉伸
				m++;
				if(m==mid)
				{
					n=n-mid*src_h;
					m=0;
				}
			}
		}
		return;
	}
	//对图片进行横纵放大拉伸显示
	else if(pic_paint_type==2)
	{		
		for(j=0;j<w;j++)
		{
			for(i=0;i<h;i++)
			{
				n=((i*src_h/h)+(j*src_w/w)*src_h);
				// printf("i=%d,j=%d,n=%d\n",i,j,n);
				if(co_tran_type==1)
				{
					if(data[n]==tran_co)
					{
						continue;
					}
				}
				lcd_layer_setpixel16(p, (x+j), (y+i), data[n]);
			}
		}
		return;
	}
	//图片字模直接贴图,竖向取模
	else if(pic_paint_type==3)
	{
		h=IS_MIN(src_h,h);
		w=IS_MIN(src_w,w);
		//printf("w=%d,h=%d,data=%x,tran_co=%x,pic_paint_type=%d\n",w,h,data[0],tran_co,pic_paint_type);
		for(i=0,m=0;i<w;i++,m=m+src_h)
		{
			for(j=0,n=0;j<h;j++,n++)
			{
				if(co_tran_type==1)
				{
					if(data[m+n]==tran_co)
					{
						continue;
					}
				}
				lcd_layer_setpixel16(p, (x+i), (y+j), data[m+n]);
			}
			//printf("m+n=%d\n",(m+n));
		}
		//printf("m+n=%d\n",(m+n));
		return;
	}
	return;
}
*/
/*
uint32 RGB(uint8 R,uint8 G,uint8 B)
{
	return ((R<<16)|(G<<8)|(B));
}      */
extern const char Gbmp_frm[391703];
//#define DEBUG

void paint_bmp_bg(uint16 x,uint16 y,uint16 w,uint16 h,const uint8 *bgp)
{
	int32 i,j,m=0,k=0,kk=0;
	//int w,h;
	int32 n=0,t=0;
	uint32 src_w=0,src_h=0;
	char colormask,colortran;
	char colortran_per;
	char autosize;
	char *co_p,*co_data,*bg_data;
	unsigned short left=0;
	unsigned short mid=0;
	unsigned short right=0;
	
	unsigned short top=0;
	unsigned short center=0;
	unsigned short bottom=0;
	char R,G,B;
	uint32 src,mask;
#ifdef DEBUG
	
	FILE *fp;
	fp=fopen("debug.txt","wb");
#endif
	colormask=bgp[0];
	mask=RGB(bgp[3],bgp[2],bgp[1]);

#ifdef DEBUG
	src=RGB(bgp[25],bgp[24],bgp[23]);
	fprintf(fp,"colormask=%d,mask=%d,src=%d\n",colormask,(int)mask,(int)src);
#endif

	colortran=bgp[4];
	colortran_per=bgp[5];
	
	src_w=*(unsigned short *)(&bgp[6]);
	src_h=*(unsigned short *)(&bgp[8]);

	left=*(unsigned short *)(&bgp[10]);
	mid=*(unsigned short *)(&bgp[12]);
	right=*(unsigned short *)(&bgp[14]);
	
	top=*(unsigned short *)(&bgp[16]);
	center=*(unsigned short *)(&bgp[18]);
	bottom=*(unsigned short *)(&bgp[20]);
	
	autosize=bgp[22];
#ifdef DEBUG
	fprintf(fp,"left=%d,mid=%d,right=%d\n",left,mid,right);
	fprintf(fp,"top=%d,center=%d,bottom=%d\n",top,center,bottom);
#endif

	co_data=(char *)&bgp[24];
	bg_data=(char *)&Gbmp_frm[24];
	for(i=0,kk=0;i<w;i++,kk++)
	{
		n=kk;
		for(j=0,k=0;j<h;j++,n=n+src_w)
		{

			if((autosize==1)&&(j>top)&&(j<(h-bottom)))
			{
				//纵向拉伸
				k++;
				if(k==center)
				{
					n=n-center*src_w;
					k=0;
				}
			}
			else if(autosize!=1)
			{
				n=((j*src_h/h)*src_w+(i*src_w/w));
			}
			co_p=&co_data[n*3];
			src=RGB(co_p[2],co_p[1],co_p[0]);
#ifdef DEBUG
			//if(i==0) 	fprintf(fp,"i=%d,n=%d,mask=%d,src=%d\n",i,n,(int)mask,(int)src);
			fprintf(fp,"i=%d,j=%d,n=%d,\n",i,j,n);
#endif
			if((colormask)&&(src==mask)) continue;
			if(colortran)
			{
				//t=(((y-(p->h-272)/2)+j)*480+((x-p->w/2+240)+i))*3;
				R=((((uint16)bg_data[t+2])*(0xff-colortran_per))+(((uint16)co_p[0])*colortran_per))/0XFF;
				G=((((uint16)bg_data[t+1])*(0xff-colortran_per))+(((uint16)co_p[1])*colortran_per))/0XFF;
				B=((((uint16)bg_data[t])*(0xff-colortran_per))+(((uint16)co_p[2])*colortran_per))/0XFF;
				src=RGB(R,G,B);
			}
			//hdc->Canvas->Pixels[i][h-j-1]=src;
			lcd_layer_setpixel((x+i),(y+h-j-1),src);
		}
		if((autosize==1)&&(i>=left)&&(i<(w-right-1)))
		{
			//横向拉伸
			m++;
			if(m==mid)
			{
				kk=kk-mid;
				m=0;
			}
		}
	}        
#ifdef DEBUG
	fclose(fp);
#endif
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -