drv_touch.c 5.2 KB

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