Skip to content

Commit 7da7258

Browse files
committed
feat(multi-touch): Replace get_coordinates with get_data
1 parent 22ebd5e commit 7da7258

File tree

3 files changed

+126
-30
lines changed

3 files changed

+126
-30
lines changed

components/lcd_touch/esp_lcd_touch/esp_lcd_touch.c

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
*/
@@ -30,7 +30,8 @@ static const char *TAG = "TP";
3030

3131
esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp)
3232
{
33-
assert(tp != NULL);
33+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
34+
3435
if (tp->enter_sleep == NULL) {
3536
ESP_LOGE(TAG, "Sleep mode not supported!");
3637
return ESP_FAIL;
@@ -41,7 +42,8 @@ esp_err_t esp_lcd_touch_enter_sleep(esp_lcd_touch_handle_t tp)
4142

4243
esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp)
4344
{
44-
assert(tp != NULL);
45+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
46+
4547
if (tp->exit_sleep == NULL) {
4648
ESP_LOGE(TAG, "Sleep mode not supported!");
4749
return ESP_FAIL;
@@ -52,7 +54,7 @@ esp_err_t esp_lcd_touch_exit_sleep(esp_lcd_touch_handle_t tp)
5254

5355
esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp)
5456
{
55-
assert(tp != NULL);
57+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
5658
assert(tp->read_data != NULL);
5759

5860
return tp->read_data(tp);
@@ -62,10 +64,10 @@ bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint1
6264
{
6365
bool touched = false;
6466

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

7072
touched = tp->get_xy(tp, x, y, strength, point_num, max_point_num);
7173
if (!touched) {
@@ -106,11 +108,78 @@ bool esp_lcd_touch_get_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint1
106108
return touched;
107109
}
108110

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

115184
*state = 0;
116185

@@ -140,8 +209,8 @@ esp_err_t esp_lcd_touch_set_swap_xy(esp_lcd_touch_handle_t tp, bool swap)
140209

141210
esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap)
142211
{
143-
assert(tp != NULL);
144-
assert(swap != NULL);
212+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
213+
ESP_RETURN_ON_FALSE(swap != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
145214

146215
/* Is swap supported by HW? */
147216
if (tp->get_swap_xy) {
@@ -155,7 +224,7 @@ esp_err_t esp_lcd_touch_get_swap_xy(esp_lcd_touch_handle_t tp, bool *swap)
155224

156225
esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror)
157226
{
158-
assert(tp != NULL);
227+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
159228

160229
tp->config.flags.mirror_x = mirror;
161230

@@ -169,8 +238,8 @@ esp_err_t esp_lcd_touch_set_mirror_x(esp_lcd_touch_handle_t tp, bool mirror)
169238

170239
esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror)
171240
{
172-
assert(tp != NULL);
173-
assert(mirror != NULL);
241+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
242+
ESP_RETURN_ON_FALSE(mirror != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
174243

175244
/* Is swap supported by HW? */
176245
if (tp->get_mirror_x) {
@@ -184,7 +253,7 @@ esp_err_t esp_lcd_touch_get_mirror_x(esp_lcd_touch_handle_t tp, bool *mirror)
184253

185254
esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror)
186255
{
187-
assert(tp != NULL);
256+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
188257

189258
tp->config.flags.mirror_y = mirror;
190259

@@ -198,8 +267,8 @@ esp_err_t esp_lcd_touch_set_mirror_y(esp_lcd_touch_handle_t tp, bool mirror)
198267

199268
esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror)
200269
{
201-
assert(tp != NULL);
202-
assert(mirror != NULL);
270+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
271+
ESP_RETURN_ON_FALSE(mirror != NULL, ESP_ERR_INVALID_ARG, TAG, "Pointer to an argument can't be NULL");
203272

204273
/* Is swap supported by HW? */
205274
if (tp->get_mirror_y) {
@@ -213,7 +282,7 @@ esp_err_t esp_lcd_touch_get_mirror_y(esp_lcd_touch_handle_t tp, bool *mirror)
213282

214283
esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp)
215284
{
216-
assert(tp != NULL);
285+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
217286

218287
if (tp->del != NULL) {
219288
return tp->del(tp);
@@ -225,7 +294,7 @@ esp_err_t esp_lcd_touch_del(esp_lcd_touch_handle_t tp)
225294
esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, esp_lcd_touch_interrupt_callback_t callback)
226295
{
227296
esp_err_t ret = ESP_OK;
228-
assert(tp != NULL);
297+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
229298

230299
/* Interrupt pin is not selected */
231300
if (tp->config.int_gpio_num == GPIO_NUM_NC) {
@@ -259,7 +328,7 @@ esp_err_t esp_lcd_touch_register_interrupt_callback(esp_lcd_touch_handle_t tp, e
259328

260329
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)
261330
{
262-
assert(tp != NULL);
331+
ESP_RETURN_ON_FALSE(tp != NULL, ESP_ERR_INVALID_ARG, TAG, "Touch point handler can't be NULL");
263332

264333
tp->config.user_data = user_data;
265334
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: 35 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,19 @@ 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+
* - Returns true, when touched and coordinates were read. Otherwise returns false.
298+
*/
299+
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);
300+
274301
/**
275302
* @brief Read coordinates from touch controller
276303
*
@@ -284,9 +311,9 @@ esp_err_t esp_lcd_touch_read_data(esp_lcd_touch_handle_t tp);
284311
* @return
285312
* - Returns true, when touched and coordinates readed. Otherwise returns false.
286313
*/
314+
[[deprecated("This API will be removed in version 2.0.0. Use esp_lcd_touch_get_data instead!")]]
287315
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);
288316

289-
290317
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
291318
/**
292319
* @brief Get button state

0 commit comments

Comments
 (0)