Skip to content

Commit ab346fc

Browse files
committed
feat(multi-touch): Add get_data API to esp_lcd_touch
1 parent e3ed08e commit ab346fc

File tree

3 files changed

+130
-30
lines changed

3 files changed

+130
-30
lines changed

components/lcd_touch/esp_lcd_touch/esp_lcd_touch.c

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#include <stdlib.h>
8+
#include <string.h>
89
#include "freertos/FreeRTOS.h"
910
#include "freertos/task.h"
1011
#include "driver/gpio.h"
@@ -30,7 +31,8 @@ static const char *TAG = "TP";
3031

3132
esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp)
3233
{
33-
assert(tp != NULL);
34+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
35+
3436
if (tp->enter_sleep == NULL) {
3537
ESP_LOGE(TAG, "Sleep mode not supported!");
3638
return ESP_FAIL;
@@ -41,7 +43,8 @@ esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp)
4143

4244
esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp)
4345
{
44-
assert(tp != NULL);
46+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
47+
4548
if (tp->exit_sleep == NULL) {
4649
ESP_LOGE(TAG, "Sleep mode not supported!");
4750
return ESP_FAIL;
@@ -52,7 +55,7 @@ esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp)
5255

5356
esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp)
5457
{
55-
assert(tp != NULL);
58+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
5659
assert(tp->read_data != NULL);
5760

5861
return tp->read_data(tp);
@@ -62,10 +65,10 @@ bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint1
6265
{
6366
bool touched = false;
6467

65-
assert(tp != NULL);
66-
assert(x != NULL);
67-
assert(y != NULL);
68-
assert(tp->get_xy != NULL);
68+
ESP_RETURN_ON_FALSE(tp != NULL, false, TAG, "Touch point handler can't be NULL");
69+
ESP_RETURN_ON_FALSE(x != NULL, false, TAG, "X coordinates data array can't be NULL");
70+
ESP_RETURN_ON_FALSE(y != NULL, false, TAG, "Y coordinates data array can't be NULL");
71+
ESP_RETURN_ON_FALSE(tp->get_xy != NULL, false, TAG, "Touch driver must be initialized");
6972

7073
touched = tp->get_xy(tp, x, y, strength, point_num, max_point_num);
7174
if (!touched) {
@@ -106,11 +109,79 @@ bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint1
106109
return touched;
107110
}
108111

112+
esp_err_t esp_lcd_touch_get_data(esp_lcd_touch_handle_t tp, esp_lcd_touch_point_data_t *data, uint8_t *point_cnt, uint8_t max_point_cnt)
113+
{
114+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
115+
ESP_RETURN_ON_FALSE(data != NULL, ESP_ERR_INVALID_ARG, TAG, "Data array can't be NULL");
116+
ESP_RETURN_ON_FALSE(tp->get_xy != NULL, ESP_ERR_INVALID_STATE, TAG, "Touch driver must be initialized");
117+
118+
uint16_t x[max_point_cnt];
119+
uint16_t y[max_point_cnt];
120+
uint16_t strength[max_point_cnt];
121+
uint8_t track_id[max_point_cnt];
122+
123+
bool touched = tp->get_xy(tp, x, y, strength, point_cnt, max_point_cnt);
124+
125+
if (!touched) {
126+
return ESP_OK;
127+
}
128+
129+
/* Process coordinates by user */
130+
if (tp->config.process_coordinates != NULL) {
131+
tp->config.process_coordinates(tp, x, y, strength, point_cnt, max_point_cnt);
132+
}
133+
134+
/* Software coordinates adjustment needed */
135+
bool sw_adj_needed = ((tp->config.flags.mirror_x && (tp->set_mirror_x == NULL)) ||
136+
(tp->config.flags.mirror_y && (tp->set_mirror_y == NULL)) ||
137+
(tp->config.flags.swap_xy && (tp->set_swap_xy == NULL)));
138+
139+
/* Adjust all coordinates */
140+
for (int i = 0; (sw_adj_needed && i < *point_cnt); i++) {
141+
142+
/* Mirror X coordinates (if not supported by HW) */
143+
if (tp->config.flags.mirror_x && tp->set_mirror_x == NULL) {
144+
x[i] = tp->config.x_max - x[i];
145+
}
146+
147+
/* Mirror Y coordinates (if not supported by HW) */
148+
if (tp->config.flags.mirror_y && tp->set_mirror_y == NULL) {
149+
y[i] = tp->config.y_max - y[i];
150+
}
151+
152+
/* Swap X and Y coordinates (if not supported by HW) */
153+
if (tp->config.flags.swap_xy && tp->set_swap_xy == NULL) {
154+
uint16_t tmp = x[i];
155+
x[i] = y[i];
156+
y[i] = tmp;
157+
}
158+
}
159+
160+
/* Process read track IDs */
161+
if (tp->get_track_id != NULL && touched) {
162+
tp->get_track_id(tp, track_id, *point_cnt);
163+
}
164+
165+
/* Initialize the struct array since some features might not be available */
166+
memset(data, 0, sizeof(esp_lcd_touch_point_data_t) * max_point_cnt);
167+
168+
for (int i = 0; i < *point_cnt; i++) {
169+
data[i].x = x[i];
170+
data[i].y = y[i];
171+
data[i].strength = strength[i];
172+
if (tp->get_track_id != NULL) {
173+
data[i].track_id = track_id[i];
174+
}
175+
}
176+
177+
return ESP_OK;
178+
}
179+
109180
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
110181
esp_err_t esp_lcd_touch_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state)
111182
{
112-
assert(tp != NULL);
113-
assert(state != NULL);
183+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
184+
ESP_RETURN_ON_FALSE(state != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
114185

115186
*state = 0;
116187

@@ -140,8 +211,8 @@ esp_err_t esp_lcd_touch_set_swap_xy(esp_lcd_touch_handle_t tp, bool swap)
140211

141212
esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap)
142213
{
143-
assert(tp != NULL);
144-
assert(swap != NULL);
214+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
215+
ESP_RETURN_ON_FALSE(swap != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
145216

146217
/* Is swap supported by HW? */
147218
if (tp->get_swap_xy) {
@@ -155,7 +226,7 @@ esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap)
155226

156227
esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror)
157228
{
158-
assert(tp != NULL);
229+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
159230

160231
tp->config.flags.mirror_x = mirror;
161232

@@ -169,8 +240,8 @@ esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror)
169240

170241
esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror)
171242
{
172-
assert(tp != NULL);
173-
assert(mirror != NULL);
243+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
244+
ESP_RETURN_ON_FALSE(mirror != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
174245

175246
/* Is swap supported by HW? */
176247
if (tp->get_mirror_x) {
@@ -184,7 +255,7 @@ esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror)
184255

185256
esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror)
186257
{
187-
assert(tp != NULL);
258+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
188259

189260
tp->config.flags.mirror_y = mirror;
190261

@@ -198,8 +269,8 @@ esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror)
198269

199270
esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror)
200271
{
201-
assert(tp != NULL);
202-
assert(mirror != NULL);
272+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
273+
ESP_RETURN_ON_FALSE(mirror != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
203274

204275
/* Is swap supported by HW? */
205276
if (tp->get_mirror_y) {
@@ -213,7 +284,7 @@ esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror)
213284

214285
esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp)
215286
{
216-
assert(tp != NULL);
287+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
217288

218289
if (tp->del != NULL) {
219290
return tp->del(tp);
@@ -225,7 +296,7 @@ esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp)
225296
esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback)
226297
{
227298
esp_err_t ret = ESP_OK;
228-
assert(tp != NULL);
299+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
229300

230301
/* Interrupt pin is not selected */
231302
if (tp->config.int_gpio_num == GPIO_NUM_NC) {
@@ -259,7 +330,7 @@ esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, e
259330

260331
esp_err_t esp_lcd_touch_register_interrupt_callback_with_data(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback, void *user_data)
261332
{
262-
assert(tp != NULL);
333+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
263334

264335
tp->config.user_data = user_data;
265336
return esp_lcd_touch_register_interrupt_callback(tp, callback);

components/lcd_touch/esp_lcd_touch/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.1.2"
1+
version: "1.2.0"
22
description: ESP LCD Touch - main component for using touch screen controllers
33
url: https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch
44
dependencies:

components/lcd_touch/esp_lcd_touch/include/esp_lcd_touch.h

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -69,13 +69,15 @@ typedef struct {
6969
} esp_lcd_touch_config_t;
7070

7171
typedef struct {
72-
uint8_t points; /*!< Count of touch points saved */
72+
uint8_t track_id;
73+
uint16_t x; /*!< X coordinate */
74+
uint16_t y; /*!< Y coordinate */
75+
uint16_t strength; /*!< Strength */
76+
} esp_lcd_touch_point_data_t;
7377

74-
struct {
75-
uint16_t x; /*!< X coordinate */
76-
uint16_t y; /*!< Y coordinate */
77-
uint16_t strength; /*!< Strength */
78-
} coords[CONFIG_ESP_LCD_TOUCH_MAX_POINTS];
78+
typedef struct {
79+
uint8_t points; /*!< Count of touch points saved */
80+
esp_lcd_touch_point_data_t points_data[CONFIG_ESP_LCD_TOUCH_MAX_POINTS];
7981

8082
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
8183
uint8_t buttons; /*!< Count of buttons states saved */
@@ -145,6 +147,18 @@ struct esp_lcd_touch_s {
145147
*/
146148
bool (*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);
147149

150+
/**
151+
* @brief Get track ids of touch points
152+
*
153+
* @param tp: Touch handler
154+
* @param track_id: Array of track ids
155+
* @param point_num: Count of touched points to return
156+
*
157+
* @return
158+
* - ESP_OK on success, otherwise returns ESP_ERR_xxx
159+
*/
160+
esp_err_t (*get_track_id)(esp_lcd_touch_handle_t tp, uint8_t *track_id, uint8_t point_num);
161+
148162

149163
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
150164
/**
@@ -271,6 +285,21 @@ struct esp_lcd_touch_s {
271285
*/
272286
esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp);
273287

288+
/**
289+
* @brief Get each touch point data from touch controller
290+
*
291+
* @param tp: Touch handler
292+
* @param data: Array of data structures to be filled out
293+
* @param point_cnt: Count of points touched (equals with count of items in x and y array)
294+
* @param max_point_cnt: Maximum count of touched points to return (equals with max size of x and y array)
295+
*
296+
* @return
297+
* - ESP_OK on success
298+
* - ESP_ERR_INVALID_ARG if parameter is invalid
299+
* - ESP_ERR_INVALID_STATE if parameter is uninitialized
300+
*/
301+
esp_err_t esp_lcd_touch_get_data(esp_lcd_touch_handle_t tp, esp_lcd_touch_point_data_t *data, uint8_t *point_cnt, uint8_t max_point_cnt);
302+
274303
/**
275304
* @brief Read coordinates from touch controller
276305
*
@@ -284,9 +313,9 @@ esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp);
284313
* @return
285314
* - Returns true, when touched and coordinates readed. Otherwise returns false.
286315
*/
316+
[[deprecated("This API will be removed in version 2.0.0. Use esp_lcd_touch_get_data instead!")]]
287317
bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num);
288318

289-
290319
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
291320
/**
292321
* @brief Get button state

0 commit comments

Comments
 (0)