touch.c 5.9 KB

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