drv_touch.c 5.8 KB

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