gui_touch.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "touch.h"
  11. #include <string.h>
  12. #include <rtgui/event.h>
  13. #include <rtgui/rtgui_server.h>
  14. #ifndef PKG_TOUCH_SAMPLE_HZ
  15. #define PKG_TOUCH_SAMPLE_HZ (50)
  16. #endif
  17. /*Enable debug information*/
  18. #define DBG_ENABLE
  19. #ifdef DBG_ENABLE
  20. #define DBG_SECTION_NAME "TOUCH"
  21. #define DBG_LEVEL DBG_LOG
  22. #define DBG_COLOR
  23. #include <rtdbg.h>
  24. #endif
  25. static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t id)
  26. {
  27. struct rtgui_event_mouse emouse;
  28. emouse.parent.sender = RT_NULL;
  29. emouse.wid = RT_NULL;
  30. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  31. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  32. emouse.x = x;
  33. emouse.y = y;
  34. emouse.ts = rt_tick_get();
  35. emouse.id = id;
  36. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  37. }
  38. static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t id)
  39. {
  40. struct rtgui_event_mouse emouse;
  41. emouse.parent.sender = RT_NULL;
  42. emouse.wid = RT_NULL;
  43. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  44. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  45. emouse.x = x;
  46. emouse.y = y;
  47. emouse.ts = rt_tick_get();
  48. emouse.id = id;
  49. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  50. }
  51. static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t id)
  52. {
  53. struct rtgui_event_mouse emouse;
  54. emouse.parent.sender = RT_NULL;
  55. emouse.wid = RT_NULL;
  56. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  57. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  58. emouse.x = x;
  59. emouse.y = y;
  60. emouse.ts = rt_tick_get();
  61. emouse.id = id;
  62. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  63. }
  64. #define THREAD_PRIORITY 25
  65. #define THREAD_STACK_SIZE 1024
  66. #define THREAD_TIMESLICE 5
  67. static rt_thread_t touch_thread;
  68. static rt_sem_t touch_sem;
  69. static rt_device_t dev;
  70. static struct rt_touch_data *read_data;
  71. static void gui_touch_entry(void *parameter)
  72. {
  73. static rt_tick_t emouse_id = 0;
  74. read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data));
  75. while (1)
  76. {
  77. rt_sem_take(touch_sem, RT_WAITING_FOREVER);
  78. if (rt_device_read(dev, 0, read_data, 1) == 1)
  79. {
  80. switch(read_data->event)
  81. {
  82. case RT_TOUCH_EVENT_UP:
  83. post_up_event(read_data->x_coordinate, read_data->y_coordinate, emouse_id);
  84. break;
  85. case RT_TOUCH_EVENT_DOWN:
  86. emouse_id = rt_tick_get();
  87. post_down_event(read_data->x_coordinate, read_data->y_coordinate, emouse_id);
  88. break;
  89. case RT_TOUCH_EVENT_MOVE:
  90. post_motion_event(read_data->x_coordinate, read_data->y_coordinate, emouse_id);
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  97. }
  98. }
  99. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  100. {
  101. rt_sem_release(touch_sem);
  102. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  103. return RT_EOK;
  104. }
  105. int gui_touch(const char* name, rt_uint16_t x, rt_uint16_t y)
  106. {
  107. dev = rt_device_find(name); /* you touch device name*/
  108. if (dev == RT_NULL)
  109. {
  110. LOG_E("can't find device:%s\n", name);
  111. return -1;
  112. }
  113. if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  114. {
  115. LOG_E("open device failed!");
  116. return -1;
  117. }
  118. rt_device_control(dev, RT_TOUCH_CTRL_SET_X_RANGE, &x); /* if possible you can set your x y coordinate*/
  119. rt_device_control(dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y);
  120. rt_device_set_rx_indicate(dev, rx_callback);
  121. touch_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
  122. if (touch_sem == RT_NULL)
  123. {
  124. LOG_E("create dynamic semaphore failed.\n");
  125. return -1;
  126. }
  127. touch_thread = rt_thread_create("gui_touch",
  128. gui_touch_entry,
  129. RT_NULL,
  130. THREAD_STACK_SIZE,
  131. THREAD_PRIORITY,
  132. THREAD_TIMESLICE);
  133. if (touch_thread != RT_NULL)
  134. rt_thread_startup(touch_thread);
  135. return 0;
  136. }