Skip to content

Commit 0d32432

Browse files
committed
touch driver: Add LVGL v8 compatibility
1 parent f726311 commit 0d32432

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed

lvgl_touch/gt911.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright © 2021 Sturnus Inc.
3+
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
5+
* software and associated documentation files (the “Software”), to deal in the Software
6+
* without restriction, including without limitation the rights to use, copy, modify, merge,
7+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
8+
* to whom the Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
15+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
16+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
#include <esp_log.h>
22+
#include <driver/i2c.h>
23+
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
24+
#include <lvgl.h>
25+
#else
26+
#include <lvgl/lvgl.h>
27+
#endif
28+
#include "gt911.h"
29+
#include "tp_i2c.h"
30+
#include "../lvgl_i2c_conf.h"
31+
32+
#define TAG "GT911"
33+
34+
gt911_status_t gt911_status;
35+
36+
//TODO: handle multibyte read and refactor to just one read transaction
37+
esp_err_t gt911_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) {
38+
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
39+
40+
i2c_master_start(i2c_cmd);
41+
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true);
42+
i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK);
43+
i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK);
44+
45+
i2c_master_start(i2c_cmd);
46+
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true);
47+
48+
i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK);
49+
i2c_master_stop(i2c_cmd);
50+
esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS);
51+
i2c_cmd_link_delete(i2c_cmd);
52+
return ret;
53+
}
54+
55+
esp_err_t gt911_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) {
56+
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
57+
58+
i2c_master_start(i2c_cmd);
59+
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true);
60+
i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK);
61+
i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK);
62+
i2c_master_write_byte(i2c_cmd, data, I2C_MASTER_ACK);
63+
i2c_master_stop(i2c_cmd);
64+
esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS);
65+
i2c_cmd_link_delete(i2c_cmd);
66+
return ret;
67+
}
68+
69+
/**
70+
* @brief Initialize for GT911 communication via I2C
71+
* @param dev_addr: Device address on communication Bus (I2C slave address of GT911).
72+
* @retval None
73+
*/
74+
void gt911_init(uint8_t dev_addr) {
75+
if (!gt911_status.inited) {
76+
gt911_status.i2c_dev_addr = dev_addr;
77+
uint8_t data_buf;
78+
esp_err_t ret;
79+
80+
ESP_LOGI(TAG, "Checking for GT911 Touch Controller");
81+
if ((ret = gt911_i2c_read(dev_addr, GT911_PRODUCT_ID1, &data_buf, 1) != ESP_OK)) {
82+
ESP_LOGE(TAG, "Error reading from device: %s",
83+
esp_err_to_name(ret)); // Only show error the first time
84+
return;
85+
}
86+
87+
// Read 4 bytes for Product ID in ASCII
88+
for (int i = 0; i < GT911_PRODUCT_ID_LEN; i++) {
89+
gt911_i2c_read(dev_addr, (GT911_PRODUCT_ID1 + i), (uint8_t *)&(gt911_status.product_id[i]), 1);
90+
}
91+
ESP_LOGI(TAG, "\tProduct ID: %s", gt911_status.product_id);
92+
93+
gt911_i2c_read(dev_addr, GT911_VENDOR_ID, &data_buf, 1);
94+
ESP_LOGI(TAG, "\tVendor ID: 0x%02x", data_buf);
95+
96+
gt911_i2c_read(dev_addr, GT911_X_COORD_RES_L, &data_buf, 1);
97+
gt911_status.max_x_coord = data_buf;
98+
gt911_i2c_read(dev_addr, GT911_X_COORD_RES_H, &data_buf, 1);
99+
gt911_status.max_x_coord |= ((uint16_t)data_buf << 8);
100+
ESP_LOGI(TAG, "\tX Resolution: %d", gt911_status.max_x_coord);
101+
102+
gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_L, &data_buf, 1);
103+
gt911_status.max_y_coord = data_buf;
104+
gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_H, &data_buf, 1);
105+
gt911_status.max_y_coord |= ((uint16_t)data_buf << 8);
106+
ESP_LOGI(TAG, "\tY Resolution: %d", gt911_status.max_y_coord);
107+
gt911_status.inited = true;
108+
}
109+
}
110+
111+
/**
112+
* @brief Get the touch screen X and Y positions values. Ignores multi touch
113+
* @param drv:
114+
* @param data: Store data here
115+
* @retval Always false
116+
*/
117+
bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
118+
uint8_t touch_pnt_cnt; // Number of detected touch points
119+
static int16_t last_x = 0; // 12bit pixel value
120+
static int16_t last_y = 0; // 12bit pixel value
121+
uint8_t data_buf;
122+
uint8_t status_reg;
123+
124+
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_STATUS_REG, &status_reg, 1);
125+
// ESP_LOGI(TAG, "\tstatus: 0x%02x", status_reg);
126+
touch_pnt_cnt = status_reg & 0x0F;
127+
if ((status_reg & 0x80) || (touch_pnt_cnt < 6)) {
128+
//Reset Status Reg Value
129+
gt911_i2c_write8(gt911_status.i2c_dev_addr, GT911_STATUS_REG, 0x00);
130+
}
131+
if (touch_pnt_cnt != 1) { // ignore no touch & multi touch
132+
data->point.x = last_x;
133+
data->point.y = last_y;
134+
data->state = LV_INDEV_STATE_REL;
135+
return false;
136+
}
137+
138+
// gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_TRACK_ID1, &data_buf, 1);
139+
// ESP_LOGI(TAG, "\ttrack_id: %d", data_buf);
140+
141+
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_L, &data_buf, 1);
142+
last_x = data_buf;
143+
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_H, &data_buf, 1);
144+
last_x |= ((uint16_t)data_buf << 8);
145+
146+
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_L, &data_buf, 1);
147+
last_y = data_buf;
148+
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_H, &data_buf, 1);
149+
last_y |= ((uint16_t)data_buf << 8);
150+
151+
#if CONFIG_LV_GT911_INVERT_X
152+
last_x = gt911_status.max_x_coord - last_x;
153+
#endif
154+
#if CONFIG_LV_GT911_INVERT_Y
155+
last_y = gt911_status.max_y_coord - last_y;
156+
#endif
157+
#if CONFIG_LV_GT911_SWAPXY
158+
int16_t swap_buf = last_x;
159+
last_x = last_y;
160+
last_y = swap_buf;
161+
#endif
162+
data->point.x = last_x;
163+
data->point.y = last_y;
164+
data->state = LV_INDEV_STATE_PR;
165+
ESP_LOGI(TAG, "X=%u Y=%u", data->point.x, data->point.y);
166+
ESP_LOGV(TAG, "X=%u Y=%u", data->point.x, data->point.y);
167+
return false;
168+
}

lvgl_touch/gt911.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#ifndef __GT911_H
2+
/*
3+
* Copyright © 2021 Sturnus Inc.
4+
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
6+
* software and associated documentation files (the “Software”), to deal in the Software
7+
* without restriction, including without limitation the rights to use, copy, modify, merge,
8+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9+
* to whom the Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all copies or
12+
* substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
#define __GT911_H
23+
24+
#include <stdint.h>
25+
#include <stdbool.h>
26+
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
27+
#include "lvgl.h"
28+
#else
29+
#include "lvgl/lvgl.h"
30+
#endif
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
#define GT911_I2C_SLAVE_ADDR 0x5D
37+
38+
#define GT911_PRODUCT_ID_LEN 4
39+
40+
/* Register Map of GT911 */
41+
#define GT911_PRODUCT_ID1 0x8140
42+
#define GT911_PRODUCT_ID2 0x8141
43+
#define GT911_PRODUCT_ID3 0x8142
44+
#define GT911_PRODUCT_ID4 0x8143
45+
#define GT911_FIRMWARE_VER_L 0x8144
46+
#define GT911_FIRMWARE_VER_H 0x8145
47+
#define GT911_X_COORD_RES_L 0x8146
48+
#define GT911_X_COORD_RES_H 0x8147
49+
#define GT911_Y_COORD_RES_L 0x8148
50+
#define GT911_Y_COORD_RES_H 0x8149
51+
#define GT911_VENDOR_ID 0x814A
52+
53+
#define GT911_STATUS_REG 0x814E
54+
#define GT911_STATUS_REG_BUF 0x80
55+
#define GT911_STATUS_REG_LARGE 0x40
56+
#define GT911_STATUS_REG_PROX_VALID 0x20
57+
#define GT911_STATUS_REG_HAVEKEY 0x10
58+
#define GT911_STATUS_REG_PT_MASK 0x0F
59+
60+
#define GT911_TRACK_ID1 0x814F
61+
#define GT911_PT1_X_COORD_L 0x8150
62+
#define GT911_PT1_X_COORD_H 0x8151
63+
#define GT911_PT1_Y_COORD_L 0x8152
64+
#define GT911_PT1_Y_COORD_H 0x8153
65+
#define GT911_PT1_X_SIZE_L 0x8154
66+
#define GT911_PT1_X_SIZE_H 0x8155
67+
68+
typedef struct {
69+
bool inited;
70+
char product_id[GT911_PRODUCT_ID_LEN];
71+
uint16_t max_x_coord;
72+
uint16_t max_y_coord;
73+
uint8_t i2c_dev_addr;
74+
} gt911_status_t;
75+
76+
/**
77+
* @brief Initialize for GT911 communication via I2C
78+
* @param dev_addr: Device address on communication Bus (I2C slave address of GT911).
79+
* @retval None
80+
*/
81+
void gt911_init(uint8_t dev_addr);
82+
83+
/**
84+
* @brief Get the touch screen X and Y positions values. Ignores multi touch
85+
* @param drv:
86+
* @param data: Store data here
87+
* @retval Always false
88+
*/
89+
bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
90+
91+
#ifdef __cplusplus
92+
}
93+
#endif
94+
#endif /* __GT911_H */

lvgl_touch/touch_driver.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ void touch_driver_init(void)
2626
#endif
2727
}
2828

29+
#if LVGL_VERSION_MAJOR >= 8
30+
void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
31+
#else
2932
bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
33+
#endif
3034
{
3135
bool res = false;
3236

@@ -46,6 +50,10 @@ bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
4650
res = gt911_read(drv, data);
4751
#endif
4852

53+
#if LVGL_VERSION_MAJOR >= 8
54+
data->continue_reading = res;
55+
#else
4956
return res;
57+
#endif
5058
}
5159

lvgl_touch/touch_driver.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ extern "C" {
4444
* GLOBAL PROTOTYPES
4545
**********************/
4646
void touch_driver_init(void);
47+
48+
#if LVGL_VERSION_MAJOR >= 8
49+
void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
50+
#else
4751
bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
52+
#endif
4853

4954
#ifdef __cplusplus
5055
} /* extern "C" */

0 commit comments

Comments
 (0)