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

📄 displib.cpp

📁 其于ARM的USB操作,控制芯片为CH375实现操作.以及其他功能.包括AC,串口操作.
💻 CPP
字号:
#include "DispLib.h"
#include "string.h"
#include "math.h"

const S8 HexList[16]=
{
	'0','1','2','3','4','5','6','7',
	'8','9','A','B','C','D','E','F',
};

CDispLib::CDispLib()
{
	pHz16 = NULL;
	pAsc16 = NULL;
	iXSize = 128;
	iYSize = 64;
	pDispBuf = NULL;
}

CDispLib::~CDispLib()
{
}

void CDispLib::SetHz( U32* p )
{
	pHz16 = p;
}

void CDispLib::SetAsc( U32* p )
{
	pAsc16 = p;
}

void CDispLib::SetXSize( U32 size )
{
	iXSize = size;
}

void CDispLib::SetYSize( U32 size )
{
	iYSize = size;
}

void CDispLib::SetDispBuf( U8* p )
{
	pDispBuf = p;
}

void CDispLib::ClrScr()
{
	memset( pDispBuf, 0, ((iXSize*iYSize)/8) );
}

void CDispLib::SetPixel(S32 x, S32 y,S32 col)
{
	if( x < iXSize )
	{
		if( y < iYSize )
		{
			if( col )
			{
				pDispBuf[x+((y/8)*iXSize)] |= ( 0x1 << (y%8) );
			}
			else
			{
			}
		}
	}
}

//显示字符
void CDispLib::DispAsc(S8 asc, S32 x, S32 y, S32 col )
{
	U32 temp;
	for( U32 i=0; i<4; i++ )
	{
		temp=*(pAsc16+asc*4+i);
		for( U32 j=0; j<32; j++ )
		{
			if( temp & 0x80000000 )
			{
				SetPixel( x+(j%8) , y+i*4+(j/8) , col );
			}
			temp <<= 1;
		}
	}
}

//显示汉字
void CDispLib::DispHz16(S8 *src, S32 x, S32 y, S32 col)
{
	U32 temp;
	U32 d1,c1,c2;

	d1=(U32)(*src);
	src++;
	if(d1>=0xa1)
	{
		d1-=0xa1;
	}
	c1=d1&0x7f;

	d1=(U32)(*src);
	src++;
	if(d1>=0xa1)
	{
		d1-=0xa1;
	}
	c2=d1&0x7f;
	d1=c1*94+c2;

	d1*=8;
	for( U32 i=0; i<8; i++ )
	{
		temp=*(pHz16+i+d1);
		for( U32 j=0; j<32; j++)
		{
			if( temp & 0x80000000 )
			{
				SetPixel( x+(j%16) , y+i*2+(j/16) , col );
			}
			temp <<= 1;
		}
	}
}

/********************************************************
//功能描述:显示安符串
//函数入口:asc=要显示字符串,x,y=要显示汉字座标
//函数出口:无
//操    作:往显示缓冲区中写入一串字符
//版	本:V1.00
//作	者:周登勇
//时	间:2004.11.01 13:44
*********************************************************/
void CDispLib::DispStr(S8 *str, S32 x, S32 y, S32 col)
{
	
	if( str )
	{
		U32 count;
		count = 0;
		while(*str)
		{
			if( (*str) & 0x80 )
			{//汉字
				DispHz16(str,x+count*8,y,col);
				str++;
				str++;
				count++;
				count++;
			}
			else
			{//字符
				DispAsc(*str,x+count*8,y,col);
				count++;
				str++;
			}
		}
	}
}

/********************************************************
//功能描述:显示数字
//函数入口:dat= 数字,x,y=起点座标,col=颜色
//			iBefore = 小数点前位数,iBack = 小数点后位数
//函数出口:无
//操    作:往显示缓冲区中写入数字
//版	本:V1.00
//作	者:周登勇
//时	间:2005.03.01 11:12
*********************************************************/
void CDispLib::DispNumb( S32 dat,S32 x,S32 y,S32 iBefore,S32 iBack,S32 col )
{
	//转换
	U32 locat;
	
	if( iBack != 0 )
	{
	locat = iBefore + iBack +2;
	}
	else
	{
		locat = iBefore + iBack + 1;
	}

	if( dat < 0 )
	{
		pBuf[0] = '-';
		dat *= -1;
	}
	else
	{
		pBuf[0] = ' ';
	}

	//结束
	pBuf[ locat ] = '\0';
	locat --;
	//尾数补0
	S32 i;
	if( iBack > 3 )
	{
		for( i=3; i<iBack; i++ )
		{
			pBuf[locat] = '0';
			locat --;
		}

		iBack = 3;
	}
	//移位
	for( i=3; i>iBack; i-- )
	{
		dat /= 10;
	}
	//计算小数点后
	for( i=0; i<iBack; i++ )
	{
		pBuf[locat] = dat%10 + '0';
		locat --;
		dat /= 10;
	}
	//小数点
	if( i != 0 )
	{
		pBuf[locat] = '.';
		locat --;
	}
	//计算小数点前
	for( i=0; i<iBefore; i++ )
	{
		pBuf[locat] = dat%10 + '0';
		locat --;
		dat /= 10;
	}
	DispStr( pBuf,x,y,col );
}

/********************************************************
//功能描述:显示16进制数
//函数入口:dat= 数字,x,y=起点座标,col=颜色
//函数出口:无
//操    作:往显示缓冲区中写入数字
//版	本:V1.00
//作	者:周登勇
//时	间:2007.01.23 14:20
*********************************************************/
void CDispLib::DispHex( U8 dat, S32 x, S32 y, S32 col )
{
	DispAsc( HexList[dat>>4],x,y,col );
	DispAsc( HexList[dat&0xf],x+8,y,col );
}

/********************************************************
//功能描述:画多边形
//函数入口:xy各项点座标,count=顶点个数
//函数出口:无
//操    作:将各个顶点连一直线
//版	本:V1.00
//作	者:周登勇
//时	间:2004.10.27 19:27
//
//注	意:顶点个数必须大于等于3
*********************************************************/
void CDispLib::DrawPoly(S32 *xy, U32 count, U32 col)
{
	if( count<3 )
	{
		return;
	}
	for(U32 i=0; i<count-1;i++)
	{
		DrawLine(*(xy+i*2),*(xy+i*2+1),*(xy+i*2+2),*(xy+i*2+3),col);
	}
	DrawLine(*(xy),*(xy+1),*(xy+count*2-2),*(xy+count*2-1),col);
}

void CDispLib::DrawPoint( S32 x, S32 y, S32 col, S32 width)
{
	switch( width )
	{
	case 0:
	case 1:
		SetPixel( x, y, col );
		break;
	case 2:
		SetPixel( x, y, col );
		SetPixel( x, y+1, col );
		SetPixel( x+1,y,col);
		SetPixel( x+1,y+1,col);
		break;
	default:
		DrawCicle(x,y,width,col);
		break;
	}
}

/********************************************************
//功能描述:画椭圆
//函数入口:x1,x2,y1,y2,矩形顶点
//函数出口:无
//操    作:,画一椭圆
//版	本:V1.00
//作	者:周登勇
//时	间:2004.10.27 18:50
*********************************************************/
void CDispLib::DrawEllipse(S32 x1, S32 y1, S32 x2, S32 y2, S32 col)
{
	S32 a;
	S32 b;
	S32 x;
	S32 y;

	S32 old;
	S32 x0,y0;
#ifdef ARM
	a=abs((F32)x1-(F32)x2);
	b=abs((F32)y1-(F32)y2);
#else
	a=abs(x1-x2);
	b=abs(y1-y2);
#endif
	x0=(x1+x2)/2;
	y0=(y1+y2)/2;
	a/=2;
	b/=2;

	if(a>b)
	{
		old=b;
		for(S32 i=0;i<=a;i++)
		{
			y=(S32)(sqrt((F32)(a*a-i*i))*((F32)b/(F32)a)+0.5);
			for(;y<=old;old--)
			{
				SetPixel(x0+i,y0+old,col);
				SetPixel(x0-i,y0+old,col);
				SetPixel(x0+i,y0-old,col);
				SetPixel(x0-i,y0-old,col);
			}
			old=y;
		}
	}
	else
	{
		old=a;
		for(S32 i=0;i<=b;i++)
		{
			x=(S32)(sqrt((F32)(b*b-i*i))*((F32)a/(F32)b)+0.5);
			for(;x<=old;old--)
			{
				SetPixel(x0+old,y0+i,col);
				SetPixel(x0-old,y0+i,col);
				SetPixel(x0+old,y0-i,col);
				SetPixel(x0-old,y0-i,col);
			}
			old=x;
		}
	}
}

/********************************************************
//功能描述:画直线
//函数入口:x1,y1起点座标,x2,y2终点座标
//函数出口:无
//操    作:在起点和终点之间画一条实线
//版	本:V1.00
//作	者:周登勇
//时	间:2004.10.27 16:57
*********************************************************/
void CDispLib::DrawLine(S32 x1, S32 y1, S32 x2, S32 y2, S32 col)
{
	if( x1 == x2 )
	{//竖线
		if(y1<y2)
		{
			for(;y1<=y2;y1++)
			{
				SetPixel(x1,y1,col);
			}
		}
		else
		{
			for(;y2<=y1;y2++)
			{
				SetPixel(x1,y2,col);
			}
		}
		return;
	}
	if( y1 == y2 )
	{//横线
		if(x1<x2)
		{
			for(;x1<=x2;x1++)
			{
				SetPixel(x1,y1,col);
			}
		}
		else
		{
			for(;x2<=x1;x2++)
			{
				SetPixel(x2,y1,col);
			}
		}
		return;
	}
	F32 a;
	F32 b;
	S32 y;
	S32 x;
	
	
	S32 tempx1;
	S32 tempx2;
	S32 tempy1;
	S32 tempy2;
	
	tempx1 = x1;
	tempx2 = x2;
	tempy1 = y1;
	tempy2 = y2;
	
	a=(F32)(y2-y1)/(F32)(x2-x1);
	b=(F32)y1-a*(F32)x1;
	if(x1<x2)
	{
		for(;x1<=x2;x1++)
		{
			y=(S32)(a*(F32)x1+b);
			SetPixel(x1,y,col);
		}
	}
	else
	{
		for(;x2<=x1;x2++)
		{
			y=(S32)(a*(F32)x2+b);
			SetPixel(x2,y,col);
		}
	}
	
	x1 = tempx1;
	x2 = tempx2;
	y1 = tempy1;
	y2 = tempy2;
	
	a=(F32)(x2-x1)/(F32)(y2-y1);
	b=(F32)x1-a*(F32)y1;
	if(y1<y2)
	{
		for(;y1<=y2;y1++)
		{
			x=(S32)(a*(F32)y1+b);
			SetPixel(x,y1,col);
		}
	}
	else
	{
		for(;y2<=y1;y2++)
		{
			x=(S32)(a*(F32)y2+b);
			SetPixel(x,y2,col);
		}
	}

}

/********************************************************
//功能描述:画矩形
//函数入口:x1,y1顶点座标,x2,y2对角顶点座标
//函数出口:无
//操    作:已知对角顶点座标,画一矩形
//版	本:V1.00
//作	者:周登勇
//时	间:2004.10.27 18:37
*********************************************************/
void CDispLib::DrawRect(S32 x1, S32 y1, S32 x2, S32 y2, S32 col)
{
	if(x1==x2)
	{
		return;
	}
	if(y1==y2)
	{
		return;
	}
	S32 a,b;
	S32 x,y;
	if(x1<x2)
	{
		a=x1;
		b=x2;
	}
	else
	{
		a=x2;
		b=x1;
	}
	if(y1<y2)
	{
		x=y1;
		y=y2;
	}
	else
	{
		x=y2;
		y=y1;
	}
	S32 i;
	for(i=a;i<=b;i++)
	{
		SetPixel(i,y1,col);
		SetPixel(i,y2,col);
	}
	for(i=x;i<=y;i++)
	{
		SetPixel(x1,i,col);
		SetPixel(x2,i,col);
	}
}

/********************************************************
//功能描述:画圆
//函数入口:x1,y1中点座标,r半径
//函数出口:无
//操    作:已知中点座标及半径,画一圆
//版	本:V1.00
//作	者:周登勇
//时	间:2004.10.27 18:50
*********************************************************/
void CDispLib::DrawCicle(S32 x, S32 y, S32 r, U32 col)
{
	S32 temp;
	S32 old=0;
	old=r;
	col = col;

	for(S32 i=0;i<=r;i++)
	{
		temp=(S32)(sqrt((F32)(r*r-i*i))+0.5);

		for(;temp<=old;old--)
		{
		//DrawLine(i+x-1,y+old,i+x,y+temp);
		//old=temp;
		SetPixel(i+x,y+old);
		SetPixel(x-i,y+old);
		SetPixel(x+i,y-old);
		SetPixel(x-i,y-old);
		}
		old=temp;
	}
}

/********************************************************
//功能描述:清空一矩形显示区域
//函数入口:x1,y1,x2,y2=矩形顶点座标
//函数出口:无
//操    作:清空一显示区域
//版	本:V1.00
//作	者:周登勇
//时	间:2005.05.16 11:55
*********************************************************/
void CDispLib::ClrRect( S32 x1, S32 y1, S32 x2, S32 y2 )
{
	S32 i;
	S32 j;
	S32 count;
	U32 locat;

	i = ( x1 + 31 ) / 32;
	count = x2 / 32;
	for( ; i<count; i++ )
	{
		for( j=y1; j<y2; j++ )
		{
			locat = j*(iXSize/32)+i;
			if( locat < (iXSize*iYSize)/8 )
			{
				pDispBuf[ locat ] = 0;
			}
		}
	}

	j = x1 % 32;
	if( j != 0 )
	{
		U32 temp;
		temp = ( ~(0) << ( 32 -j ) );
		j = x1/32;
		for( i=y1; i<y2; i++ )
		{
			locat = i*iXSize/32+j;
			if( locat < (iXSize*iYSize)/8 )
			{
				pDispBuf[ locat ] &= temp;
			}
		}
	}

	j = x2 % 32;
	if( j != 0 )
	{
		U32 temp;
		temp = ~( ~(0) << ( 32 - j ) );
		j = x2/32;
		for( i=y1; i<y2; i++ )
		{
			locat = i*iXSize/32+j;
			if( locat < (iXSize*iYSize)/8 )
			{
				pDispBuf[ locat ] &= temp;
			}
		}
	}
}

⌨️ 快捷键说明

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