touch.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * 2019-05-20 tyustli the first version
  9. */
  10. #include "touch.h"
  11. #include <string.h>
  12. #define DBG_TAG "touch"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. /* ISR for touch interrupt */
  16. void rt_hw_touch_isr(rt_touch_t touch)
  17. {
  18. RT_ASSERT(touch);
  19. if (touch->parent.rx_indicate == RT_NULL)
  20. {
  21. return;
  22. }
  23. if (touch->irq_handle != RT_NULL)
  24. {
  25. touch->irq_handle(touch);
  26. }
  27. touch->parent.rx_indicate(&touch->parent, 1);
  28. }
  29. #ifdef RT_TOUCH_PIN_IRQ
  30. static void touch_irq_callback(void *param)
  31. {
  32. rt_hw_touch_isr((rt_touch_t)param);
  33. }
  34. #endif
  35. /* touch interrupt initialization function */
  36. static rt_err_t rt_touch_irq_init(rt_touch_t touch)
  37. {
  38. #ifdef RT_TOUCH_PIN_IRQ
  39. if (touch->config.irq_pin.pin == RT_PIN_NONE)
  40. {
  41. return -RT_EINVAL;
  42. }
  43. rt_pin_mode(touch->config.irq_pin.pin, touch->config.irq_pin.mode);
  44. if (touch->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN)
  45. {
  46. rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_RISING, touch_irq_callback, (void *)touch);
  47. }
  48. else if (touch->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
  49. {
  50. rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, touch_irq_callback, (void *)touch);
  51. }
  52. else if (touch->config.irq_pin.mode == PIN_MODE_INPUT)
  53. {
  54. rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, touch_irq_callback, (void *)touch);
  55. }
  56. rt_pin_irq_enable(touch->config.irq_pin.pin, PIN_IRQ_ENABLE);
  57. #endif
  58. return RT_EOK;
  59. }
  60. /* touch interrupt enable */
  61. static void rt_touch_irq_enable(rt_touch_t touch)
  62. {
  63. #ifdef RT_TOUCH_PIN_IRQ
  64. if (touch->config.irq_pin.pin != RT_PIN_NONE)
  65. {
  66. rt_pin_irq_enable(touch->config.irq_pin.pin, RT_TRUE);
  67. }
  68. #endif
  69. }
  70. /* touch interrupt disable */
  71. static void rt_touch_irq_disable(rt_touch_t touch)
  72. {
  73. #ifdef RT_TOUCH_PIN_IRQ
  74. if (touch->config.irq_pin.pin != RT_PIN_NONE)
  75. {
  76. rt_pin_irq_enable(touch->config.irq_pin.pin, RT_FALSE);
  77. }
  78. #endif
  79. }
  80. static rt_err_t rt_touch_open(rt_device_t dev, rt_uint16_t oflag)
  81. {
  82. rt_touch_t touch;
  83. RT_ASSERT(dev != RT_NULL);
  84. touch = (rt_touch_t)dev;
  85. if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
  86. {
  87. /* Initialization touch interrupt */
  88. rt_touch_irq_init(touch);
  89. }
  90. return RT_EOK;
  91. }
  92. static rt_err_t rt_touch_close(rt_device_t dev)
  93. {
  94. rt_touch_t touch;
  95. RT_ASSERT(dev != RT_NULL);
  96. touch = (rt_touch_t)dev;
  97. /* touch disable interrupt */
  98. rt_touch_irq_disable(touch);
  99. return RT_EOK;
  100. }
  101. static rt_size_t rt_touch_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
  102. {
  103. rt_touch_t touch;
  104. rt_size_t result = 0;
  105. RT_ASSERT(dev != RT_NULL);
  106. touch = (rt_touch_t)dev;
  107. if (buf == NULL || len == 0)
  108. {
  109. return 0;
  110. }
  111. result = touch->ops->touch_readpoint(touch, buf, len);
  112. return result;
  113. }
  114. static rt_err_t rt_touch_control(rt_device_t dev, int cmd, void *args)
  115. {
  116. rt_touch_t touch;
  117. rt_err_t result = RT_EOK;
  118. RT_ASSERT(dev != RT_NULL);
  119. touch = (rt_touch_t)dev;
  120. switch (cmd)
  121. {
  122. case RT_TOUCH_CTRL_GET_ID:
  123. if (args)
  124. {
  125. result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_GET_ID, args);
  126. }
  127. else
  128. {
  129. result = -RT_ERROR;
  130. }
  131. break;
  132. case RT_TOUCH_CTRL_GET_INFO:
  133. if (args)
  134. {
  135. result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_GET_INFO, args);
  136. }
  137. else
  138. {
  139. result = -RT_ERROR;
  140. }
  141. break;
  142. case RT_TOUCH_CTRL_SET_MODE:
  143. result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_MODE, args);
  144. if (result == RT_EOK)
  145. {
  146. rt_uint16_t mode;
  147. mode = *(rt_uint16_t*)args;
  148. if (mode == RT_DEVICE_FLAG_INT_RX)
  149. {
  150. rt_touch_irq_enable(touch); /* enable interrupt */
  151. }
  152. }
  153. break;
  154. case RT_TOUCH_CTRL_SET_X_RANGE:
  155. result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_X_RANGE, args);
  156. if (result == RT_EOK)
  157. {
  158. touch->info.range_x = *(rt_int32_t *)args;
  159. LOG_D("set x coordinate range :%d\n", touch->info.range_x);
  160. }
  161. break;
  162. case RT_TOUCH_CTRL_SET_Y_RANGE:
  163. result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_Y_RANGE, args);
  164. if (result == RT_EOK)
  165. {
  166. touch->info.range_y = *(rt_uint32_t *)args;
  167. LOG_D("set y coordinate range :%d \n", touch->info.range_x);
  168. }
  169. break;
  170. case RT_TOUCH_CTRL_DISABLE_INT:
  171. rt_touch_irq_disable(touch);
  172. break;
  173. case RT_TOUCH_CTRL_ENABLE_INT:
  174. rt_touch_irq_enable(touch);
  175. break;
  176. default:
  177. return -RT_ERROR;
  178. }
  179. return result;
  180. }
  181. #ifdef RT_USING_DEVICE_OPS
  182. const static struct rt_device_ops rt_touch_ops =
  183. {
  184. RT_NULL,
  185. rt_touch_open,
  186. rt_touch_close,
  187. rt_touch_read,
  188. RT_NULL,
  189. rt_touch_control
  190. };
  191. #endif
  192. /*
  193. * touch register
  194. */
  195. int rt_hw_touch_register(rt_touch_t touch,
  196. const char *name,
  197. rt_uint32_t flag,
  198. void *data)
  199. {
  200. rt_int8_t result;
  201. rt_device_t device;
  202. RT_ASSERT(touch != RT_NULL);
  203. device = &touch->parent;
  204. #ifdef RT_USING_DEVICE_OPS
  205. device->ops = &rt_touch_ops;
  206. #else
  207. device->init = RT_NULL;
  208. device->open = rt_touch_open;
  209. device->close = rt_touch_close;
  210. device->read = rt_touch_read;
  211. device->write = RT_NULL;
  212. device->control = rt_touch_control;
  213. #endif
  214. device->type = RT_Device_Class_Touch;
  215. device->rx_indicate = RT_NULL;
  216. device->tx_complete = RT_NULL;
  217. device->user_data = data;
  218. result = rt_device_register(device, name, flag | RT_DEVICE_FLAG_STANDALONE);
  219. if (result != RT_EOK)
  220. {
  221. LOG_E("rt_touch register err code: %d", result);
  222. return result;
  223. }
  224. LOG_I("rt_touch init success");
  225. return RT_EOK;
  226. }