tpc_worker.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "touch.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 = (const char *)parameter;
  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_uint16_t u16X, u16Y;
  74. u16X = read_data[i].x_coordinate;
  75. u16Y = read_data[i].y_coordinate;
  76. #if defined(NU_PKG_TPC_REVERSE_XY)
  77. u16X = info.range_x - u16X;
  78. u16Y = info.range_y - u16Y;
  79. #endif
  80. //rt_kprintf("[%d] %d %d\n", read_data[i].event, u16X, u16Y);
  81. nu_touch_inputevent_cb(u16X, u16Y, read_data[i].event);
  82. }
  83. }
  84. }
  85. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  86. }
  87. }
  88. /* Test function */
  89. int tpc_sample(const char *tpc_name)
  90. {
  91. rt_thread_t tpc_thread;
  92. tpc_thread = rt_thread_create("tpc",
  93. tpc_entry,
  94. (void *)tpc_name,
  95. THREAD_STACK_SIZE,
  96. THREAD_PRIORITY,
  97. THREAD_TIMESLICE);
  98. if (tpc_thread != RT_NULL)
  99. rt_thread_startup(tpc_thread);
  100. return 0;
  101. }