drv_touch.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-02-08 Zhangyihong the first version
  9. */
  10. #include "drv_touch.h"
  11. #include <string.h>
  12. #ifdef BSP_USING_TOUCH
  13. #ifdef PKG_USING_GUIENGINE
  14. #include <rtgui/event.h>
  15. #include <rtgui/rtgui_server.h>
  16. #elif defined(PKG_USING_LITTLEVGL2RTT)
  17. #include <littlevgl2rtt.h>
  18. #endif
  19. #define BSP_TOUCH_SAMPLE_HZ (50)
  20. #define DBG_ENABLE
  21. #define DBG_SECTION_NAME "TOUCH"
  22. #define DBG_LEVEL DBG_ERROR
  23. #define DBG_COLOR
  24. #include <rtdbg.h>
  25. static rt_list_t driver_list;
  26. void rt_touch_drivers_register(touch_drv_t drv)
  27. {
  28. rt_list_insert_before(&driver_list, &drv->list);
  29. }
  30. static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
  31. {
  32. #ifdef PKG_USING_GUIENGINE
  33. struct rtgui_event_mouse emouse;
  34. emouse.parent.sender = RT_NULL;
  35. emouse.wid = RT_NULL;
  36. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  37. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  38. emouse.x = x;
  39. emouse.y = y;
  40. emouse.ts = rt_tick_get();
  41. emouse.id = ts;
  42. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  43. #elif defined(PKG_USING_LITTLEVGL2RTT)
  44. littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_DOWN);
  45. #endif
  46. }
  47. static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
  48. {
  49. #ifdef PKG_USING_GUIENGINE
  50. struct rtgui_event_mouse emouse;
  51. emouse.parent.sender = RT_NULL;
  52. emouse.wid = RT_NULL;
  53. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  54. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  55. emouse.x = x;
  56. emouse.y = y;
  57. emouse.ts = rt_tick_get();
  58. emouse.id = ts;
  59. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  60. #elif defined(PKG_USING_LITTLEVGL2RTT)
  61. littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_MOVE);
  62. #endif
  63. }
  64. static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
  65. {
  66. #ifdef PKG_USING_GUIENGINE
  67. struct rtgui_event_mouse emouse;
  68. emouse.parent.sender = RT_NULL;
  69. emouse.wid = RT_NULL;
  70. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  71. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  72. emouse.x = x;
  73. emouse.y = y;
  74. emouse.ts = rt_tick_get();
  75. emouse.id = ts;
  76. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  77. #elif defined(PKG_USING_LITTLEVGL2RTT)
  78. littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_MOVE);
  79. #endif
  80. }
  81. static void touch_thread_entry(void *parameter)
  82. {
  83. touch_drv_t touch = (touch_drv_t)parameter;
  84. struct touch_message msg;
  85. rt_tick_t emouse_id = 0;
  86. touch->ops->isr_enable(RT_TRUE);
  87. while (1)
  88. {
  89. if (rt_sem_take(touch->isr_sem, 10) != RT_EOK)
  90. {
  91. continue;
  92. }
  93. while(touch->ops->read_point(&msg) == RT_EOK)
  94. {
  95. switch (msg.event)
  96. {
  97. case TOUCH_EVENT_UP:
  98. post_up_event(msg.x, msg.y, emouse_id);
  99. break;
  100. case TOUCH_EVENT_DOWN:
  101. emouse_id = rt_tick_get();
  102. post_down_event(msg.x, msg.y, emouse_id);
  103. break;
  104. case TOUCH_EVENT_MOVE:
  105. post_motion_event(msg.x, msg.y, emouse_id);
  106. break;
  107. default:
  108. break;
  109. }
  110. rt_thread_delay(RT_TICK_PER_SECOND / BSP_TOUCH_SAMPLE_HZ);
  111. }
  112. touch->ops->isr_enable(RT_TRUE);
  113. }
  114. }
  115. static int rt_touch_driver_init(void)
  116. {
  117. rt_list_init(&driver_list);
  118. return 0;
  119. }
  120. INIT_BOARD_EXPORT(rt_touch_driver_init);
  121. static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
  122. static int rt_touch_thread_init(void)
  123. {
  124. rt_list_t *l;
  125. touch_drv_t current_driver;
  126. rt_thread_t tid = RT_NULL;
  127. i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(BSP_I2C1_NAME);
  128. RT_ASSERT(i2c_bus);
  129. current_driver = RT_NULL;
  130. if (rt_device_open((rt_device_t)i2c_bus, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  131. return -1;
  132. for (l = driver_list.next; l != &driver_list; l = l->next)
  133. {
  134. if (rt_list_entry(l, struct touch_drivers, list)->probe(i2c_bus))
  135. {
  136. current_driver = rt_list_entry(l, struct touch_drivers, list);
  137. break;
  138. }
  139. }
  140. if (current_driver == RT_NULL)
  141. {
  142. LOG_E("no touch screen or do not have driver\r\n");
  143. rt_device_close((rt_device_t)i2c_bus);
  144. return -1;
  145. }
  146. current_driver->ops->init(i2c_bus);
  147. LOG_I("touch screen found driver\r\n");
  148. tid = rt_thread_create("touch", touch_thread_entry, current_driver, 2048, 27, 20);
  149. if (tid == RT_NULL)
  150. {
  151. current_driver->ops->deinit();
  152. rt_device_close((rt_device_t)i2c_bus);
  153. return -1;
  154. }
  155. rt_thread_startup(tid);
  156. return 0;
  157. }
  158. static void touch_init_thread_entry(void *parameter)
  159. {
  160. rt_touch_thread_init();
  161. }
  162. static int touc_bg_init(void)
  163. {
  164. rt_thread_t tid = RT_NULL;
  165. tid = rt_thread_create("touchi", touch_init_thread_entry, RT_NULL, 2048, 28, 20);
  166. if (tid == RT_NULL)
  167. {
  168. return -1;
  169. }
  170. rt_thread_startup(tid);
  171. return 0;
  172. }
  173. INIT_APP_EXPORT(touc_bg_init);
  174. #endif