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-02-01 Rudy Lo The first version
9 */
10
11 #include <lvgl.h>
12 #include <stdbool.h>
13 #include <rtdevice.h>
14 #include <drv_gpio.h>
15
16 #include "ft6236.h"
17
18 #define TOUCH_DEVICE_NAME "touch_ft" /* Touch device name */
19 #define TOUCH_DEVICE_I2C_BUS "i2c2" /* SCL -> PH15(127), SDA -> PH13(125) */
20 #define REST_PIN GET_PIN(A, 3) /* reset pin */
21 #define USER_BUTTON_PIN GET_PIN(H, 4) /* Reserve for LV_INDEV_TYPE_BUTTON */
22
23 static rt_device_t ts; /* Touch device handle, Touchscreen */
24 static struct rt_touch_data *read_data;
25
26 static rt_int16_t last_x = 0;
27 static rt_int16_t last_y = 0;
28
touchpad_is_pressed(void)29 static bool touchpad_is_pressed(void)
30 {
31 if (RT_EOK == rt_device_read(ts, 0, read_data, 1)) /* return RT_EOK is a bug (ft6236) */
32 {
33 if (read_data->event == RT_TOUCH_EVENT_DOWN) {
34 /* swap x and y */
35 rt_int16_t tmp_x = read_data->y_coordinate;
36 rt_int16_t tmp_y = read_data->x_coordinate;
37
38 /* invert y */
39 tmp_y = 320 - tmp_y;
40
41 /* restore data */
42 last_x = tmp_x;
43 last_y = tmp_y;
44
45 rt_kprintf("touch: x = %d, y = %d\n", last_x, last_y);
46 return true;
47 }
48 }
49
50 return false;
51 }
52
touchpad_get_xy(rt_int16_t * x,rt_int16_t * y)53 static void touchpad_get_xy(rt_int16_t *x, rt_int16_t *y)
54 {
55 *x = last_x;
56 *y = last_y;
57 }
58
touchpad_read(lv_indev_drv_t * indev,lv_indev_data_t * data)59 static void touchpad_read(lv_indev_drv_t *indev, lv_indev_data_t *data)
60 {
61 /*`touchpad_is_pressed` and `touchpad_get_xy` needs to be implemented by you*/
62 if(touchpad_is_pressed()) {
63 data->state = LV_INDEV_STATE_PRESSED;
64 touchpad_get_xy(&data->point.x, &data->point.y);
65 } else {
66 data->state = LV_INDEV_STATE_RELEASED;
67 }
68 }
69
rt_hw_ft6236_register(void)70 rt_err_t rt_hw_ft6236_register(void)
71 {
72 struct rt_touch_config config;
73 config.dev_name = TOUCH_DEVICE_I2C_BUS;
74 rt_hw_ft6236_init(TOUCH_DEVICE_NAME, &config, REST_PIN);
75
76 ts = rt_device_find(TOUCH_DEVICE_NAME);
77 if (!ts) {
78 return -RT_ERROR;
79 }
80
81 read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));
82 if (!read_data) {
83 return -RT_ENOMEM;
84 }
85
86 if (!rt_device_open(ts, RT_DEVICE_FLAG_RDONLY)) {
87 struct rt_touch_info info;
88 rt_device_control(ts, RT_TOUCH_CTRL_GET_INFO, &info);
89 rt_kprintf("type :%d\n", info.type);
90 rt_kprintf("vendor :%s\n", info.vendor);
91 rt_kprintf("point_num :%d\n", info.point_num);
92 rt_kprintf("range_x :%d\n", info.range_x);
93 rt_kprintf("range_y :%d\n", info.range_y);
94 return RT_EOK;
95 } else {
96 rt_kprintf("open touch device failed.\n");
97 return -RT_ERROR;
98 }
99 }
100
101 lv_indev_t * touch_indev;
102
lv_port_indev_init(void)103 void lv_port_indev_init(void)
104 {
105 static lv_indev_drv_t indev_drv; /* Descriptor of a input device driver */
106 lv_indev_drv_init(&indev_drv); /* Basic initialization */
107 indev_drv.type = LV_INDEV_TYPE_POINTER; /* Touch pad is a pointer-like device */
108 indev_drv.read_cb = touchpad_read; /* Set your driver function */
109
110 /* Register the driver in LVGL and save the created input device object */
111 touch_indev = lv_indev_drv_register(&indev_drv);
112
113 /* Register touch device */
114 rt_hw_ft6236_register();
115 }
116