drv_touch_ft6x36.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-06-29 solar the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "touch.h"
  13. #include "lcd_port.h"
  14. #include "drv_common.h"
  15. #define DBG_TAG "ft6236"
  16. #define DBG_LVL DBG_LOG
  17. #include <rtdbg.h>
  18. #ifdef BSP_USING_TOUCH_FT6X36
  19. #include "ft6236.h"
  20. #define BSP_TOUCH_I2C_BUS_NAME "i2c1"
  21. #define BSP_TOUCH_I2C_RESET_PIN 119 /* PH.7 */
  22. #ifdef PKG_USING_LVGL
  23. #include <lvgl.h>
  24. extern void lv_port_indev_input(rt_int16_t x, rt_int16_t y, lv_indev_state_t state);
  25. #endif /* PKG_USING_LVGL */
  26. rt_thread_t ft6236_thread;
  27. rt_device_t touch;
  28. void ft6236_thread_entry(void *parameter)
  29. {
  30. struct rt_touch_data *read_data;
  31. read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));
  32. while (1)
  33. {
  34. rt_device_read(touch, 0, read_data, 1);
  35. #ifdef PKG_USING_LVGL
  36. /* Since the origin of the LCD screen and the origin of the touch screen are
  37. * different, the parameters passed in here need to be simply converted. */
  38. if (read_data->event == RT_TOUCH_EVENT_DOWN)
  39. lv_port_indev_input(read_data->y_coordinate, read_data->x_coordinate, LV_INDEV_STATE_PR);
  40. if (read_data->event == RT_TOUCH_EVENT_MOVE)
  41. lv_port_indev_input(read_data->y_coordinate, read_data->x_coordinate, LV_INDEV_STATE_PR);
  42. if (read_data->event == RT_TOUCH_EVENT_UP)
  43. lv_port_indev_input(read_data->y_coordinate, read_data->x_coordinate, LV_INDEV_STATE_REL);
  44. #endif /* PKG_USING_LVGL */
  45. if (read_data->event != RT_TOUCH_EVENT_NONE)
  46. LOG_I("LCD point x: %03d y: %03d", read_data->y_coordinate, LCD_HEIGHT - read_data->x_coordinate);
  47. rt_thread_delay(10);
  48. }
  49. }
  50. int ft6236_for_lvgl(void)
  51. {
  52. struct rt_touch_config cfg;
  53. cfg.dev_name = BSP_TOUCH_I2C_BUS_NAME;
  54. rt_hw_ft6236_init("touch", &cfg, BSP_TOUCH_I2C_RESET_PIN);
  55. touch = rt_device_find("touch");
  56. rt_device_open(touch, RT_DEVICE_FLAG_RDONLY);
  57. ft6236_thread = rt_thread_create("touch", ft6236_thread_entry, RT_NULL, 1024, 10, 20);
  58. if (ft6236_thread == RT_NULL)
  59. {
  60. LOG_D("create ft6236 thread err");
  61. return -RT_ENOMEM;
  62. }
  63. rt_thread_startup(ft6236_thread);
  64. return RT_EOK;
  65. }
  66. INIT_ENV_EXPORT(ft6236_for_lvgl);
  67. #endif /* BSP_USING_TOUCH_FT6X36 */