drv_touch.c 5.6 KB

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