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
3132esp_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
4244esp_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
5356esp_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 )
110181esp_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
141212esp_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
156227esp_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
170241esp_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
185256esp_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
199270esp_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
214285esp_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)
225296esp_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
260331esp_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 );
0 commit comments