drv_touch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * 2018-10-29 XY
  10. * 2019-04-11 sundm75 modify for ls1c300 & RTGUI
  11. */
  12. #include "drv_touch.h"
  13. #define TOUCH_I2C_NAME "i2c1"
  14. #ifndef TOUCH_SAMPLE_HZ
  15. #define TOUCH_SAMPLE_HZ (50)
  16. #endif
  17. #ifndef TOUCH_I2C_NAME
  18. #error "Please define touch i2c name!"
  19. #endif
  20. #ifdef TINA_USING_TOUCH
  21. #if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
  22. #include <rtgui/event.h>
  23. #include <rtgui/rtgui_server.h>
  24. #endif
  25. #if 0
  26. #define TPDEBUG rt_kprintf
  27. #else
  28. #define TPDEBUG(...)
  29. #endif
  30. static rt_slist_t _driver_list;
  31. static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
  32. static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
  33. {
  34. #if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
  35. rt_err_t result;
  36. struct rtgui_event_mouse emouse;
  37. emouse.parent.sender = RT_NULL;
  38. emouse.wid = RT_NULL;
  39. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  40. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  41. emouse.x = x;
  42. emouse.y = y;
  43. #ifdef PKG_USING_GUIENGINE
  44. emouse.ts = rt_tick_get();
  45. emouse.id = id;
  46. #endif
  47. #ifdef PKG_USING_GUIENGINE
  48. do
  49. {
  50. result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  51. if (result != RT_EOK)
  52. {
  53. rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
  54. }
  55. }
  56. while (result != RT_EOK);
  57. #else
  58. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  59. #endif
  60. TPDEBUG("[TP] touch down [%d, %d]\n", emouse.x, emouse.y);
  61. #endif
  62. }
  63. static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
  64. {
  65. #if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
  66. struct rtgui_event_mouse emouse;
  67. emouse.parent.sender = RT_NULL;
  68. emouse.wid = RT_NULL;
  69. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  70. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  71. emouse.x = x;
  72. emouse.y = y;
  73. #ifdef PKG_USING_GUIENGINE
  74. emouse.ts = rt_tick_get();
  75. emouse.id = id;
  76. #endif
  77. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  78. TPDEBUG("[TP] touch motion [%d, %d]\n", emouse.x, emouse.y);
  79. #endif
  80. }
  81. static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_uint32_t id)
  82. {
  83. #if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
  84. rt_err_t result;
  85. struct rtgui_event_mouse emouse;
  86. emouse.parent.sender = RT_NULL;
  87. emouse.wid = RT_NULL;
  88. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  89. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  90. emouse.x = x;
  91. emouse.y = y;
  92. #ifdef PKG_USING_GUIENGINE
  93. emouse.ts = rt_tick_get();
  94. emouse.id = id;
  95. #endif
  96. #ifdef PKG_USING_GUIENGINE
  97. do
  98. {
  99. result = rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  100. if (result != RT_EOK)
  101. {
  102. rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
  103. }
  104. }
  105. while (result != RT_EOK);
  106. #else
  107. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  108. #endif
  109. TPDEBUG("[TP] touch up [%d, %d]\n", emouse.x, emouse.y);
  110. #endif
  111. }
  112. static void touch_run(void* parameter)
  113. {
  114. rt_tick_t emouse_id = 0;
  115. struct touch_message msg;
  116. rt_slist_t *driver_list = NULL;
  117. struct touch_driver *current_driver = RT_NULL;
  118. i2c_bus = rt_i2c_bus_device_find(TOUCH_I2C_NAME);
  119. RT_ASSERT(i2c_bus);
  120. if(rt_device_open(&i2c_bus->parent, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  121. {
  122. return;
  123. }
  124. rt_slist_for_each(driver_list, &_driver_list)
  125. {
  126. current_driver = (struct touch_driver *)driver_list;
  127. if(current_driver->probe(i2c_bus) == RT_TRUE)
  128. {
  129. break;
  130. }
  131. current_driver = RT_NULL;
  132. }
  133. if(current_driver == RT_NULL)
  134. {
  135. rt_kprintf("[TP] No touch pad or driver.\n");
  136. rt_device_close((rt_device_t)i2c_bus);
  137. return;
  138. }
  139. current_driver->ops->init(i2c_bus);
  140. while (1)
  141. {
  142. if (rt_sem_take(current_driver->isr_sem, RT_WAITING_FOREVER) != RT_EOK)
  143. {
  144. continue;
  145. }
  146. if (current_driver->ops->read_point(&msg) != RT_EOK)
  147. {
  148. continue;
  149. }
  150. switch (msg.event)
  151. {
  152. case TOUCH_EVENT_MOVE:
  153. post_motion_event(msg.x, msg.y, emouse_id);
  154. break;
  155. case TOUCH_EVENT_DOWN:
  156. emouse_id = rt_tick_get();
  157. post_down_event(msg.x, msg.y, emouse_id);
  158. break;
  159. case TOUCH_EVENT_UP:
  160. post_up_event(msg.x, msg.y, emouse_id);
  161. break;
  162. default:
  163. break;
  164. }
  165. rt_thread_delay(RT_TICK_PER_SECOND / TOUCH_SAMPLE_HZ);
  166. }
  167. }
  168. rt_err_t rt_touch_drivers_register(touch_driver_t drv)
  169. {
  170. RT_ASSERT(drv != RT_NULL);
  171. RT_ASSERT(drv->ops != RT_NULL);
  172. RT_ASSERT(drv->probe != RT_NULL);
  173. rt_slist_append(&_driver_list, &drv->list);
  174. return RT_EOK;
  175. }
  176. static int rt_touch_list_init(void)
  177. {
  178. rt_slist_init(&_driver_list);
  179. return RT_EOK;
  180. }
  181. INIT_BOARD_EXPORT(rt_touch_list_init);
  182. static int rt_touch_init(void)
  183. {
  184. rt_thread_t thread = RT_NULL;
  185. thread = rt_thread_create("touch", touch_run, RT_NULL, 2048, 28, 20);
  186. if(thread)
  187. {
  188. return rt_thread_startup(thread);
  189. }
  190. return -RT_ERROR;
  191. }
  192. INIT_APP_EXPORT(rt_touch_init);
  193. int rt_touch_read(rt_uint16_t addr, void *cmd_buf, size_t cmd_len, void *data_buf, size_t data_len)
  194. {
  195. struct rt_i2c_msg msgs[2];
  196. msgs[0].addr = addr;
  197. msgs[0].flags = RT_I2C_WR;
  198. msgs[0].buf = cmd_buf;
  199. msgs[0].len = cmd_len;
  200. msgs[1].addr = addr;
  201. msgs[1].flags = RT_I2C_RD;
  202. msgs[1].buf = data_buf;
  203. msgs[1].len = data_len;
  204. if (rt_i2c_transfer(i2c_bus, msgs, 2) == 2)
  205. return 0;
  206. else
  207. return -1;
  208. }
  209. int rt_touch_write(rt_uint16_t addr, void *data_buf, size_t data_len)
  210. {
  211. struct rt_i2c_msg msgs[1];
  212. msgs[0].addr = addr;
  213. msgs[0].flags = RT_I2C_WR;
  214. msgs[0].buf = data_buf;
  215. msgs[0].len = data_len;
  216. if (rt_i2c_transfer(i2c_bus, msgs, 1) == 1)
  217. return 0;
  218. else
  219. return -1;
  220. }
  221. #endif