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
3131esp_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
4243esp_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
5355esp_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 )
110179esp_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
141210esp_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
156225esp_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
170239esp_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
185254esp_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
199268esp_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
214283esp_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)
225294esp_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
260329esp_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 );
0 commit comments