Day7_SlipTouch

[TOC]

滑动事件

lcd.c

#include"lcd.h"

/*
	Init_LCD:初始化显示屏
	参数为空
	返回值 int*
		成功 返回映射区域的首地址
		失败 返回NULL
*/
int *Init_LCD(int *fd)
{
	//1.打开帧缓冲
	*fd = open("/dev/fb0",O_RDWR);
	if(-1 == *fd)
	{

		perror("open fail");
		return NULL;
	}
	//2.映射
	int *plcd = mmap(NULL,800*480*4
			,PROT_READ |  PROT_WRITE,
			MAP_SHARED,*fd,0);
	if(MAP_FAILED == plcd)
	{
		perror("mmap fail");
		return NULL;
	}
	return plcd;
}

/*
	Uninit_LCD:解初始化屏幕
	@fd:帧缓冲的文件描述符
	@plcd:
	返回值:无
*/
void Uninit_LCD(int fd,int *plcd)
{
	//1.解映射
	munmap(plcd,800*480*4);
	//2.关闭帧缓冲
	close(fd);
}

void Lcd_draw_point(int x,int y,int color,int *plcd)
{
    if(NULL == plcd)
    {
        printf("error:plcd == NULL\n");
        return ;
    }
    if(x>=0&&x<800&&y>=0&&y<480)
    {
   		*(plcd+800*y+x) = color;
	}
}

/*
	Bmp_display:在屏幕的指定的位置显示bmp图片
	@bmp_file:图片的路径名
	@x0 y0 图片左上角在屏幕上的坐标
	@plcd:帧缓冲映射区域的首地址
	返回值:
		-1 失败
		0  成功
*/
int Bmp_display(const char *bmp_file,int x0,int y0,int *plcd)
{
	if(plcd == NULL || !(x0>=0&&x0<800&&y0>=0&&y0<480))
	{
		return -1;
	}
	//1.打开图片
	int fd = open(bmp_file,O_RDONLY);
	if(-1 == fd)
	{
		printf("%s",bmp_file);
		perror("open fail");
		return -1;
	}
	//2.判断到底是不是一张bmp图片
	char buf[4]={0};
	read(fd,buf,2);
	if(buf[0]!=0x42 || buf[1]!=0x4D)
	{
		printf("NOT BMP\n");
		close(fd);
		return -1;
	}
	//3.解析图片 宽 高 色深
	lseek(fd,0x12,SEEK_SET);
	read(fd,buf,4);
	int width = buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0];

	lseek(fd,0x16,SEEK_SET);
	read(fd,buf,4);
	int height = buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0];

	lseek(fd,0x1c,SEEK_SET);
	read(fd,buf,2);
	short depth = buf[1]<<8 | buf[0];

	if(!(depth == 24 || depth == 32))
	{
		printf("NOT SUPPORT!\n");
		close(fd);
		return -1;
	}

	printf("%s:%d*%d depth:%d\n",bmp_file,width,height,depth);
	//4.获取像素数组
	int line_vaild_bytes=abs(width)*depth/8;
	int line_bytes;//一行总字节数=有效字节数+赖子数
	int laizi = 0;

	if(line_vaild_bytes%4)
	{
		laizi = 4 - line_vaild_bytes%4;
	}
	line_bytes = line_vaild_bytes + laizi;
	int total_bytes = line_bytes*abs(height);//整个像素数组的大小

	//从文件中读取像素数组
	lseek(fd,54,SEEK_SET);
	unsigned char piexl[total_bytes];
	read(fd,piexl,total_bytes);
	//5.在屏幕的对应位置显示即可
	unsigned char a,r,g,b;
	int color;
	int i=0;
	int x,y;
	//遍历整个像素数组
	for(y=0;y<abs(height);y++)
	{
  		for(x=0;x<abs(width);x++)
  		{
			b=piexl[i++];
			g=piexl[i++];
			r=piexl[i++];
    		if(depth==32)
			{
				a=piexl[i++];
			}
    		else
			{
				a=0;//不透明
			}
    		color=a<<24|r<<16|g<<8|b;
    		Lcd_draw_point(width>0?x+x0:x0+abs(width)-x-1,
          	height>0?y0+abs(height)-y-1:y0+y,color,plcd);
		}
  		//每一行末尾可能存在赖子
  		i+=laizi;
	}
	close(fd);
	return 0;
}

lcd.h

#ifndef __LCD_H__
#define __LCD_H__

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>


/*宏定义*/
#define FILE_PATH "/dev/fb0"

/*函数声明*/
int *Init_LCD(int *fd);
void Uninit_LCD(int fd,int *plcd);
void Lcd_draw_point(int x,int y,int color,int *plcd);
int Bmp_display(const char *bmp_file,int x0,int y0,int *plcd);

#endif

touch.h

#ifndef __TOUCH_H__
#define __TOUCH_H__

#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/input.h>//输入子系统


#define UP    1
#define DOWN  2
#define LEFT  3
#define RIGHT 4

/*按一下屏幕,终端打印屏幕坐标*/
void getTouch(int* x,int* y);

int getTouchWay();

#endif

touch.c

int getTouchWay()
{
	int x_start = -1;
	int y_start = -1;
	int x_end = -1;
	int y_end = -1;
    //1.打开触摸屏
    int fd = open("/dev/input/event0",O_RDONLY);
    if(-1 == fd)
    {
        perror("open ScreenTouch failed\n");
        return -1;
    }

    //2.操作
    struct input_event ev;
    while(1)
    {
		int ev_long=read(fd,&ev,sizeof(ev));
		if(ev_long != sizeof(ev))
		{
			continue;
		}
		//
		// if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0)
		// {
		// 	break;
		// }
		if(ev.type == EV_ABS)
		{
			// if(ev.code == ABS_PRESSURE && ev.value == 0)
			// {
			// 	break;
			// }
			if(ev.code == ABS_X)
			{
				if(x_start == -1)
				{
					x_start = ev.value;
				}
				x_end = ev.value;
			}
			if(ev.code == ABS_Y)
			{
				if(y_start == -1)
				{
					y_start = ev.value;
				}
				y_end = ev.value;
			}
		}
		if(abs(x_end - x_start) > abs(y_end - y_start))
		{
			if(x_end - x_start > 0)
			{
				printf("RIGHT\n");
				return RIGHT;
			}
			else
			{
				printf("LEFT\n");
				return LEFT;
			}
			
		}
		if(abs(x_end - x_start) < abs(y_end - y_start))
		{
			if(y_end - y_start > 0)
			{
				printf("DOWN\n");
				return DOWN;
			}
			else
			{
				printf("UP\n");
				return UP;
			}
		}
	}
}

main.c

#include"touch.h"
#include"lcd.h"
int main(int argc,char **argv)
{
	int fd=-1;
	int* plcd = Init_LCD(&fd);
	int x=-1,y=-1;
    long int i=0;
    int flag=0;
    char* str[]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp","6.bmp","7.bmp","8.bmp","9.bmp","10.bmp"};
	//初始化屏幕
    for(y=0;y<480;y++)
	{
		for(x=0;x<800;x++)
		{
			Lcd_draw_point(x,y,0x58962564,plcd);
		}
	} 
    Bmp_display("start.bmp",300,180,plcd);
    
        
    while(1)
    {
        getTouch(&x,&y);
        printf("( %d , %d )\n",x,y);
        if(300<x&&500>x&&180<y&&300>y)
        {
            
            flag=1;
            break;
        }  
    }
    while(flag)
    { 
        int act= getTouchWay();
        Bmp_display(str[i],0,0,plcd);
        if(3==act)
        {
            i--;
            if(i<0)
            {
                i=9;
            }
        }
        if(4==act)
        {
            i++;
            if(10==i)
            {
                i=0;
            }
        }
        if(1==act)
        {
            break;
        }
    }
    
	Uninit_LCD(fd,plcd);
    //getTouch();
}