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

📄 display.c

📁 这是一个基于frvlinux开发系统的lcd的图形显示的API函数的示例~
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <linux/fr400cc_vdc.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include	<math.h>
#include	"zifu.h"
#define	MAX_X 320
#define	MAX_Y 240
static	int	fd=0;
static	int	pix_x=0;
static	int	pix_y=0;
static	int	pix_sz=0;
static	unsigned	char 	*data=NULL; 
static	int backcolor[3]={233,255,0};
static	int color[3]={128,0,128};
int		point_all[2]={0};
static	unsigned	long 	index_char_dis=0;

/*      set backcolor   */
void	set_backcolor(void)
{
	int x,y;
	ioctl(fd,VDCIOCSDAT,0);
	for(x=0;x<MAX_X;x++)
		for(y=0;y<MAX_Y;y++)
			{
				data[y*320*3+x*3]=backcolor[0];
				data[y*320*3+x*3+1]=backcolor[1];
				data[y*320*3+x*3+2]=backcolor[2];
			}
}

void	clrscr(void)
{
	int x,y;
	for(x=0;x<MAX_X;x++)
		for(y=0;y<MAX_Y;y++)
			{
				data[y*320*3+x*3]=backcolor[0];
				data[y*320*3+x*3+1]=backcolor[1];
				data[y*320*3+x*3+2]=backcolor[2];
			}
}
/*	draw a point 	*/
void 	draw_point(int	x,int	y)
{
	ioctl(fd,VDCIOCSDAT,0);
	data[y*320*3+x*3]=color[0];
	data[y*320*3+x*3+1]=color[1];
	data[y*320*3+x*3+2]=color[2];
}

/*	init the lcd	*/
int	lcdinit(void)
{
	struct	fr400vdc_config	cfg={320,240,3,0,320*240*3,1,1,1,0,320,1,1,1,0,240,1,1,1,0,0,6,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,3,3,2,1};
	fd=open("/dev/fr400cc_vdc",O_RDONLY|O_NONBLOCK);//open the device
	ioctl(fd,VDCIOCSCFG,&cfg);
	data=mmap(0,(cfg.pix_x)*(cfg.pix_y)*(cfg.pix_sz),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	set_backcolor();//set backcolor
	ioctl(fd,VDCIOCSTART,0);//open the lcd
}

/*	move to a new point	*/
void	moveto(int x,int y)
{
	point_all[0]=x;
	point_all[1]=y;
}

/*	swap two data (used int draw line )	*/
void	swap(int       *x,int  *y)
{
	int	*tmp=NULL;
	*tmp=*x;*x=*y;*y=*tmp;
}

/*	draw a line 	*/
/*	suppose the line is y=k*x+b	*/
int	line(int	x1,int y1,int x2,int y2)
{
	int	delta_x,delta_y,p,p0,x,y;
	point_all[0]=x2;
	point_all[1]=y2;
	delta_x=abs(x1-x2);
	delta_y=abs(y1-y2);
	/*	if k=-1	*/
	if((x1-x2)==(y2-y1))
	{	
		if(x1>x2)
		{swap(&x1,&x2);swap(&y1,&y2);}
		y=y1;
		for(x=x1;x<=x2;x++)
		{
			draw_point(x,y);
			y--;
		}
	}
	/*	if k=1	*/
	if((x1-x2)==(y1-y2))
	{	y=y1;
		if(x1>x2)
		{swap(&x1,&x2);swap(&y1,&y2);}
		for(x=x1;x<=x2;x++)
		{	
			draw_point(x,y);			
			y++;
		}
	}
	/*	if	k>1	*/
	if(delta_y>delta_x)
	{
			swap(&delta_x,&delta_y);
			swap(&x1,&y1);
			swap(&x2,&y2);

		p=2*(delta_y-delta_x);
		p0=p;
		y=y1;
		draw_point(x,y);
		for(x=x1;x<x2;x++)
		{
			if(p<0)
			{
				p+=2*delta_y;
				draw_point(y,x+1);
			}
			else
			{
				p=p+p0;
				y++;
				draw_point(y,x+1);
			}
		}
		return 0;
	}
	/*	if k<1	*/
	if(delta_y<delta_x)
	{
		p=2*(delta_y-delta_x);
		p0=p;
		y=y1;
		
		draw_point(x,y);
		for(x=x1;x<x2;x++)
		{
			if(p<0)
			{
				p+=2*delta_y;
				draw_point(x+1,y);
			}
			else
			{
				p=p+p0;
				y++;
				draw_point(x+1,y);
			}
		}
	}
	
}
/*	draw a line from the crrunt point to a new point	*/
void	lineto(int x,int	y){	
	line(point_all[0],point_all[1],x,y);
	point_all[0]=x;
	point_all[1]=y;
}

/*	draw a circle	using Bresenham method	*/
void	print_circle(int x,int	y ,int *p)
{
	draw_point(x-p[0],y+p[1]);
	draw_point(x+p[0],y+p[1]);
	draw_point(x+p[0],y-p[1]);
	draw_point(x-p[0],y-p[1]);
	draw_point(x-p[1],y-p[0]);
	draw_point(x-p[1],y+p[0]);
	draw_point(x+p[1],y+p[0]);
	draw_point(x+p[1],y-p[0]);
}
/*	draw a circle	*/
void	circle(int	xc,int yc,int radius)
{
		int	point[2]={0,radius};
		int	p0,p;
		p=1-radius;
		print_circle(xc,yc,point);
		while(point[0]<=point[1])
		{
			point[0]++;
			if(p<0)
			{
				p=p+2*point[0]+1;
				print_circle(xc,yc,point);
			}
			else
			{
				point[1]--;
				p=p+2*point[0]+1-2*point[1];
				print_circle(xc,yc,point);
			}
		}
}

/*	set the color of background		*/
void	setbackcolor(int	*p)
{
	backcolor[0]=p[0];
	backcolor[1]=p[1];
	backcolor[2]=p[2];
	set_backcolor();
}

/*	set the color of line color	*/
void	setcolor(int	*p)
{
	color[0]=p[0];
	color[1]=p[1];
	color[2]=p[2];
}

/*	close the lcd	*/
void	closelcd(void)
{
	ioctl(fd,VDCIOCSTOP,0);
}

/*	display 	a bmp picture	*/
void	bmpdisp(	FILE	*fp)
{
	int	x,y;
	/*	apply a data area 		*/
	unsigned	char *lcd_data=malloc(320*240*3);
	unsigned	char tmp=0;
	fseek(fp,0,SEEK_SET);
	//fgetpos(fp,
	fread(lcd_data,1,320*240*3,fp);
	/*	exchange the data in the righe order	*/
	for(x=0;x<MAX_X;x++)
	{
		for(y=0;y<(MAX_Y/2);y++)
		{
			tmp=lcd_data[(MAX_Y-y)*320*3+x*3+2];
			lcd_data[(MAX_Y-y)*320*3+x*3+2]=lcd_data[y*320*3+x*3];
			lcd_data[y*320*3+x*3]=tmp;
			tmp=lcd_data[(MAX_Y-y)*320*3+x*3+1];
			lcd_data[(MAX_Y-y)*320*3+x*3+1]=lcd_data[y*320*3+x*3+1];
			lcd_data[y*320*3+x*3+1]=tmp;
			tmp=lcd_data[(MAX_Y-y)*320*3+x*3];
			lcd_data[(MAX_Y-y)*320*3+x*3]=lcd_data[y*320*3+x*3+2];
			lcd_data[y*320*3+x*3+2]=tmp;	
		}
		tmp=lcd_data[MAX_Y/2*320*3+x*3+2];
		lcd_data[MAX_Y/2*320*3+x*3+2]=lcd_data[MAX_Y/2*320*3+x*3];
		lcd_data[MAX_Y/2*320*3+x*3]=tmp;
	}
	bcopy(lcd_data,data,320*240*3);
}

void	 char_dis(unsigned char characte)
{
	int	char_y,char_x;
	int	x,y;
 	characte=characte-0x20;
  char_y=index_char_dis/40;
	char_x=index_char_dis%40;
	for(y=0;y<16;y++){
		for(x=0;x<8;x++){
		if((char_data[characte][y]>>(7-x))&0x01==1)
			{
				data[(char_y*16+y)*320*3+(char_x*8+x)*3]=color[0];
				data[(char_y*16+y)*320*3+(char_x*8+x)*3+1]=color[1];
				data[(char_y*16+y)*320*3+(char_x*8+x)*3+2]=color[2];
			}
		else
		{
			data[(char_y*16+y)*320*3+(char_x*8+x)*3]=backcolor[0];
			data[(char_y*16+y)*320*3+(char_x*8+x)*3+1]=backcolor[1];
			data[(char_y*16+y)*320*3+(char_x*8+x)*3+2]=backcolor[2];
		}
		}
	}
	index_char_dis++;
}
void	set_char_pos(int	x,int	y){
	index_char_dis=x+y*40;
}

⌨️ 快捷键说明

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