-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcy_capsense_centroid.c
2200 lines (2036 loc) · 81.4 KB
/
cy_capsense_centroid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************//**
* \file cy_capsense_centroid.c
* \version 3.0
*
* \brief
* This file provides the source code for the centroid calculation methods
* of the CapSense middleware.
*
********************************************************************************
* \copyright
* Copyright 2018-2020, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include "cy_syslib.h"
#include "cy_capsense_centroid.h"
#include "cy_capsense_common.h"
#include "cy_capsense_lib.h"
#include "cy_capsense_structure.h"
#include "cy_capsense_filter.h"
#if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2) || defined(CY_IP_M0S8MSCV3))
/*******************************************************************************
* Local definition
*******************************************************************************/
#define CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_LENGTH (3u)
#define CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_PREVIOUS (0u)
#define CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_CENTER (1u)
#define CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_NEXT (2u)
/* Minimum valid age */
#define CY_CAPSENSE_CSX_TOUCHPAD_AGE_START (0x0100u)
#define CY_CAPSENSE_CSX_TOUCHPAD_Z_SHIFT (0x04u)
#define CY_CAPSENSE_CSX_TOUCHPAD_BYTE_SHIFT (8u)
#define CY_CAPSENSE_CENTROID_ROUND_VALUE (0x7Fu)
#define CY_CAPSENSE_NO_LOCAL_MAX (0xFFFFu)
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
/******************************************************************************/
/** \cond SECTION_CAPSENSE_INTERNAL */
/** \addtogroup group_capsense_internal *//** \{ */
/******************************************************************************/
static void Cy_CapSense_TransferTouch(
uint32_t newIndex,
uint32_t oldIndex,
const cy_stc_capsense_widget_config_t * ptrWdConfig);
static void Cy_CapSense_NewTouch(
uint32_t newIndex,
const cy_stc_capsense_widget_config_t * ptrWdConfig);
static uint32_t Cy_CapSense_CalcDistance(
uint32_t newIndex,
uint32_t oldIndex,
const cy_stc_capsense_widget_config_t * ptrWdConfig);
static void Cy_CapSense_Hungarian(
const cy_stc_capsense_widget_config_t * ptrWdConfig);
static void Cy_CapSense_CopyTouchRecord(
cy_stc_capsense_position_t * destination,
const cy_stc_capsense_position_t * source);
__STATIC_INLINE void Cy_CapSense_TouchDownDebounce(
const cy_stc_capsense_widget_config_t * ptrWdConfig);
__STATIC_INLINE void Cy_CapSense_SortByAge(
const cy_stc_capsense_widget_config_t * ptrWdConfig);
__STATIC_INLINE uint8_t Cy_CapSense_GetLowestId(uint8_t idMask);
/** \} \endcond */
/*******************************************************************************
* Function Name: Cy_CapSense_DpCentroidDiplex
****************************************************************************//**
*
* Finds touch position of a Linear slider widget with enabled diplexing.
*
* In scope of position searching this function finds the local maximum with the
* highest raw count. If such maximums are more than one, then the maximum with the
* bigger sum of neighboring sensors is taken for further processing. Then the position
* is calculated using centroid algorithm with three sensors.
*
* At least two neighboring sensors should cross finger threshold. Then the algorithm
* is able to distinguish where real touch is located (direct part of slider or
* diplex part of slider) and corresponding position is reported. Otherwise no
* touch is reported.
*
* This function does not detect two or more touches.
*
* \param newTouch
* The pointer to the touch structure where found position is stored.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpCentroidDiplex(
cy_stc_capsense_touch_t * newTouch,
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t sum;
uint32_t diffM;
uint32_t diffP;
uint32_t snsIndex;
cy_stc_capsense_sensor_context_t * ptrSnsCxt;
const uint8_t * ptrDpxTable;
uint32_t snsCount = ptrWdConfig->numSns;
uint32_t maxSum = 0u;
uint32_t maxDiff = 0u;
uint32_t maxIndex = CY_CAPSENSE_NO_LOCAL_MAX;
uint32_t threshold = ptrWdConfig->ptrWdContext->fingerTh;
int32_t numerator = 0;
int32_t denominator = 0;
uint32_t multiplier;
uint32_t offset;
threshold -= ptrWdConfig->ptrWdContext->hysteresis;
ptrDpxTable = ptrWdConfig->ptrDiplexTable;
/* Find maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < snsCount; snsIndex++)
{
if (maxDiff < ptrSnsCxt->diff)
{
maxDiff = ptrSnsCxt->diff;
}
ptrSnsCxt++;
}
/* Find sensor index with maximum sum ([i-1],[i],[i+1]) including diplex part */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < (snsCount << 1u); snsIndex++)
{
/* Potential maximum */
if (maxDiff == ptrSnsCxt[ptrDpxTable[snsIndex]].diff)
{
/* Get sum of differences around maximum */
diffM = (snsIndex > 0u) ? ptrSnsCxt[ptrDpxTable[snsIndex - 1u]].diff : 0u;
diffP = (snsIndex < ((snsCount << 1u) - 1u)) ? ptrSnsCxt[ptrDpxTable[snsIndex + 1u]].diff : 0u;
sum = ptrSnsCxt[ptrDpxTable[snsIndex]].diff + diffM + diffP;
if ((diffM < threshold) && (diffP < threshold))
{
sum = 0u;
}
if (maxSum < sum)
{
/* New maximum */
maxIndex = snsIndex;
maxSum = sum;
numerator = (int32_t)diffP - (int32_t)diffM;
}
}
}
if ((maxIndex != CY_CAPSENSE_NO_LOCAL_MAX) && (maxSum > 0u))
{
multiplier = (uint32_t)ptrWdConfig->xResolution << 8u;
/* Calculate position */
if (0u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CALC_METHOD_MASK))
{
multiplier /= ((snsCount << 1u) - 1u);
offset = 0u;
}
else
{
multiplier /= (snsCount << 1u);
offset = multiplier >> 1u;
}
denominator = (int32_t)maxSum;
denominator = ((numerator * (int32_t)multiplier) / denominator) + (((int32_t)maxIndex * (int32_t)multiplier) + (int32_t)offset);
/* Round result and shift 8 bits left */
newTouch->numPosition = CY_CAPSENSE_POSITION_ONE;
newTouch->ptrPosition[0u].x = CY_LO16(((uint32_t)denominator + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
}
else
{
newTouch->numPosition = CY_CAPSENSE_POSITION_NONE;
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpCentroidLinear
****************************************************************************//**
*
* Finds touch position of a Linear slider widget.
*
* In scope of position searching this function finds the local maximum with the
* highest raw count. If such maximums are more than one, then the maximum with
* bigger sum of neighboring sensors is taken for further processing. Then the position
* is calculated using centroid algorithm with three sensors.
*
* This function does not detect two or more touches.
*
* \param newTouch
* The pointer to the touch structure where the found position is stored.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpCentroidLinear(
cy_stc_capsense_touch_t * newTouch,
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t snsIndex = 0u;
uint32_t snsCount = ptrWdConfig->numSns;
uint32_t diffM;
uint32_t diffP;
uint32_t sum = 0u;
uint32_t maxSum = 0u;
uint32_t maxDiff = 0u;
uint32_t maxIndex = CY_CAPSENSE_NO_LOCAL_MAX;
cy_stc_capsense_sensor_context_t * ptrSnsCxt;
int32_t numerator = 0;
int32_t denominator = 0;
uint32_t multiplier;
uint32_t offset;
CY_ASSERT(snsCount > 2u);
if (1u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_NUMBER_MASK))
{
/* Find maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < snsCount; snsIndex++)
{
if (ptrSnsCxt->diff > maxDiff)
{
maxDiff = ptrSnsCxt->diff;
}
ptrSnsCxt++;
}
/* Find index of sensor with maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < snsCount; snsIndex++)
{
/* Potential maximum */
if (maxDiff == ptrSnsCxt->diff)
{
/* Get sum of differences around maximum */
diffM = (snsIndex > 0u) ? (ptrSnsCxt - 1u)->diff : 0u;
diffP = (snsIndex < (snsCount - 1u)) ? (ptrSnsCxt + 1u)->diff : 0u;
sum = ptrSnsCxt->diff + diffM + diffP;
if (maxSum < sum)
{
/* New maximum */
maxIndex = snsIndex;
maxSum = sum;
numerator = (int32_t)diffP - (int32_t)diffM;
}
}
ptrSnsCxt++;
}
if ((maxIndex != CY_CAPSENSE_NO_LOCAL_MAX) && (maxSum > 0u))
{
/* Calculate position */
multiplier = (uint32_t)ptrWdConfig->xResolution << 8u;
if (0u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CALC_METHOD_MASK))
{
multiplier /= (snsCount - 1u);
offset = 0u;
}
else
{
multiplier /= snsCount;
offset = multiplier >> 1u;
}
denominator = (int32_t)maxSum;
denominator = ((numerator * (int32_t)multiplier) / denominator) + (((int32_t)maxIndex * (int32_t)multiplier) + (int32_t)offset);
/* Round result and shift 8 bits left */
newTouch->numPosition = CY_CAPSENSE_POSITION_ONE;
newTouch->ptrPosition[0u].x = CY_LO16(((uint32_t)denominator + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
}
else
{
newTouch->numPosition = CY_CAPSENSE_POSITION_NONE;
}
}
else
{
/* This is a place holder for local maximum searching when number of centroids could be more than one */
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpCentroidRadial
****************************************************************************//**
*
* Finds touch position of a Radial slider widget.
*
* In scope of position searching this function finds the local maximum with the
* highest raw count. If such maximums are more than one, then the maximum with
* bigger sum of neighboring sensors is taken for further processing. Then the position
* is calculated using centroid algorithm with three sensors.
*
* This function does not detect two or more touches.
*
* \param newTouch
* The pointer to the touch structure where found position is stored.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpCentroidRadial(
cy_stc_capsense_touch_t * newTouch,
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t snsIndex = 0u;
uint32_t snsCount = ptrWdConfig->numSns;
uint32_t diffM;
uint32_t diffP;
uint32_t sum = 0u;
uint32_t maxSum = 0u;
uint32_t maxDiff = 0u;
uint32_t maxIndex = CY_CAPSENSE_NO_LOCAL_MAX;
cy_stc_capsense_sensor_context_t * ptrSnsCxt;
int32_t numerator = 0;
int32_t denominator = 0;
uint32_t multiplier;
if (1u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_NUMBER_MASK))
{
/* Find maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < snsCount; snsIndex++)
{
if (ptrSnsCxt->diff > maxDiff)
{
maxDiff = ptrSnsCxt->diff;
}
ptrSnsCxt++;
}
/* Find index of sensor with maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < snsCount; snsIndex++)
{
/* Potential maximum */
if (maxDiff == ptrSnsCxt->diff)
{
/* Get sum of differences around maximum */
diffM = (snsIndex > 0u) ? (ptrSnsCxt - 1u)->diff : ptrWdConfig->ptrSnsContext[snsCount - 1u].diff;
diffP = (snsIndex < (snsCount - 1u)) ? (ptrSnsCxt + 1u)->diff : ptrWdConfig->ptrSnsContext[0u].diff;
sum = ptrSnsCxt->diff + diffM + diffP;
if (maxSum < sum)
{
/* New maximum */
maxIndex = snsIndex;
maxSum = sum;
numerator = (int32_t)diffP - (int32_t)diffM;
}
}
ptrSnsCxt++;
}
if ((maxIndex != CY_CAPSENSE_NO_LOCAL_MAX) && (maxSum > 0u))
{
/* Calculate position */
multiplier = ((uint32_t)ptrWdConfig->xResolution << 8u) / snsCount;
denominator = (int32_t)maxSum;
denominator = ((numerator * (int32_t)multiplier) / denominator) + ((int32_t)maxIndex * (int32_t)multiplier);
if (denominator < 0)
{
denominator += ((int32_t)snsCount * (int32_t)multiplier);
}
/* Round result and shift 8 bits left */
newTouch->numPosition = CY_CAPSENSE_POSITION_ONE;
newTouch->ptrPosition[0u].x = CY_LO16(((uint32_t)denominator + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
}
else
{
newTouch->numPosition = CY_CAPSENSE_POSITION_NONE;
}
}
else
{
/* This is a place holder for local maximum searching when number of centroids could be more than one */
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpCentroidTouchpad
****************************************************************************//**
*
* Finds touch position of a CSD Touchpad widget.
*
* In scope of position searching this function finds the local maximum with the
* highest raw count. If such maximums are more than one, then the maximum with
* bigger sum of neighboring sensors is taken for further processing. Then the position
* is calculated using centroid algorithm with three sensors.
*
* This function does not detect two or more touches.
*
* \param newTouch
* The pointer to the touch structure where found position is stored.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpCentroidTouchpad(
cy_stc_capsense_touch_t * newTouch,
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t snsIndex = 0u;
uint32_t snsCount = ptrWdConfig->numSns;
uint32_t colCount = ptrWdConfig->numCols;
uint32_t rowCount = ptrWdConfig->numRows;
uint32_t diffM;
uint32_t diffP;
uint32_t sum = 0u;
uint32_t maxSum = 0u;
uint32_t maxDiff = 0u;
uint32_t maxIndex = CY_CAPSENSE_NO_LOCAL_MAX;
cy_stc_capsense_sensor_context_t * ptrSnsCxt;
int32_t numerator = 0;
int32_t denominator = 0;
uint32_t multiplier;
uint32_t offset;
CY_ASSERT(colCount > 2u);
CY_ASSERT(rowCount > 2u);
if (1u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_NUMBER_MASK))
{
/***********************************************************************
* X Axis (Cols)
***********************************************************************/
sum = 0u;
maxSum = 0u;
maxDiff = 0u;
/* Find maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < colCount; snsIndex++)
{
if (ptrSnsCxt->diff > maxDiff)
{
maxDiff = ptrSnsCxt->diff;
}
ptrSnsCxt++;
}
/* Find index of sensor with maximum signal */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
for (snsIndex = 0u; snsIndex < colCount; snsIndex++)
{
/* Potential maximum */
if (maxDiff == ptrSnsCxt->diff)
{
/* Get sum of differences around maximum */
diffM = (snsIndex > 0u) ? (ptrSnsCxt - 1u)->diff : 0u;
diffP = (snsIndex < (colCount - 1u)) ? (ptrSnsCxt + 1u)->diff : 0u;
sum = ptrSnsCxt->diff + diffM + diffP;
/* Check whether this sum is maximum sum */
if (maxSum < sum)
{
/* New maximum */
maxIndex = snsIndex;
maxSum = sum;
numerator = (int32_t)diffP - (int32_t)diffM;
}
}
ptrSnsCxt++;
}
if ((maxIndex != CY_CAPSENSE_NO_LOCAL_MAX) && (maxSum > 0u))
{
/* Calculate position */
multiplier = (uint32_t)ptrWdConfig->xResolution << 8u;
if (0u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CALC_METHOD_MASK))
{
multiplier /= (colCount - 1u);
offset = 0u;
}
else
{
multiplier /= colCount;
offset = multiplier >> 1u;
}
denominator = (int32_t)maxSum;
denominator = ((numerator * (int32_t)multiplier) / denominator) + (((int32_t)maxIndex * (int32_t)multiplier) + (int32_t)offset);
/* Round result and shift 8 bits left */
newTouch->numPosition = CY_CAPSENSE_POSITION_ONE;
newTouch->ptrPosition[0u].x = CY_LO16(((uint32_t)denominator + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
}
else
{
newTouch->numPosition = CY_CAPSENSE_POSITION_NONE;
}
if(newTouch->numPosition != CY_CAPSENSE_POSITION_NONE)
{
/***********************************************************************
* Y Axis (Rows)
***********************************************************************/
sum = 0u;
maxSum = 0u;
maxDiff = 0u;
maxIndex = CY_CAPSENSE_NO_LOCAL_MAX;
/* Find maximum signal */
ptrSnsCxt = &ptrWdConfig->ptrSnsContext[colCount];
for (snsIndex = colCount; snsIndex < snsCount; snsIndex++)
{
if (ptrSnsCxt->diff > maxDiff)
{
maxDiff = ptrSnsCxt->diff;
}
ptrSnsCxt++;
}
/* Find index of sensor with maximum signal */
ptrSnsCxt = &ptrWdConfig->ptrSnsContext[colCount];
for (snsIndex = 0u; snsIndex < rowCount; snsIndex++)
{
/* Potential maximum */
if (maxDiff == ptrSnsCxt->diff)
{
/* Get sum of differences around maximum */
diffM = (snsIndex > 0u) ? (ptrSnsCxt - 1u)->diff : 0u;
diffP = (snsIndex < (rowCount - 1u)) ? (ptrSnsCxt + 1u)->diff : 0u;
sum = ptrSnsCxt->diff + diffM + diffP;
/* Check if this sum is maximum sum */
if (maxSum < sum)
{
/* New maximum */
maxIndex = snsIndex;
maxSum = sum;
numerator = (int32_t)diffP - (int32_t)diffM;
}
}
ptrSnsCxt++;
}
if ((maxIndex != CY_CAPSENSE_NO_LOCAL_MAX) && (maxSum > 0u))
{
/* Calculate position */
multiplier = (uint32_t)ptrWdConfig->yResolution << 8u;
if (0u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CALC_METHOD_MASK))
{
multiplier /= (rowCount - 1u);
offset = 0u;
}
else
{
multiplier /= rowCount;
offset = multiplier >> 1u;
}
denominator = (int32_t)maxSum;
denominator = ((numerator * (int32_t)multiplier) / denominator) + (((int32_t)maxIndex * (int32_t)multiplier) + (int32_t)offset);
/* Round result and shift 8 bits left */
newTouch->ptrPosition[0].y = CY_LO16(((uint32_t)denominator + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
}
else
{
newTouch->numPosition = CY_CAPSENSE_POSITION_NONE;
}
}
}
else
{
/* This is a place holder for local maximum searching when number of centroids could be more than one */
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpAdvancedCentroidTouchpad
****************************************************************************//**
*
* Finds touch position of a CSD touchpad widget using an advanced centroid
* algorithm.
*
* This function is able to detect two touch positions using a centroid algorithm
* with matrix 5*5 of sensors and virtual sensors on the edges.
*
* \param newTouch
* The pointer to the touch structure where the found position is stored.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpAdvancedCentroidTouchpad(
cy_stc_capsense_touch_t * newTouch,
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t i;
cy_stc_capsense_sensor_context_t * ptrSnsIndex = ptrWdConfig->ptrSnsContext;
uint16_t * ptrDiffIndex = ptrWdConfig->ptrCsdTouchBuffer;
cy_stc_capsense_advanced_centroid_config_t advCfg;
advCfg.fingerTh = ptrWdConfig->ptrWdContext->fingerTh;
advCfg.penultimateTh = ptrWdConfig->advConfig.penultimateTh;
advCfg.virtualSnsTh = ptrWdConfig->advConfig.virtualSnsTh;
advCfg.resolutionX = ptrWdConfig->xResolution;
advCfg.resolutionY = ptrWdConfig->yResolution;
advCfg.snsCountX = ptrWdConfig->numCols;
advCfg.snsCountY = ptrWdConfig->numRows;
advCfg.crossCouplingTh = ptrWdConfig->advConfig.crossCouplingTh;
advCfg.edgeCorrectionEn = 0u;
advCfg.twoFingersEn = 0u;
if ((ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_NUMBER_MASK) > CY_CAPSENSE_POSITION_ONE)
{
advCfg.twoFingersEn = 1u;
}
if (0u != (ptrWdConfig->centroidConfig & CY_CAPSENSE_EDGE_CORRECTION_MASK))
{
advCfg.edgeCorrectionEn = 1u;
}
for (i = 0u; i < ptrWdConfig->numSns; i++)
{
ptrDiffIndex[i] = ptrSnsIndex[i].diff;
}
Cy_CapSense_AdvancedCentroidGetTouchCoordinates_Lib(
&advCfg,
ptrWdConfig->ptrCsdTouchBuffer,
newTouch);
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpFindLocalMaxDd
****************************************************************************//**
*
* Finds up to five local maximums for CSX Touchpad.
*
* This function takes an array of differences of the specified widget and
* finds up to five local maximums. The found maximums are stored in the CSX buffer
* ptrCsxTouchBuffer \ref cy_stc_capsense_csx_touch_buffer_t.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure
* \ref cy_stc_capsense_widget_config_t.
*
*******************************************************************************/
void Cy_CapSense_DpFindLocalMaxDd(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
uint32_t thresholdOff = (uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis;
uint32_t thresholdOn = (uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis;
uint16_t currDiff;
uint8_t rx;
uint8_t tx;
uint8_t snsShift;
uint8_t lastRx = ptrWdConfig->numCols - 1u;
uint8_t lastTx = ptrWdConfig->numRows - 1u;
uint32_t proceed = 0u;
uint32_t touchNum = 0u;
cy_stc_capsense_position_t * ptrNewPeak = &ptrWdConfig->ptrCsxTouchBuffer->newPeak[0u];
for (rx = CY_CAPSENSE_CSX_TOUCHPAD_MAX_PEAKS; rx-- > 0u;)
{
ptrNewPeak[rx].id = CY_CAPSENSE_CSX_TOUCHPAD_ID_UNDEFINED;
}
ptrNewPeak = &ptrWdConfig->ptrCsxTouchBuffer->newPeak[0u];
/* Go through all Rx electrodes */
for (rx = 0u; rx <= lastRx; rx++)
{
/*
* Go through all Tx and RX (changed above) electrodes intersections
* and check whether the local maximum requirement is met.
*/
for (tx = 0u; tx <= lastTx; tx++)
{
proceed = 0u;
currDiff = ptrSnsCxt->diff;
if (thresholdOff <= (uint32_t)currDiff)
{
/*
* Check local maximum requirement: Comparing raw count
* of a local maximum candidate with raw counts of sensors
* from the previous row.
*/
if (rx > 0u)
{
/* Sensor(i-1, j+1) */
snsShift = lastTx;
if ((tx < lastTx) && (currDiff <= (ptrSnsCxt - snsShift)->diff))
{
proceed = 1u;
}
if (0u == proceed)
{
/* Sensor(i-1, j) */
snsShift++;
if (currDiff <= (ptrSnsCxt - snsShift)->diff)
{
proceed = 1u;
}
}
if (0u == proceed)
{
/* Sensor(i-1, j-1) */
snsShift++;
if ((tx > 0u) && (currDiff <= (ptrSnsCxt - snsShift)->diff))
{
proceed = 1u;
}
}
}
/*
* Check local maximum requirement: Comparing raw count
* of a local maximum candidate with raw counts of sensors
* from the next row.
*/
if ((0u == proceed) && (rx < lastRx))
{
/* Sensor(i+1, j+1) */
snsShift = lastTx + 2u;
if ((tx < lastTx) && (currDiff < (ptrSnsCxt + snsShift)->diff))
{
proceed = 1u;
}
if (0u == proceed)
{
/* Sensor(i+1, j) */
snsShift--;
if (currDiff < (ptrSnsCxt + snsShift)->diff)
{
proceed = 1u;
}
}
if (0u == proceed)
{
/* Sensor(i+1, j-1) */
snsShift--;
if ((tx > 0u) && (currDiff < (ptrSnsCxt + snsShift)->diff))
{
proceed = 1u;
}
}
}
/*
* Check local maximum requirement: Comparing raw count
* of a local maximum candidate with raw counts of sensors
* from the same row Sensor(i, j+1). */
if ((0u == proceed) && (tx < lastTx))
{
if (currDiff < (ptrSnsCxt + 1u)->diff)
{
proceed = 1u;
}
}
/* Sensor(i, j-1) */
if ((0u == proceed) && (tx > 0u))
{
if (currDiff <= (ptrSnsCxt - 1u)->diff)
{
proceed = 1u;
}
}
/* Add local maximum to the touch structure if there is room. */
if (0u == proceed)
{
ptrNewPeak->x = rx;
ptrNewPeak->y = tx;
if (currDiff < thresholdOn)
{
ptrNewPeak->id |= CY_CAPSENSE_CSX_TOUCHPAD_ID_ON_FAIL;
}
touchNum++;
ptrNewPeak++;
}
}
ptrSnsCxt++;
if (touchNum >= CY_CAPSENSE_CSX_TOUCHPAD_MAX_PEAKS)
{
break;
}
}
if (touchNum >= CY_CAPSENSE_CSX_TOUCHPAD_MAX_PEAKS)
{
break;
}
}
ptrWdConfig->ptrCsxTouchBuffer->newPeakNumber = (uint8_t)touchNum;
}
/* CY_ID633 */
#if defined(__ICCARM__)
#pragma optimize=none
#endif /* (__ICCARM__) */
/*******************************************************************************
* Function Name: Cy_CapSense_DpCalcTouchPadCentroid
****************************************************************************//**
*
* Calculates the position for each local maximum using the 3x3 algorithm.
*
* This function calculates position coordinates of found local maximums.
* The found positions are stored in the CSX buffer ptrCsxTouchBuffer
* \ref cy_stc_capsense_csx_touch_buffer_t.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure
* \ref cy_stc_capsense_widget_config_t.
*
*******************************************************************************/
void Cy_CapSense_DpCalcTouchPadCentroid(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
uint8_t number;
uint8_t i;
uint8_t j;
uint32_t lastRx = (uint32_t)ptrWdConfig->numCols - 1u;
uint32_t lastTx = (uint32_t)ptrWdConfig->numRows - 1u;
uint16_t centroid[3u][3u];
int32_t weightedSumX;
int32_t weightedSumY;
uint32_t totalSum;
uint32_t multiplierX;
uint32_t offsetX;
uint32_t multiplierY;
uint32_t offsetY;
uint32_t touchNum = ptrWdConfig->ptrCsxTouchBuffer->newPeakNumber;
cy_stc_capsense_position_t * ptrNewPeak = &ptrWdConfig->ptrCsxTouchBuffer->newPeak[0];
for(number = 0u; number < touchNum; number++)
{
/* Set the sensor pointer to the local maximum sensor */
ptrSnsCxt = ptrWdConfig->ptrSnsContext;
ptrSnsCxt += (ptrNewPeak->y + (ptrNewPeak->x * ptrWdConfig->numRows));
/* Prepare 3x3 centroid two dimensional array */
/* Fill each row */
for (i = 0u; i < CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_LENGTH; i++)
{
/*
* The first condition could be valid only when local max on the first row (0 row) of Touchpad
* The second condition could be valid only when local max on the last row of Touchpad
* Then corresponding row (zero or the last) of 3x3 array is initialized to 0u
*/
if (((((int32_t)ptrNewPeak->x - 1) + (int32_t)i) < 0) ||
((((int32_t)ptrNewPeak->x - 1) + (int32_t)i) > (int32_t)lastRx))
{
centroid[CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_PREVIOUS][i] = 0u;
centroid[CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_CENTER][i] = 0u;
centroid[CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_NEXT][i] = 0u;
}
else
{
/* Fill each column */
for (j = 0u; j < CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_LENGTH; j++)
{
/*
* The first condition could be valid only when local max
* on the first column (0 row) of Touchpad. The second
* condition could be valid only when local max on the last
* column of Touchpad. Then corresponding column (zero or
* the last) of 3x3 array is initialized to 0u.
*/
if (((((int32_t)ptrNewPeak->y - 1) + (int32_t)j) < 0) ||
((((int32_t)ptrNewPeak->y - 1) + (int32_t)j) > (int32_t)lastTx))
{
centroid[j][i] = 0u;
}
else
{
centroid[j][i] = (uint16_t)(ptrSnsCxt + (((i - 1u) * ptrWdConfig->numRows) + (j - 1u)))->diff; //MISRA?
}
}
}
}
weightedSumX = 0;
weightedSumY = 0;
totalSum = 0u;
/* Calculate centroid */
for (i = 0u; i < CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_LENGTH; i++)
{
for (j = 0u; j < CY_CAPSENSE_CSX_TOUCHPAD_CENTROID_LENGTH; j++)
{
totalSum += centroid[i][j];
weightedSumX += (int32_t)centroid[i][j] * ((int32_t)j - 1);
weightedSumY += (int32_t)centroid[i][j] * ((int32_t)i - 1);
}
}
/* The X position is calculated.
* The weightedSumX value depends on a finger position shifted regarding
* the X electrode (ptrNewTouches.x).
* The multiplier ptrWdConfig->xCentroidMultiplier is a short from:
* CY_CAPSENSE_TOUCHPAD0_X_RESOLUTION * 256u) / (CY_CAPSENSE_TOUCHPAD0_NUM_RX - CONFIG))
* where CONFIG = 0 or 1 depends on TouchpadMultiplerMethod parameter.
*/
/* Calculate position */
multiplierX = (uint32_t)ptrWdConfig->xResolution << 8u;
multiplierY = (uint32_t)ptrWdConfig->yResolution << 8u;
if (0u == (ptrWdConfig->centroidConfig & CY_CAPSENSE_CALC_METHOD_MASK))
{
multiplierX /= lastRx;
offsetX = 0u;
multiplierY /= lastTx;
offsetY = 0u;
}
else
{
multiplierX /= (lastRx + 1u);
offsetX = multiplierX >> 1u;
multiplierY /= (lastTx + 1u);
offsetY = multiplierY >> 1u;
}
weightedSumX = ((weightedSumX * (int32_t)multiplierX) / (int32_t)totalSum) +
((((int32_t)ptrNewPeak->x) * (int32_t)multiplierX) + (int32_t)offsetX);
/* The X position is rounded to the nearest integer value and normalized to the resolution range */
ptrNewPeak->x = CY_LO16(((uint32_t)weightedSumX + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
/* The Y position is calculated.
* The weightedSumY value depends on a finger position shifted regarding the Y electrode (ptrWdConfig->ptrNewTouches.y)
* The multiplier ptrWdConfig->yCentroidMultiplier is a short from:
* CY_CAPSENSE_TOUCHPAD0_Y_RESOLUTION * 256u) / (CY_CAPSENSE_TOUCHPAD0_NUM_TX - CONFIG))
* where CONFIG = 0 or 1 depends on TouchpadMultiplerMethod parameter
*/
weightedSumY = ((weightedSumY * (int32_t)multiplierY) / (int32_t)totalSum) +
((((int32_t)ptrNewPeak->y) * (int32_t)multiplierY) + (int32_t)offsetY);
/* The Y position is rounded to the nearest integer value and normalized to the resolution range */
ptrNewPeak->y = CY_LO16(((uint32_t)weightedSumY + CY_CAPSENSE_CENTROID_ROUND_VALUE) >> 8u);
/* The z value is a sum of raw counts of sensors that form 3x3 matrix with a local maximum in the center */
ptrNewPeak->z = CY_LO8(totalSum >> CY_CAPSENSE_CSX_TOUCHPAD_Z_SHIFT);
ptrNewPeak++;
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpTouchTracking
****************************************************************************//**
*
* Tracks touches.
*
* This function tracks the found touches:
* - associates them with previous touches applying the Hungarian algorithm.
* - applies debounce filters.
* - suppresses excessive touches.
*
* The final touch data are stored in the CSX buffer ptrCsxTouchBuffer
* \ref cy_stc_capsense_csx_touch_buffer_t. This function should be called
* each scan cycle even when touch is not detected.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure
* \ref cy_stc_capsense_widget_config_t.
*
*******************************************************************************/
void Cy_CapSense_DpTouchTracking(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t i;
cy_stc_capsense_position_t * ptrNewPeak;
cy_stc_capsense_position_t * ptrOldPeak;
uint32_t newTouchNum = ptrWdConfig->ptrCsxTouchBuffer->newPeakNumber;
uint32_t oldTouchNum = ptrWdConfig->ptrCsxTouchHistory->oldPeakNumber;
int8_t * fingerPosIndex = &ptrWdConfig->ptrCsxTouchBuffer->fingerPosIndexMap[0u];
if ((0u != newTouchNum) || (0u != oldTouchNum))
{
/* Initialize variables */
ptrWdConfig->ptrCsxTouchBuffer->newActiveIdsMask = 0u;
/* Getting active touch IDs from previous scan */
ptrWdConfig->ptrCsxTouchHistory->oldActiveIdsMask = 0u;