Skip to content

Commit e3ed08e

Browse files
committed
feat(multi-touch): Add track id reading to GT911 driver
1 parent a5634c6 commit e3ed08e

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

components/lcd_touch/esp_lcd_touch_gt911/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,11 @@ Read data from the touch controller and store it in RAM memory. It should be cal
5656
esp_lcd_touch_read_data(tp);
5757
```
5858

59-
Get one X and Y coordinates with strength of touch.
59+
Get attributes of a single touch point.
6060

6161
```
62-
uint16_t touch_x[1];
63-
uint16_t touch_y[1];
64-
uint16_t touch_strength[1];
62+
esp_lcd_touch_point_data_t touch_point_data[1];
6563
uint8_t touch_cnt = 0;
6664
67-
bool touchpad_pressed = esp_lcd_touch_get_coordinates(tp, touch_x, touch_y, touch_strength, &touch_cnt, 1);
65+
ESP_ERROR_CHECK(esp_lcd_touch_get_data(tp, touch_point_data, &touch_cnt, 1));
6866
```

components/lcd_touch/esp_lcd_touch_gt911/esp_lcd_touch_gt911.c

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -34,6 +34,7 @@ static const char *TAG = "GT911";
3434
*******************************************************************************/
3535
static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp);
3636
static bool esp_lcd_touch_gt911_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num);
37+
static esp_err_t esp_lcd_touch_gt911_get_track_id(esp_lcd_touch_handle_t tp, uint8_t *track_id, uint8_t point_num);
3738
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
3839
static esp_err_t esp_lcd_touch_gt911_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state);
3940
#endif
@@ -60,9 +61,9 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
6061
{
6162
esp_err_t ret = ESP_OK;
6263

63-
assert(io != NULL);
64-
assert(config != NULL);
65-
assert(out_touch != NULL);
64+
ESP_RETURN_ON_FALSE(io != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch panel handler pointer can't be NULL");
65+
ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG, TAG, "Config pointer can't be NULL");
66+
ESP_RETURN_ON_FALSE(out_touch != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch handler pointer can't be NULL");
6667

6768
/* Prepare main structure */
6869
esp_lcd_touch_handle_t esp_lcd_touch_gt911 = heap_caps_calloc(1, sizeof(esp_lcd_touch_t), MALLOC_CAP_DEFAULT);
@@ -74,6 +75,7 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
7475
/* Only supported callbacks are set */
7576
esp_lcd_touch_gt911->read_data = esp_lcd_touch_gt911_read_data;
7677
esp_lcd_touch_gt911->get_xy = esp_lcd_touch_gt911_get_xy;
78+
esp_lcd_touch_gt911->get_track_id = esp_lcd_touch_gt911_get_track_id;
7779
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
7880
esp_lcd_touch_gt911->get_button_state = esp_lcd_touch_gt911_get_button_state;
7981
#endif
@@ -214,7 +216,7 @@ static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp)
214216
uint8_t clear = 0;
215217
size_t i = 0;
216218

217-
assert(tp != NULL);
219+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
218220

219221
err = touch_gt911_i2c_read(tp, ESP_LCD_TOUCH_GT911_READ_XY_REG, buf, 1);
220222
ESP_RETURN_ON_ERROR(err, TAG, "I2C read error!");
@@ -277,9 +279,10 @@ static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp)
277279

278280
/* Fill all coordinates */
279281
for (i = 0; i < touch_cnt; i++) {
280-
tp->data.coords[i].x = ((uint16_t)buf[(i * 8) + 3] << 8) + buf[(i * 8) + 2];
281-
tp->data.coords[i].y = (((uint16_t)buf[(i * 8) + 5] << 8) + buf[(i * 8) + 4]);
282-
tp->data.coords[i].strength = (((uint16_t)buf[(i * 8) + 7] << 8) + buf[(i * 8) + 6]);
282+
tp->data.points_data[i].track_id = buf[(i * 8) + 1];
283+
tp->data.points_data[i].x = ((uint16_t)buf[(i * 8) + 3] << 8) + buf[(i * 8) + 2];
284+
tp->data.points_data[i].y = (((uint16_t)buf[(i * 8) + 5] << 8) + buf[(i * 8) + 4]);
285+
tp->data.points_data[i].strength = (((uint16_t)buf[(i * 8) + 7] << 8) + buf[(i * 8) + 6]);
283286
}
284287

285288
portEXIT_CRITICAL(&tp->data.lock);
@@ -290,23 +293,23 @@ static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp)
290293

291294
static bool esp_lcd_touch_gt911_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num)
292295
{
293-
assert(tp != NULL);
294-
assert(x != NULL);
295-
assert(y != NULL);
296-
assert(point_num != NULL);
297-
assert(max_point_num > 0);
296+
ESP_RETURN_ON_FALSE(tp != NULL, false, TAG, "Touch point handler pointer can't be NULL");
297+
ESP_RETURN_ON_FALSE(x != NULL, false, TAG, "Data array pointer can't be NULL");
298+
ESP_RETURN_ON_FALSE(y != NULL, false, TAG, "Data array pointer can't be NULL");
299+
ESP_RETURN_ON_FALSE(point_num != NULL, false, TAG, "Pointer to number of touch points can't be NULL");
300+
ESP_RETURN_ON_FALSE(max_point_num > 0, false, TAG, "Array size must be at least 1");
298301

299302
portENTER_CRITICAL(&tp->data.lock);
300303

301304
/* Count of points */
302305
*point_num = (tp->data.points > max_point_num ? max_point_num : tp->data.points);
303306

304307
for (size_t i = 0; i < *point_num; i++) {
305-
x[i] = tp->data.coords[i].x;
306-
y[i] = tp->data.coords[i].y;
308+
x[i] = tp->data.points_data[i].x;
309+
y[i] = tp->data.points_data[i].y;
307310

308311
if (strength) {
309-
strength[i] = tp->data.coords[i].strength;
312+
strength[i] = tp->data.points_data[i].strength;
310313
}
311314
}
312315

@@ -315,12 +318,29 @@ static bool esp_lcd_touch_gt911_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, u
315318
return (*point_num > 0);
316319
}
317320

321+
static esp_err_t esp_lcd_touch_gt911_get_track_id(esp_lcd_touch_handle_t tp, uint8_t *track_id, uint8_t max_point_num)
322+
{
323+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
324+
ESP_RETURN_ON_FALSE(track_id != NULL, ESP_ERR_INVALID_ARG, TAG, "Track ID array pointer can't be NULL");
325+
ESP_RETURN_ON_FALSE(max_point_num > 0, ESP_ERR_INVALID_ARG, TAG, "Array size must be at least 1");
326+
327+
portENTER_CRITICAL(&tp->data.lock);
328+
329+
for (int i = 0; i < max_point_num; i++) {
330+
track_id[i] = tp->data.points_data[i].track_id;
331+
}
332+
333+
portEXIT_CRITICAL(&tp->data.lock);
334+
335+
return ESP_OK;
336+
}
337+
318338
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
319339
static esp_err_t esp_lcd_touch_gt911_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state)
320340
{
321341
esp_err_t err = ESP_OK;
322-
assert(tp != NULL);
323-
assert(state != NULL);
342+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
343+
ESP_RETURN_ON_FALSE(state != NULL, ESP_ERR_INVALID_ARG, TAG, "State array pointer can't be NULL");
324344

325345
*state = 0;
326346

@@ -340,7 +360,7 @@ static esp_err_t esp_lcd_touch_gt911_get_button_state(esp_lcd_touch_handle_t tp,
340360

341361
static esp_err_t esp_lcd_touch_gt911_del(esp_lcd_touch_handle_t tp)
342362
{
343-
assert(tp != NULL);
363+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
344364

345365
/* Reset GPIO pin settings */
346366
if (tp->config.int_gpio_num != GPIO_NUM_NC) {
@@ -367,7 +387,7 @@ static esp_err_t esp_lcd_touch_gt911_del(esp_lcd_touch_handle_t tp)
367387
/* Reset controller */
368388
static esp_err_t touch_gt911_reset(esp_lcd_touch_handle_t tp)
369389
{
370-
assert(tp != NULL);
390+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
371391

372392
if (tp->config.rst_gpio_num != GPIO_NUM_NC) {
373393
ESP_RETURN_ON_ERROR(gpio_set_level(tp->config.rst_gpio_num, tp->config.levels.reset), TAG, "GPIO set level error!");
@@ -383,7 +403,7 @@ static esp_err_t touch_gt911_read_cfg(esp_lcd_touch_handle_t tp)
383403
{
384404
uint8_t buf[4];
385405

386-
assert(tp != NULL);
406+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
387407

388408
ESP_RETURN_ON_ERROR(touch_gt911_i2c_read(tp, ESP_LCD_TOUCH_GT911_PRODUCT_ID_REG, (uint8_t *)&buf[0], 3), TAG, "GT911 read error!");
389409
ESP_RETURN_ON_ERROR(touch_gt911_i2c_read(tp, ESP_LCD_TOUCH_GT911_CONFIG_REG, (uint8_t *)&buf[3], 1), TAG, "GT911 read error!");
@@ -396,16 +416,16 @@ static esp_err_t touch_gt911_read_cfg(esp_lcd_touch_handle_t tp)
396416

397417
static esp_err_t touch_gt911_i2c_read(esp_lcd_touch_handle_t tp, uint16_t reg, uint8_t *data, uint8_t len)
398418
{
399-
assert(tp != NULL);
400-
assert(data != NULL);
419+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
420+
ESP_RETURN_ON_FALSE(data != NULL, ESP_ERR_INVALID_ARG, TAG, "Data array pointer can't be NULL");
401421

402422
/* Read data */
403423
return esp_lcd_panel_io_rx_param(tp->io, reg, data, len);
404424
}
405425

406426
static esp_err_t touch_gt911_i2c_write(esp_lcd_touch_handle_t tp, uint16_t reg, uint8_t data)
407427
{
408-
assert(tp != NULL);
428+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler pointer can't be NULL");
409429

410430
// *INDENT-OFF*
411431
/* Write data */
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
version: "1.1.3"
1+
version: "1.2.0"
22
description: ESP LCD Touch GT911 - touch controller GT911
33
url: https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch_gt911
44
dependencies:
55
idf: ">=4.4.2"
66
esp_lcd_touch:
7-
version: "^1.1.0"
7+
version: "^1.2.0"
88
public: true
99
override_path: ../esp_lcd_touch

0 commit comments

Comments
 (0)