日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

GEC6818开发板——触摸屏

發(fā)布時(shí)間:2024/1/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GEC6818开发板——触摸屏 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、觸摸屏(touch screen =TS)
1.分類
電阻觸摸屏:壓力感應(yīng) x+ x- y+ y-
電容觸摸屏:電壓感應(yīng) vcc gnd int(中斷) rst(復(fù)位) scl(i2c的時(shí)鐘) sda(i2c的數(shù)據(jù))

2.觸摸屏的文件路徑名
/dev/input/event0

?

3.觸摸屏有哪些信息
1)需要讀取觸摸屏的設(shè)備文件信息
2)設(shè)備文件信息包含3個(gè)信息 type\code\value

4.設(shè)備文件信息
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/05/code/zuoye$ vi /usr/include/linux/input.h?
?23 struct input_event {
?24 ? ? struct timeval time;
?25 ? ? __u16 type;
?26 ? ? __u16 code;
?27 ? ? __s32 value;
?28 };

說(shuō)明(頭文件的使用方法):
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/06/code$ ls /usr/include/stdio.h
/usr/include/stdio.h ? ?----->#include <stdio.h>
gec@ubuntu:/mnt/hgfs/GZ2264/6_文件IO/06/code$ ls /usr/include/linux/input.h?
/usr/include/linux/input.h ? ----->#include <linux/input.h>

5.怎樣讀取觸摸屏的設(shè)備文件
struct input_event ts;
read(fd,&ts,sizeof(struct input_event));
練習(xí)1:
通過(guò)指針傳參來(lái)取值---指針的高級(jí)用法1
int get_value(int *x,int *y)
{
?? ?*x = 100;
?? ?*y = 250;?? ?
?? ?return 0;//不用通過(guò)返回值就可以從子函數(shù)中取值
}
int main()
{
?? ?int num1=0,num2=0; //注意這里不要定義成int *num1與int *num2
?? ?get_value(&num1,&num2);
?? ?printf("num1=%d num2=%d\n",num1,num2);
?? ?return 0;
}

6.分析觸摸屏讀取的type\code\value的值
練習(xí)2:(代碼要在開發(fā)板上面執(zhí)行)
1)讀取一遍 type\code\value的值是什么?

//打開觸摸屏int fd;fd = open("/dev/input/event0",O_RDWR);//要用系統(tǒng)IO open函數(shù)打開硬件設(shè)備文件if(fd < 0){perror("open ts fail");return 0;}//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)在ts中struct input_event ts;while(1){ //讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS &&ts.code == ABS_X){//printf("x=%d ",(int)(ts.value*0.78)); //黑色板x軸的坐標(biāo)值printf("x=%d ",ts.value*800/1024); //黑色板x軸的坐標(biāo)值//printf("x=%d ",ts.value); //藍(lán)色板x軸的坐標(biāo)值}if(ts.type == EV_ABS &&ts.code == ABS_Y){//printf("y=%d\n",(int)(ts.value*0.8)); //黑色板y軸的坐標(biāo)值printf("y=%d\n",ts.value*480/600); //黑色板y軸的坐標(biāo)值//printf("y=%d\n",ts.value); //藍(lán)色板y軸的坐標(biāo)值}} //關(guān)閉觸摸屏close(fd);


2)死循環(huán)讀取type\code\value的值是什么,觀察規(guī)律?
ts.type=3
ts.code=0
ts.value=493 ?//觸摸屏x軸的坐標(biāo)
ts.type=3
ts.code=1
ts.value=286 //觸摸屏y軸的坐標(biāo)
ts.type=1
ts.code=330
ts.value=1 ? ?//按鍵按下去
ts.type=1
ts.code=330
ts.value=0 ? ? //按鍵松手后

說(shuō)明:
黑色的板觸摸屏分辨率:1024*460;如果是這種,它的分辨率和LCD不一致,需要軟件轉(zhuǎn)換
藍(lán)色的板觸摸屏分辨率:800*480;如果是這種,它的分辨率和LCD一致,不需要軟件轉(zhuǎn)換

7.如何獲取觸摸屏里面的x軸和y軸的值
//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)
read(fd,&ts,sizeof(struct input_event));
if(ts.type == 3 &&ts.code == 0)
?? ?printf("x=%d ",ts.value); ?//x軸的坐標(biāo)值
if(ts.type == 3 &&ts.code == 1)
?? ?printf("y=%d\n",ts.value); //y軸的坐標(biāo)值
練習(xí)3:用代碼分別獲取觸摸屏5個(gè)點(diǎn)的坐標(biāo)(4個(gè)邊角點(diǎn)和1個(gè)中心點(diǎn))

8.type、code、value的含義
type:輸入事件的類型
code:輸入事件的編碼
value:輸入事件的值(也是我們開發(fā)人員需要拿到的值)

type:
#define EV_ABS?? ?0x3 ?//觸摸屏事件類型
#define EV_KEY?? ?0x1 //按鍵事件類型
code:
#define ABS_X?? ?0x0 //x軸編碼
#define ABS_Y?? ?0x1 //y軸編碼
#define BTN_TOUCH?? ? 0x14a ?//按鍵編碼

搜索指令grep
gec@ubuntu:/usr/include/linux$ grep -rn "EV_ABS" ?//搜索字符串"EV_ABS"
input-event-codes.h:40:#define EV_ABS?? ??? ??? ?0x03
gec@ubuntu:/usr/include/linux$ vi input-event-codes.h +40 //打開這個(gè)文件 直接跳到第30行

思考問(wèn)題:
1)為什么有時(shí)候一直打印x軸的值
? 當(dāng)你橫著滑動(dòng)屏幕的時(shí)候,此時(shí)y軸的值沒(méi)有產(chǎn)生變化,輸入子系統(tǒng)認(rèn)為你y軸的編碼事件沒(méi)有變化,不打印y軸值
2)為什么有時(shí)候一直打印y軸的值
?當(dāng)你豎著滑動(dòng)屏幕的時(shí)候,此時(shí)x軸的值沒(méi)有產(chǎn)生變化,輸入子系統(tǒng)認(rèn)為你x軸的編碼事件沒(méi)有變化,不打印x軸值
3)怎樣點(diǎn)擊觸摸屏讓它x和y軸的值成對(duì)出現(xiàn)
?當(dāng)你斜著滑動(dòng)屏幕的時(shí)候,輸入子系統(tǒng)認(rèn)為你x軸和y軸的編碼事件都有變化,都打印。

9.如何將觸摸屏的分辨率轉(zhuǎn)換成LCD的分辨率一模一樣(強(qiáng)制類型轉(zhuǎn)換)
//printf("x=%d ",(int)(ts.value*0.78)); ?//黑色板x軸的坐標(biāo)值
printf("x=%d ",ts.value*800/1024); ?//黑色板x軸的坐標(biāo)值
//printf("y=%d\n",(int)(ts.value*0.8)); //黑色板y軸的坐標(biāo)值
printf("y=%d\n",ts.value*480/600); //黑色板y軸的坐標(biāo)值

10.按下去和松手后的操作識(shí)別(如何避免點(diǎn)下去的時(shí)候出現(xiàn)好幾次打印)
1)單片機(jī):
按鍵消抖:按下去的時(shí)候延時(shí)一會(huì)兒,再檢測(cè)電平
松手檢測(cè):松手的時(shí)候延時(shí)一會(huì)兒,再檢測(cè)電平
2)linux:
這里沒(méi)有按鍵消抖和松手檢測(cè)這個(gè)概念,它將按下去和松手后封裝成兩個(gè)事件
按下去的事件
if(ts.type==EV_KEY && ts.code==BTN_TOUCH && ts.value == 1)//按下去
{
?? ?printf("按下去 x=%d y=%d\n",x,y);
}?
松手后的事件
if(ts.type==EV_KEY && ts.code==BTN_TOUCH && ts.value == 0)//松手
{
?? ?printf("松手 x=%d y=%d\n",x,y);
}

練習(xí)4:
點(diǎn)擊屏幕左邊顯示left 點(diǎn)擊屏幕右邊顯示right

//打開觸摸屏int fd;fd = open("/dev/input/event0",O_RDWR);//要用系統(tǒng)IO open函數(shù)打開硬件設(shè)備文件if(fd < 0){perror("open ts fail");return 0;}//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)在ts中struct input_event ts;int x=0,y=0;while(1){ //讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS &&ts.code == ABS_X){printf("x=%d ",ts.value*800/1024); //黑色板x軸的坐標(biāo)值//printf("x=%d ",ts.value); //藍(lán)色板x軸的坐標(biāo)值}if(ts.type == EV_ABS &&ts.code == ABS_Y){printf("y=%d\n",ts.value*480/600); //黑色板y軸的坐標(biāo)值//printf("y=%d\n",ts.value); //藍(lán)色板y軸的坐標(biāo)值}/* //方法一:if(x<400) //有可能會(huì)出現(xiàn)多次打印left或right的情況printf("left\n");if(x>400)printf("right\n"); */if(ts.type==EV_KEY && ts.code==BTN_TOUCH && ts.value == 1)//按下去{//方法二printf("按下去 x=%d y=%d\n",x,y);if(x<400) printf("left\n");if(x>400)printf("right\n");} } //關(guān)閉觸摸屏close(fd);

10.滑動(dòng)的原理
左右滑動(dòng):按下和松手后x軸的坐標(biāo)差值
上下滑動(dòng):按下和松手后y軸的坐標(biāo)差值
1)抓取按下的坐標(biāo)值
while(1)
{?? ?
?? ?//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)
?? ?read(fd,&ts,sizeof(struct input_event));
?? ?if(ts.type == EV_ABS &&ts.code == ABS_X)
?? ?{
?? ??? ?//printf("x=%d ",ts.value*800/1024); ?//黑色板x軸的坐標(biāo)值
?? ??? ?x = ts.value*800/1024;
?? ?}
?? ?if(ts.type == EV_ABS &&ts.code == ABS_Y)
?? ?{
?? ??? ?//printf("y=%d\n",ts.value*480/600); //黑色板y軸的坐標(biāo)值?? ?
?? ??? ?y = ts.value*480/600;
?? ?}
?? ?//按下去的時(shí)刻或者松手后的時(shí)刻
?? ?if(ts.type==EV_KEY && ts.code==BTN_TOUCH && ts.value == 1)//按下去
?? ?{
?? ??? ?printf("按下去 x=%d y=%d\n",x,y);
?? ??? ?break;
?? ?} ?? ?
}?? ?
2)抓取松手后的坐標(biāo)值
while(1)
{?? ?
?? ?//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)
?? ?read(fd,&ts,sizeof(struct input_event));
?? ?if(ts.type == EV_ABS &&ts.code == ABS_X)
?? ?{
?? ??? ?//printf("x=%d ",ts.value*800/1024); ?//黑色板x軸的坐標(biāo)值
?? ??? ?x1 = ts.value*800/1024;
?? ?}
?? ?if(ts.type == EV_ABS &&ts.code == ABS_Y)
?? ?{
?? ??? ?//printf("y=%d\n",ts.value*480/600); //黑色板y軸的坐標(biāo)值?? ?
?? ??? ?y1 = ts.value*480/600;
?? ?}
?? ?if(ts.type==EV_KEY && ts.code==BTN_TOUCH && ts.value == 0)//松手
?? ?{
?? ??? ?printf("松手 x1=%d y1=%d\n",x1,y1);
?? ??? ?break;
?? ?}?
}?? ?

?

練習(xí)5:
將點(diǎn)擊的代碼和滑動(dòng)合在一起

#include "my_head.h"enum SLIDE{left,right,up,down}; enum TOUCH{left_area,right_area};int fd; //獲取點(diǎn)擊區(qū)域位置 int touch() {//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)到ts中struct input_event ts;//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)int x,y;while(1){read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x = ts.value*800/1024;//x軸坐標(biāo)}if(ts.type == EV_ABS && ts.code == ABS_Y){y = ts.value*480/600;//y軸坐標(biāo)}//按下去的時(shí)刻或者松手后的時(shí)刻if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 0)//松手后{if(x <400)return left;elsereturn right;}} }//拿去上下左右滑動(dòng)狀態(tài) int slide() {//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)到ts中struct input_event ts;int x,y,x1,y1;while(1){while(1){read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x = ts.value*800/1024;}if(ts.type == EV_ABS && ts.code == ABS_Y){y = ts.value*480/600;}//按下去的時(shí)刻或者松手后的時(shí)刻if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 1)//按下去{printf("按下去 (%d,%d)\n",x,y);break;}}//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)while(1){read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x1 = ts.value*800/1024;}if(ts.type == EV_ABS && ts.code == ABS_Y){y1 = ts.value*480/600;}if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 0)//松手{printf("松手后 (%d,%d)\n",x1,y1);break;}}if(x1-x > 100)return right;if(x1-x < -100)return left;if(y1-y > 100)return down;if(y1-y < -100)return up;}return 0; }int main(int argc,char *argv[]) {//打開觸摸屏fd = open("/dev/input/event0",O_RDWR);if(fd < 0){perror("open fd error!\n");return 0;}while(1){//點(diǎn)擊switch(touch()){case left:printf("touch left!\n");break;case right:printf("touch right!\n");break;}//滑動(dòng)switch(slide()){case up:printf("up slide!\n");break;case down:printf("down slide!\n");break;case left:printf("left slide!\n");break;case right:printf("right slide!\n");break;}}//關(guān)閉觸摸屏close(fd);return 0; }

練習(xí)6:
使用滑動(dòng)來(lái)切換圖片
?

#include "my_head.h"enum SLIDE{left,right,up,down}; int fd,fd_lcd;int show_pic(int *p,char *bmp_path) {//打開bmp圖片int bmp = open(bmp_path,O_RDWR);if(bmp < 0){printf("open bmp fail!\n");return -1;}//去除掉頭54個(gè)字節(jié)lseek(bmp,54,SEEK_SET);//存儲(chǔ)bmp圖片的buffer:800*480*3char buf[800*480*3] = {0};int ret1 = read(bmp,buf,800*480*3); sleep(1);//讀bmp圖片//將buf數(shù)據(jù)通過(guò)指針p填充到LCD中int x;//x表示橫軸int y;//y表示縱軸for(y=0;y<480;y++){for(x=0;x<800;x++){*(p+((479-y)*800+x)) = (buf[3*(y*800+x)+0]) | (buf[3*(y*800+x)+1]<<8) | (buf[3*(y*800+x)+2]<<16);}}close(bmp);return 0; }//拿去上下左右滑動(dòng)狀態(tài) int slide() {//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)到ts中struct input_event ts;int x,y,x1,y1;while(1){while(1){read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x = ts.value*800/1024;}if(ts.type == EV_ABS && ts.code == ABS_Y){y = ts.value*480/600;}//按下去的時(shí)刻或者松手后的時(shí)刻if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 1)//按下去{printf("按下去 (%d,%d)\n",x,y);break;}}//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)while(1){read(fd,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x1 = ts.value*800/1024;}if(ts.type == EV_ABS && ts.code == ABS_Y){y1 = ts.value*480/600;}if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 0)//松手{printf("松手后 (%d,%d)\n",x1,y1);break;}}if(x1-x > 100)return right;if(x1-x < -100)return left;if(y1-y > 100)return down;if(y1-y < -100)return up;}return 0; }int main(int argc,char *argv[]) {//打開觸摸屏fd = open("/dev/input/event0",O_RDWR);if(fd < 0){perror("open fd error!\n");return 0;}//打開lcdfd_lcd = open("/dev/fb0",O_RDWR);if(fd_lcd < 0){printf("open fd_lcd error!\n");return 0;}//lcd映射//指針指向一個(gè)像素點(diǎn)int *p = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd_lcd,0);if(p == NULL){perror("mmap fail!\n");return -1;}while(1){//指針數(shù)組存儲(chǔ)字符串char *bmp_path[] = {"james.bmp","wade.bmp"};int i=0;//滑動(dòng)switch(slide()){case up:printf("up slide!\n");show_pic(p,bmp_path[0]);break;case down:printf("down slide!\n");show_pic(p,bmp_path[1]);break;case left:printf("left slide!\n");show_pic(p,bmp_path[0]);break;case right:printf("right slide!\n");show_pic(p,bmp_path[1]);break;}}//lcd映射釋放munmap(p,800*480*4);//關(guān)閉觸摸屏close(fd);return 0; }

作業(yè)1:
點(diǎn)擊左邊顯示一張bmp 點(diǎn)擊右邊顯示一張jpg

//打開lcdint fd_lcd = open("/dev/fb0",O_RDWR);if(fd_lcd < 0){printf("open fd_lcd error!\n");return 0;}//lcd映射//指針指向一個(gè)像素點(diǎn)int *p = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd_lcd,0);if(p == NULL){perror("mmap fail!\n");return -1;}//打開觸摸屏int fd_ts = open("/dev/input/event0",O_RDWR);if(fd_ts < 0){printf("open fd_ts error!\n");return 0;}//定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)到ts中struct input_event ts;//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)int x,y;while(1){//指針數(shù)組存儲(chǔ)字符串char *bmp_path[] = {"james.bmp","wade.bmp"};int i;read(fd_ts,&ts,sizeof(struct input_event));if(ts.type == EV_ABS && ts.code == ABS_X){x = ts.value*800/1024;//x軸坐標(biāo)}if(ts.type == EV_ABS && ts.code == ABS_Y){y = ts.value*480/600;//y軸坐標(biāo)}//按下去的時(shí)刻或者松手后的時(shí)刻if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 1)//按下去{if(x <400){if(i<2){printf("left!\n");printf("show[%d]\n",i+1); show_pic(p,bmp_path[i]); i++;}// else// {// i=0;// show_pic(p,bmp_path[i]);// }}else if(x > 400){printf("right!\n");if(i==2){i=0;show_pic(p,bmp_path[i]);}else{lcd_draw_jpg(0,0,"./jr.jpg",NULL,0,0);}} }}//lcd映射釋放munmap(p,800*480*4);//關(guān)閉lcd\bmp\tsclose(fd_lcd);close(fd_ts);

附加題:
//觸摸屏的函數(shù)封裝如下
int get_xy(int *x,int *y)
{

}
int main()
{
?? ?int x,y;
?? ?get_xy(&x,&y); ?//通過(guò)調(diào)用get_xy來(lái)獲取x和y軸的坐標(biāo)值
?? ?printf("x=%d y=%d\n",x,y);?? ?
?? ?return 0;
}

#include "my_head.h"int get_xy(int *x,int *y) {//打開觸摸屏int fd;fd = open("/dev/input/event0",O_RDWR);if(fd < 0){perror("open fd error!\n");return 0;} //定義一個(gè)存儲(chǔ)觸摸屏信息的結(jié)構(gòu)體,將讀取的設(shè)備文件信息存儲(chǔ)到ts中struct input_event ts;while(1){//讀觸摸屏信息--阻塞函數(shù)(點(diǎn)擊觸摸屏之后才會(huì)往下執(zhí)行)read(fd,&ts,sizeof(ts));//3. 判斷事件 xif(ts.type == EV_ABS && ts.code == ABS_X){*x = ts.value*800/1024;//x軸坐標(biāo)}if(ts.type == EV_ABS && ts.code == ABS_Y){*y = ts.value*480/600;//y軸坐標(biāo)}if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 0)//松手后break;}//關(guān)閉觸摸屏close(fd);}int main(int argc,char *argv[]) {int x,y;get_xy(&x,&y); //通過(guò)調(diào)用get_xy來(lái)獲取x和y軸的坐標(biāo)值printf("(%d , %d)\n",x,y);return 0; }

總結(jié)

以上是生活随笔為你收集整理的GEC6818开发板——触摸屏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。