1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Touch Component    (Touch)                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 #include "gx_api.h"
24 #include "gx_system.h"
25 #include "gx_widget.h"
26 #include "gx_multi_line_text_view.h"
27 #include "gx_icon.h"
28 
29 #if !defined(GX_TARGET_WIN32)
30 
31 #if defined(GX_DISABLE_THREADX_BINDING)
32 #define GX_TOUCH_TASK_SLEEP  GX_GENERIC_TIME_DELAY
33 #else
34 #define GX_TOUCH_TASK_SLEEP tx_thread_sleep
35 #endif
36 
37 VOID _gx_touch_driver_generic_resistive_setup(GX_RESISTIVE_TOUCH *touch, GX_RESISTIVE_TOUCH_INFO *info);
38 VOID _gx_touch_driver_generic_resistive_update(GX_RESISTIVE_TOUCH *touch);
39 VOID _gx_touch_driver_generic_resistive_calibrate(GX_RESISTIVE_TOUCH *touch);
40 
41 /**************************************************************************/
42 /*                                                                        */
43 /*  FUNCTION                                               RELEASE        */
44 /*                                                                        */
45 /*    _gx_touch_driver_generic_resistive_pen_down_event_send              */
46 /*                                                        PORTABLE C      */
47 /*                                                           6.1          */
48 /*  AUTHOR                                                                */
49 /*                                                                        */
50 /*    Kenneth Maxwell, Microsoft Corporation                              */
51 /*                                                                        */
52 /*  DESCRIPTION                                                           */
53 /*                                                                        */
54 /*    This internal function sends GX_EVENT_PEN_DOWN into GUIX            */
55 /*      event queue.                                                      */
56 /*                                                                        */
57 /*  INPUT                                                                 */
58 /*                                                                        */
59 /*    touch                                 Pointer to touch control      */
60 /*                                            structure.                  */
61 /*                                                                        */
62 /*  OUTPUT                                                                */
63 /*                                                                        */
64 /*    None                                                                */
65 /*                                                                        */
66 /*  CALLS                                                                 */
67 /*                                                                        */
68 /*    _gx_system_event_send                                               */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Internal Logic                                                      */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
79 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_gx_touch_driver_generic_resistive_pen_down_event_send(GX_RESISTIVE_TOUCH * touch)83 static VOID _gx_touch_driver_generic_resistive_pen_down_event_send(GX_RESISTIVE_TOUCH *touch)
84 {
85     GX_EVENT event;
86     event.gx_event_type = GX_EVENT_PEN_DOWN;
87     event.gx_event_payload.gx_event_pointdata = touch -> gx_resistive_touch_current_touch_coord;
88     event.gx_event_sender = 0;
89     event.gx_event_target = 0;
90     event.gx_event_display_handle = 0;
91     _gx_system_event_send(&event);
92     touch -> gx_resistive_touch_last_touch_coord = touch -> gx_resistive_touch_current_touch_coord;
93     touch -> gx_resistive_touch_last_touch_state = GX_TOUCH_STATE_TOUCHED;
94 }
95 
96 /**************************************************************************/
97 /*                                                                        */
98 /*  FUNCTION                                               RELEASE        */
99 /*                                                                        */
100 /*    _gx_touch_driver_generic_resistive_pen_drag_event_send              */
101 /*                                                        PORTABLE C      */
102 /*                                                           6.1.6        */
103 /*  AUTHOR                                                                */
104 /*                                                                        */
105 /*    Kenneth Maxwell, Microsoft Corporation                              */
106 /*                                                                        */
107 /*  DESCRIPTION                                                           */
108 /*                                                                        */
109 /*    This internal function sends GX_EVENT_PEN_DRAG into GUIX            */
110 /*      event queue.                                                      */
111 /*                                                                        */
112 /*  INPUT                                                                 */
113 /*                                                                        */
114 /*    touch                                 Pointer to touch control      */
115 /*                                            structure.                  */
116 /*                                                                        */
117 /*  OUTPUT                                                                */
118 /*                                                                        */
119 /*    None                                                                */
120 /*                                                                        */
121 /*  CALLS                                                                 */
122 /*                                                                        */
123 /*    _gx_system_event_send                                               */
124 /*                                                                        */
125 /*  CALLED BY                                                             */
126 /*                                                                        */
127 /*    Internal Logic                                                      */
128 /*                                                                        */
129 /*  RELEASE HISTORY                                                       */
130 /*                                                                        */
131 /*    DATE              NAME                      DESCRIPTION             */
132 /*                                                                        */
133 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
134 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
135 /*                                            resulting in version 6.1    */
136 /*  04-02-2021     Kenneth Maxwell          Modified comment(s),          */
137 /*                                            replace usage of abs(),     */
138 /*                                            resulting in version 6.1.6  */
139 /*                                                                        */
140 /**************************************************************************/
141 
_gx_touch_driver_generic_resistive_pen_drag_event_send(GX_RESISTIVE_TOUCH * touch)142 static VOID _gx_touch_driver_generic_resistive_pen_drag_event_send(GX_RESISTIVE_TOUCH *touch)
143 {
144     GX_EVENT event;
145     INT x_delta = GX_ABS(touch -> gx_resistive_touch_current_touch_coord.gx_point_x - touch -> gx_resistive_touch_last_touch_coord.gx_point_x);
146     INT y_delta = GX_ABS(touch -> gx_resistive_touch_current_touch_coord.gx_point_y - touch -> gx_resistive_touch_last_touch_coord.gx_point_y);
147 
148     if (x_delta > touch -> gx_resistive_touch_min_drag_delta ||
149         y_delta > touch -> gx_resistive_touch_min_drag_delta)
150     {
151         event.gx_event_type = GX_EVENT_PEN_DRAG;
152         event.gx_event_payload.gx_event_pointdata = touch -> gx_resistive_touch_current_touch_coord;
153         event.gx_event_sender = 0;
154         event.gx_event_target = 0;
155         event.gx_event_display_handle = 0;
156         touch -> gx_resistive_touch_last_touch_coord = touch -> gx_resistive_touch_current_touch_coord;
157         _gx_system_event_fold(&event);
158     }
159 }
160 
161 /**************************************************************************/
162 /*                                                                        */
163 /*  FUNCTION                                               RELEASE        */
164 /*                                                                        */
165 /*    _gx_touch_driver_generic_resistive_pen_up_event_send                */
166 /*                                                        PORTABLE C      */
167 /*                                                           6.1          */
168 /*  AUTHOR                                                                */
169 /*                                                                        */
170 /*    Kenneth Maxwell, Microsoft Corporation                              */
171 /*                                                                        */
172 /*  DESCRIPTION                                                           */
173 /*                                                                        */
174 /*    This internal function sends GX_EVENT_PEN_UP into GUIX              */
175 /*      event queue.                                                      */
176 /*                                                                        */
177 /*  INPUT                                                                 */
178 /*                                                                        */
179 /*    touch                                 Pointer to touch control      */
180 /*                                            structure.                  */
181 /*                                                                        */
182 /*  OUTPUT                                                                */
183 /*                                                                        */
184 /*    None                                                                */
185 /*                                                                        */
186 /*  CALLS                                                                 */
187 /*                                                                        */
188 /*    _gx_system_event_send                                               */
189 /*                                                                        */
190 /*  CALLED BY                                                             */
191 /*                                                                        */
192 /*    Internal Logic                                                      */
193 /*                                                                        */
194 /*  RELEASE HISTORY                                                       */
195 /*                                                                        */
196 /*    DATE              NAME                      DESCRIPTION             */
197 /*                                                                        */
198 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
199 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
200 /*                                            resulting in version 6.1    */
201 /*                                                                        */
202 /**************************************************************************/
_gx_touch_driver_generic_resistive_pen_up_event_send(GX_RESISTIVE_TOUCH * touch)203 static VOID _gx_touch_driver_generic_resistive_pen_up_event_send(GX_RESISTIVE_TOUCH *touch)
204 {
205     GX_EVENT event;
206     event.gx_event_type = GX_EVENT_PEN_UP;
207     event.gx_event_payload.gx_event_pointdata = touch->gx_resistive_touch_current_touch_coord;
208     event.gx_event_sender = 0;
209     event.gx_event_target = 0;
210     event.gx_event_display_handle = 0;
211     touch -> gx_resistive_touch_last_touch_coord = touch -> gx_resistive_touch_current_touch_coord;
212     _gx_system_event_send(&event);
213     touch -> gx_resistive_touch_last_touch_state = GX_TOUCH_STATE_RELEASED;
214 }
215 
216 
217 /**************************************************************************/
218 /*                                                                        */
219 /*  FUNCTION                                               RELEASE        */
220 /*                                                                        */
221 /*    _gx_touch_driver_generic_resistive_coord_calculate                  */
222 /*                                                        PORTABLE C      */
223 /*                                                           6.1          */
224 /*  AUTHOR                                                                */
225 /*                                                                        */
226 /*    Kenneth Maxwell, Microsoft Corporation                              */
227 /*                                                                        */
228 /*  DESCRIPTION                                                           */
229 /*                                                                        */
230 /*    Internal function to convert x,y adc position into x,y pixel        */
231 /*      positio.                                                          */
232 /*                                                                        */
233 /*  INPUT                                                                 */
234 /*                                                                        */
235 /*    touch                                 Pointer to touch control      */
236 /*                                            structure.                  */
237 /*    raw_coord                             ADC coordinate measurement    */
238 /*                                                                        */
239 /*  OUTPUT                                                                */
240 /*                                                                        */
241 /*    Updates touch control structure                                     */
242 /*                                                                        */
243 /*  CALLS                                                                 */
244 /*                                                                        */
245 /*    None                                                                */
246 /*                                                                        */
247 /*  CALLED BY                                                             */
248 /*                                                                        */
249 /*    Internal Logic                                                      */
250 /*                                                                        */
251 /*  RELEASE HISTORY                                                       */
252 /*                                                                        */
253 /*    DATE              NAME                      DESCRIPTION             */
254 /*                                                                        */
255 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
256 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
257 /*                                            resulting in version 6.1    */
258 /*                                                                        */
259 /**************************************************************************/
_gx_touch_driver_generic_resistive_coord_calculate(GX_RESISTIVE_TOUCH * touch,GX_POINT * raw_coord)260 static UINT _gx_touch_driver_generic_resistive_coord_calculate(GX_RESISTIVE_TOUCH *touch,
261                                                                GX_POINT *raw_coord)
262 {
263     UINT ret_val = (UINT) GX_SUCCESS;
264     TOUCH_CALIBRATION_MATRIX *matrix_ptr = &touch->gx_resistive_touch_calibration_matrix;
265 
266     if (matrix_ptr->Divider != 0)
267     {
268         touch -> gx_resistive_touch_current_touch_coord.gx_point_x =
269                         (GX_VALUE) (((matrix_ptr -> An * raw_coord -> gx_point_x) +
270                           (matrix_ptr -> Bn * raw_coord -> gx_point_y) +
271                            matrix_ptr -> Cn) / matrix_ptr -> Divider);
272 
273         touch -> gx_resistive_touch_current_touch_coord.gx_point_y =
274                         (GX_VALUE) (((matrix_ptr -> Dn * raw_coord -> gx_point_x) +
275                           (matrix_ptr -> En * raw_coord -> gx_point_y) +
276                            matrix_ptr -> Fn) / matrix_ptr -> Divider);
277     }
278     else
279     {
280         ret_val = (UINT) GX_FAILURE;
281     }
282 
283     return(ret_val) ;
284 }
285 
286 
287 /**************************************************************************/
288 /*                                                                        */
289 /*  FUNCTION                                               RELEASE        */
290 /*                                                                        */
291 /*    _gx_touch_driver_generic_resistive_setup            PORTABLE C      */
292 /*                                                           6.1          */
293 /*  AUTHOR                                                                */
294 /*                                                                        */
295 /*    Kenneth Maxwell, Microsoft Corporation                              */
296 /*                                                                        */
297 /*  DESCRIPTION                                                           */
298 /*                                                                        */
299 /*    Assign the calibration matrix used to do raw coordinates            */
300 /*    translation to pixel coordinates.                                   */
301 /*                                                                        */
302 /*  INPUT                                                                 */
303 /*                                                                        */
304 /*    touch                                 Pointer to touch control      */
305 /*                                            structure.                  */
306 /*    info                                  touch configuration           */
307 /*                                                                        */
308 /*  OUTPUT                                                                */
309 /*                                                                        */
310 /*    Updates touch control structure                                     */
311 /*                                                                        */
312 /*  CALLS                                                                 */
313 /*                                                                        */
314 /*    memset                                                              */
315 /*    memcpy                                                              */
316 /*                                                                        */
317 /*  CALLED BY                                                             */
318 /*                                                                        */
319 /*    Application                                                         */
320 /*                                                                        */
321 /*  RELEASE HISTORY                                                       */
322 /*                                                                        */
323 /*    DATE              NAME                      DESCRIPTION             */
324 /*                                                                        */
325 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
326 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
327 /*                                            resulting in version 6.1    */
328 /*                                                                        */
329 /**************************************************************************/
_gx_touch_driver_generic_resistive_setup(GX_RESISTIVE_TOUCH * touch,GX_RESISTIVE_TOUCH_INFO * info)330 VOID _gx_touch_driver_generic_resistive_setup(GX_RESISTIVE_TOUCH *touch, GX_RESISTIVE_TOUCH_INFO *info)
331 {
332     memset(touch, 0, sizeof(GX_RESISTIVE_TOUCH));
333     memcpy(&touch->gx_resistive_touch_info, info, sizeof(GX_RESISTIVE_TOUCH_INFO)); /* Use case of memcpy is verified. */
334 }
335 
336 /**************************************************************************/
337 /*                                                                        */
338 /*  FUNCTION                                               RELEASE        */
339 /*                                                                        */
340 /*    _gx_touch_driver_generic_resistive_calibration_matrix_set           */
341 /*                                                        PORTABLE C      */
342 /*                                                           6.1          */
343 /*  AUTHOR                                                                */
344 /*                                                                        */
345 /*    Kenneth Maxwell, Microsoft Corporation                              */
346 /*                                                                        */
347 /*  DESCRIPTION                                                           */
348 /*                                                                        */
349 /*    Assign the calibration matrix used to convert raw touch coordinates */
350 /*    to x,y pixel position.                                              */
351 /*                                                                        */
352 /*  INPUT                                                                 */
353 /*                                                                        */
354 /*    touch                                 Pointer to touch control      */
355 /*                                            structure.                  */
356 /*    raw_coord                             ADC coordinate measurement    */
357 /*                                                                        */
358 /*  OUTPUT                                                                */
359 /*                                                                        */
360 /*    Updates touch control structure                                     */
361 /*                                                                        */
362 /*  CALLS                                                                 */
363 /*                                                                        */
364 /*    None                                                                */
365 /*                                                                        */
366 /*  CALLED BY                                                             */
367 /*                                                                        */
368 /*    Internal Logic                                                      */
369 /*                                                                        */
370 /*  RELEASE HISTORY                                                       */
371 /*                                                                        */
372 /*    DATE              NAME                      DESCRIPTION             */
373 /*                                                                        */
374 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
375 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
376 /*                                            resulting in version 6.1    */
377 /*                                                                        */
378 /**************************************************************************/
_gx_touch_driver_generic_resistive_calibration_matrix_set(GX_RESISTIVE_TOUCH * touch,GX_POINT * display_points,GX_POINT * touch_points)379 static UINT _gx_touch_driver_generic_resistive_calibration_matrix_set(GX_RESISTIVE_TOUCH *touch,
380                                                                      GX_POINT *display_points,
381                                                                      GX_POINT *touch_points)
382 {
383     UINT ret_val = (UINT) GX_SUCCESS ;
384     TOUCH_CALIBRATION_MATRIX *matrix_ptr = &touch->gx_resistive_touch_calibration_matrix;
385 
386     matrix_ptr -> Divider = ((touch_points[0].gx_point_x - touch_points[2].gx_point_x) * (touch_points[1].gx_point_y - touch_points[2].gx_point_y)) -
387         ((touch_points[1].gx_point_x - touch_points[2].gx_point_x) * (touch_points[0].gx_point_y - touch_points[2].gx_point_y)) ;
388 
389     if (matrix_ptr->Divider == 0)
390     {
391         ret_val = (UINT) GX_FAILURE;
392     }
393     else
394     {
395         matrix_ptr -> An = ((display_points[0].gx_point_x - display_points[2].gx_point_x) * (touch_points[1].gx_point_y - touch_points[2].gx_point_y)) -
396                         ((display_points[1].gx_point_x - display_points[2].gx_point_x) * (touch_points[0].gx_point_y - touch_points[2].gx_point_y)) ;
397 
398         matrix_ptr -> Bn = ((touch_points[0].gx_point_x - touch_points[2].gx_point_x) * (display_points[1].gx_point_x - display_points[2].gx_point_x)) -
399                         ((display_points[0].gx_point_x - display_points[2].gx_point_x) * (touch_points[1].gx_point_x - touch_points[2].gx_point_x)) ;
400 
401         matrix_ptr -> Cn = (touch_points[2].gx_point_x * display_points[1].gx_point_x - touch_points[1].gx_point_x * display_points[2].gx_point_x) * touch_points[0].gx_point_y +
402                         (touch_points[0].gx_point_x * display_points[2].gx_point_x - touch_points[2].gx_point_x * display_points[0].gx_point_x) * touch_points[1].gx_point_y +
403                         (touch_points[1].gx_point_x * display_points[0].gx_point_x - touch_points[0].gx_point_x * display_points[1].gx_point_x) * touch_points[2].gx_point_y ;
404 
405         matrix_ptr -> Dn = ((display_points[0].gx_point_y - display_points[2].gx_point_y) * (touch_points[1].gx_point_y - touch_points[2].gx_point_y)) -
406                         ((display_points[1].gx_point_y - display_points[2].gx_point_y) * (touch_points[0].gx_point_y - touch_points[2].gx_point_y)) ;
407 
408         matrix_ptr -> En = ((touch_points[0].gx_point_x - touch_points[2].gx_point_x) * (display_points[1].gx_point_y - display_points[2].gx_point_y)) -
409                         ((display_points[0].gx_point_y - display_points[2].gx_point_y) * (touch_points[1].gx_point_x - touch_points[2].gx_point_x)) ;
410 
411         matrix_ptr -> Fn = (touch_points[2].gx_point_x * display_points[1].gx_point_y - touch_points[1].gx_point_x * display_points[2].gx_point_y) * touch_points[0].gx_point_y +
412                         (touch_points[0].gx_point_x * display_points[2].gx_point_y - touch_points[2].gx_point_x * display_points[0].gx_point_y) * touch_points[1].gx_point_y +
413                         (touch_points[1].gx_point_x * display_points[0].gx_point_y - touch_points[0].gx_point_x * display_points[1].gx_point_y) * touch_points[2].gx_point_y ;
414     }
415 
416     return(ret_val) ;
417 }
418 
419 /**************************************************************************/
420 /*                                                                        */
421 /*  FUNCTION                                               RELEASE        */
422 /*                                                                        */
423 /*    _gx_touch_driver_generic_resistive_raw_read                         */
424 /*                                                        PORTABLE C      */
425 /*                                                           6.1.6        */
426 /*  AUTHOR                                                                */
427 /*                                                                        */
428 /*    Kenneth Maxwell, Microsoft Corporation                              */
429 /*                                                                        */
430 /*  DESCRIPTION                                                           */
431 /*                                                                        */
432 /*    Called to read raw touch coordinate and perform debounce logic      */
433 /*                                                                        */
434 /*  INPUT                                                                 */
435 /*                                                                        */
436 /*    touch                                 Pointer to touch control      */
437 /*                                            structure.                  */
438 /*    ret_val                               Pointer to coord storage      */
439 /*                                                                        */
440 /*  OUTPUT                                                                */
441 /*                                                                        */
442 /*    Updates touch control structure                                     */
443 /*                                                                        */
444 /*  CALLS                                                                 */
445 /*                                                                        */
446 /*    Indirect call to user-supplied hardware read function               */
447 /*                                                                        */
448 /*  CALLED BY                                                             */
449 /*                                                                        */
450 /*    Internal Logic                                                      */
451 /*                                                                        */
452 /*  RELEASE HISTORY                                                       */
453 /*                                                                        */
454 /*    DATE              NAME                      DESCRIPTION             */
455 /*                                                                        */
456 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
457 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
458 /*                                            resulting in version 6.1    */
459 /*  04-02-2021     Kenneth Maxwell          Modified comment(s),          */
460 /*                                            replace usage of abs(),     */
461 /*                                            resulting in version 6.1.6  */
462 /*                                                                        */
463 /**************************************************************************/
_gx_touch_driver_generic_resistive_raw_read(GX_RESISTIVE_TOUCH * touch,GX_POINT * ret_val)464 static GX_BOOL _gx_touch_driver_generic_resistive_raw_read(GX_RESISTIVE_TOUCH *touch, GX_POINT *ret_val)
465 {
466     GX_POINT *samples;
467     INT loop;
468     INT delta_x;
469     INT delta_y;
470     INT retries = 0;
471     INT stability_limit;
472     INT sample_size;
473     INT max_retries;
474     GX_BOOL stable_sample = GX_FALSE;
475     USHORT (*read_function)(GX_VALUE axis);
476 
477     samples = touch -> gx_resistive_touch_sample_ram;
478     read_function = touch -> gx_resistive_touch_sample_read;
479     stability_limit = touch -> gx_resistive_touch_stability_limit;
480     sample_size = touch -> gx_resistive_touch_sample_size;
481     max_retries = touch -> gx_resistive_touch_max_retries;
482 
483     while(!stable_sample)
484     {
485         retries += 1;
486 
487         if (retries < max_retries)
488         {
489             stable_sample = GX_TRUE;
490             loop = 0;
491             retries += 1;
492 
493             while(loop < sample_size)
494             {
495                 samples[loop].gx_point_x = (GX_VALUE) read_function(GX_TOUCH_X_AXIS);
496                 samples[loop].gx_point_y = (GX_VALUE) read_function(GX_TOUCH_Y_AXIS);
497 
498                 if (loop > 0)
499                 {
500                     delta_x = GX_ABS(samples[loop].gx_point_x - samples[loop - 1].gx_point_x);
501                     if (delta_x > stability_limit)
502                     {
503                         stable_sample = GX_FALSE;
504                         break;
505                     }
506                     delta_y = GX_ABS(samples[loop].gx_point_y - samples[loop - 1].gx_point_y);
507                     if (delta_y > stability_limit)
508                     {
509                         stable_sample = GX_FALSE;
510                         break;
511                     }
512                 }
513                 loop++;
514             }
515         }
516         else
517         {
518             break;
519         }
520     }
521 
522     if (stable_sample)
523     {
524         /* Average the samples: */
525 
526         delta_x = delta_y = 0;
527 
528         for (loop = 0; loop < sample_size; loop++)
529         {
530             delta_x += samples[loop].gx_point_x;
531             delta_y += samples[loop].gx_point_y;
532         }
533         ret_val->gx_point_x = (GX_VALUE) (delta_x / sample_size);
534         ret_val->gx_point_y = (GX_VALUE) (delta_y / sample_size);
535     }
536     return stable_sample;
537 }
538 
539 
540 /**************************************************************************/
541 /*                                                                        */
542 /*  FUNCTION                                               RELEASE        */
543 /*                                                                        */
544 /*    _gx_touch_driver_generic_resistive_sample_read                      */
545 /*                                                        PORTABLE C      */
546 /*                                                           6.1          */
547 /*  AUTHOR                                                                */
548 /*                                                                        */
549 /*    Kenneth Maxwell, Microsoft Corporation                              */
550 /*                                                                        */
551 /*  DESCRIPTION                                                           */
552 /*                                                                        */
553 /*    Read one stable touch sample                                        */
554 /*                                                                        */
555 /*  INPUT                                                                 */
556 /*                                                                        */
557 /*    touch                                 Pointer to touch control      */
558 /*                                            structure.                  */
559 /*    ret_val                               Address to write touch sample */
560 /*                                                                        */
561 /*  OUTPUT                                                                */
562 /*                                                                        */
563 /*    Updates touch control structure                                     */
564 /*                                                                        */
565 /*  CALLS                                                                 */
566 /*                                                                        */
567 /*    _gx_touch_driver_generic_raw_read                                   */
568 /*                                                                        */
569 /*  CALLED BY                                                             */
570 /*                                                                        */
571 /*    Internal Logic                                                      */
572 /*                                                                        */
573 /*  RELEASE HISTORY                                                       */
574 /*                                                                        */
575 /*    DATE              NAME                      DESCRIPTION             */
576 /*                                                                        */
577 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
578 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
579 /*                                            resulting in version 6.1    */
580 /*                                                                        */
581 /**************************************************************************/
_gx_touch_driver_generic_resistive_sample_read(GX_RESISTIVE_TOUCH * touch,GX_POINT * ret_val)582 static GX_BOOL _gx_touch_driver_generic_resistive_sample_read(GX_RESISTIVE_TOUCH *touch, GX_POINT *ret_val)
583 {
584     GX_POINT raw_val;
585     if (_gx_touch_driver_generic_resistive_raw_read(touch, &raw_val))
586     {
587         ret_val -> gx_point_x = (GX_VALUE) ((raw_val.gx_point_x + 2 ) / 4);
588         ret_val -> gx_point_y = (GX_VALUE) ((raw_val.gx_point_y + 2 ) / 4);
589         return GX_TRUE;
590     }
591     return GX_FALSE;
592 }
593 
594 /**************************************************************************/
595 /*                                                                        */
596 /*  FUNCTION                                               RELEASE        */
597 /*                                                                        */
598 /*    _gx_touch_driver_generic_resistive_coordinate_update                */
599 /*                                                        PORTABLE C      */
600 /*                                                           6.1          */
601 /*  AUTHOR                                                                */
602 /*                                                                        */
603 /*    Kenneth Maxwell, Microsoft Corporation                              */
604 /*                                                                        */
605 /*  DESCRIPTION                                                           */
606 /*                                                                        */
607 /*    Read raw sample, convert to x,y coordinates                         */
608 /*                                                                        */
609 /*  INPUT                                                                 */
610 /*                                                                        */
611 /*    touch                                 Pointer to touch control      */
612 /*                                            structure.                  */
613 /*                                                                        */
614 /*  OUTPUT                                                                */
615 /*                                                                        */
616 /*    Updates touch control structure                                     */
617 /*                                                                        */
618 /*  CALLS                                                                 */
619 /*                                                                        */
620 /*    _gx_touch_driver_generic_resistive_sample_read                      */
621 /*                                                                        */
622 /*  CALLED BY                                                             */
623 /*                                                                        */
624 /*    Internal logic                                                      */
625 /*                                                                        */
626 /*  RELEASE HISTORY                                                       */
627 /*                                                                        */
628 /*    DATE              NAME                      DESCRIPTION             */
629 /*                                                                        */
630 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
631 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
632 /*                                            resulting in version 6.1    */
633 /*                                                                        */
634 /**************************************************************************/
_gx_touch_driver_generic_resistive_coordinate_update(GX_RESISTIVE_TOUCH * touch)635 static GX_BOOL _gx_touch_driver_generic_resistive_coordinate_update(GX_RESISTIVE_TOUCH *touch)
636 {
637     GX_POINT sample;
638     GX_BOOL status = GX_TRUE;
639 
640     if (touch -> gx_resistive_touch_pen_down_detect())
641     {
642         status =  _gx_touch_driver_generic_resistive_sample_read(touch, &sample);
643         if (status)
644         {
645             touch -> gx_resistive_touch_current_touch_state = GX_TOUCH_STATE_TOUCHED;
646             _gx_touch_driver_generic_resistive_coord_calculate(touch, &sample);
647         }
648     }
649     else
650     {
651         touch -> gx_resistive_touch_current_touch_state = GX_TOUCH_STATE_RELEASED;
652     }
653     return status;
654 }
655 
656 /**************************************************************************/
657 /*                                                                        */
658 /*  FUNCTION                                               RELEASE        */
659 /*                                                                        */
660 /*    _gx_touch_driver_generic_resistive_update                           */
661 /*                                                        PORTABLE C      */
662 /*                                                           6.1          */
663 /*  AUTHOR                                                                */
664 /*                                                                        */
665 /*    Kenneth Maxwell, Microsoft Corporation                              */
666 /*                                                                        */
667 /*  DESCRIPTION                                                           */
668 /*                                                                        */
669 /*    Update touch status, send required event(s)                         */
670 /*                                                                        */
671 /*  INPUT                                                                 */
672 /*                                                                        */
673 /*    touch                                 Pointer to touch control      */
674 /*                                            structure.                  */
675 /*                                                                        */
676 /*  OUTPUT                                                                */
677 /*                                                                        */
678 /*    Touch events generated                                              */
679 /*                                                                        */
680 /*  CALLS                                                                 */
681 /*                                                                        */
682 /*    _gx_touch_driver_generic_resistive_coordinate_update                */
683 /*    _gx_touch_driver_generic_resistive_pen_drag_event_send              */
684 /*    _gx_touch_driver_generic_resistive_pen_down_event_send              */
685 /*    _gx_touch_driver_generic_resistive_pen_up_event_send                */
686 /*                                                                        */
687 /*  CALLED BY                                                             */
688 /*                                                                        */
689 /*    Application                                                         */
690 /*                                                                        */
691 /*  RELEASE HISTORY                                                       */
692 /*                                                                        */
693 /*    DATE              NAME                      DESCRIPTION             */
694 /*                                                                        */
695 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
696 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
697 /*                                            resulting in version 6.1    */
698 /*                                                                        */
699 /**************************************************************************/
_gx_touch_driver_generic_resistive_update(GX_RESISTIVE_TOUCH * touch)700 VOID _gx_touch_driver_generic_resistive_update(GX_RESISTIVE_TOUCH *touch)
701 {
702     if (_gx_touch_driver_generic_resistive_coordinate_update(touch))
703     {
704         if (touch -> gx_resistive_touch_current_touch_state == GX_TOUCH_STATE_TOUCHED)
705         {
706             if (touch -> gx_resistive_touch_last_touch_state == GX_TOUCH_STATE_TOUCHED)
707             {
708                 _gx_touch_driver_generic_resistive_pen_drag_event_send(touch);
709             }
710             else
711             {
712                 _gx_touch_driver_generic_resistive_pen_down_event_send(touch);
713             }
714         }
715         else
716         {
717             if (touch -> gx_resistive_touch_last_touch_state == GX_TOUCH_STATE_TOUCHED)
718             {
719                 _gx_touch_driver_generic_resistive_pen_up_event_send(touch);
720             }
721         }
722     }
723 }
724 
725 
726 /**************************************************************************/
727 /*                                                                        */
728 /*  FUNCTION                                               RELEASE        */
729 /*                                                                        */
730 /*    _gx_touch_driver_generic_resistive_wait_for_touch                   */
731 /*                                                        PORTABLE C      */
732 /*                                                           6.1          */
733 /*  AUTHOR                                                                */
734 /*                                                                        */
735 /*    Kenneth Maxwell, Microsoft Corporation                              */
736 /*                                                                        */
737 /*  DESCRIPTION                                                           */
738 /*                                                                        */
739 /*    Wait for touch screen pen down                                      */
740 /*                                                                        */
741 /*  INPUT                                                                 */
742 /*                                                                        */
743 /*    touch                                 Pointer to touch control      */
744 /*                                            structure.                  */
745 /*                                                                        */
746 /*  OUTPUT                                                                */
747 /*                                                                        */
748 /*    None                                                                */
749 /*                                                                        */
750 /*  CALLS                                                                 */
751 /*                                                                        */
752 /*    Indirectly calls pen down event detection                           */
753 /*                                                                        */
754 /*  CALLED BY                                                             */
755 /*                                                                        */
756 /*    Internal Logic                                                      */
757 /*                                                                        */
758 /*  RELEASE HISTORY                                                       */
759 /*                                                                        */
760 /*    DATE              NAME                      DESCRIPTION             */
761 /*                                                                        */
762 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
763 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
764 /*                                            resulting in version 6.1    */
765 /*                                                                        */
766 /**************************************************************************/
_gx_touch_driver_generic_resistive_wait_for_touch(GX_RESISTIVE_TOUCH * touch)767 static VOID _gx_touch_driver_generic_resistive_wait_for_touch(GX_RESISTIVE_TOUCH *touch)
768 {
769     while(!touch -> gx_resistive_touch_pen_down_detect())
770     {
771         GX_TOUCH_TASK_SLEEP(5);
772     }
773 }
774 
775 
776 /**************************************************************************/
777 /*                                                                        */
778 /*  FUNCTION                                               RELEASE        */
779 /*                                                                        */
780 /*    _gx_touch_driver_generic_resistive_wait_for_no_touch                */
781 /*                                                        PORTABLE C      */
782 /*                                                           6.1          */
783 /*  AUTHOR                                                                */
784 /*                                                                        */
785 /*    Kenneth Maxwell, Microsoft Corporation                              */
786 /*                                                                        */
787 /*  DESCRIPTION                                                           */
788 /*                                                                        */
789 /*    Wait for touch screen pen not down                                  */
790 /*                                                                        */
791 /*  INPUT                                                                 */
792 /*                                                                        */
793 /*    touch                                 Pointer to touch control      */
794 /*                                            structure.                  */
795 /*                                                                        */
796 /*  OUTPUT                                                                */
797 /*                                                                        */
798 /*    None                                                                */
799 /*                                                                        */
800 /*  CALLS                                                                 */
801 /*                                                                        */
802 /*    Indirectly calls pen down event detection                           */
803 /*                                                                        */
804 /*  CALLED BY                                                             */
805 /*                                                                        */
806 /*    Internal Logic                                                      */
807 /*                                                                        */
808 /*  RELEASE HISTORY                                                       */
809 /*                                                                        */
810 /*    DATE              NAME                      DESCRIPTION             */
811 /*                                                                        */
812 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
813 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
814 /*                                            resulting in version 6.1    */
815 /*                                                                        */
816 /**************************************************************************/
_gx_touch_driver_generic_resistive_wait_for_no_touch(GX_RESISTIVE_TOUCH * touch)817 static VOID _gx_touch_driver_generic_resistive_wait_for_no_touch(GX_RESISTIVE_TOUCH *touch)
818 {
819     while(touch -> gx_resistive_touch_pen_down_detect())
820     {
821         GX_TOUCH_TASK_SLEEP(5);
822     }
823 }
824 
825 /**************************************************************************/
826 /*                                                                        */
827 /*  FUNCTION                                               RELEASE        */
828 /*                                                                        */
829 /*    _gx_touch_driver_generic_resistive_calibrate                        */
830 /*                                                        PORTABLE C      */
831 /*                                                           6.1          */
832 /*  AUTHOR                                                                */
833 /*                                                                        */
834 /*    Kenneth Maxwell, Microsoft Corporation                              */
835 /*                                                                        */
836 /*  DESCRIPTION                                                           */
837 /*                                                                        */
838 /*    Perform touch screen calibration sequence                           */
839 /*                                                                        */
840 /*  INPUT                                                                 */
841 /*                                                                        */
842 /*    touch                                 Pointer to touch control      */
843 /*                                            structure.                  */
844 /*                                                                        */
845 /*  OUTPUT                                                                */
846 /*                                                                        */
847 /*    None                                                                */
848 /*                                                                        */
849 /*  CALLS                                                                 */
850 /*                                                                        */
851 /*    _gx_widget_hide                                                     */
852 /*    _gx_widget_show                                                     */
853 /*    _gx_multi_line_text_view_text_id_set                                */
854 /*    _gx_system_canvas_refresh()                                         */
855 /*    _gx_touch_driver_generic_resistive_wait_for_no_touch                */
856 /*    _gx_touch_driver_generic_resistive_wait_for_touch                   */
857 /*    _gx_touch_driver_generic_resistive_sample_read                      */
858 /*    GX_TOUCH_TASK_SLEEP                                                 */
859 /*    _gx_touch_driver_generic_resistive_calibration_matrix_set           */
860 /*                                                                        */
861 /*  CALLED BY                                                             */
862 /*                                                                        */
863 /*    Application                                                         */
864 /*                                                                        */
865 /*  RELEASE HISTORY                                                       */
866 /*                                                                        */
867 /*    DATE              NAME                      DESCRIPTION             */
868 /*                                                                        */
869 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
870 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
871 /*                                            fixed compiler warnings,    */
872 /*                                            resulting in version 6.1    */
873 /*                                                                        */
874 /**************************************************************************/
_gx_touch_driver_generic_resistive_calibrate(GX_RESISTIVE_TOUCH * touch)875 VOID _gx_touch_driver_generic_resistive_calibrate(GX_RESISTIVE_TOUCH *touch)
876 {
877     GX_EVENT calibration_event;
878 
879     GX_POINT raw_cal_points[3];
880     GX_POINT display_cal_points[3];
881 
882     int icon_size;
883     GX_RECTANGLE newsize;
884     GX_BOOL (*pen_down)(VOID);
885 
886     GX_MULTI_LINE_TEXT_VIEW *text = touch->gx_resistive_touch_info.gx_touch_info_text;
887     GX_WIDGET *icon = (GX_WIDGET *) touch->gx_resistive_touch_info.gx_touch_info_target;
888     pen_down = touch -> gx_resistive_touch_pen_down_detect;
889 
890     icon_size = icon->gx_widget_size.gx_rectangle_right - icon->gx_widget_size.gx_rectangle_left + 1;
891 
892     display_cal_points[0].gx_point_x = (GX_VALUE) (icon_size / 2);
893     display_cal_points[0].gx_point_y = (GX_VALUE) (icon_size / 2);
894     display_cal_points[1].gx_point_x = (GX_VALUE) (touch->gx_resistive_touch_x_range - (icon_size / 2));
895     display_cal_points[1].gx_point_y = (GX_VALUE) (touch->gx_resistive_touch_y_range / 2);
896     display_cal_points[2].gx_point_x = (GX_VALUE) (icon_size / 2);
897     display_cal_points[2].gx_point_y = (GX_VALUE) (touch->gx_resistive_touch_y_range - (icon_size / 2));
898 
899     if (pen_down())
900     {
901         _gx_widget_hide(icon);
902         _gx_multi_line_text_view_text_id_set(text, touch->gx_resistive_touch_info.gx_touch_info_string_id_no_touch);
903         _gx_system_canvas_refresh();
904         _gx_touch_driver_generic_resistive_wait_for_no_touch(touch);
905 
906         _gx_multi_line_text_view_text_id_set(text, touch->gx_resistive_touch_info.gx_touch_info_string_id_touch_1);
907         _gx_widget_show(icon);
908         _gx_system_canvas_refresh();
909     }
910 
911     /*  wait for a touch: */
912     while(1)
913     {
914         _gx_touch_driver_generic_resistive_wait_for_touch(touch);
915 
916         if (_gx_touch_driver_generic_resistive_sample_read(touch, &raw_cal_points[0]))
917         {
918             break;
919         }
920         GX_TOUCH_TASK_SLEEP(20);
921     }
922     _gx_widget_hide(icon);
923     _gx_multi_line_text_view_text_set_ext(text, GX_NULL);
924     _gx_system_canvas_refresh();
925     _gx_touch_driver_generic_resistive_wait_for_no_touch(touch);
926     GX_TOUCH_TASK_SLEEP(20);
927 
928     /*  move icon to middle-right */
929     _gx_widget_shift(icon,
930                     (GX_VALUE) (touch->gx_resistive_touch_x_range - icon_size),
931                     (GX_VALUE) ((touch->gx_resistive_touch_y_range / 2) - (icon_size / 2)),
932                      GX_FALSE);
933 
934     _gx_widget_show(icon);
935     _gx_multi_line_text_view_text_id_set(text, touch->gx_resistive_touch_info.gx_touch_info_string_id_touch_2);
936     _gx_system_canvas_refresh();
937 
938     /*  wait for a touch: */
939     while(1)
940     {
941         _gx_touch_driver_generic_resistive_wait_for_touch(touch);
942         if (_gx_touch_driver_generic_resistive_sample_read(touch, &raw_cal_points[1]))
943         {
944             break;
945         }
946         GX_TOUCH_TASK_SLEEP(20);
947     }
948     _gx_widget_hide(icon);
949     _gx_multi_line_text_view_text_set_ext(text, GX_NULL);
950     _gx_system_canvas_refresh();
951     _gx_touch_driver_generic_resistive_wait_for_no_touch(touch);
952     GX_TOUCH_TASK_SLEEP(20);
953 
954     /* move icon to bottom-left */
955     newsize.gx_rectangle_left = (GX_VALUE) 0;
956     newsize.gx_rectangle_bottom = (GX_VALUE) (touch->gx_resistive_touch_y_range);
957     newsize.gx_rectangle_top = (GX_VALUE) (newsize.gx_rectangle_bottom - icon_size + 1);
958     newsize.gx_rectangle_right = (GX_VALUE) (newsize.gx_rectangle_left + icon_size - 1);
959     _gx_widget_resize(icon, &newsize);
960     _gx_widget_show(icon);
961     _gx_multi_line_text_view_text_id_set(text, touch->gx_resistive_touch_info.gx_touch_info_string_id_touch_3);
962     _gx_system_canvas_refresh();
963 
964     /* wait for a touch: */
965     while(1)
966     {
967         _gx_touch_driver_generic_resistive_wait_for_touch(touch);
968         if (_gx_touch_driver_generic_resistive_sample_read(touch, &raw_cal_points[2]))
969         {
970             break;
971         }
972         GX_TOUCH_TASK_SLEEP(20);
973     }
974     _gx_widget_hide(icon);
975     _gx_multi_line_text_view_text_set_ext(text, GX_NULL);
976     _gx_system_canvas_refresh();
977     _gx_touch_driver_generic_resistive_wait_for_no_touch(touch);
978 
979     /* Initialize touch screen calibration factor matrix with new values  */
980     _gx_touch_driver_generic_resistive_calibration_matrix_set(touch,
981                          (GX_POINT *) &display_cal_points,   /* Display coordinates   */
982                          (GX_POINT *) raw_cal_points);             /* Touch coordinates     */
983 
984     touch -> gx_resistive_touch_calibrated = GX_TRUE;
985 
986     calibration_event.gx_event_type = GX_EVENT_TOUCH_CALIBRATION_COMPLETE;
987     calibration_event.gx_event_target = icon;
988     _gx_system_event_send(&calibration_event);
989 }
990 
991 #endif  /* WIN32 build conditional */
992 
993