嵌入式电子简单电子相册左右滑动

嵌入式电子简单电子相册左右滑动,第1张

嵌入式电子简单电子相册左右滑动

1.通过 *** 作嵌入式的触屏文件

2.打开lcd的文件显示图片

3.使用读取文件来读取照片的bmp,与获取屏幕的下x,y坐标 确定屏幕位置

代码实现:

#include 
#include 
#include 
#include 
#include 
#include //输入设备的头文件

int show_bmp(char *path_name,int *addr)
{

	//读取bmp图片
	int fd_bmp;
	fd_bmp = open(path_name,O_RDWR);
	if(fd_bmp < 0)
	{
		printf("open bmp failn");
		// printf("path_namen");
		// printf("pathn");
		return -1;
	}
	
	lseek(fd_bmp,54,SEEK_SET);//去掉头54个字节
	char buf[800*480*3];
	read(fd_bmp,buf,800*480*3);

	int x,y;
	int i = 0;
	for(y=0;y<480;y++)
	{
		for(x=0;x<800;x++)
			*(addr+(479-y)*800+x) = buf[3*(y*800+x)]|buf[3*(y*800+x)+1]<<8|buf[3*(y*800+x)+2]<<16;
	}
	
	close(fd_bmp);
	return 0;
}

int main()
{
	//打开触摸屏
	int fd_ts;
	fd_ts = open("/dev/input/event0",O_RDWR);
	if(fd_ts < 0)
	{
		printf("open ts failn");
		return -1;
	}
	
	//打开LCD
	int fd_lcd;
	fd_lcd = open("/dev/fb0",O_RDWR);
	if(fd_lcd < 0)
	{
		printf("open lcd failn");
		return -1;
	}
	
		//映射LCD
	int *addr;
	addr = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd_lcd,0);
	if(addr == NULL)
	{
		printf("mmap failn");
		return -1;
	}
	
	//将要显示的图片
	char *path[5] = {"a.bmp","b.bmp","c.bmp","d.bmp","e.bmp"};
	//默认第一张图片
	int i=0;
	show_bmp(path[i],addr);
	
	struct input_event ts;
	int x,y,x1,y1;
	
	while(1)
	{	
		//取出按下去的值
		while(1)
		{	
			read(fd_ts,&ts,sizeof(struct input_event));
			if(ts.type == EV_ABS&&ts.code == ABS_X)
			{
				//printf("x=%d ",ts.value); //蓝色
				x = ts.value;	
			}
			if(ts.type == EV_ABS&&ts.code == ABS_Y)
			{
				//printf("y=%dn",ts.value); //蓝色
				y = ts.value;
			}
			
			if(ts.type == EV_KEY && ts.code == BTN_TOUCH&&ts.value==1)
			{
				printf("按下: %d %dn",x,y);
				break;
			}	
		}
		
		//取出松手后的值
		while(1)
		{	
			read(fd_ts,&ts,sizeof(struct input_event));
			if(ts.type == EV_ABS&&ts.code == ABS_X)
			{
				//printf("x=%d ",ts.value); //蓝色
				x1 = ts.value;	
			}
			if(ts.type == EV_ABS&&ts.code == ABS_Y)
			{
				//printf("y=%dn",ts.value); //蓝色
				y1 = ts.value;
			}
			
			if(ts.type == EV_KEY && ts.code == BTN_TOUCH&&ts.value==0)
			{	
				printf("松手:%d %dn",x1,y1);
				break;
			}	
		}		
		
		//通过比较两者的差值来判断滑动的方向
		//右滑动
		if(x1-x > 50)
		{
			i++;	
			if (i>4) i=0;
			show_bmp(path[i],addr);
			printf("rightn");	
		}
		//左滑动
		if(x1-x < -50)
		{
			i--;
			if (i<0) i=4;
			show_bmp(path[i],addr);
			printf("leftn");
		}	
		//下滑动
		if(y1-y > 50)
		{
			printf("downn");
		}
		//上滑动
		if(y1-y < -50)
		{
			printf("upn");
		}		
	}
	
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存