-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctp.c
893 lines (798 loc) · 24.9 KB
/
ctp.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
/*
* This is a userspace touchscreen driver for cypress ctma300 as used
* in HP Veer configured for WebOS.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* The code was written from scratch, and understanding the
* device output by xndchn @ gmail
* It gets lots of help from CM project in
* https://github.com/CyanogenMod/android_device_hp_tenderloin.git
* and cpoy most codes of calc_point()
*/
#include <stdio.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
int spi_fd = 0;
int int_fd = 0;
int ss_fd = 0;
int ctp_fd = 0;
void enable_ctp()
{
int vdd_fd, xres_fd;
int rc;
__u8 wordbits = 8;
__u32 speedhz = 1600000;
if (!spi_fd)
spi_fd = open("/dev/spidev0.2", O_RDWR);
if (spi_fd <= 0)
printf("TScontrol: Cannot open spi - %d\n", errno);
if (!ss_fd)
ss_fd = open("/sys/user_hw/pins/ctp/ss/level", O_RDWR);
if (ss_fd <= 0)
printf("TScontrol: Cannot open ss - %d\n", errno);
if (!int_fd)
int_fd = open("/sys/user_hw/pins/ctp/int/irqrequest", O_RDWR);
if (int_fd <= 0)
printf("TScontrol: Cannot open int - %d\n", errno);
if (!ctp_fd)
ctp_fd = open("/sys/user_hw/pins/ctp/int/irq", O_RDONLY);
if (ctp_fd <= 0)
printf("TScontrol: Cannot open ctp - %d\n", errno);
usleep(500);
rc = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &wordbits);
usleep(500);
rc = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speedhz);
usleep(50000);
vdd_fd = open("/sys/devices/platform/cy8ctma300/vcpin", O_WRONLY);
if (vdd_fd < 0)
printf("TScontrol: Cannot open vdd - %d\n", errno);
xres_fd = open("/sys/devices/platform/cy8ctma300/xres", O_WRONLY);
if (xres_fd < 0)
printf("TScontrol: Cannot open xres - %d\n", errno);
rc = write(xres_fd, "1", 1);
if (rc != 1)
printf("TSpower, failed set xres\n");
usleep(500);
rc = write(xres_fd, "0", 1);
if (rc != 1)
printf("TSpower, failed reset xres\n");
usleep(50000);
rc = write(int_fd, "1", 1);
if (rc != 1)
printf("TSpower, failed to enable int\n");
rc = write(vdd_fd, "1", 1);
if (rc != 1)
printf("TSpower, failed to enable vdd\n");
rc = write(ss_fd, "1", 1);
if (rc != 1)
printf("TSpower, failed to enable ss\n");
usleep(500);
rc = write(xres_fd, "1", 1);
if (rc != 1)
printf("TSpower, failed set xres\n");
usleep(500);
rc = write(xres_fd, "0", 1);
if (rc != 1)
printf("TSpower, failed reset xres\n");
rc = write(ss_fd, "0", 1);
if (rc != 1)
printf("TSpower, failed to reset ss\n");
}
unsigned char init1[] = {0x01, 0x01, 0x61};
unsigned char init2[] = {0x06, 0x0a, 0x61};
unsigned char init3[] = {0x07, 0x07, 0x61};
unsigned char init4[] = {0x10, 0x0b, 0x61};
unsigned char init5[] = {0x20, 0x06, 0x61};
unsigned char init6[] = {0x03, 0x00, 0x62};
unsigned char init7[] = {0x01, 0x40, 0x51};
unsigned char init8[] = {0x01, 0x20, 0x02};
unsigned char *init_array[] = {init1, init2, init3, init4, init5, init6, init7, init8};
void init_ctp()
{
int rc;
int i;
char buf[32];
for (i = 0; i < 8; i++) {
rc = write(spi_fd, init_array[i], 3);
if (rc != 3)
printf("TSpower, failed to write init%d\n", i + 1);
rc = write(ss_fd, "1", 1);
if (i < 7)
rc = write(ss_fd, "0", 1);
usleep(500);
}
read(ctp_fd, buf, 2);
}
unsigned char rx_buf[132];
#include <math.h>
#define MAX_TOUCH 3 // Max touches that will be reported
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define X_AXIS_POINTS 7
#define Y_AXIS_POINTS 10
#define X_AXIS_MINUS1 X_AXIS_POINTS - 1 // 6
#define Y_AXIS_MINUS1 Y_AXIS_POINTS - 1 // 9
#define X_RESOLUTION 320
#define Y_RESOLUTION 500
#define X_LOCATION_VALUE ((float)X_RESOLUTION) / ((float)X_AXIS_MINUS1)
#define Y_LOCATION_VALUE ((float)Y_RESOLUTION) / ((float)Y_AXIS_MINUS1)
#define X_RESOLUTION_MINUS1 X_RESOLUTION - 1
#define Y_RESOLUTION_MINUS1 Y_RESOLUTION - 1
#define X_SCREEN 320
#define Y_SCREEN 400
#define GESTURE_Y_THRESHOLD 20
#define GESTURE_X_THRESHOLD 40
#define GESTURE_TIME_THRESHOLD 200000
#define GESTURE_LONGTOUCH_TIME_THRESHOLD 325000
#define TOUCH_INITIAL_THRESHOLD 32
int touch_initial_thresh = TOUCH_INITIAL_THRESHOLD;
// Previous touches that have already been reported will continue to be
// reported so long as they stay above this threshold
#define TOUCH_CONTINUE_THRESHOLD 26
int touch_continue_thresh = TOUCH_CONTINUE_THRESHOLD;
#define LARGE_AREA_UNPRESS 22 //TOUCH_CONTINUE_THRESHOLD
#define LARGE_AREA_FRINGE 5 // Threshold for large area fringe
// This is used to help calculate ABS_TOUCH_MAJOR
// This is roughly the value of 500 / 10 or 320 / 7
#define PIXELS_PER_X 45
#define PIXELS_PER_Y 50
struct touchpoint {
// Power or weight of the touch, used for calculating the center point.
int pw;
// These store the average of the locations in the digitizer matrix that
// make up the touch. Used for calculating the center point.
float i;
float j;
// Tracking ID that is assigned to this touch.
int tracking_id;
// Index location of this touch in the previous set of touches.
int prev_loc;
#if MAX_DELTA_FILTER
// Direction and distance between this touch and the previous touch.
float direction;
int distance;
#endif
// Size of the touch area.
int touch_major;
// X and Y locations of the touch. These values may have been changed by a
// filter.
int x;
int y;
// Unfiltered location of the touch.
int unfiltered_x;
int unfiltered_y;
// The highest value found in the digitizer matrix of this touch area.
int highest_val;
// Delay count for touches that do not have a very high highest_val.
int touch_delay;
#if HOVER_DEBOUNCE_FILTER
// Location that we are tracking for hover debounce
int hover_x;
int hover_y;
int hover_delay;
#endif
};
// This array contains the current touches (tpoint), previous touches
// (prevtpoint) and the touches from 2 times ago (prev2tpoint)
struct touchpoint tp[3][MAX_TOUCH];
// These indexes locate the appropriate set of touches in tp
int tpoint, prevtpoint, prev2tpoint;
struct touchpoint start_tp, finish_tp;
// Contains all of the data from the digitizer
unsigned char matrix[X_AXIS_POINTS][Y_AXIS_POINTS];
// Indicates if a point in the digitizer matrix has already been scanned.
int invalid_matrix[X_AXIS_POINTS][Y_AXIS_POINTS];
void determine_area_loc_fringe(float *isum, float *jsum, int *tweight, int i,
int j, int cur_touch_id){
float powered;
// Set fringe point to used for this touch point
invalid_matrix[i][j] = cur_touch_id;
// Track touch values to help determine the pixel x, y location
powered = pow(matrix[i][j], 1.5);
*tweight += powered;
*isum += powered * i;
*jsum += powered * j;
// Check the nearby points to see if they are above LARGE_AREA_FRINGE
// but still decreasing in value to ensure that they are part of the same
// touch and not a nearby, pinching finger.
if (i > 0 && invalid_matrix[i-1][j] != cur_touch_id)
{
if (matrix[i-1][j] >= LARGE_AREA_FRINGE &&
matrix[i-1][j] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j,
cur_touch_id);
}
if (i < X_AXIS_MINUS1 && invalid_matrix[i+1][j] != cur_touch_id)
{
if (matrix[i+1][j] >= LARGE_AREA_FRINGE &&
matrix[i+1][j] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j,
cur_touch_id);
}
if (j > 0 && invalid_matrix[i][j-1] != cur_touch_id) {
if (matrix[i][j-1] >= LARGE_AREA_FRINGE &&
matrix[i][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i, j - 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && invalid_matrix[i][j+1] != cur_touch_id)
{
if (matrix[i][j+1] >= LARGE_AREA_FRINGE &&
matrix[i][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i, j + 1,
cur_touch_id);
}
if (i > 0 && j > 0 && invalid_matrix[i-1][j-1] != cur_touch_id)
{
if (matrix[i-1][j-1] >= LARGE_AREA_FRINGE &&
matrix[i-1][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j - 1,
cur_touch_id);
}
if (i < X_AXIS_MINUS1 && j > 0 && invalid_matrix[i+1][j-1] != cur_touch_id)
{
if (matrix[i+1][j-1] >= LARGE_AREA_FRINGE &&
matrix[i+1][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j - 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && i > 0 && invalid_matrix[i-1][j+1] != cur_touch_id)
{
if (matrix[i-1][j+1] >= LARGE_AREA_FRINGE &&
matrix[i-1][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j + 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && i < X_AXIS_MINUS1 &&
invalid_matrix[i+1][j+1] != cur_touch_id)
{
if (matrix[i+1][j+1] >= LARGE_AREA_FRINGE &&
matrix[i+1][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j + 1,
cur_touch_id);
}
}
void determine_area_loc(float *isum, float *jsum, int *tweight, int i, int j,
int *mini, int *maxi, int *minj, int *maxj, int cur_touch_id,
int *highest_val){
float powered;
// Invalidate this touch point so that we don't process it later
invalid_matrix[i][j] = cur_touch_id;
// Track the size of the touch for TOUCH_MAJOR
if (i < *mini)
*mini = i;
if (i > *maxi)
*maxi = i;
if (j < *minj)
*minj = j;
if (j > *maxj)
*maxj = j;
// Track the highest value of the touch to determine which threshold
// applies.
if (matrix[i][j] > *highest_val)
*highest_val = matrix[i][j];
// Track touch values to help determine the pixel x, y location
powered = pow(matrix[i][j], 1.5);
*tweight += powered;
*isum += powered * i;
*jsum += powered * j;
// Check nearby points to see if they are above LARGE_AREA_UNPRESS
// or if they are above LARGE_AREA_FRINGE but the next nearby point is
// decreasing in value. If the value is not decreasing and below
// LARGE_AREA_UNPRESS then we have 2 fingers pinched close together.
if (i > 0 && invalid_matrix[i-1][j] != cur_touch_id)
{
if (matrix[i-1][j] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i - 1, j, mini, maxi, minj,
maxj, cur_touch_id, highest_val);
else if (matrix[i-1][j] >= LARGE_AREA_FRINGE &&
matrix[i-1][j] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j,
cur_touch_id);
}
if (i < X_AXIS_MINUS1 && invalid_matrix[i+1][j] != cur_touch_id)
{
if (matrix[i+1][j] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i + 1, j, mini, maxi, minj,
maxj, cur_touch_id, highest_val);
else if (matrix[i+1][j] >= LARGE_AREA_FRINGE &&
matrix[i+1][j] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j,
cur_touch_id);
}
if (j > 0 && invalid_matrix[i][j-1] != cur_touch_id)
{
if (matrix[i][j-1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i, j - 1, mini, maxi, minj,
maxj, cur_touch_id, highest_val);
else if (matrix[i][j-1] >= LARGE_AREA_FRINGE &&
matrix[i][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i, j - 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && invalid_matrix[i][j+1] != cur_touch_id)
{
if (matrix[i][j+1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i, j + 1, mini, maxi, minj,
maxj, cur_touch_id, highest_val);
else if (matrix[i][j+1] >= LARGE_AREA_FRINGE &&
matrix[i][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i, j + 1,
cur_touch_id);
}
if (i > 0 && j > 0 && invalid_matrix[i-1][j-1] != cur_touch_id)
{
if (matrix[i-1][j-1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i - 1, j - 1, mini, maxi,
minj, maxj, cur_touch_id, highest_val);
else if (matrix[i-1][j-1] >= LARGE_AREA_FRINGE &&
matrix[i-1][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j - 1,
cur_touch_id);
}
if (i < X_AXIS_MINUS1 && j > 0 && invalid_matrix[i+1][j-1] != cur_touch_id)
{
if (matrix[i+1][j-1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i + 1, j - 1, mini, maxi,
minj, maxj, cur_touch_id, highest_val);
else if (matrix[i+1][j-1] >= LARGE_AREA_FRINGE &&
matrix[i+1][j-1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j - 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && i > 0 && invalid_matrix[i-1][j+1] != cur_touch_id)
{
if (matrix[i-1][j+1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i - 1, j + 1, mini, maxi,
minj, maxj, cur_touch_id, highest_val);
else if (matrix[i-1][j+1] >= LARGE_AREA_FRINGE &&
matrix[i-1][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i - 1, j + 1,
cur_touch_id);
}
if (j < Y_AXIS_MINUS1 && i < X_AXIS_MINUS1 &&
invalid_matrix[i+1][j+1] != cur_touch_id)
{
if (matrix[i+1][j+1] >= LARGE_AREA_UNPRESS)
determine_area_loc(isum, jsum, tweight, i + 1, j + 1, mini, maxi,
minj, maxj, cur_touch_id, highest_val);
else if (matrix[i+1][j+1] >= LARGE_AREA_FRINGE &&
matrix[i+1][j+1] < matrix[i][j])
determine_area_loc_fringe(isum, jsum, tweight, i + 1, j + 1,
cur_touch_id);
}
}
#include <linux/input.h>
#include <linux/uinput.h>
#define UINPUT_LOCATION "/dev/uinput"
//#define EVENT_DEBUG 1
int uinput_fd;
int send_uevent(int fd, __u16 type, __u16 code, __s32 value)
{
struct input_event event;
#if EVENT_DEBUG
char ctype[20], ccode[20];
switch (type) {
case EV_ABS:
strcpy(ctype, "EV_ABS");
break;
case EV_KEY:
strcpy(ctype, "EV_KEY");
break;
case EV_SYN:
strcpy(ctype, "EV_SYN");
break;
}
switch (code) {
case ABS_MT_SLOT:
strcpy(ccode, "ABS_MT_SLOT");
break;
case ABS_MT_TRACKING_ID:
strcpy(ccode, "ABS_MT_TRACKING_ID");
break;
case ABS_MT_TOUCH_MAJOR:
strcpy(ccode, "ABS_MT_TOUCH_MAJOR");
break;
case ABS_MT_POSITION_X:
strcpy(ccode, "ABS_MT_POSITION_X");
break;
case ABS_MT_POSITION_Y:
strcpy(ccode, "ABS_MT_POSITION_Y");
break;
case SYN_MT_REPORT:
strcpy(ccode, "SYN_MT_REPORT");
break;
case SYN_REPORT:
strcpy(ccode, "SYN_REPORT");
break;
case BTN_TOUCH:
strcpy(ccode, "BTN_TOUCH");
break;
}
printf("event type: '%s' code: '%s' value: %i \n", ctype, ccode, value);
#endif
memset(&event, 0, sizeof(event));
event.type = type;
event.code = code;
event.value = value;
if (write(fd, &event, sizeof(event)) != sizeof(event)) {
fprintf(stderr, "Error on send_event %d", sizeof(event));
return -1;
}
return 0;
}
int first_touch = 1;
int is_gesture = 0;
enum Gesture {
NONE, UP, LEFT, RIGHT, DOUBLE_CLICK, LONG_TOUCH, LONG_OFF
} gesture, last_gesture;
struct timeval last_touch;
int need_liftoff = 0;
inline long diff_time(struct timeval a, struct timeval b)
{
return (a.tv_sec - b.tv_sec) * 1000000 + (a.tv_usec - b.tv_usec);
}
int calc_point(void)
{
int i, j, k;
int tweight = 0;
int tpc = 0;
float isum = 0, jsum = 0;
float avgi, avgj;
static int previoustpc, tracking_id = 0;
#if DEBOUNCE_FILTER
int new_debounce_touch = 0;
static int initialx, initialy;
#endif
struct timeval now;
gettimeofday(&now, NULL);
if (tp[tpoint][0].x < -20) {
// We had a total liftoff
previoustpc = 0;
#if DEBOUNCE_FILTER
new_debounce_touch = 1;
#endif
} else {
// Re-assign array indexes
prev2tpoint = prevtpoint;
prevtpoint = tpoint;
tpoint++;
if (tpoint > 2)
tpoint = 0;
}
// Scan the digitizer data and generate a list of touches
memset(&invalid_matrix, 0, sizeof(invalid_matrix));
for(i=0; i < X_AXIS_POINTS; i++) {
for(j=0; j < Y_AXIS_POINTS; j++) {
#if RAW_DATA_DEBUG
if (matrix[i][j] < RAW_DATA_THRESHOLD)
printf(" ");
else
printf("%2.2X ", matrix[i][j]);
#endif
if (tpc < MAX_TOUCH && matrix[i][j] > touch_continue_thresh &&
!invalid_matrix[i][j]) {
isum = 0;
jsum = 0;
tweight = 0;
int mini = i, maxi = i, minj = j, maxj = j;
int highest_val = matrix[i][j];
determine_area_loc(&isum, &jsum, &tweight, i, j, &mini,
&maxi, &minj, &maxj, tpc + 1, &highest_val);
avgi = isum / (float)tweight;
avgj = jsum / (float)tweight;
maxi = maxi - mini;
maxj = maxj - minj;
tp[tpoint][tpc].pw = tweight;
tp[tpoint][tpc].i = avgi;
tp[tpoint][tpc].j = avgj;
tp[tpoint][tpc].touch_major = MAX(maxi * PIXELS_PER_X, maxj * PIXELS_PER_Y);
tp[tpoint][tpc].tracking_id = -1;
tp[tpoint][tpc].x = tp[tpoint][tpc].i * X_LOCATION_VALUE;
tp[tpoint][tpc].y = tp[tpoint][tpc].j * Y_LOCATION_VALUE;
if (tp[tpoint][tpc].x < 0)
tp[tpoint][tpc].x = 0;
if (tp[tpoint][tpc].x > X_SCREEN)
tp[tpoint][tpc].x = X_SCREEN;
if (tp[tpoint][tpc].y < 0)
tp[tpoint][tpc].y = 0;
if (tp[tpoint][tpc].y > Y_SCREEN &&
tp[tpoint][tpc].y < Y_SCREEN + GESTURE_Y_THRESHOLD)
tp[tpoint][tpc].y = Y_SCREEN;
tp[tpoint][tpc].unfiltered_x = tp[tpoint][tpc].x;
tp[tpoint][tpc].unfiltered_y = tp[tpoint][tpc].y;
tp[tpoint][tpc].highest_val = highest_val;
tp[tpoint][tpc].touch_delay = 0;
#if HOVER_DEBOUNCE_FILTER
tp[tpoint][tpc].hover_x = tp[tpoint][tpc].x;
tp[tpoint][tpc].hover_y = tp[tpoint][tpc].y;
tp[tpoint][tpc].hover_delay = HOVER_DEBOUNCE_DELAY;
#endif
//printf("touch %d, %d\n", tp[tpoint][tpc].x, tp[tpoint][tpc].y);
tpc++;
}
}
}
for (k = 0; k < tpc; k++) {
if (tp[tpoint][k].highest_val && !tp[tpoint][k].touch_delay) {
#if EVENT_DEBUG
printf("send event for tracking ID: %i\n",
tp[tpoint][k].tracking_id);
#endif
if (first_touch) {
first_touch = 0;
gettimeofday(&last_touch, NULL);
start_tp = tp[tpoint][k];
if (start_tp.y >= Y_SCREEN + GESTURE_Y_THRESHOLD) {
is_gesture = 1;
}
} else {
finish_tp = tp[tpoint][k];
}
if (is_gesture) {
if (start_tp.x - finish_tp.x < GESTURE_X_THRESHOLD &&
finish_tp.x - start_tp.x < GESTURE_X_THRESHOLD)
if (diff_time(now, last_touch) > GESTURE_LONGTOUCH_TIME_THRESHOLD) {
gesture = LONG_TOUCH;
need_liftoff = 1;
previoustpc = tpc;
return 0;
}
continue;
}
send_uevent(uinput_fd, EV_ABS, ABS_MT_POSITION_X, tp[tpoint][k].x);
send_uevent(uinput_fd, EV_ABS, ABS_MT_POSITION_Y, tp[tpoint][k].y);
send_uevent(uinput_fd, EV_KEY, BTN_TOUCH, 1);
send_uevent(uinput_fd, EV_SYN, SYN_MT_REPORT, 0);
} else if (tp[tpoint][k].touch_delay) {
// This touch didn't meet the threshold so we don't report it yet
tp[tpoint][k].touch_delay--;
}
}
if (tpc > 0) {
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
}
previoustpc = tpc;
return tpc;
}
void clear_arrays(void)
{
// Clears array (for after a total liftoff occurs)
int i, j;
for (i=0; i<3; i++) {
for(j=0; j<MAX_TOUCH; j++) {
tp[i][j].pw = -1000;
tp[i][j].i = -1000;
tp[i][j].j = -1000;
tp[i][j].tracking_id = -1;
tp[i][j].prev_loc = -1;
#if MAX_DELTA_FILTER
tp[i][j].direction = 0;
tp[i][j].distance = 0;
#endif
tp[i][j].touch_major = 0;
tp[i][j].x = -1000;
tp[i][j].y = -1000;
tp[i][j].unfiltered_x = -1000;
tp[i][j].unfiltered_y = -1000;
tp[i][j].highest_val = -1000;
tp[i][j].touch_delay = -1000;
#if HOVER_DEBOUNCE_FILTER
tp[i][j].hover_x = -1000;
tp[i][j].hover_y = -1000;
tp[i][j].hover_delay = HOVER_DEBOUNCE_DELAY;
#endif
}
}
}
struct timeval last_liftoff;
void liftoff(void)
{
// Sends liftoff events - nothing is touching the screen
#if EVENT_DEBUG
printf("liftoff function\n");
#endif
struct timeval now;
gettimeofday(&now, NULL);
//send_uevent(uinput_fd, EV_KEY, BTN_TOUCH, 0);
//send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
//send_uevent(uinput_fd, EV_ABS, ABS_MT_TOUCH_MAJOR,0);
//send_uevent(uinput_fd, EV_ABS, ABS_MT_PRESSURE, 0);
send_uevent(uinput_fd, EV_KEY, BTN_TOUCH, 0);
send_uevent(uinput_fd, EV_SYN, SYN_MT_REPORT, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
first_touch = 1;
if (is_gesture) {
is_gesture = 0;
if (gesture == NONE) {
if (finish_tp.y <= Y_SCREEN) {
gesture = UP;
} else {
if (start_tp.x - finish_tp.x >= GESTURE_X_THRESHOLD) {
gesture = LEFT;
} else if (finish_tp.x - start_tp.x >= GESTURE_X_THRESHOLD) {
gesture = RIGHT;
} else if (diff_time(now, last_liftoff) < GESTURE_TIME_THRESHOLD) {
gesture = DOUBLE_CLICK;
}
}
if (last_gesture == LONG_TOUCH)
gesture = LONG_OFF;
gettimeofday(&last_liftoff, NULL);
}
switch (gesture) {
case UP:
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 1);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
break;
case LEFT:
send_uevent(uinput_fd, EV_KEY, KEY_BACK, 1);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
send_uevent(uinput_fd, EV_KEY, KEY_BACK, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
break;
case RIGHT:
send_uevent(uinput_fd, EV_KEY, KEY_MENU, 1);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
send_uevent(uinput_fd, EV_KEY, KEY_MENU, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
break;
case DOUBLE_CLICK:
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 1);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
break;
case LONG_TOUCH:
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 1);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
break;
case LONG_OFF:
send_uevent(uinput_fd, EV_KEY, KEY_HOME, 0);
send_uevent(uinput_fd, EV_SYN, SYN_REPORT, 0);
default:
break;
}
last_gesture = gesture;
gesture = NONE;
}
}
#define LIFTOFF_TIMEOUT 25000
void irq_message()
{
int ret;
fd_set rdfds;
struct timeval tv;
char buf[32];
int i;
int x, y;
struct spi_ioc_transfer ioc;
FD_ZERO(&rdfds);
FD_SET(ctp_fd, &rdfds);
ioc.tx_buf = NULL;
ioc.rx_buf = rx_buf;
ioc.len = 132;
ioc.speed_hz = 1600000;
ioc.delay_usecs = 0;
ioc.bits_per_word = 8;
ioc.cs_change = 0;
ioc.pad = 0;
while (1) {
FD_ZERO(&rdfds);
FD_SET(ctp_fd, &rdfds);
tv.tv_sec = 0;
tv.tv_usec = LIFTOFF_TIMEOUT;
ret = select(ctp_fd + 1, &rdfds, NULL, NULL, &tv);
if(ret < 0)
perror("select");
else if(ret == 0) {
if (need_liftoff) {
//#if EVENT_DEBUG
//printf("timeout called liftoff\n");
//#endif
liftoff();
clear_arrays();
need_liftoff = 0;
}
}
else {
if(FD_ISSET(ctp_fd, &rdfds)) {
read(ctp_fd, buf, 2);
ret = write(ss_fd, "0", 1);
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &ioc);
ret = write(ss_fd, "1", 1);
for (y = 0; y < 10 ; y++) {
for (x = 0; x < 7 ; x++) {
matrix[x][y] = rx_buf[x * 10 + y + 2];
}
}
if (!calc_point()) {
// Sometimes there's data but no valid touches due to threshold
if (need_liftoff) {
liftoff();
clear_arrays();
need_liftoff = 0;
}
} else {
need_liftoff = 1;
}
}
}
}
}
void init_uinput()
{
struct uinput_user_dev device;
memset(&device, 0, sizeof(device));
uinput_fd=open(UINPUT_LOCATION, O_WRONLY);
strcpy(device.name,"HPVeer");
device.id.bustype = BUS_VIRTUAL;
device.id.vendor = 1;
device.id.product = 1;
device.id.version = 1;
//device.absmax[ABS_MT_PRESSURE] = 255;
//device.absmin[ABS_MT_PRESSURE] = 0;
//device.absmax[ABS_MT_TOUCH_MAJOR] = 255;
//device.absmin[ABS_MT_TOUCH_MAJOR] = 0;
device.absmax[ABS_MT_POSITION_X] = X_SCREEN;
device.absmax[ABS_MT_POSITION_Y] = Y_SCREEN;
device.absmin[ABS_MT_POSITION_X] = 0;
device.absmin[ABS_MT_POSITION_Y] = 0;
device.absfuzz[ABS_MT_POSITION_X] = 2;
device.absflat[ABS_MT_POSITION_X] = 0;
device.absfuzz[ABS_MT_POSITION_Y] = 1;
device.absflat[ABS_MT_POSITION_Y] = 0;
if (write(uinput_fd,&device,sizeof(device)) != sizeof(device))
fprintf(stderr, "error setup\n");
if (ioctl(uinput_fd,UI_SET_EVBIT, EV_KEY) < 0)
fprintf(stderr, "error evbit key\n");
if (ioctl(uinput_fd,UI_SET_EVBIT,EV_ABS) < 0)
fprintf(stderr, "error evbit rel\n");
if (ioctl(uinput_fd,UI_SET_KEYBIT, BTN_TOUCH) < 0)
fprintf(stderr, "error keybit key\n");
if (ioctl(uinput_fd,UI_SET_KEYBIT, KEY_BACK) < 0)
fprintf(stderr, "error keybit key\n");
if (ioctl(uinput_fd,UI_SET_KEYBIT, KEY_MENU) < 0)
fprintf(stderr, "error keybit key\n");
if (ioctl(uinput_fd,UI_SET_KEYBIT, KEY_HOME) < 0)
fprintf(stderr, "error keybit key\n");
if (ioctl(uinput_fd,UI_SET_EVBIT, EV_SYN) < 0)
fprintf(stderr, "error evbit key\n");
//if (ioctl(uinput_fd,UI_SET_ABSBIT,ABS_MT_TOUCH_MAJOR) < 0)
// fprintf(stderr, "error tool rel\n");
if (ioctl(uinput_fd,UI_SET_ABSBIT,ABS_MT_POSITION_X) < 0)
fprintf(stderr, "error tool rel\n");
if (ioctl(uinput_fd,UI_SET_ABSBIT,ABS_MT_POSITION_Y) < 0)
fprintf(stderr, "error tool rel\n");
if (ioctl(uinput_fd,UI_SET_ABSBIT,ABS_MT_PRESSURE) < 0)
fprintf(stderr, "error tool rel\n");
if (ioctl(uinput_fd,UI_DEV_CREATE) < 0)
fprintf(stderr, "error create\n");
}
int main()
{
init_uinput();
enable_ctp();
usleep(10000);
init_ctp();
liftoff();
irq_message();
return 0;
}