-
Notifications
You must be signed in to change notification settings - Fork 4
/
cy_capsense_processing.c
2087 lines (1897 loc) · 82.3 KB
/
cy_capsense_processing.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_processing.c
* \version 5.0
*
* \brief
* This file provides the source code for the Data Processing module functions.
* The Data Processing module is responsible for the low-level raw count
* processing provided by the sensing module, maintaining baseline and
* difference values and performing high-level widget processing like
* updating button status or calculating slider position.
*
********************************************************************************
* \copyright
* Copyright 2018-2024, Cypress Semiconductor Corporation (an Infineon company)
* or an affiliate of 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 "cy_syslib.h"
#include <stddef.h>
#include <string.h>
#include "cycfg_capsense_defines.h"
#include "cy_capsense_common.h"
#include "cy_capsense_processing.h"
#include "cy_capsense_filter.h"
#include "cy_capsense_lib.h"
#include "cy_capsense_centroid.h"
#if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2) || defined(CY_IP_M0S8MSCV3) || defined(CY_IP_M0S8MSCV3LP))
/*******************************************************************************
* Local definition
*******************************************************************************/
/* Raw data normalization and scaling */
#define CY_CAPSENSE_SCALING_SHIFT (15)
#define CY_CAPSENSE_MAX_TX_PATTERN_NUM (32)
/** \} \endcond */
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeAllStatuses
****************************************************************************//**
*
* Performs initialization of all statuses and related modules including
* debounce counters and touch positions of all the widgets.
*
* The initialization includes the following tasks:
* * Reset the debounce counters of all the widgets.
* * Reset the number of touches.
* * Reset the position filter history for slider and touchpad widgets.
* * Clear all status of widgets and sensors.
* * Enable all the widgets.
*
* Calling this function is accompanied by
* * Cy_CapSense_InitializeAllBaselines().
* * Cy_CapSense_InitializeAllFilters().
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeAllStatuses(const cy_stc_capsense_context_t * context)
{
uint32_t widgetId;
#if (CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN_LP)
#if (CY_CAPSENSE_LP_EN)
context->ptrCommonContext->status &= (uint32_t)~((uint32_t)CY_CAPSENSE_MW_STATE_LP_ACTIVE_MASK);
#endif
#endif
for (widgetId = CY_CAPSENSE_TOTAL_WIDGET_COUNT; widgetId-- > 0u;)
{
Cy_CapSense_InitializeWidgetStatus(widgetId, context);
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN)
Cy_CapSense_InitializeWidgetGestures(widgetId, context);
#endif
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeWidgetStatus
****************************************************************************//**
*
* Performs initialization of all statuses, debounce counters, and touch positions
* of the specified widget.
*
* The initialization includes:
* * Resets the debounce counter of the widget.
* * Resets the number of touches.
* * Resets the position filter history for slider and touchpad widgets.
* * Clears widget and sensor statuses.
* * Enables the widget.
*
* The Button and Matrix Button widgets have individual debounce counters per
* sensor for the CSD widgets and per node for the CSX and ISX widgets.
*
* The Slider and Touchpad widgets have a single debounce counter per widget.
*
* The Proximity widget has two debounce counters per sensor. One is for the
* proximity event and the second is for the touch event.
*
* All debounce counters during initialization are set to the value of the
* onDebounce widget parameter.
*
* Calling this function is accompanied by
* * Cy_CapSense_InitializeWidgetBaseline().
* * Cy_CapSense_InitializeWidgetFilter().
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \note For the fifth-generation low power CAPSENSE™ widgets
* of the \ref CY_CAPSENSE_WD_LOW_POWER_E type are not processed.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeWidgetStatus(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
uint32_t snsIndex;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdCfg->ptrSnsContext;
uint32_t snsNumber = ptrWdCfg->numSns;
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN)
uint32_t filterSize;
cy_stc_capsense_position_t * ptrHistory;
#endif
#if (CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN_LP)
if ((uint8_t)CY_CAPSENSE_WD_LOW_POWER_E != ptrWdCfg->wdType)
#endif /* CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN_LP */
{
/* Clear widget active status */
ptrWdCxt->status &= (uint8_t)~(CY_CAPSENSE_WD_ACTIVE_MASK);
/* Clear sensor status */
for (snsIndex = snsNumber; snsIndex-- >0u;)
{
ptrSnsCxt->status &= (uint8_t)~(CY_CAPSENSE_SNS_TOUCH_STATUS_MASK | CY_CAPSENSE_SNS_TOUCH_PROX_STATUS_MASK);
ptrSnsCxt++;
}
/* Reset debounce counters */
switch (ptrWdCfg->wdType)
{
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_MATRIX_EN))
case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
/* Each button requires one debounce counter */
(void)memset(ptrWdCfg->ptrDebounceArr, (int32_t)ptrWdCxt->onDebounce, (size_t)snsNumber);
break;
#endif
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN))
case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
/* Each widget requires one debounce counter */
switch (ptrWdCfg->senseMethod)
{
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
case CY_CAPSENSE_CSD_GROUP:
*(ptrWdCfg->ptrDebounceArr) = ptrWdCxt->onDebounce;
break;
#endif
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
case CY_CAPSENSE_CSX_GROUP:
if ((uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E == ptrWdCfg->wdType)
{
*(ptrWdCfg->ptrDebounceArr) = ptrWdCxt->onDebounce;
}
/*
* CSX Touchpad has debounce located in another place. Moreover,
* debounce counter is initialized at ID assignment, so no need
* to do it here.
*/
break;
#endif
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_ISX_EN)
case CY_CAPSENSE_ISX_GROUP:
*(ptrWdCfg->ptrDebounceArr) = ptrWdCxt->onDebounce;
break;
#endif
default:
/* No action */
break;
}
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
case (uint8_t)CY_CAPSENSE_WD_PROXIMITY_E:
/* Proximity widgets have 2 debounce counters per sensor (for touch and prox detection) */
(void)memset(ptrWdCfg->ptrDebounceArr, (int32_t)ptrWdCxt->onDebounce, (size_t)(snsNumber << 1u));
break;
#endif
default:
/* No other widget types */
break;
}
/* Reset touch numbers */
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_MATRIX_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN))
switch (ptrWdCfg->wdType)
{
case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
/* Clean number of touches */
ptrWdCxt->wdTouch.numPosition = CY_CAPSENSE_POSITION_NONE;
if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
{
ptrWdCfg->ptrPosFilterHistory->numPosition = CY_CAPSENSE_POSITION_NONE;
}
break;
default:
/* No action on other widget types */
break;
}
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_BALLISTIC_MULTIPLIER_EN)
/* Reset ballistic displacement */
if (0u != (ptrWdCfg->centroidConfig & CY_CAPSENSE_CENTROID_BALLISTIC_MASK))
{
ptrWdCxt->xDelta = 0;
ptrWdCxt->yDelta = 0;
ptrWdCfg->ptrBallisticContext->oldTouchNumber = 0u;
}
#endif
/* Reset touch history */
if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
{
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN)
switch (ptrWdCfg->wdType)
{
case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
/* Clean position filter history */
if (ptrWdCfg->senseMethod == CY_CAPSENSE_CSX_GROUP)
{
/* Reset all history IDs to undefined state */
ptrHistory = ptrWdCfg->ptrPosFilterHistory->ptrPosition;
filterSize = (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_SIZE_MASK) >> CY_CAPSENSE_POSITION_FILTERS_SIZE_OFFSET;
for (snsIndex = 0u; snsIndex < CY_CAPSENSE_MAX_CENTROIDS; snsIndex++)
{
ptrHistory->id = CY_CAPSENSE_CSX_TOUCHPAD_ID_UNDEFINED;
ptrHistory += filterSize;
}
}
break;
default:
/* No action on other widget types */
break;
}
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ADAPTIVE_FILTER_EN)
/* Init Adaptive IIR filter */
if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_AIIR_MASK))
{
Cy_CapSense_AdaptiveFilterInitialize_Lib(&ptrWdCfg->aiirConfig,
ptrWdCfg->ptrPosFilterHistory->ptrPosition);
}
#endif
}
}
}
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_InitializeWidgetGestures
****************************************************************************//**
*
* Performs initialization of all gestures for the specified widget.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_InitializeWidgetGestures(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
if (NULL != ptrWdCfg->ptrGestureConfig)
{
if (0u != (ptrWdCfg->ptrGestureConfig->gestureEnableMask & CY_CAPSENSE_GESTURE_ALL_GESTURES_MASK))
{
ptrWdCxt->gestureDetected = 0u;
ptrWdCxt->gestureDirection = 0u;
Cy_CapSense_Gesture_ResetState(ptrWdCfg->ptrGestureContext);
}
}
}
/*******************************************************************************
* Function Name: Cy_CapSense_DecodeWidgetGestures
****************************************************************************//**
*
* Performs decoding of all gestures for the specified widget.
*
* This function should be called by application program only after all sensors
* are scanned and all data processing is executed using
* Cy_CapSense_ProcessAllWidgets() or Cy_CapSense_ProcessWidget() functions
* for the widget. Calling this function multiple times without a new sensor
* scan and process causes unexpected behavior.
*
* \note The function (Gesture detection functionality) requires a timestamp
* for its operation. The timestamp should be initialized and maintained
* in the application program prior to calling this function. See the
* descriptions of the Cy_CapSense_SetGestureTimestamp() and
* Cy_CapSense_IncrementGestureTimestamp() functions for details.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the detected Gesture mask and direction of detected gestures.
* The same information is stored in ptrWdContext->gestureDetected and
* ptrWdContext->gestureDirection registers. Corresponding macros could be found
* \ref group_capsense_macros_gesture.
* * bit[0..15] - detected gesture masks gesture
* * bit[0] - one-finger single click gesture
* * bit[1] - one-finger double click gesture
* * bit[2] - one-finger click and drag gesture
* * bit[3] - two-finger single click gesture
* * bit[4] - one-finger scroll gesture
* * bit[5] - two-finger scroll gesture
* * bit[6] - one-finger edge swipe
* * bit[7] - one-finger flick
* * bit[8] - one-finger rotate
* * bit[9] - two-finger zoom
* * bit[10] - one-finger long press
* * bit[13] - touchdown event
* * bit[14] - liftoff event
* * bit[16..31] - gesture direction if detected
* * bit[0..1] - direction of one-finger scroll gesture
* * bit[2..3] - direction of two-finger scroll gesture
* * bit[4..5] - direction of one-finger edge swipe gesture
* * bit[6] - direction of one-finger rotate gesture
* * bit[7] - direction of two-finger zoom gesture
* * bit[8..10] - direction of one-finger flick gesture
*
* \funcusage
*
* An example of gesture decoding:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_Gesture
*
* An example of gesture status parsing:
* \snippet capsense/snippet/main.c snippet_Cy_CapSense_Gesture_Macro
*
*******************************************************************************/
uint32_t Cy_CapSense_DecodeWidgetGestures(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
uint32_t gestureStatus = 0u;
uint32_t posIndex;
uint32_t positionNum;
const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
cy_stc_capsense_gesture_position_t position[CY_CAPSENSE_MAX_CENTROIDS];
if (NULL != ptrWdCfg->ptrGestureConfig)
{
if (0u != (ptrWdCfg->ptrGestureConfig->gestureEnableMask & CY_CAPSENSE_GESTURE_ALL_GESTURES_MASK))
{
if (((uint8_t)CY_CAPSENSE_WD_BUTTON_E == ptrWdCfg->wdType) ||
((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdCfg->wdType))
{
positionNum = (uint32_t)ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK;
for (posIndex = 0u; posIndex < positionNum; posIndex++)
{
position[posIndex].x = 0u;
position[posIndex].y = 0u;
}
}
else
{
positionNum = ptrWdCxt->wdTouch.numPosition;
if (positionNum > CY_CAPSENSE_MAX_CENTROIDS)
{
positionNum = 0u;
}
for (posIndex = 0u; posIndex < positionNum; posIndex++)
{
position[posIndex].x = ptrWdCxt->wdTouch.ptrPosition[posIndex].x;
position[posIndex].y = ptrWdCxt->wdTouch.ptrPosition[posIndex].y;
}
}
Cy_CapSense_Gesture_Decode(context->ptrCommonContext->timestamp, positionNum,
&position[0u], ptrWdCfg->ptrGestureConfig, ptrWdCfg->ptrGestureContext);
ptrWdCxt->gestureDetected = ptrWdCfg->ptrGestureContext->detected;
ptrWdCxt->gestureDirection = ptrWdCfg->ptrGestureContext->direction;
gestureStatus = (uint32_t)ptrWdCxt->gestureDetected | ((uint32_t)ptrWdCxt->gestureDirection << CY_CAPSENSE_GESTURE_DIRECTION_OFFSET);
}
}
return gestureStatus;
}
#endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN) */
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessWidgetRawCounts
****************************************************************************//**
*
* Performs default processing of the raw counts of the specified widget.
*
* The processing includes the following tasks:
* - Run Filters.
* - Update Baselines.
* - Update Differences.
* The same process is applied to all the sensors of the specified widget.
*
* \param widgetId
* Specifies the ID number of the widget. A macro for the widget ID can be found
* in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the specified widget processing operation:
* - Zero - if operation was successfully completed.
* - Non-zero - if baseline processing of any sensor of the specified widget
* failed. The result is concatenated with the index of failed sensor.
*
*******************************************************************************/
uint32_t Cy_CapSense_DpProcessWidgetRawCounts(
uint32_t widgetId,
const cy_stc_capsense_context_t * context)
{
uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
uint32_t snsIndex;
uint32_t snsHistorySize;
uint32_t freqChIndex;
uint16_t * ptrHistorySns;
uint16_t * ptrBslnInvSns;
uint8_t * ptrHistoryLowSns = NULL;
cy_stc_capsense_sensor_context_t * ptrSnsCxtSns;
const cy_stc_capsense_widget_config_t * ptrWdCfg;
ptrWdCfg = &context->ptrWdConfig[widgetId];
#if ((CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) && \
(CY_CAPSENSE_ENABLE == CY_CAPSENSE_SMARTSENSE_FULL_EN))
cy_stc_capsense_smartsense_csd_noise_envelope_t * ptrNEHistory = ptrWdCfg->ptrNoiseEnvelope;
#endif
snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
for (freqChIndex = 0u; freqChIndex < CY_CAPSENSE_CONFIGURED_FREQ_NUM; freqChIndex++)
{
ptrSnsCxtSns = &ptrWdCfg->ptrSnsContext[freqChIndex * context->ptrCommonConfig->numSns];
ptrBslnInvSns = &ptrWdCfg->ptrBslnInv[freqChIndex * context->ptrCommonConfig->numSns];
ptrHistorySns = &ptrWdCfg->ptrRawFilterHistory[freqChIndex * (CY_CAPSENSE_RAW_HISTORY_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
if (CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
{
ptrHistoryLowSns = &ptrWdCfg->ptrRawFilterHistoryLow[freqChIndex *
(CY_CAPSENSE_IIR_HISTORY_LOW_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
}
for (snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
{
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdCfg, ptrSnsCxtSns, ptrHistorySns, ptrHistoryLowSns);
#endif
/* Run auto-tuning activities */
#if ((CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) && \
(CY_CAPSENSE_ENABLE == CY_CAPSENSE_SMARTSENSE_FULL_EN))
if (CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseMethod)
{
Cy_CapSense_RunNoiseEnvelope_Lib(ptrSnsCxtSns->raw, ptrWdCfg->ptrWdContext->sigPFC, ptrNEHistory);
Cy_CapSense_DpUpdateThresholds(ptrWdCfg->ptrWdContext, ptrNEHistory, snsIndex);
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
if ((uint8_t)CY_CAPSENSE_WD_PROXIMITY_E == ptrWdCfg->wdType)
{
ptrWdCfg->ptrWdContext->proxTh = ptrWdCfg->ptrWdContext->fingerTh;
ptrWdCfg->ptrWdContext->fingerTh = (uint16_t)(((uint32_t)ptrWdCfg->ptrWdContext->fingerTh *
context->ptrCommonConfig->proxTouchCoeff) / CY_CAPSENSE_PERCENTAGE_100);
}
#endif
ptrNEHistory++;
}
#endif
result |= Cy_CapSense_FtUpdateBaseline(ptrWdCfg->ptrWdContext, ptrSnsCxtSns, ptrBslnInvSns, context);
Cy_CapSense_DpUpdateDifferences(ptrWdCfg->ptrWdContext, ptrSnsCxtSns);
ptrSnsCxtSns++;
ptrBslnInvSns++;
ptrHistorySns += snsHistorySize;
if (NULL != ptrHistoryLowSns)
{
ptrHistoryLowSns++;
}
}
}
#if ((CY_CAPSENSE_PLATFORM_BLOCK_FOURTH_GEN) && \
(CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN))
ptrSnsCxtSns = ptrWdCfg->ptrSnsContext;
for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
{
Cy_CapSense_RunMfsFiltering(ptrSnsCxtSns, context);
ptrSnsCxtSns++;
}
#endif
#if ((CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_WIDGET_EN) && \
((CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN) || (CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN_LP)))
(void)Cy_CapSense_RunMfsMedian(widgetId, context);
#endif
return result;
}
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessCsxWidgetStatus
****************************************************************************//**
*
* Updates the status of the CSX widget in the Data Structure.
*
* This function determines the type of widget and runs the appropriate function
* that implements the status update algorithm for this type of widget.
*
* When the widget-specific processing completes this function updates the
* sensor and widget status registers in the data structure.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpProcessCsxWidgetStatus(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
switch (ptrWdConfig->wdType)
{
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_BUTTON_EN) ||\
(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN))
case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
Cy_CapSense_DpProcessButton(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_LINEAR_SLIDER_EN)
case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
Cy_CapSense_DpProcessSlider(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_TOUCHPAD_EN)
case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
Cy_CapSense_DpProcessCsxTouchpad(ptrWdConfig);
break;
#endif
default:
/* Nothing to process since widget type is not valid */
break;
}
}
#endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN) */
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessCsdWidgetStatus
****************************************************************************//**
*
* Updates the status of the CSD widget in the Data Structure.
*
* This function determines the type of widget and runs the appropriate function
* that implements the status update algorithm for this type of widget.
*
* When the widget-specific processing completes this function updates the
* sensor and widget status registers in the data structure.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
*******************************************************************************/
void Cy_CapSense_DpProcessCsdWidgetStatus(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_context_t * context)
{
(void)context;
switch (ptrWdConfig->wdType)
{
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_BUTTON_EN)
case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
Cy_CapSense_DpProcessButton(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_SLIDER_EN)
case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
Cy_CapSense_DpProcessSlider(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN)
case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
Cy_CapSense_DpProcessCsdMatrix(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN)
case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
Cy_CapSense_DpProcessCsdTouchpad(ptrWdConfig, context);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
case (uint8_t)CY_CAPSENSE_WD_PROXIMITY_E:
Cy_CapSense_DpProcessProximity(ptrWdConfig);
break;
#endif
default:
/* No other widget types */
break;
}
}
#endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) */
#if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_ISX_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessIsxWidgetStatus
****************************************************************************//**
*
* Updates the status of the ISX widget in the Data Structure.
*
* This function determines the type of widget and runs the appropriate function
* that implements the status update algorithm for this type of widget.
*
* When the widget-specific processing completes this function updates the
* sensor and widget status registers in the data structure.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpProcessIsxWidgetStatus(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
switch (ptrWdConfig->wdType)
{
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ISX_BUTTON_EN)
case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
Cy_CapSense_DpProcessButton(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ISX_LINEAR_SLIDER_EN)
case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
Cy_CapSense_DpProcessSlider(ptrWdConfig);
break;
#endif
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ISX_PROXIMITY_EN)
case (uint8_t)CY_CAPSENSE_WD_PROXIMITY_E:
Cy_CapSense_DpProcessProximity(ptrWdConfig);
break;
#endif
default:
/* No other widget types */
break;
}
}
#endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_ISX_EN) */
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessSensorRawCountsExt
****************************************************************************//**
*
* Performs customized processing of the sensor raw counts.
*
* If all bits are set at once, the default processing order will take place.
* For a custom order, this function can be called multiple times and execute
* one task at a time.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
* \param ptrSnsRawHistory
* The pointer to the filter history.
*
* \param ptrSnsRawHistoryLow
* The pointer to the extended filter history.
*
* \param mode
* The bit-mask with the data processing tasks to be executed.
* The mode parameters can take the following values:
* - CY_CAPSENSE_PROCESS_FILTER (0x01) Run Firmware Filter
* - CY_CAPSENSE_PROCESS_BASELINE (0x02) Update Baselines
* - CY_CAPSENSE_PROCESS_DIFFCOUNTS (0x04) Update Difference Counts
* - CY_CAPSENSE_PROCESS_ALL Execute all tasks
*
* \param ptrSnsBslnInv
* The pointer to the sensor baseline inversion used for BIST if enabled.
*
* \param context
* The pointer to the CAPSENSE™ context structure \ref cy_stc_capsense_context_t.
*
* \return
* Returns the status of the specified sensor processing operation:
* - CY_CAPSENSE_STATUS_SUCCESS if operation was successfully completed;
* - Non-zero - if baseline processing of any
* sensor of the specified widget failed. The result is concatenated with the index
* of failed sensor.
*
*******************************************************************************/
uint32_t Cy_CapSense_DpProcessSensorRawCountsExt(
const cy_stc_capsense_widget_config_t * ptrWdConfig,
cy_stc_capsense_sensor_context_t * ptrSnsContext,
uint16_t * ptrSnsRawHistory,
uint8_t * ptrSnsRawHistoryLow,
uint32_t mode,
uint16_t * ptrSnsBslnInv,
const cy_stc_capsense_context_t * context)
{
uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
if (0u != (mode & CY_CAPSENSE_PROCESS_FILTER))
{
Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdConfig, ptrSnsContext,
ptrSnsRawHistory, ptrSnsRawHistoryLow);
}
#else
(void)ptrSnsRawHistory;
(void)ptrSnsRawHistoryLow;
#endif
if (0u != (mode & CY_CAPSENSE_PROCESS_BASELINE))
{
result = Cy_CapSense_FtUpdateBaseline(ptrWdCxt, ptrSnsContext, ptrSnsBslnInv, context);
}
if (0u != (mode & CY_CAPSENSE_PROCESS_DIFFCOUNTS))
{
Cy_CapSense_DpUpdateDifferences(ptrWdCxt, ptrSnsContext);
}
return result;
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpUpdateThresholds
****************************************************************************//**
*
* Updates noise and finger thresholds for a specified widget.
*
* Used for smart sensing algorithm.
*
* \param ptrWdContext
* The pointer to the widget context structure.
*
* \param ptrNoiseEnvelope
* The pointer to the noise-envelope history structure.
*
* \param startFlag
* The flag indicates when a new widget is processed.
*
*******************************************************************************/
void Cy_CapSense_DpUpdateThresholds(
cy_stc_capsense_widget_context_t * ptrWdContext,
const cy_stc_capsense_smartsense_csd_noise_envelope_t * ptrNoiseEnvelope,
uint32_t startFlag)
{
cy_stc_capsense_smartsense_update_thresholds_t thresholds;
/* Calculate Thresholds */
thresholds.fingerTh = ptrWdContext->fingerTh;
Cy_CapSense_UpdateThresholds_Lib(ptrNoiseEnvelope, &thresholds, ptrWdContext->sigPFC, startFlag);
/* Update CAPSENSE™ context */
ptrWdContext->fingerTh = thresholds.fingerTh;
ptrWdContext->noiseTh = (uint16_t)thresholds.noiseTh;
ptrWdContext->nNoiseTh = (uint16_t)thresholds.nNoiseTh;
ptrWdContext->hysteresis = (uint16_t)thresholds.hysteresis;
}
/*******************************************************************************
* Function Name: Cy_CapSense_DpUpdateDifferences
****************************************************************************//**
*
* Calculates new difference values.
*
* This function calculates the difference between the baseline and raw counts.
* If the difference is positive (raw > baseline), and higher than the widget
* noise threshold, it is saved into the data structure for further processing.
* Otherwise the difference is set to zero. The final difference value is saved
* with the subtracted noise threshold value.
*
* \param ptrWdContext
* The pointer to the widget context structure.
*
* \param ptrSnsContext
* The pointer to the sensor context structure.
*
*******************************************************************************/
void Cy_CapSense_DpUpdateDifferences(
const cy_stc_capsense_widget_context_t * ptrWdContext,
cy_stc_capsense_sensor_context_t * ptrSnsContext)
{
ptrSnsContext->diff = 0u;
if ((uint32_t)ptrSnsContext->raw > ((uint32_t)ptrSnsContext->bsln + ptrWdContext->noiseTh))
{
ptrSnsContext->diff = ptrSnsContext->raw - ptrSnsContext->bsln;
}
}
#if ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) || (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN))
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessButton
****************************************************************************//**
*
* Processes the status of the Button widget.
*
* This function processes the status of the CSD/CSX/ISX Button widget and
* CSX Matrix Button widget. It applies the hysteresis and debounce algorithm
* to each sensor difference value. This function is expected to be called
* after each new widget scan. If it is called multiple times for the same
* scan data, the debounce algorithm will not work properly.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpProcessButton(
const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t snsIndex;
uint32_t activeCount = 0u;
uint32_t touchTh;
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
uint32_t startIndex = 0u;
#endif
uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
ptrWdCxt->status &= (uint8_t)~CY_CAPSENSE_WD_ACTIVE_MASK;
/* Go through all widget's sensors */
for (snsIndex = 0u; snsIndex < ptrWdConfig->numSns; snsIndex++)
{
/* Calculate touch threshold based on current sensor state */
touchTh = (0u == ptrSnsCxt->status) ?
((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
if (0u < (*ptrDebounceCnt))
{
/* Decrement debounce counter */
(*ptrDebounceCnt)--;
}
/* No touch */
if (ptrSnsCxt->diff <= touchTh)
{
/* Reset debounce counter */
*ptrDebounceCnt = ptrWdCxt->onDebounce;
ptrSnsCxt->status = 0u;
}
/* New touch or touch still exists */
if (0u == (*ptrDebounceCnt))
{
ptrSnsCxt->status = CY_CAPSENSE_SNS_TOUCH_STATUS_MASK;
activeCount++;
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
startIndex = snsIndex;
#endif
}
/* Update widget status */
if (0u != ptrSnsCxt->status)
{
ptrWdCxt->status |= (uint8_t)CY_CAPSENSE_WD_ACTIVE_MASK;
}
ptrSnsCxt++;
ptrDebounceCnt++;
}
/* Update position struct */
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
if (((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdConfig->wdType) &&
(CY_CAPSENSE_CSX_GROUP == ptrWdConfig->senseMethod))
{
ptrWdCxt->wdTouch.numPosition = (uint8_t)activeCount;
ptrWdCxt->wdTouch.ptrPosition->id = (uint16_t)startIndex;
ptrWdCxt->wdTouch.ptrPosition->x = (uint16_t)(startIndex / ptrWdConfig->numRows);
ptrWdCxt->wdTouch.ptrPosition->y = (uint16_t)(startIndex % ptrWdConfig->numRows);
}
#endif
}
#endif /* ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) || (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)) */
#if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_PROXIMITY_EN)
/*******************************************************************************
* Function Name: Cy_CapSense_DpProcessProximity
****************************************************************************//**
*
* Processes the status of the Proximity widget.
*
* This function processes the status of the CSD Proximity widget. It applies the
* hysteresis and debounce algorithm to each sensor difference value.
* The proximity and touch events are considered independently so debounce and
* hysteresis are also applied independently.
*
* This function is expected to be called after each new widget scan. If it is
* called multiple times for the same scan data the debounce algorithm
* will not work properly.
*
* \param ptrWdConfig
* The pointer to the widget configuration structure.
*
*******************************************************************************/
void Cy_CapSense_DpProcessProximity(const cy_stc_capsense_widget_config_t * ptrWdConfig)
{
uint32_t snsTh;
uint32_t snsIndex;
uint32_t snsStMask;
uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
/* Reset widget status */
ptrWdCxt->status &= (uint8_t)~CY_CAPSENSE_WD_ACTIVE_MASK;
/* Go through all sensor's status bits */
for (snsIndex = 0u; snsIndex < ((uint32_t)ptrWdConfig->numSns << 1u); snsIndex++)
{
/*
* Proximity - even; Touch - odd. Example:
* Bit [1] indicates that a touch is detected.
* Bit [0] indicates that a proximity is detected.
*/
snsTh = ptrWdCxt->fingerTh;
snsStMask = CY_CAPSENSE_SNS_TOUCH_PROX_STATUS_MASK;
if (0u == (snsIndex & 0x01u))
{
snsTh = ptrWdCxt->proxTh;
snsStMask = CY_CAPSENSE_SNS_PROX_STATUS_MASK;
}
/* Calculate threshold based on current sensor state */