-
Notifications
You must be signed in to change notification settings - Fork 7
/
AdvCapTouch.cpp
588 lines (458 loc) · 13.6 KB
/
AdvCapTouch.cpp
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
#include "AdvCapTouch.h"
/*
These capacitive touch detection library is written as an extension of the Arduino capacitive touch
library by Paul Bagder and Paul Stoffregen, it offers two detection algorithms that solves two of the main problems associated with
detecting capacitive touch on the Arduino.The library also allows advance touch detections; such
as, float tap, Short press and Long press.Also featured in the library is a haptics controller, this
allow you to connect and control a vibration motor as a feedback when you interact with the
capacitive touch surface.
.... By Ahmed Oyenuga (Ahmsville 2019).
*/
void AdvCapTouch::read_valueFromNoise_interrupt()
{
if (startWith_interrupt)
{
}
//SerialUSB.println("read");
}
AdvCapTouch::AdvCapTouch() {
}
AdvCapTouch::AdvCapTouch(bool useint) {
interruptValue_aquisition = useint;
}
void AdvCapTouch::initialize_capTouch(int numofpads) {
set_adaptiveSensitivity(1, 0.09, true); //set touch sensitivity to adaptive (very helpful for noisy signals)--- (number of pads, sensitivity(0 to 1), turn-on/off(true or false))
set_inputTypeThresholds(300, 1000, 2000, 250); // set the thresholds for the four input types (singletap, shortpress, longpress, floattapspeed)
pad0.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1
pad1.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 2
pad2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 3
pad3.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 4
pinMode(sendpin, OUTPUT);
for (int i = 0; i < numofpads; i++) {
pinMode(receivepin[i], INPUT);
}
}
void AdvCapTouch::set_inputTypeThresholds(int stp, int spr, int lpr, int dcls) { //sets the thresholds for the four input types (singletap, shortpress, longpress, floattapspeed)
singletaptresh = stp;
shortpressthresh = spr;
longpressthresh = lpr;
floatclickspeed = dcls;
}
void AdvCapTouch::set_adaptiveSensitivity(int padnum, bool act) {
//read_valueFromNoise(padnum);
detectionThreshold[padnum] = (maxsample[padnum] - minsample[padnum]);
rejectionThreshold[padnum] = detectionThreshold[padnum];
adaptsensitivity = act;
}
void AdvCapTouch::set_SensitivityRange(int numofpads, float min, float max) {
minsense = min;
maxsense = max;
}
void AdvCapTouch::set_adaptiveSensitivity(int numofpads, float mastersensitivity, bool act) {
sensitivity = mastersensitivity;
adaptsensitivity = act;
}
bool AdvCapTouch::checkTouch(int padnum) { //checks for valid capacitive touch
if (basevalue[padnum] == 0)
{
return false;
}
if (detectionstate == 0) {
if (changevalue[padnum] > (basevalue[padnum] + (sensitivity * detectionThreshold[padnum]))) {
if (touchspikedetected)
{
detectionstate = 0;
input_type = 0;
returntouchtype = true;
return false;
}
else {
//SerialUSB.print(standarddeviation);
//SerialUSB.print("\t");
//SerialUSB.println(SDs_away);
prevchangevalue[padnum] += detectionThreshold[padnum];
return true;
}
}
else {
return false;
}
}
else {
if (changevalue[padnum] > (treshbasevalue + (sensitivity * detectionThreshold[padnum]))) {
if (touchspikedetected)
{
detectionstate = 0;
input_type = 0;
returntouchtype = true;
return false;
}
else {
//SerialUSB.print(standarddeviation);
//SerialUSB.print("\t");
//SerialUSB.println(SDs_away);
return true;
}
}
else {
return false;
}
}
}
bool AdvCapTouch::checkRelease(int padnum) { //checks for valid capacitive touch
if (touchspikedetected)
{
detectionstate = 0;
input_type = 0;
returntouchtype = true;
return false;
}
else {
if (changevalue[padnum] < (treshbasevalue - (sensitivity * detectionThreshold[padnum]))) {
return true;
}
else {
return false;
}
}
}
bool AdvCapTouch::repopulateSample(int padnum) { //checks for valid capacitive touch
for (int i = 0; i < testsignalsize; i++) {
samplevalues[i] = 9999999;
}
//repopulating = true;
}
void AdvCapTouch::addSample(int padnum, float value) {
if (touchspikedetected)
{
if ((millis() - spikedetecttime) > spikereleasetime) {
touchspikedetected = false;
}
}
if (samplecounter < testsignalsize) {
prevsignalsample = samplevalues[samplecounter];
samplevalues[samplecounter] = value;
samplecounter += 1;
}
if (samplecounter >= testsignalsize) {
samplecounter = 0;
}
}
void AdvCapTouch::set_capTouchPins(int sp, int rp0, int rp1, int rp2, int rp3) { //sets the arduino pins used for the capacitive touch detection (sendpin, receivepin1, receivepin2, receivepin3, receivepin4)
sendpin = sp;
receivepin[0] = rp0;
receivepin[1] = rp1;
receivepin[2] = rp2;
receivepin[3] = rp3;
pad0 = CapacitiveSensor(sendpin, receivepin[0]);
pad1 = CapacitiveSensor(sendpin, receivepin[1]);
pad2 = CapacitiveSensor(sendpin, receivepin[2]);
pad3 = CapacitiveSensor(sendpin, receivepin[3]);
}
int AdvCapTouch::detect_touchFromNoise(int padnum) { //touch type detection function
if (detectionstate == -10)
{
read_valueFromNoise(padnum);
if (debounce < (millis() - time_touched))
{
if (checkTouch(padnum) == true)
{
haptics(1); //turn on viberation motor to acknowledge touch
input_type = 1; //set input type as single tap but proceed to check for second touch
detectionstate = 1;
treshbasevalue = basevalue[padnum];
time_touched = millis();
startWith_interrupt = false;
//changevalue[padnum] += detectionThreshold[padnum];
}
}
}
if (detectionstate == 0) {
read_valueFromNoise(padnum);
if (checkTouch(padnum) == true) {
detectionstate = -10;
//changevalue[padnum] =
time_touched = millis();
master_time_touched = time_touched;
}
}
else if (detectionstate == 1) {
read_valueFromNoise(padnum);
if (checkTouch(padnum) == true) { //check again
long temptime = (millis() - time_touched);
if (temptime < 4000) {
haptics(0);
}
if (temptime > shortpressthresh && temptime < longpressthresh) { //shortpress
input_type = 3;
if (!shortpresshaptics) {
haptics(1); //turn on viberation to indicate long pressthresh
shortpresshaptics = true;
}
}
if (temptime > longpressthresh && temptime < 4000) {
input_type = 4;
if (!longpresshaptics) {
haptics(1); //turn on viberation to indicate long pressthresh
longpresshaptics = true;
}
//returntouchtype = true;
}
if (temptime >= 4000) { //extra long touch input for special operations
input_type = 5;
if (!extralongpresshaptics) {
haptics(1); //turn on viberation to indicate long pressthresh
extralongpresshaptics = true;
}
if (checkRelease(padnum)) {
returntouchtype = true;
haptics(0);
}
else {
if (temptime >= 5000) {
input_type = 0;
returntouchtype = true;
}
}
}
}
else { //detected release
if ((millis() - time_touched) > floatclickspeed) {
returntouchtype = true;
}
//Serial.println((millis() - time_touched));
detectionstate = 2;
//changevalue[padnum] = minsample[padnum];
}
}
else if (detectionstate == 2) {
if (input_type > 2) { //already set to shortpress or longpress
returntouchtype = true;
}
else if (input_type == 2)//wait for release
{
haptics(0);
read_valueFromNoise(padnum);
if (checkRelease(padnum))
{
returntouchtype = true;
}
}
else {
haptics(0);
read_valueFromNoise(padnum);
if (checkTouch(padnum) == true) {
read_valueFromNoise(padnum);
if (checkTouch(padnum) == true) { //check again
//Serial.println((millis() - time_touched));
if ((millis() - time_touched) > floatclickspeed) {
haptics(1);
input_type = 2;
}
}
}
else {
if ((millis() - time_touched) > singletaptresh) { //float tap timeout
returntouchtype = true;
}
}
}
}
if (returntouchtype == true) {
longpresshaptics = false;
shortpresshaptics = false;
extralongpresshaptics = false;
//samplecounter = testsignalsize;
//changevalue[padnum] = minsample[padnum]; // avoid repititive detection
detectionThreshold[padnum] = 999999;
returntouchtype = false;
detectionstate = 0;
tempdetectedinput = input_type;
samplecounter = testsignalsize - 1;
update_basevalueFromNoise(padnum);
//Serial.println((millis() - time_touched));
startWith_interrupt = true;
return tempdetectedinput;
}
else {
if (detectionstate != 0)
{
if (detectionstate == -10 && (millis() - master_time_touched) > singletaptresh)
{
detectionstate = 0;
input_type = 0;
master_time_touched = millis();
}
else if (input_type < 3) { //detection has started
update_basevalueFromNoise(padnum);
/*
SerialUSB.print(detectionstate);
SerialUSB.print("\t");
SerialUSB.print((millis() - time_touched));
SerialUSB.print("\t");*/
return -1;
}
}
update_basevalueFromNoise(padnum);
return 0;
}
}
int AdvCapTouch::detect_touchFromNoise_single(int padnum) { //single tap detection function
}
float AdvCapTouch::read_value(int padnum) {
if (padnum == 0) {
changevalue[padnum] = pad0.capacitiveSensor(speed_normal);
}
else if (padnum == 1) {
changevalue[padnum] = pad1.capacitiveSensor(speed_normal);
}
else if (padnum == 2) {
changevalue[padnum] = pad2.capacitiveSensor(speed_normal);
}
else if (padnum == 3) {
changevalue[padnum] = pad3.capacitiveSensor(speed_normal);
}
return changevalue[padnum];
}
float AdvCapTouch::read_valueFromNoise(int padnum) { //returns only peak value from a small sample of the signal
if (!interruptValue_aquisition || !startWith_interrupt)
{
if (padnum == 0) {
tempread = pad0.capacitiveSensor(speed_noise);
//set the changevalue(damping drastic changes)
diff = prevchangevalue[padnum] - tempread;
if (diff < 0)
{
diff *= -1;
}
if (diff < smallvaluerange)
{
changevalue[padnum] = (tempread * S_alpha2) + (prevchangevalue[padnum] * (1 - S_alpha2));
prevchangevalue[padnum] = changevalue[padnum];
}
else {
changevalue[padnum] = (tempread * S_alpha) + (prevchangevalue[padnum] * (1 - S_alpha));
prevchangevalue[padnum] = changevalue[padnum];
}
if (tempread < (basevalue[padnum] + maxtouchrange))
{
addSample(padnum, tempread);
//standarddeviationtest(changevalue[padnum]);
//getmean();
//getstandarddeviation();
//SerialUSB.println("accepted/////////////////");
}
else {
addSample(padnum, tempread);
//SerialUSB.println("rejected/////////////////");
if (!touchspikedetected)
{
//SerialUSB.print(standarddeviation);
//SerialUSB.print("\t");
//SerialUSB.println("rejected...............");
//delay(50);
touchspikedetected = true;
spikedetecttime = millis();
}
}
}
/*
SerialUSB.print(tempread);
SerialUSB.print("\t");
SerialUSB.print(basevalue[0]);
SerialUSB.print("\t");
SerialUSB.println(changevalue[padnum]);
*/
}
//SerialUSB.print(basevalue[0]);
//SerialUSB.print("\t");
//SerialUSB.println(changevalue[padnum]);
//getmedian();
return changevalue[padnum];
}
void AdvCapTouch::update_basevalueFromNoise(int padnum) { // keeps both the basevalue and the touch thresholds updated
if (detectionstate == 0) {
//getmean();
//getstandarddeviation();
haptics(0);
update_basevalueSmooth(padnum);
resetvalues();
}
}
void AdvCapTouch::update_basevalueSmooth(int padnum) { //Function to gradually update basevalue
//
//tempread = pad0.capacitiveSensor(speed_noise); // save a small sample to the sample array
if (samplecounter == (testsignalsize - 1)) { //find new max and min values
maxsample[padnum] = 0;
minsample[padnum] = 9999999;
for (int i = 0; i < testsignalsize; i++) {
if (samplevalues[i] != 0) { //ignore remaining samples if sample collection is terminated early
if (samplevalues[i] > maxsample[padnum]) {
maxsample[padnum] = samplevalues[i];
}
if (samplevalues[i] < minsample[padnum]) {
minsample[padnum] = samplevalues[i];
}
}
}
//update touch thresholds (adaptive touch sensitivity)
if (adaptsensitivity == true) {
detectionThreshold[padnum] = (maxsample[padnum] - minsample[padnum]);
rejectionThreshold[padnum] = detectionThreshold[padnum];
//minsample[padnum] = maxsample[padnum];
}
basevalue[padnum] = maxsample[padnum];
}
}
void AdvCapTouch::resetvalues() {
if (detectionstate == 0) {
input_type = 0;
}
}
void AdvCapTouch::set_haptics(int pin, int duration, int strength) { //use to set haptics variables (arduino pwm pin, duration of haptics(ms), strength from 0-255)
haptics_pin = pin;
haptics_duration = duration;
haptics_strength = strength;
}
void AdvCapTouch::haptics(int state) { //function for viberation feedback
if (state == 1) {
analogWrite(haptics_pin, haptics_strength);
haptics_ontime = millis();
haptics_state = 1;
}
else if (state == 3) {
analogWrite(haptics_pin, (haptics_strength * 0.7));
}
else {
haptics_offtime = millis();
if (((haptics_offtime - haptics_ontime) >= haptics_duration) && haptics_state == 1) {
analogWrite(haptics_pin, 0);
haptics_state = 0;
}
}
/*
analogWrite(haptics_pin, haptics_strength);
delay(haptics_duration);
analogWrite(haptics_pin, 0);
*/
}
void AdvCapTouch::show_levels(int padnum) {
//read_valueFromNoise(padnum);
//getmean();
//detect_touchFromNoise(padnum);
/*SerialUSB.print(changevalue[padnum]);
SerialUSB.print("\t");
SerialUSB.print(basevalue[padnum]);
SerialUSB.print("\t");
SerialUSB.println(tempread);*/
//checkTouch(0);
/*
SerialUSB.print(mean);
SerialUSB.print("\t");
SerialUSB.print(humpmean);
SerialUSB.print("\t");
SerialUSB.println(changevalue[padnum]);
*/
//SerialUSB.println(pad0.capacitiveSensor(speed_noise));
//delay(5);
}