gt9147_sample.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * 2019-06-01 tyustli the first version
  9. */
  10. #include <rtthread.h>
  11. #include "gt9147.h"
  12. #define THREAD_PRIORITY 25
  13. #define THREAD_STACK_SIZE 1024
  14. #define THREAD_TIMESLICE 5
  15. static rt_thread_t gt9147_thread = RT_NULL;
  16. static rt_sem_t gt9147_sem = RT_NULL;
  17. static rt_device_t dev = RT_NULL;
  18. static struct rt_touch_data *read_data;
  19. static struct rt_touch_info info;
  20. static void gt9147_entry(void *parameter)
  21. {
  22. rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
  23. read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
  24. while (1)
  25. {
  26. rt_sem_take(gt9147_sem, RT_WAITING_FOREVER);
  27. if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num)
  28. {
  29. for (rt_uint8_t i = 0; i < info.point_num; i++)
  30. {
  31. if (read_data[i].event == RT_TOUCH_EVENT_DOWN || read_data[i].event == RT_TOUCH_EVENT_MOVE)
  32. {
  33. rt_kprintf("%d %d %d %d %d\n", read_data[i].track_id,
  34. read_data[i].x_coordinate,
  35. read_data[i].y_coordinate,
  36. read_data[i].timestamp,
  37. read_data[i].width);
  38. }
  39. }
  40. }
  41. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  42. }
  43. }
  44. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  45. {
  46. rt_sem_release(gt9147_sem);
  47. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  48. return 0;
  49. }
  50. /* Test function */
  51. int gt9147_sample(const char *name, rt_uint16_t x, rt_uint16_t y)
  52. {
  53. void *id;
  54. dev = rt_device_find(name);
  55. if (dev == RT_NULL)
  56. {
  57. rt_kprintf("can't find device:%s\n", name);
  58. return -1;
  59. }
  60. if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  61. {
  62. rt_kprintf("open device failed!");
  63. return -1;
  64. }
  65. id = rt_malloc(sizeof(struct rt_touch_info));
  66. rt_device_control(dev, RT_TOUCH_CTRL_GET_ID, id);
  67. rt_uint8_t * read_id = (rt_uint8_t *)id;
  68. rt_kprintf("id = %d %d %d %d \n", read_id[0] - '0', read_id[1] - '0', read_id[2] - '0', read_id[3] - '0');
  69. rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x); /* if possible you can set your x y coordinate*/
  70. rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y);
  71. rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, id);
  72. rt_kprintf("range_x = %d \n", (*(struct rt_touch_info*)id).range_x);
  73. rt_kprintf("range_y = %d \n", (*(struct rt_touch_info*)id).range_y);
  74. rt_kprintf("point_num = %d \n", (*(struct rt_touch_info*)id).point_num);
  75. rt_free(id);
  76. gt9147_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
  77. if (gt9147_sem == RT_NULL)
  78. {
  79. rt_kprintf("create dynamic semaphore failed.\n");
  80. return -1;
  81. }
  82. rt_device_set_rx_indicate(dev, rx_callback);
  83. gt9147_thread = rt_thread_create("thread1",
  84. gt9147_entry,
  85. RT_NULL,
  86. THREAD_STACK_SIZE,
  87. THREAD_PRIORITY,
  88. THREAD_TIMESLICE);
  89. if (gt9147_thread != RT_NULL)
  90. rt_thread_startup(gt9147_thread);
  91. return 0;
  92. }