drv_touch.c 5.6 KB

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