ili_sample.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-25 Wayne the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "ili.h"
  13. #define THREAD_PRIORITY 5
  14. #define THREAD_STACK_SIZE 2048
  15. #define THREAD_TIMESLICE 5
  16. static rt_sem_t tpc_sem = RT_NULL;
  17. rt_weak void nu_touch_inputevent_cb(rt_int16_t x, rt_int16_t y, rt_uint8_t state)
  18. {
  19. rt_kprintf("[%d] %d %d\n", state, x, y);
  20. }
  21. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  22. {
  23. return rt_sem_release(tpc_sem);
  24. }
  25. static void tpc_entry(void *parameter)
  26. {
  27. struct rt_touch_data *read_data;
  28. struct rt_touch_info info;
  29. rt_device_t dev = RT_NULL;
  30. const char *name = "ili_tpc";
  31. rt_uint32_t x_range = BSP_LCD_WIDTH;
  32. rt_uint32_t y_range = BSP_LCD_HEIGHT;
  33. dev = rt_device_find(name);
  34. if (dev == RT_NULL)
  35. {
  36. rt_kprintf("can't find device:%s\n", name);
  37. return;
  38. }
  39. if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  40. {
  41. rt_kprintf("open device failed!");
  42. return;
  43. }
  44. rt_kprintf("[%s] x: %d, y: %d\n", __func__, x_range, y_range);
  45. rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x_range); /* if possible you can set your x y coordinate*/
  46. rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y_range);
  47. tpc_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
  48. if (tpc_sem == RT_NULL)
  49. {
  50. rt_kprintf("create dynamic semaphore failed.\n");
  51. return;
  52. }
  53. rt_device_set_rx_indicate(dev, rx_callback);
  54. rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
  55. rt_kprintf("range_x = %d \n", info.range_x);
  56. rt_kprintf("range_y = %d \n", info.range_y);
  57. rt_kprintf("point_num = %d \n", info.point_num);
  58. read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
  59. RT_ASSERT(read_data);
  60. rt_memset(read_data, 0, sizeof(struct rt_touch_data) * info.point_num);
  61. while (1)
  62. {
  63. rt_sem_take(tpc_sem, RT_WAITING_FOREVER);
  64. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  65. if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num)
  66. {
  67. for (rt_uint8_t i = 0; i < 1; i++) // Only report one point.
  68. {
  69. if (read_data[i].event == RT_TOUCH_EVENT_DOWN
  70. || read_data[i].event == RT_TOUCH_EVENT_UP
  71. || read_data[i].event == RT_TOUCH_EVENT_MOVE)
  72. {
  73. //rt_kprintf("[%d] %d %d\n", read_data[i].event, read_data[i].x_coordinate, read_data[i].y_coordinate);
  74. nu_touch_inputevent_cb(read_data[i].x_coordinate, read_data[i].y_coordinate, read_data[i].event);
  75. }
  76. }
  77. }
  78. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  79. }
  80. }
  81. /* Test function */
  82. int tpc_sample(void)
  83. {
  84. rt_thread_t tpc_thread;
  85. tpc_thread = rt_thread_create("tpc",
  86. tpc_entry,
  87. RT_NULL,
  88. THREAD_STACK_SIZE,
  89. THREAD_PRIORITY,
  90. THREAD_TIMESLICE);
  91. if (tpc_thread != RT_NULL)
  92. rt_thread_startup(tpc_thread);
  93. return 0;
  94. }
  95. INIT_APP_EXPORT(tpc_sample);