平台:tiny4412SDK 1161B + HD700
kernel:linux 3.5
touch control:ft5406
學習HD700的過程中,顯示圖片后,需要對該屏幕的觸摸控制進行了解。閱讀HD700原理圖可知該款LCD使用的是ft5406型號的控制器。在學習linux驅動過程中,知道linux3.5的內核具有一個ft5X0X的驅動,我們是可以使用的;該驅動是多點觸控驅動(5個點)。屏幕的驅動是通過input子系統的框架編程,跟系統當中的鍵盤、鼠標一樣,是通過該子系統的事件進行上報的。所以編程過程中需要先了解input子系統。
准備工作:
確保自己的內核包含該驅動,並編譯進內核;該驅動內核菜單選項路徑如下圖所示。
終端使用設備:
1)、查找我們的觸摸屏設備,在input子系統中,通過配置文件進行查找。在開發板終端中輸入cat /proc/bus/input/devices,結果如下:
2)、從上一步我們能夠獲知到ft5x0x的驅動設備名是event0(不同的環境可能是其他),在/dev下能夠找到該文件。通過cat /dev/event0讀取該設備數據,執行完該命令后,我們觸及LCD屏幕,能夠發現終端有亂碼輸出,說明該驅動是能夠正常工作的。
3)、使用另外的命令hexdump /dev/event0,能獲得input子系統hex格式的數據輸出。其中每一行為一條上報信息。
通過對上報信息的分析,發現觸擊屏幕會上報事件類型為3的絕對事件和為0的同步事件,同步事件只為同步事件使用不需理會。
在絕對事件中,又有碼值為0x35、0x36、0x30、0x39四條信息,對應的分別是ABS_MT_POSITION_X(觸點x坐標)、ABS_MT_POSITION_Y(觸點y坐標)、ABS_MT_TOUCH_MAJOR(觸點軸直徑,一直為默認為200,不改變)、ABS_MT_TRACKING_ID(多點觸控中第幾個點,0~4);在編程過程中只要獲取對應碼值的具體數值就能知道觸擊的具體坐標以及是第幾個點啦。
4)、上報的數據格式:
struct input_event {
struct timeval time; //時間戳
__u16 type; //事件類型
__u16 code; //碼值
__s32 value; //數值
};
代碼實現:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>
#if 0
/*
* 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
#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)
#define ABS_MT_SLOT 0x2f /* MT slot being modified */
#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device */
#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
#define ABS_MT_DISTANCE 0x3b /* Contact hover distance */
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
#endif
int main(void)
{
struct input_event touchData;
int fd = open("/dev/event0", O_RDWR);
if (fd < 3) {
printf("Not device /dev/event0!\n");
exit(-1);
}
while(1) {
read(fd, &touchData, sizeof(struct input_event));
/* printf("------------------------------\n");
printf("touchData.type = %d\n", touchData.type);
printf("touchData.code = %d\n", touchData.code);
printf("touchData.value = %d\n", touchData.value);*/
if (touchData.type == EV_ABS) {
switch(touchData.code) {
case ABS_MT_POSITION_X:
printf("x = %d\n", touchData.value);
break;
case ABS_MT_POSITION_Y:
printf("y = %d\n", touchData.value);
break;
case ABS_MT_TOUCH_MAJOR:
printf("major = %d\n", touchData.value);
break;
case ABS_MT_TRACKING_ID:
printf("id = %d\n", touchData.value);
break;
default:
printf("other input value!\n");
break;
}
}
}
close(fd);
return 0;
}
結果: