383|0

32

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【匠芯创D133CBS】--3.触摸屏位置信息读取 [复制链接]

 
## 简介 从以下官方的的开发板简介可知,板子配置了电容触摸接口。我们基于此硬件基础上验证触摸功能。 > D133CBV-QFN88-V1-2 是一款基于 D13x 芯片设计的人机交互应用开发板,配备 7 寸 LVDS 显示屏以及电容触摸屏,同时预留 MIPI 屏幕、电阻触摸屏接口。开发板集成了 USB 烧录、TF-Card 升级烧录、JTAG、串口打印等调试接口,方便调试开发。开发板集成 16MB NOR FLASH 并可兼容 NAND,同时引出 USB Host、CAN、RS485、SDIO WIFI、以太网、Speaker、数字 MIC、蜂鸣器等功能,预留 I2S、I2C、UART 等接口,方便用户快速进行技术预研与产品开发,满足用户对不同人机交互场景的开发需求。

硬件链接

从硬件原理图上可以看出电容触摸屏硬件上有四根PIN脚和MCU 连接。

对应连接关系如下:
PA8 I2C2_SDA
PA9 I2C2_SCL
PA10 RST
PA11 INT

通过 Menuconfig 开启如下touch GT911 配置开关即可将touch 驱动程序加入工程编译。

编译后下载运行终端输入list_device 命令发现系统的驱动设备中已经添加了touch 设备。

对应的驱动代码如下:

  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-13 RiceChen the first version
  9. * 2023-04-30 Geo modified for ArtInChip
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <string.h>
  14. #define DBG_TAG "gt911"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #include "gt911.h"
  18. static struct rt_i2c_client gt911_client;
  19. /* hardware section */
  20. static rt_uint8_t GT911_CFG_TBL[] = {
  21. 0x6b, 0x00, 0x04, 0x58, 0x02, 0x05, 0x0d, 0x00, 0x01, 0x0f, 0x28, 0x0f,
  22. 0x50, 0x32, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x8a, 0x2a, 0x0c, 0x45, 0x47, 0x0c, 0x08, 0x00, 0x00,
  24. 0x00, 0x40, 0x03, 0x2c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x64, 0x32,
  25. 0x00, 0x00, 0x00, 0x28, 0x64, 0x94, 0xd5, 0x02, 0x07, 0x00, 0x00, 0x04,
  26. 0x95, 0x2c, 0x00, 0x8b, 0x34, 0x00, 0x82, 0x3f, 0x00, 0x7d, 0x4c, 0x00,
  27. 0x7a, 0x5b, 0x00, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x18, 0x16, 0x14, 0x12, 0x10, 0x0e, 0x0c, 0x0a,
  31. 0x08, 0x06, 0x04, 0x02, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x18,
  33. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x24, 0x13, 0x12, 0x10, 0x0f,
  34. 0x0a, 0x08, 0x06, 0x04, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00, 0x79, 0x01,
  37. };
  38. static rt_err_t gt911_write_reg(struct rt_i2c_client *dev, rt_uint8_t *data,
  39. rt_uint8_t len)
  40. {
  41. struct rt_i2c_msg msgs;
  42. msgs.addr = dev->client_addr;
  43. msgs.flags = RT_I2C_WR;
  44. msgs.buf = data;
  45. msgs.len = len;
  46. if (rt_i2c_transfer(dev->bus, &msgs, 1) == 1) {
  47. return RT_EOK;
  48. } else {
  49. return -RT_ERROR;
  50. }
  51. }
  52. static rt_err_t gt911_read_regs(struct rt_i2c_client *dev, rt_uint8_t *reg,
  53. rt_uint8_t *data, rt_uint8_t len)
  54. {
  55. struct rt_i2c_msg msgs[2];
  56. msgs[0].addr = dev->client_addr;
  57. msgs[0].flags = RT_I2C_WR;
  58. msgs[0].buf = reg;
  59. msgs[0].len = GT911_REGITER_LEN;
  60. msgs[1].addr = dev->client_addr;
  61. msgs[1].flags = RT_I2C_RD;
  62. msgs[1].buf = data;
  63. msgs[1].len = len;
  64. if (rt_i2c_transfer(dev->bus, msgs, 2) == 2) {
  65. return RT_EOK;
  66. } else {
  67. return -RT_ERROR;
  68. }
  69. }
  70. static rt_err_t gt911_get_product_id(struct rt_i2c_client *dev,
  71. rt_uint8_t *data, rt_uint8_t len)
  72. {
  73. rt_uint8_t reg[2];
  74. reg[0] = (rt_uint8_t)(GT911_PRODUCT_ID >> 8);
  75. reg[1] = (rt_uint8_t)(GT911_PRODUCT_ID & 0xff);
  76. if (gt911_read_regs(dev, reg, data, len) != RT_EOK) {
  77. LOG_E("read id failed");
  78. return -RT_ERROR;
  79. }
  80. return RT_EOK;
  81. }
  82. static rt_err_t gt911_get_info(struct rt_i2c_client *dev,
  83. struct rt_touch_info *info)
  84. {
  85. rt_uint8_t reg[2];
  86. rt_uint8_t out_info[7];
  87. rt_uint8_t out_len = 7;
  88. reg[0] = (rt_uint8_t)(GT911_CONFIG_REG >> 8);
  89. reg[1] = (rt_uint8_t)(GT911_CONFIG_REG & 0xFF);
  90. if (gt911_read_regs(dev, reg, out_info, out_len) != RT_EOK) {
  91. LOG_E("read info failed");
  92. return -RT_ERROR;
  93. }
  94. info->range_x = (out_info[2] << 8) | out_info[1];
  95. info->range_y = (out_info[4] << 8) | out_info[3];
  96. info->point_num = out_info[5] & 0x0f;
  97. // FIXME: temporarily set to 1, only 1 point data return
  98. // info->point_num = 1;
  99. return RT_EOK;
  100. }
  101. #if 0
  102. static rt_err_t gt911_soft_reset(struct rt_i2c_client *dev)
  103. {
  104. rt_uint8_t buf[3];
  105. buf[0] = (rt_uint8_t)(GT911_COMMAND_REG >> 8);
  106. buf[1] = (rt_uint8_t)(GT911_COMMAND_REG & 0xFF);
  107. buf[2] = 0x02;
  108. if (gt911_write_reg(dev, buf, 3) != RT_EOK) {
  109. LOG_E("soft reset failed");
  110. return -RT_ERROR;
  111. }
  112. return RT_EOK;
  113. }
  114. static rt_err_t gt911_soft_reset_finish(struct rt_i2c_client *dev)
  115. {
  116. rt_uint8_t buf[3];
  117. buf[0] = (rt_uint8_t)(GT911_COMMAND_REG >> 8);
  118. buf[1] = (rt_uint8_t)(GT911_COMMAND_REG & 0xFF);
  119. buf[2] = 0x00;
  120. if (gt911_write_reg(dev, buf, 3) != RT_EOK) {
  121. LOG_E("soft reset failed");
  122. return -RT_ERROR;
  123. }
  124. return RT_EOK;
  125. }
  126. static rt_err_t gt911_set_irq_pin_int(struct rt_i2c_client *dev)
  127. {
  128. rt_uint8_t buf[3];
  129. buf[0] = (rt_uint8_t)(GT911_MOD_SWT_REG >> 8);
  130. buf[1] = (rt_uint8_t)(GT911_MOD_SWT_REG & 0xFF);
  131. buf[2] = 0x01;
  132. if (gt911_write_reg(dev, buf, 3) != RT_EOK) {
  133. LOG_E("set up failed");
  134. return -RT_ERROR;
  135. }
  136. return RT_EOK;
  137. }
  138. #endif
  139. static int16_t pre_x[GT911_MAX_TOUCH] = { -1, -1, -1, -1, -1 };
  140. static int16_t pre_y[GT911_MAX_TOUCH] = { -1, -1, -1, -1, -1 };
  141. static int16_t pre_w[GT911_MAX_TOUCH] = { -1, -1, -1, -1, -1 };
  142. static rt_uint8_t s_tp_dowm[GT911_MAX_TOUCH];
  143. static struct rt_touch_data *read_data;
  144. static void gt911_touch_up(void *buf, int8_t id)
  145. {
  146. read_data = (struct rt_touch_data *)buf;
  147. if (s_tp_dowm[id] == 1) {
  148. s_tp_dowm[id] = 0;
  149. read_data[id].event = RT_TOUCH_EVENT_UP;
  150. } else {
  151. read_data[id].event = RT_TOUCH_EVENT_NONE;
  152. }
  153. read_data[id].timestamp = rt_touch_get_ts();
  154. read_data[id].width = pre_w[id];
  155. read_data[id].x_coordinate = pre_x[id];
  156. read_data[id].y_coordinate = pre_y[id];
  157. read_data[id].track_id = id;
  158. pre_x[id] = -1; /* last point is none */
  159. pre_y[id] = -1;
  160. pre_w[id] = -1;
  161. }
  162. static void gt911_touch_down(void *buf, int8_t id, int16_t x, int16_t y,
  163. int16_t w)
  164. {
  165. read_data = (struct rt_touch_data *)buf;
  166. if (s_tp_dowm[id] == 1) {
  167. read_data[id].event = RT_TOUCH_EVENT_MOVE;
  168. } else {
  169. read_data[id].event = RT_TOUCH_EVENT_DOWN;
  170. s_tp_dowm[id] = 1;
  171. }
  172. read_data[id].timestamp = rt_touch_get_ts();
  173. read_data[id].width = w;
  174. read_data[id].x_coordinate = x;
  175. read_data[id].y_coordinate = y;
  176. read_data[id].track_id = id;
  177. pre_x[id] = x; /* save last point */
  178. pre_y[id] = y;
  179. pre_w[id] = w;
  180. }
  181. static rt_size_t gt911_read_point(struct rt_touch_device *touch, void *buf,
  182. rt_size_t read_num)
  183. {
  184. rt_uint8_t point_status = 0;
  185. rt_uint8_t touch_num = 0;
  186. rt_uint8_t write_buf[3];
  187. rt_uint8_t cmd[2];
  188. rt_uint8_t read_buf[8 * GT911_MAX_TOUCH] = { 0 };
  189. rt_uint8_t read_index;
  190. int8_t read_id = 0;
  191. int16_t input_x = 0;
  192. int16_t input_y = 0;
  193. int16_t input_w = 0;
  194. static rt_uint8_t pre_touch = 0;
  195. static int8_t pre_id[GT911_MAX_TOUCH] = { 0 };
  196. rt_memset(buf, 0, sizeof(struct rt_touch_data) * read_num);
  197. /* point status register */
  198. cmd[0] = (rt_uint8_t)((GT911_READ_STATUS >> 8) & 0xFF);
  199. cmd[1] = (rt_uint8_t)(GT911_READ_STATUS & 0xFF);
  200. if (gt911_read_regs(>911_client, cmd, &point_status, 1) != RT_EOK) {
  201. LOG_D("read point failed\n");
  202. read_num = 0;
  203. goto exit_;
  204. }
  205. if (point_status == 0) /* no data */
  206. {
  207. read_num = 0;
  208. goto exit_;
  209. }
  210. if ((point_status & 0x80) == 0) /* data is not ready */
  211. {
  212. read_num = 0;
  213. goto exit_;
  214. }
  215. touch_num = point_status & 0x0f; /* get point num */
  216. if (touch_num > GT911_MAX_TOUCH) /* point num is not correct */
  217. {
  218. read_num = 0;
  219. goto exit_;
  220. }
  221. cmd[0] = (rt_uint8_t)((GT911_POINT1_REG >> 8) & 0xFF);
  222. cmd[1] = (rt_uint8_t)(GT911_POINT1_REG & 0xFF);
  223. /* read point num is touch_num */
  224. if (gt911_read_regs(>911_client, cmd, read_buf,
  225. read_num * GT911_POINT_INFO_NUM) != RT_EOK) {
  226. LOG_D("read point failed\n");
  227. read_num = 0;
  228. goto exit_;
  229. }
  230. if (pre_touch > touch_num) /* point up */
  231. {
  232. for (read_index = 0; read_index < pre_touch; read_index++) {
  233. rt_uint8_t j;
  234. for (j = 0; j < touch_num; j++) /* this time touch num */
  235. {
  236. read_id = read_buf[j * 8] & 0x0F;
  237. if (pre_id[read_index] == read_id) /* this id is not free */
  238. break;
  239. if (j >= touch_num - 1) {
  240. rt_uint8_t up_id;
  241. up_id = pre_id[read_index];
  242. gt911_touch_up(buf, up_id);
  243. }
  244. }
  245. }
  246. }
  247. if (touch_num) /* point down */
  248. {
  249. rt_uint8_t off_set;
  250. for (read_index = 0; read_index < touch_num; read_index++) {
  251. off_set = read_index * 8;
  252. read_id = read_buf[off_set] & 0x0f;
  253. pre_id[read_index] = read_id;
  254. input_x =
  255. read_buf[off_set + 1] | (read_buf[off_set + 2] << 8); /* x */
  256. input_y =
  257. read_buf[off_set + 3] | (read_buf[off_set + 4] << 8); /* y */
  258. input_w =
  259. read_buf[off_set + 5] | (read_buf[off_set + 6] << 8); /* size */
  260. gt911_touch_down(buf, read_id, input_x, input_y, input_w);
  261. }
  262. } else if (pre_touch) {
  263. for (read_index = 0; read_index < pre_touch; read_index++) {
  264. gt911_touch_up(buf, pre_id[read_index]);
  265. }
  266. }
  267. pre_touch = touch_num;
  268. exit_:
  269. write_buf[0] = (rt_uint8_t)((GT911_READ_STATUS >> 8) & 0xFF);
  270. write_buf[1] = (rt_uint8_t)(GT911_READ_STATUS & 0xFF);
  271. write_buf[2] = 0x00;
  272. gt911_write_reg(>911_client, write_buf, 3);
  273. return read_num;
  274. }
  275. static rt_err_t gt911_control(struct rt_touch_device *touch, int cmd, void *arg)
  276. {
  277. if (cmd == RT_TOUCH_CTRL_GET_ID) {
  278. return gt911_get_product_id(>911_client, arg, 6);
  279. }
  280. if (cmd == RT_TOUCH_CTRL_GET_INFO) {
  281. return gt911_get_info(>911_client, arg);
  282. }
  283. rt_uint8_t buf[4];
  284. rt_uint8_t i = 0;
  285. rt_uint8_t *config;
  286. config =
  287. (rt_uint8_t *)rt_calloc(1, sizeof(GT911_CFG_TBL) + GT911_REGITER_LEN);
  288. if (config == RT_NULL) {
  289. LOG_D("malloc config memory failed\n");
  290. return -RT_ERROR;
  291. }
  292. config[0] = (rt_uint8_t)((GT911_CONFIG_REG >> 8) & 0xFF);
  293. config[1] = (rt_uint8_t)(GT911_CONFIG_REG & 0xFF);
  294. memcpy(&config[2], GT911_CFG_TBL, sizeof(GT911_CFG_TBL));
  295. switch (cmd) {
  296. case RT_TOUCH_CTRL_SET_X_RANGE: {
  297. rt_uint16_t x_range;
  298. x_range = *(rt_uint16_t *)arg;
  299. config[4] = (rt_uint8_t)(x_range >> 8);
  300. config[3] = (rt_uint8_t)(x_range & 0xff);
  301. GT911_CFG_TBL[2] = config[4];
  302. GT911_CFG_TBL[1] = config[3];
  303. break;
  304. }
  305. case RT_TOUCH_CTRL_SET_Y_RANGE: {
  306. rt_uint16_t y_range;
  307. y_range = *(rt_uint16_t *)arg;
  308. config[6] = (rt_uint8_t)(y_range >> 8);
  309. config[5] = (rt_uint8_t)(y_range & 0xff);
  310. GT911_CFG_TBL[4] = config[6];
  311. GT911_CFG_TBL[3] = config[5];
  312. break;
  313. }
  314. case RT_TOUCH_CTRL_SET_X_TO_Y: {
  315. config[8] ^= (1 << 3);
  316. break;
  317. }
  318. case RT_TOUCH_CTRL_SET_MODE: {
  319. rt_uint16_t trig_type;
  320. trig_type = *(rt_uint16_t *)arg;
  321. switch (trig_type) {
  322. case RT_DEVICE_FLAG_INT_RX:
  323. config[8] &= 0xFC;
  324. break;
  325. case RT_DEVICE_FLAG_RDONLY:
  326. config[8] &= 0xFC;
  327. config[8] |= 0x02;
  328. break;
  329. default:
  330. break;
  331. }
  332. break;
  333. }
  334. default: {
  335. break;
  336. }
  337. }
  338. if (gt911_write_reg(>911_client, config,
  339. sizeof(GT911_CFG_TBL) + GT911_ADDR_LEN) != RT_EOK) {
  340. LOG_D("send config failed");
  341. return -1;
  342. }
  343. buf[0] = (rt_uint8_t)((GT911_CHECK_SUM >> 8) & 0xFF);
  344. buf[1] = (rt_uint8_t)(GT911_CHECK_SUM & 0xFF);
  345. buf[2] = 0;
  346. for (i = GT911_ADDR_LEN; i < sizeof(GT911_CFG_TBL) + GT911_ADDR_LEN; i++) {
  347. buf[GT911_ADDR_LEN] += config<i>;
  348. }
  349. buf[2] = (~buf[2]) + 1;
  350. buf[3] = 1;
  351. gt911_write_reg(>911_client, buf, 4);
  352. rt_free(config);
  353. return RT_EOK;
  354. }
  355. static struct rt_touch_ops gt911_touch_ops = {
  356. .touch_readpoint = gt911_read_point,
  357. .touch_control = gt911_control,
  358. };
  359. static int rt_hw_gt911_init(const char *name, struct rt_touch_config *cfg)
  360. {
  361. struct rt_touch_device *touch_device = RT_NULL;
  362. touch_device =
  363. (struct rt_touch_device *)rt_malloc(sizeof(struct rt_touch_device));
  364. if (touch_device == RT_NULL) {
  365. LOG_E("touch device malloc fail");
  366. return -RT_ERROR;
  367. }
  368. rt_memset((void *)touch_device, 0, sizeof(struct rt_touch_device));
  369. /* hw init*/
  370. // rst output 0
  371. rt_pin_mode(*(rt_uint8_t *)cfg->user_data, PIN_MODE_OUTPUT);
  372. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_LOW);
  373. rt_thread_delay(10);
  374. // irq output 0
  375. rt_pin_mode(cfg->irq_pin.pin, PIN_MODE_OUTPUT);
  376. rt_pin_write(cfg->irq_pin.pin, PIN_LOW);
  377. rt_thread_delay(2);
  378. // rst output 1
  379. rt_pin_mode(*(rt_uint8_t *)cfg->user_data, PIN_MODE_OUTPUT);
  380. rt_pin_write(*(rt_uint8_t *)cfg->user_data, PIN_HIGH);
  381. rt_thread_delay(5);
  382. // rst input
  383. rt_pin_mode(*(rt_uint8_t *)cfg->user_data, PIN_MODE_INPUT);
  384. //irq output 0
  385. rt_pin_mode(cfg->irq_pin.pin, PIN_MODE_OUTPUT);
  386. rt_pin_write(cfg->irq_pin.pin, PIN_LOW);
  387. rt_thread_delay(50);
  388. rt_pin_mode(cfg->irq_pin.pin, PIN_MODE_INPUT);
  389. gt911_client.bus =
  390. (struct rt_i2c_bus_device *)rt_device_find(cfg->dev_name);
  391. if (gt911_client.bus == RT_NULL) {
  392. LOG_E("Can't find %s device", cfg->dev_name);
  393. return -RT_ERROR;
  394. }
  395. if (rt_device_open((rt_device_t)gt911_client.bus, RT_DEVICE_FLAG_RDWR) !=
  396. RT_EOK) {
  397. LOG_E("open %s device failed", cfg->dev_name);
  398. return -RT_ERROR;
  399. }
  400. gt911_client.client_addr = GT911_ADDRESS_HIGH;
  401. /* register touch device */
  402. touch_device->info.type = RT_TOUCH_TYPE_CAPACITANCE;
  403. touch_device->info.vendor = RT_TOUCH_VENDOR_GT;
  404. rt_memcpy(&touch_device->config, cfg, sizeof(struct rt_touch_config));
  405. touch_device->ops = >911_touch_ops;
  406. if (RT_EOK != rt_hw_touch_register(touch_device, name, RT_DEVICE_FLAG_INT_RX, RT_NULL)) {
  407. LOG_E("touch device gt911 init failed !!!");
  408. return -RT_ERROR;
  409. }
  410. LOG_I("touch device gt911 init success");
  411. return RT_EOK;
  412. }
  413. static int rt_gt911_gpio_cfg()
  414. {
  415. unsigned int g, p;
  416. long pin;
  417. // RST
  418. pin = drv_pin_get(GT911_RST_PIN);
  419. g = GPIO_GROUP(pin);
  420. p = GPIO_GROUP_PIN(pin);
  421. hal_gpio_direction_input(g, p);
  422. // INT
  423. pin = drv_pin_get(GT911_INT_PIN);
  424. g = GPIO_GROUP(pin);
  425. p = GPIO_GROUP_PIN(pin);
  426. hal_gpio_direction_input(g, p);
  427. hal_gpio_set_irq_mode(g, p, 0);
  428. return 0;
  429. }
  430. static int rt_hw_gt911_port(void)
  431. {
  432. struct rt_touch_config cfg;
  433. rt_uint8_t rst_pin;
  434. rt_gt911_gpio_cfg();
  435. rst_pin = drv_pin_get(GT911_RST_PIN);
  436. cfg.dev_name = GT911_I2C_CHAN;
  437. cfg.irq_pin.pin = drv_pin_get(GT911_INT_PIN);
  438. cfg.irq_pin.mode = PIN_MODE_INPUT;
  439. cfg.user_data = &rst_pin;
  440. rt_hw_gt911_init("gt911", &cfg);
  441. return 0;
  442. }
  443. INIT_DEVICE_EXPORT(rt_hw_gt911_port);

驱动程序是配好了,我们使用example 的测试代码验证touch 功能,开启如下配置

对应的测试代码如下:

  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-13 RiceChen the first version
  9. * 2023-05-04 GeoDong modified for ArtInChip
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "gt911.h"
  14. #define THREAD_PRIORITY 25
  15. #define THREAD_STACK_SIZE 1024
  16. #define THREAD_TIMESLICE 5
  17. static rt_thread_t gt911_thread = RT_NULL;
  18. static rt_sem_t gt911_sem = RT_NULL;
  19. static rt_device_t dev = RT_NULL;
  20. static struct rt_touch_data *read_data;
  21. static struct rt_touch_info info;
  22. static void gt911_entry(void *parameter)
  23. {
  24. rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
  25. read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
  26. while (1)
  27. {
  28. rt_sem_take(gt911_sem, RT_WAITING_FOREVER);
  29. if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num)
  30. {
  31. for (rt_uint8_t i = 0; i < info.point_num; i++)
  32. {
  33. if (read_data<i>.event == RT_TOUCH_EVENT_DOWN || read_data<i>.event == RT_TOUCH_EVENT_MOVE)
  34. {
  35. rt_kprintf("%d %d %d %d %d\n", read_data<i>.track_id,
  36. read_data<i>.x_coordinate,
  37. read_data<i>.y_coordinate,
  38. read_data<i>.timestamp,
  39. read_data<i>.width);
  40. }
  41. }
  42. }
  43. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  44. }
  45. }
  46. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  47. {
  48. rt_sem_release(gt911_sem);
  49. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  50. return 0;
  51. }
  52. /* Test function */
  53. static void test_gt911(void *parameter)
  54. {
  55. void *id;
  56. dev = rt_device_find("gt911");
  57. if (dev == RT_NULL)
  58. {
  59. rt_kprintf("can't find device:%s\n", "gt911");
  60. return;
  61. }
  62. if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  63. {
  64. rt_kprintf("open device failed!");
  65. return;
  66. }
  67. id = rt_malloc(sizeof(rt_uint8_t) * 8);
  68. rt_device_control(dev, RT_TOUCH_CTRL_GET_ID, id);
  69. rt_uint8_t * read_id = (rt_uint8_t *)id;
  70. rt_kprintf("id = GT%d%d%d \n", read_id[0] - '0', read_id[1] - '0', read_id[2] - '0');
  71. // rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x); /* if possible you can set your x y coordinate*/
  72. // rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y);
  73. rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, id);
  74. rt_kprintf("range_x = %d \n", (*(struct rt_touch_info*)id).range_x);
  75. rt_kprintf("range_y = %d \n", (*(struct rt_touch_info*)id).range_y);
  76. rt_kprintf("point_num = %d \n", (*(struct rt_touch_info*)id).point_num);
  77. rt_free(id);
  78. rt_device_set_rx_indicate(dev, rx_callback);
  79. gt911_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
  80. if (gt911_sem == RT_NULL)
  81. {
  82. rt_kprintf("create dynamic semaphore failed.\n");
  83. return;
  84. }
  85. gt911_thread = rt_thread_create("gt911",
  86. gt911_entry,
  87. RT_NULL,
  88. THREAD_STACK_SIZE,
  89. THREAD_PRIORITY,
  90. THREAD_TIMESLICE);
  91. if (gt911_thread != RT_NULL)
  92. rt_thread_startup(gt911_thread);
  93. return;
  94. }
  95. MSH_CMD_EXPORT(test_gt911, test gt911 sample);

上述测试代码会会定义测试命令 test_gt911 启动测试线程,线程中读取设备节点数据,读取到按下事件打印对应的touch 数据。

功能验证

上报数据的格式如下:

  1. struct rt_touch_data
  2. {
  3. rt_uint8_t event; /* The touch event of the data */
  4. rt_uint8_t track_id; /* Track id of point */
  5. rt_uint8_t width; /* Point of width */
  6. #ifdef AIC_RTP_DRV
  7. rt_uint16_t pressure; /* The pressure */
  8. #endif
  9. rt_uint16_t x_coordinate; /* Point of x coordinate */
  10. rt_uint16_t y_coordinate; /* Point of y coordinate */
  11. rt_tick_t timestamp; /* The timestamp when the data was received */
  12. };

对应打印输出如下

上述log对应的第二/三 对应的X/Y 坐标信息

从代码touch 上报信息信息的宏定义可以知道,TOUCH 支持多点信息上报,支持的最大数量为5

同时5个手指按下会上报多点触摸信息。

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
直播报名最后1周:艾迈斯欧司朗 OSP 开放协议,从氛围灯动态照明到传感器交互融合
直播时间:4月22日(周二)10:00
直播奖励:京东卡、蓝牙温湿度计、定制水杯

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 12

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表