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

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

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

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

代码实现:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <linux/input.h>//输入设备的头文件

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 fail\n");
		// printf("path_name\n");
		// printf("path\n");
		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 fail\n");
		return -1;
	}
	
	//打开LCD
	int fd_lcd;
	fd_lcd = open("/dev/fb0",O_RDWR);
	if(fd_lcd < 0)
	{
		printf("open lcd fail\n");
		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 fail\n");
		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=%d\n",ts.value); //蓝色
				y = ts.value;
			}
			
			if(ts.type == EV_KEY && ts.code == BTN_TOUCH&&ts.value==1)
			{
				printf("按下: %d %d\n",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=%d\n",ts.value); //蓝色
				y1 = ts.value;
			}
			
			if(ts.type == EV_KEY && ts.code == BTN_TOUCH&&ts.value==0)
			{	
				printf("松手:%d %d\n",x1,y1);
				break;
			}	
		}		
		
		//通过比较两者的差值来判断滑动的方向
		//右滑动
		if(x1-x > 50)
		{
			i++;	
			if (i>4) i=0;
			show_bmp(path[i],addr);
			printf("right\n");	
		}
		//左滑动
		if(x1-x < -50)
		{
			i--;
			if (i<0) i=4;
			show_bmp(path[i],addr);
			printf("left\n");
		}	
		//下滑动
		if(y1-y > 50)
		{
			printf("down\n");
		}
		//上滑动
		if(y1-y < -50)
		{
			printf("up\n");
		}		
	}
	
	return 0;
}

  • 2
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值