1. 程式人生 > >圖片播放器(九):觸控式螢幕處理

圖片播放器(九):觸控式螢幕處理

input事件解析:

#####################    type = 3 code = 0 value = 724    //讀取的X座標的值
                                              type = 3 code = 1 value = 328   //讀取的Y座標的值
                                              type = 1 code = 330 value = 1   //330 是觸控式螢幕的按鍵值
#####################    type = 3 code = 24 value = 1     //24   表示壓力值,驅動值定義的壓力值範圍是 0 到 1
                                              type = 0 code = 0 value = 0      //同步訊號
                                              type = 1 code = 330 value = 0 
#####################    type = 3 code = 24 value = 0   //表示彈起
                                              type = 0 code = 0 value = 0    // 下面連續傳送9次同步資訊,作為結束髮送
                                              type = 0 code = 2 value = 0 
#####################    type = 0 code = 0 value = 0 
                                              type = 0 code = 2 value = 0 
                                              type = 0 code = 0 value = 0 
#####################   type = 0 code = 2 value = 0 
                                             type = 0 code = 0 value = 0 
                                             type = 0 code = 2 value = 0 
#####################  type = 0 code = 0 value = 0

在inut.h中

/*
 * Event types
 */
#define EV_SYN			0x00  //表示支援所有事件
#define EV_KEY			0x01  //鍵盤或者按鍵,表示一個鍵碼
#define EV_REL			0x02  //滑鼠裝置,表示一個相對的游標位置結果
#define EV_ABS			0x03  //手寫板產生的值,其是一個絕對整數值
#define EV_MSC			0x04  //其他型別
#define EV_SW			0x05  //轉換
#define EV_LED			0x11  //led
#define EV_SND			0x12  //聲音裝置
#define EV_REP			0x14  //允許重複輸入按鍵型別
#define EV_FF			0x15
#define EV_PWR			0x16  //電源管理
#define EV_FF_STATUS		0x17
#define EV_MAX			0x1f
#define EV_CNT			(EV_MAX+1)

 

 

操作定義:

/* 觸控式螢幕的操作模式 */
enum to_operating_mode
{
	up_switchover = 1,    /* 向上翻頁 */
	down_switchover,  /* 向下翻頁 */
	left_switchover,  /* 向左翻頁 */
	right_switchover, /* 向右翻頁 */
	add_file,         /* 新增檔案 */
	delete_file,      /* 刪除檔案 */
};

資訊結構體:

struct read_info
{
	unsigned int x_coordinate; /* x座標*/
	unsigned int y_coordinate; /* y座標*/
	long         event_timr;   /* 觸發時間 */
};

全域性變數和巨集定義:

/* 觸控式螢幕檔案 */
#define DEVICE_KEY 	"/dev/input/event1"

struct input_event env1;  /* input 事件 */
struct read_info read_arry[READ_MAX_NUM] = {{0}};  /*把從觸控式螢幕讀到的有效資料儲存到這裡 */
int now_num = 0;  /* 現在讀取了多少個數據 */

核心處理函式:用來做總的管理

/* 核心處理函式 */
int tu_core(void)
{
	int ret = -1;
	tu_read_touch();

	ret = tu_is_judge();

	return ret;
}

 

讀取觸控式螢幕資訊並轉化為相應的可用資訊:

/* 讀取觸控式螢幕資訊 */
int tu_read_touch(void)
{
	int fd1 = -1;
	int ret = -1;
	int i = 0;
	int arry_num = 0;
	int end_num  = 4;  /* 讀取結束標誌,因為結束時會發送4次type = 0,code = 2*/
/* 結束資訊
type = 0 code = 0 value = 0 
type = 0 code = 2 value = 0 
type = 0 code = 0 value = 0 
type = 0 code = 2 value = 0 
type = 0 code = 0 value = 0 
type = 0 code = 2 value = 0 
type = 0 code = 0 value = 0 
type = 0 code = 2 value = 0 
type = 0 code = 0 value = 0 
*/

	fd1 = open(DEVICE_KEY, O_RDONLY);
	if(fd1 < 0){
		DERROR("open1\n");
		return -1;
	}
	
	for(;i < READ_MAX_NUM; i++)
	{
		ret = read(fd1, &env1, sizeof(struct input_event));
		if(ret <= 0){
			return -1;
		}

		if(end_num == 0)
			break;
		if((env1.type == 3) && (env1.code == 0)){
			read_arry[arry_num].x_coordinate = env1.value;
		}else if((env1.type == 3) && (env1.code == 1)){
		    read_arry[arry_num].y_coordinate = env1.value;			
		    read_arry[arry_num].event_timr = env1.value;
			++arry_num;
		}else if((env1.type == 0) && (env1.code == 2)){
			--end_num;
		}
		memset(&env1, 0, sizeof(struct input_event));	
	}

	/* 作為結束標誌 */
	read_arry[arry_num].x_coordinate = read_arry[arry_num].y_coordinate = read_arry[arry_num].event_timr = 0;
	now_num = arry_num;

	DEBUG("ok-------------\n");
	close(fd1);
	return 0;
}

 

判斷是哪種操作:

/* 判斷是哪種操作 */
int tu_is_judge(void)
{
	int x_add = 0;
	int y_add = 0;
	int i = 0;

	for(; i<now_num - 1;  i++)
	{
		x_add += read_arry[i].x_coordinate - read_arry[i + 1].x_coordinate;
		y_add += read_arry[i].y_coordinate - read_arry[i + 1].y_coordinate;
		//DEBUG("x = %d , y = %d \n", read_arry[i].x_coordinate, read_arry[i].y_coordinate);
	}

	
	DEBUG("x_add = %d , y_add = %d \n", x_add, y_add);
	if((x_add > 0) && (abs(x_add) > abs(y_add))){  /* 向左翻頁 */
		return left_switchover;
	}else if((x_add < 0) && (abs(x_add) > abs(y_add))){  /* 向右翻頁 */
		return right_switchover;
	}else if((y_add < 0) && (abs(x_add) < abs(y_add))){  /* 向下翻頁 */
		return down_switchover;
	}else if((y_add > 0) && (abs(x_add) < abs(y_add))){  /* 向上翻頁 */
		return up_switchover;
	}


	return 0;

}