lv_port_indev.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-03-09 Rbb666 The first version
  9. */
  10. #include <lvgl.h>
  11. #include <rtdevice.h>
  12. #include "gt911.h"
  13. #define DBG_TAG "lv_port_indev"
  14. #define DBG_LVL DBG_LOG
  15. #include <rtdbg.h>
  16. #include "hal_data.h"
  17. #define GT911_IRQ_PIN BSP_IO_PORT_00_PIN_04
  18. #define GT911_RST_PIN BSP_IO_PORT_08_PIN_01
  19. static rt_device_t touch_dev;
  20. static lv_indev_t *touch_indev;
  21. struct rt_touch_data *read_data;
  22. volatile static rt_uint8_t touch_detect_flag = 0;
  23. static void touchpad_read(lv_indev_drv_t *indev, lv_indev_data_t *data)
  24. {
  25. if (touch_detect_flag != 1)
  26. return;
  27. rt_device_read(touch_dev, 0, read_data, 1);
  28. if (read_data->event == RT_TOUCH_EVENT_NONE)
  29. return;
  30. data->point.x = read_data->x_coordinate;
  31. data->point.y = read_data->y_coordinate;
  32. if (read_data->event == RT_TOUCH_EVENT_DOWN)
  33. data->state = LV_INDEV_STATE_PR;
  34. if (read_data->event == RT_TOUCH_EVENT_MOVE)
  35. data->state = LV_INDEV_STATE_PR;
  36. if (read_data->event == RT_TOUCH_EVENT_UP)
  37. data->state = LV_INDEV_STATE_REL;
  38. touch_detect_flag = 0;
  39. rt_device_control(touch_dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL);
  40. }
  41. static rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
  42. {
  43. touch_detect_flag = 1;
  44. rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL);
  45. return 0;
  46. }
  47. rt_err_t gt911_probe(rt_uint16_t x, rt_uint16_t y)
  48. {
  49. void *id;
  50. touch_dev = rt_device_find("gt911");
  51. if (touch_dev == RT_NULL)
  52. {
  53. rt_kprintf("can't find device gt911\n");
  54. return -1;
  55. }
  56. if (rt_device_open(touch_dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
  57. {
  58. rt_kprintf("open device failed!");
  59. return -1;
  60. }
  61. id = rt_malloc(sizeof(rt_uint8_t) * 8);
  62. rt_device_control(touch_dev, RT_TOUCH_CTRL_GET_ID, id);
  63. rt_uint8_t *read_id = (rt_uint8_t *)id;
  64. rt_kprintf("id = GT%d%d%d \n", read_id[0] - '0', read_id[1] - '0', read_id[2] - '0');
  65. rt_device_control(touch_dev, RT_TOUCH_CTRL_SET_X_RANGE, &x); /* if possible you can set your x y coordinate*/
  66. rt_device_control(touch_dev, RT_TOUCH_CTRL_SET_Y_RANGE, &y);
  67. rt_device_control(touch_dev, RT_TOUCH_CTRL_GET_INFO, id);
  68. rt_kprintf("range_x = %d \n", (*(struct rt_touch_info *)id).range_x);
  69. rt_kprintf("range_y = %d \n", (*(struct rt_touch_info *)id).range_y);
  70. rt_kprintf("point_num = %d \n", (*(struct rt_touch_info *)id).point_num);
  71. rt_free(id);
  72. rt_device_set_rx_indicate(touch_dev, rx_callback);
  73. read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));
  74. if (!read_data)
  75. {
  76. return -RT_ENOMEM;
  77. }
  78. return RT_EOK;
  79. }
  80. #define RST_PIN "p801"
  81. #define INT_PIN "p004"
  82. rt_err_t rt_hw_gt911_register(void)
  83. {
  84. struct rt_touch_config cfg;
  85. rt_base_t int_pin = rt_pin_get(INT_PIN);
  86. rt_base_t rst_pin = rt_pin_get(RST_PIN);
  87. cfg.dev_name = "i2c1";
  88. cfg.irq_pin.pin = int_pin;
  89. cfg.irq_pin.mode = PIN_MODE_INPUT_PULLDOWN;
  90. cfg.user_data = &rst_pin;
  91. rt_hw_gt911_init("gt911", &cfg);
  92. gt911_probe(480, 272);
  93. return RT_EOK;
  94. }
  95. void lv_port_indev_init(void)
  96. {
  97. static lv_indev_drv_t indev_drv; /* Descriptor of a input device driver */
  98. lv_indev_drv_init(&indev_drv); /* Basic initialization */
  99. indev_drv.type = LV_INDEV_TYPE_POINTER; /* Touch pad is a pointer-like device */
  100. indev_drv.read_cb = touchpad_read; /* Set your driver function */
  101. /* Register the driver in LVGL and save the created input device object */
  102. touch_indev = lv_indev_drv_register(&indev_drv);
  103. /* Register touch device */
  104. rt_err_t res = rt_hw_gt911_register();
  105. RT_ASSERT(res == RT_EOK);
  106. }