lv_port_indev.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-07 liYony The first version (FT6336)
  9. * 2022-07-08 liYony Add FT6206
  10. */
  11. #include <lvgl.h>
  12. #include <rtdevice.h>
  13. #include <touch.h>
  14. #include <lcd_port.h>
  15. #define DBG_TAG "LVGL.port.indev"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. /* Add the include file of the package you are using */
  19. #ifdef BSP_USING_TOUCH_FT6X36
  20. #include <ft6236.h>
  21. #elif defined(BSP_USING_TOUCH_FT6206)
  22. #include <ft6206.h>
  23. #endif /* BSP_USING_TOUCH_FT6X36 */
  24. /* Touch chip connection information */
  25. #define BSP_TOUCH_I2C_BUS_NAME "i2c1"
  26. #define BSP_TOUCH_I2C_RESET_PIN 119 /* PH.7 */
  27. /* RT-Thread touch device name */
  28. #define TOUCH_DEV_NAME "touch"
  29. lv_indev_t * touch_indev;
  30. rt_device_t touch_dev;
  31. struct rt_touch_data *read_data;
  32. static void input_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data)
  33. {
  34. rt_device_read(touch_dev, 0, read_data, 1);
  35. if (read_data->event == RT_TOUCH_EVENT_NONE)
  36. return;
  37. /* Since the origin of the LCD screen and the origin of the touch screen are
  38. * different, the parameters passed in here need to be simply converted. */
  39. #ifdef BSP_USING_TOUCH_FT6X36
  40. data->point.x = read_data->y_coordinate;
  41. data->point.y = LCD_HEIGHT - read_data->x_coordinate;
  42. #elif defined(BSP_USING_TOUCH_FT6206)
  43. data->point.x = read_data->x_coordinate;
  44. data->point.y = LCD_HEIGHT - read_data->y_coordinate;
  45. #endif /* BSP_USING_TOUCH_FT6X36 */
  46. if (read_data->event == RT_TOUCH_EVENT_DOWN)
  47. data->state = LV_INDEV_STATE_PR;
  48. if (read_data->event == RT_TOUCH_EVENT_MOVE)
  49. data->state = LV_INDEV_STATE_PR;
  50. if (read_data->event == RT_TOUCH_EVENT_UP)
  51. data->state = LV_INDEV_STATE_REL;
  52. }
  53. void lv_port_indev_init(void)
  54. {
  55. static lv_indev_drv_t indev_drv;
  56. lv_indev_drv_init(&indev_drv); /*Basic initialization*/
  57. indev_drv.type = LV_INDEV_TYPE_POINTER;
  58. indev_drv.read_cb = input_read;
  59. /*Register the driver in LVGL and save the created input device object*/
  60. touch_indev = lv_indev_drv_register(&indev_drv);
  61. }
  62. static int lv_hw_touch_init(void)
  63. {
  64. struct rt_touch_config cfg;
  65. cfg.dev_name = BSP_TOUCH_I2C_BUS_NAME;
  66. #ifdef BSP_USING_TOUCH_FT6X36
  67. rt_hw_ft6236_init(TOUCH_DEV_NAME, &cfg, BSP_TOUCH_I2C_RESET_PIN);
  68. #elif defined(BSP_USING_TOUCH_FT6206)
  69. rt_hw_ft6206_init(TOUCH_DEV_NAME, &cfg);
  70. #endif /* BSP_USING_TOUCH_FT6X36 */
  71. touch_dev = rt_device_find(TOUCH_DEV_NAME);
  72. if (rt_device_open(touch_dev, RT_DEVICE_FLAG_RDONLY) != RT_EOK)
  73. {
  74. LOG_E("Can't open touch device:%s", TOUCH_DEV_NAME);
  75. return -RT_ERROR;
  76. }
  77. read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));
  78. return RT_EOK;
  79. }
  80. INIT_COMPONENT_EXPORT(lv_hw_touch_init);