vtouch.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * 2020-12-21 stackRyan first version
  9. */
  10. #include "rtconfig.h"
  11. #ifdef VDEVICE_USING_TOUCH
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "string.h"
  15. #include <touch.h>
  16. #define DBG_TAG VDEVICE_TOUCH_NAME
  17. #define DBG_LVL DBG_LOG
  18. #include <rtdbg.h>
  19. #define TOUCH_WIDTH 240 //resolution ratio config X
  20. #define TOUCH_HEIGHT 240 //resolution ratio config Y
  21. #define TOUCH_POINT_NUM 1 //touchpoint number
  22. static struct rt_touch_device touch_device;
  23. static rt_size_t device_touch_readpoint(struct rt_touch_device *touch, void *buf, rt_size_t touch_num)
  24. {
  25. struct rt_touch_data touch_data = {0};
  26. /*make a fake touch data and event*/
  27. touch_data.event = RT_TOUCH_EVENT_UP;
  28. touch_data.x_coordinate = 100;
  29. touch_data.y_coordinate = 100;
  30. rt_memcpy(buf, &touch_data, sizeof(struct rt_touch_data));
  31. LOG_D("_touch_readpoint");
  32. return touch_num;
  33. }
  34. static rt_err_t device_touch_control(struct rt_touch_device *touch, int cmd, void *arg)
  35. {
  36. RT_ASSERT(touch != RT_NULL);
  37. switch (cmd)
  38. {
  39. case RT_TOUCH_CTRL_GET_ID:
  40. {
  41. *(rt_uint8_t *)arg = 0x5a;
  42. LOG_D("RT_TOUCH_CTRL_GET_ID: %d", *(rt_uint8_t *)arg);
  43. break;
  44. }
  45. case RT_TOUCH_CTRL_GET_INFO:
  46. {
  47. *(rt_uint8_t *)arg = 0xa5;
  48. LOG_D("RT_TOUCH_CTRL_GET_INFO: %d", *(rt_uint8_t *)arg);
  49. break;
  50. }
  51. case RT_TOUCH_CTRL_SET_MODE:
  52. {
  53. /* */
  54. rt_uint8_t mode = *(rt_uint8_t *)arg;
  55. LOG_D("RT_TOUCH_CTRL_SET_MODE: %d", mode);
  56. break;
  57. }
  58. case RT_TOUCH_CTRL_SET_X_RANGE:
  59. {
  60. touch_device.info.range_x = *(rt_uint32_t *)arg;
  61. LOG_D("RT_TOUCH_CTRL_SET_X_RANGE: %d", touch_device.info.type);
  62. break;
  63. }
  64. case RT_TOUCH_CTRL_SET_Y_RANGE:
  65. {
  66. touch_device.info.range_x = *(rt_uint32_t *)arg;
  67. LOG_D("RT_TOUCH_CTRL_SET_Y_RANGE: %d", touch_device.info.type);
  68. break;
  69. }
  70. case RT_TOUCH_CTRL_SET_X_TO_Y:
  71. LOG_D("RT_TOUCH_CTRL_SET_X_TO_Y");
  72. break;
  73. case RT_TOUCH_CTRL_DISABLE_INT:
  74. LOG_D("RT_TOUCH_CTRL_DISABLE_INT");
  75. break;
  76. case RT_TOUCH_CTRL_ENABLE_INT:
  77. LOG_D("RT_TOUCH_CTRL_ENABLE_INT");
  78. break;
  79. case RT_TOUCH_CTRL_POWER_ON:
  80. LOG_D("RT_TOUCH_CTRL_POWER_ON");
  81. break;
  82. case RT_TOUCH_CTRL_POWER_OFF:
  83. LOG_D("RT_TOUCH_CTRL_POWER_OFF");
  84. break;
  85. case RT_TOUCH_CTRL_GET_STATUS:
  86. LOG_D("RT_TOUCH_CTRL_GET_STATUS");
  87. break;
  88. default:
  89. break;
  90. }
  91. return RT_EOK;
  92. }
  93. static struct rt_touch_ops touch_ops =
  94. {
  95. .touch_readpoint = device_touch_readpoint,
  96. .touch_control = device_touch_control,
  97. };
  98. static int rt_hw_touch_init(void)
  99. {
  100. rt_err_t result = RT_EOK;
  101. /* touch infomation */
  102. touch_device.info.type = RT_TOUCH_TYPE_CAPACITANCE;
  103. touch_device.info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  104. touch_device.info.point_num = TOUCH_POINT_NUM;
  105. touch_device.info.range_x = TOUCH_WIDTH;
  106. touch_device.info.range_y = TOUCH_HEIGHT;
  107. touch_device.config.user_data = RT_NULL;
  108. touch_device.ops = &touch_ops;
  109. #ifdef RT_TOUCH_USING_PIN
  110. touch_device.config.irq_pin.pin = RT_PIN_NONE;
  111. touch_device.config.irq_pin.mode = PIN_MODE_INPUT;
  112. #endif /* RT_TOUCH_USING_PIN */
  113. result = rt_hw_touch_register(&touch_device, VDEVICE_TOUCH_NAME, RT_DEVICE_FLAG_INT_RX, RT_NULL);
  114. LOG_D("rt_hw_touch_init succeed!");
  115. return result;
  116. }
  117. INIT_DEVICE_EXPORT(rt_hw_touch_init);
  118. #endif /* VDEVICE_USING_TOUCH */