天天看點

數位相框項目之觸摸屏子產品

觸摸屏驅動程式我在這篇文章有講解:請點選這裡!   有些朋會很奇怪,你這個驅動程式不是jz2440的,核心用的版本也不一樣,我想說的是你都開始做項目了,如果連這個小問題都不能解決的,我勸你還是趕快回去從頭開始學習。還是那句老話,我隻提供思路和架構,萬變不離其中這個道理你應該明白吧。如果你一味的去追求别人全部跟你做好了,你隻是copy上去,編譯成功。你學到多少呢?扯遠了。

如果你驅動移植成功了,可以點選這裡用tslib來測試,校驗,檢視列印坐标位置,我們後面用的到,測試觸摸屏按下和松開兩點的舉例時候會用的到,是以這個tslib必須測試吧,編譯的時候還會用到tslib庫。tslib測試請點選這裡!

下面就開始上代碼touchscreen.c檔案如下:

#include <config.h>

#include <input_manager.h>

#include <stdlib.h> 

#include <tslib.h>    /* tslib裡面的頭檔案,在次強調一定要安裝tslib */

#include <draw.h>   

/* 參考tslib裡的ts_print.c */   /* 可以列印兩點的距離 */

static struct tsdev *g_ttsdev;

static int gixres;

static int giyres;

static t_inputopr  g_ttouchscreenopr;   /* 定義t_inputopr類型的結構體,這個結構體定義在input_manager.h中 */

/* 注意: 由于要用到lcd的分辨率, 此函數要在selectandinitdisplay之後調用 */

static int touchscreendevinit(void)   /* 初始化touchscreen */

{

char *pctsname = null;

if ((pctsname = getenv("tslib_tsdevice")) != null )   /* 擷取環境變量,在測試tslib時候指定的 */

g_ttsdev = ts_open(pctsname, 1);

}

else

g_ttsdev = ts_open("/dev/event0", 1);  /* 沒有指定環境變量就打開/dev/event0 */

if (!g_ttsdev) {  

dbg_printf("ts_open error!\n");

return -1;

if (ts_config(g_ttsdev)) {    /* 目前我也不知道是幹嘛用的 ,猜測是做一些初始化工作,或者配置之類的*/

dbg_printf("ts_config error!\n");

if (getdispresolution(&gixres, &giyres))   /* 獲得lcd的分辨率 */

g_ttouchscreenopr.ifd = ts_fd(g_ttsdev); /* 獲得這個檔案的句柄,後面調用select函數監測 */

return 0;

static int touchscreendevexit(void)

static int isoutof500ms(struct timeval *ptpretime, struct timeval *ptnowtime)  /* 延時500ms,防止觸摸屏不停的操作 */

int iprems;

int inowms;

iprems = ptpretime->tv_sec * 1000 + ptpretime->tv_usec / 1000;

inowms = ptnowtime->tv_sec * 1000 + ptnowtime->tv_usec / 1000;

return (inowms > iprems + 500);

static int touchscreengetinputevent(pt_inputevent ptinputevent) /* 從觸摸屏擷取資料 */

struct ts_sample tsamp;

int iret;

static struct timeval tpretime;

iret = ts_read(g_ttsdev, &tsamp, 1);  /* 把讀取到的值存放在tsamp這個結構體中 */

if (iret < 0) {

/* 處理資料 */

if (isoutof500ms(&tpretime, &tsamp.tv))

/* 如果此次觸摸事件發生的時間, 距上次事件超過了500ms */

tpretime = tsamp.tv;

ptinputevent->ttime = tsamp.tv;

ptinputevent->itype = input_type_touchscreen;  /* 觸摸屏類型,不是序列槽終端或者按鍵, */

if (tsamp.y < giyres/3)

ptinputevent->ival = input_value_up;  /* 向上翻頁 */

else if (tsamp.y > 2*giyres/3)

ptinputevent->ival = input_value_down;  /* 想下翻頁 */

ptinputevent->ival = input_value_unknown;  /* 點選中間部分就是沒反應 */

static t_inputopr g_ttouchscreenopr = { /* 給這個類型的結構 指派,定義在頭檔案中定義*/

.name          = "touchscreen",

.deviceinit    = touchscreendevinit,

.deviceexit    = touchscreendevexit,

.getinputevent = touchscreengetinputevent,

};

int touchscreeninit(void)  /* 和上篇的lcd是一樣的 */

return registerinputopr(&g_ttouchscreenopr);

input_manager.c檔案如下:/* 不詳細講解了,和lcd架構是一模一樣 */

#include <string.h>

#include <sys/select.h>

static pt_inputopr g_ptinputoprhead;

static fd_set g_trfds;

static int g_imaxfd = -1;

int registerinputopr(pt_inputopr ptinputopr)

pt_inputopr pttmp;

if (!g_ptinputoprhead)

g_ptinputoprhead   = ptinputopr;

ptinputopr->ptnext = null;

pttmp = g_ptinputoprhead;

while (pttmp->ptnext)

pttmp = pttmp->ptnext;

pttmp->ptnext

 = ptinputopr;

void showinputopr(void)  /* 顯示連結清單中有哪些成員 */

int i = 0;

pt_inputopr pttmp = g_ptinputoprhead;

while (pttmp)

printf("%02d %s\n", i++, pttmp->name);

int allinputdevicesinit(void)   /* 調用連結清單中結構體裡面的初始化函數 */

int ierror = -1;

fd_zero(&g_trfds);

if (0 == pttmp->deviceinit())

fd_set(pttmp->ifd, &g_trfds);   /* 這裡還是為了select函數做的一些初始化操作 */

if (g_imaxfd < pttmp->ifd)

g_imaxfd = pttmp->ifd;

ierror = 0;

g_imaxfd++;

return ierror;

int getinputevent(pt_inputevent ptinputevent)

/* 用select函數監測stdin,touchscreen,

  有資料時再調用它們的getinputevent或獲得具體事件

*/

fd_set trfds;

trfds = g_trfds;

iret = select(g_imaxfd, &trfds, null, null, null);  /* 沒資料讀的時候就阻塞 */

if (iret > 0)

if (fd_isset(pttmp->ifd, &trfds))

if(0 == pttmp->getinputevent(ptinputevent))  /* 在連結清單中區找ttmp->ifd這個id的結構體中的出讀取函數 */

int inputinit(void)  /* 初始化。僅僅是放進連結清單中 */

int ierror;

ierror = stdininit();

ierror |= touchscreeninit();

input_manager.h檔案如下:

#ifndef _input_manager_h

#define _input_manager_h

#include <sys/time.h>

#define input_type_stdin        0

#define input_type_touchscreen  1

#define input_value_up          0   

#define input_value_down        1

#define input_value_exit        2

#define input_value_unknown     -1

typedef struct inputevent {

struct timeval ttime;

int itype;  /* stdin, touchsceen */

int ival;   /*  */

}t_inputevent, *pt_inputevent;

typedef struct inputopr {

char *name;

int ifd;

int (*deviceinit)(void);

int (*deviceexit)(void);

int (*getinputevent)(pt_inputevent ptinputevent);

struct inputopr *ptnext;

}t_inputopr, *pt_inputopr;

int inputinit(void);

int registerinputopr(pt_inputopr ptinputopr);

void showinputopr(void);

int allinputdevicesinit(void);

int getinputevent(pt_inputevent ptinputevent);

int stdininit(void);

int touchscreeninit(void);

#endif /* _input_manager_h */

繼續閱讀