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
|
/**************************************************************************//**
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-04-20 Wayne First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(NU_PKG_USING_ADC_TOUCH)
#include "NuMicro.h"
#include <rtdevice.h>
#include <dfs_file.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/statfs.h>
//#include "drv_adc.h"
#include "adc_touch.h"
#if !defined(PATH_CALIBRATION_FILE)
#define PATH_CALIBRATION_FILE "/mnt/filesystem/ts_calibration"
#endif
rt_err_t nu_adc_touch_disable(void);
rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch);
void nu_adc_touch_detect(rt_bool_t bStartDetect);
int32_t nu_adc_touch_read_xyz(uint32_t *bufX, uint32_t *bufY, uint32_t *bufZ0, uint32_t *bufZ1, int32_t dataCnt);
typedef struct
{
struct rt_touch_device dev;
rt_uint32_t x_range;
rt_uint32_t y_range;
} nu_adc_touch;
typedef nu_adc_touch *nu_adc_touch_t;
static nu_adc_touch s_NuAdcTouch = {0};
/* User can define ADC touch calibration matrix in board_dev.c. */
rt_weak S_CALIBRATION_MATRIX g_sCalMat = { 1, 0, 0, 0, 1, 0, 1 };
static volatile uint32_t g_u32Calibrated = 0;
static int nu_adc_touch_readfile(void);
static const S_CALIBRATION_MATRIX g_sCalZero = { 1, 0, 0, 0, 1, 0, 1 };
static int nu_adc_cal_mat_get(const S_COORDINATE_POINT *psDispCP, S_COORDINATE_POINT *psADCCP, S_CALIBRATION_MATRIX *psCM)
{
#if (DEF_CAL_POINT_NUM==3)
psCM->div = ((psADCCP[0].x - psADCCP[2].x) * (psADCCP[1].y - psADCCP[2].y)) -
((psADCCP[1].x - psADCCP[2].x) * (psADCCP[0].y - psADCCP[2].y)) ;
if (psCM->div == 0)
{
return -1;
}
else
{
psCM->a = ((psDispCP[0].x - psDispCP[2].x) * (psADCCP[1].y - psADCCP[2].y)) -
((psDispCP[1].x - psDispCP[2].x) * (psADCCP[0].y - psADCCP[2].y)) ;
psCM->b = ((psADCCP[0].x - psADCCP[2].x) * (psDispCP[1].x - psDispCP[2].x)) -
((psDispCP[0].x - psDispCP[2].x) * (psADCCP[1].x - psADCCP[2].x)) ;
psCM->c = (psADCCP[2].x * psDispCP[1].x - psADCCP[1].x * psDispCP[2].x) * psADCCP[0].y +
(psADCCP[0].x * psDispCP[2].x - psADCCP[2].x * psDispCP[0].x) * psADCCP[1].y +
(psADCCP[1].x * psDispCP[0].x - psADCCP[0].x * psDispCP[1].x) * psADCCP[2].y ;
psCM->d = ((psDispCP[0].y - psDispCP[2].y) * (psADCCP[1].y - psADCCP[2].y)) -
((psDispCP[1].y - psDispCP[2].y) * (psADCCP[0].y - psADCCP[2].y)) ;
psCM->e = ((psADCCP[0].x - psADCCP[2].x) * (psDispCP[1].y - psDispCP[2].y)) -
((psDispCP[0].y - psDispCP[2].y) * (psADCCP[1].x - psADCCP[2].x)) ;
psCM->f = (psADCCP[2].x * psDispCP[1].y - psADCCP[1].x * psDispCP[2].y) * psADCCP[0].y +
(psADCCP[0].x * psDispCP[2].y - psADCCP[2].x * psDispCP[0].y) * psADCCP[1].y +
(psADCCP[1].x * psDispCP[0].y - psADCCP[0].x * psDispCP[1].y) * psADCCP[2].y ;
}
#elif (DEF_CAL_POINT_NUM==5)
int i;
float n, x, y, xx, yy, xy, z, zx, zy;
float a, b, c, d, e, f, g;
float scaling = 65536.0f;
n = x = y = xx = yy = xy = 0;
for (i = 0; i < DEF_CAL_POINT_NUM; i++)
{
n += (float)1.0;
x += (float)psADCCP[i].x;
y += (float)psADCCP[i].y;
xx += (float)psADCCP[i].x * psADCCP[i].x;
yy += (float)psADCCP[i].y * psADCCP[i].y;
xy += (float)psADCCP[i].x * psADCCP[i].y;
}
d = n * (xx * yy - xy * xy) + x * (xy * y - x * yy) + y * (x * xy - y * xx);
if (d < (float)0.1 && d > (float) -0.1)
{
return -1;
}
a = (xx * yy - xy * xy) / d;
b = (xy * y - x * yy) / d;
c = (x * xy - y * xx) / d;
e = (n * yy - y * y) / d;
f = (x * y - n * xy) / d;
g = (n * xx - x * x) / d;
z = zx = zy = 0;
for (i = 0; i < DEF_CAL_POINT_NUM; i++)
{
z += (float)psDispCP[i].x;
zx += (float)psDispCP[i].x * psADCCP[i].x;
zy += (float)psDispCP[i].x * psADCCP[i].y;
}
psCM->c = (int32_t)((a * z + b * zx + c * zy) * scaling);
psCM->a = (int32_t)((b * z + e * zx + f * zy) * scaling);
psCM->b = (int32_t)((c * z + f * zx + g * zy) * scaling);
z = zx = zy = 0;
for (i = 0; i < DEF_CAL_POINT_NUM; i++)
{
z += (float)psDispCP[i].y;
zx += (float)psDispCP[i].y * psADCCP[i].x;
zy += (float)psDispCP[i].y * psADCCP[i].y;
}
psCM->f = (int32_t)((a * z + b * zx + c * zy) * scaling);
psCM->d = (int32_t)((b * z + e * zx + f * zy) * scaling);
psCM->e = (int32_t)((c * z + f * zx + g * zy) * scaling);
psCM->div = (int32_t)scaling;
#else
#error "Not supported calibration method"
#endif
return 0;
}
static void nu_adc_touch_cal(int32_t *sumx, int32_t *sumy)
{
int32_t xtemp, ytemp;
xtemp = *sumx;
ytemp = *sumy;
*sumx = (g_sCalMat.c +
g_sCalMat.a * xtemp +
g_sCalMat.b * ytemp) / g_sCalMat.div;
*sumy = (g_sCalMat.f +
g_sCalMat.d * xtemp +
g_sCalMat.e * ytemp) / g_sCalMat.div;
}
static rt_size_t nu_adc_touch_readpoint(struct rt_touch_device *device, void *buf, rt_size_t read_num)
{
static uint32_t last_report_x = 0, last_report_y = 0;
struct rt_touch_data *pPoint = (struct rt_touch_data *)buf;
nu_adc_touch_t psNuAdcTouch = (nu_adc_touch_t)device;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(buf != RT_NULL);
int i;
for (i = 0; i < read_num; i++)
{
uint32_t bufZ0 = 0, bufZ1 = 0;
int32_t sumx = 0, sumy = 0;
pPoint[i].timestamp = rt_touch_get_ts();
pPoint[i].track_id = 0;
if (nu_adc_touch_read_xyz((uint32_t *)&sumx, (uint32_t *)&sumy, &bufZ0, &bufZ1, 1) != 1)
break;
if (bufZ0 == 0)
{
/* Workaround: In this case, x, y values are unstable. so, report last point's coordinate.*/
pPoint[i].event = RT_TOUCH_EVENT_UP;
pPoint[i].x_coordinate = (uint16_t)last_report_x;
pPoint[i].y_coordinate = (uint16_t)last_report_y;
}
else
{
if (g_u32Calibrated)
{
nu_adc_touch_cal(&sumx, &sumy);
}
last_report_x = sumx;
last_report_y = sumy;
pPoint[i].event = RT_TOUCH_EVENT_DOWN;
pPoint[i].x_coordinate = (uint16_t)sumx;
pPoint[i].y_coordinate = (uint16_t)sumy;
}
if (g_u32Calibrated)
{
bufZ0 = bufZ0 >> 3;
pPoint[i].width = (bufZ0 > 255) ? 255 : bufZ0;
//Limit max x, y coordinate if value is over its range.
pPoint[i].x_coordinate = (pPoint[i].x_coordinate > psNuAdcTouch->x_range) ? psNuAdcTouch->x_range : pPoint[i].x_coordinate;
pPoint[i].y_coordinate = (pPoint[i].y_coordinate > psNuAdcTouch->y_range) ? psNuAdcTouch->y_range : pPoint[i].y_coordinate;
}
}
return (rt_size_t)i;
}
static rt_err_t nu_adc_touch_control(struct rt_touch_device *device, int cmd, void *data)
{
nu_adc_touch_t psNuAdcTouch = (nu_adc_touch_t)device;
RT_ASSERT(psNuAdcTouch != RT_NULL);
switch (cmd)
{
case RT_TOUCH_CTRL_SET_X_RANGE: /* set x range */
psNuAdcTouch->x_range = *((rt_int32_t *)data);
break;
case RT_TOUCH_CTRL_SET_Y_RANGE: /* set y range */
psNuAdcTouch->y_range = *((rt_int32_t *)data);
break;
case RT_TOUCH_CTRL_ENABLE_INT: /* enable pen_down interrupt */
nu_adc_touch_detect(RT_TRUE);
break;
case RT_TOUCH_CTRL_DISABLE_INT: /* disable pen_down interrupt */
nu_adc_touch_detect(RT_FALSE);
break;
case RT_TOUCH_CTRL_POWER_ON: /* Touch Power On */
return nu_adc_touch_enable(device);
case RT_TOUCH_CTRL_POWER_OFF: /* Touch Power Off */
return nu_adc_touch_disable();
default:
return -RT_ERROR;
}
return RT_EOK;
}
static struct rt_touch_ops touch_ops =
{
.touch_readpoint = nu_adc_touch_readpoint,
.touch_control = nu_adc_touch_control,
};
static void nu_adc_touch_update_calmat(S_CALIBRATION_MATRIX *psNewCalMat)
{
if (psNewCalMat &&
psNewCalMat->div != 0)
{
rt_memcpy(&g_sCalMat, psNewCalMat, sizeof(S_CALIBRATION_MATRIX));
g_u32Calibrated = 1;
rt_kprintf("Applied calibration data: %d, %d, %d, %d, %d, %d, %d\n",
g_sCalMat.a,
g_sCalMat.b,
g_sCalMat.c,
g_sCalMat.d,
g_sCalMat.e,
g_sCalMat.f,
g_sCalMat.div);
}
}
static void nu_adc_touch_reset_calmat(void)
{
rt_memcpy(&g_sCalMat, &g_sCalZero, sizeof(S_CALIBRATION_MATRIX));
g_u32Calibrated = 0;
}
int rt_hw_adc_touch_init(void)
{
/* Register touch device */
s_NuAdcTouch.dev.info.type = RT_TOUCH_TYPE_RESISTANCE;
s_NuAdcTouch.dev.info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
s_NuAdcTouch.dev.info.point_num = 1;
s_NuAdcTouch.dev.info.range_x = BSP_LCD_WIDTH;
s_NuAdcTouch.dev.info.range_y = BSP_LCD_HEIGHT;
s_NuAdcTouch.dev.ops = &touch_ops;
return (int)rt_hw_touch_register(&s_NuAdcTouch.dev, "adc_touch", RT_DEVICE_FLAG_INT_RX, RT_NULL);
}
INIT_DEVICE_EXPORT(rt_hw_adc_touch_init);
static rt_thread_t adc_touch_thread = RT_NULL;
static rt_sem_t adc_touch_sem = RT_NULL;
static int adc_touch_worker_run = 0;
static rt_err_t adc_touch_rx_callback(rt_device_t dev, rt_size_t size)
{
//rt_kprintf("[%s %d] %d\n", __func__, __LINE__, size);
return rt_sem_release(adc_touch_sem);
}
static rt_err_t adc_request_point(rt_device_t pdev, struct rt_touch_data *psTouchPoint)
{
rt_err_t ret = -RT_ERROR;
if ((ret = rt_sem_take(adc_touch_sem, rt_tick_from_millisecond(500))) == RT_EOK)
{
rt_memset(psTouchPoint, 0, sizeof(struct rt_touch_data));
if (rt_device_read(pdev, 0, psTouchPoint, s_NuAdcTouch.dev.info.point_num) == s_NuAdcTouch.dev.info.point_num)
{
ret = RT_EOK;
}
}
return ret;
}
rt_weak void nu_touch_inputevent_cb(rt_int16_t x, rt_int16_t y, rt_uint8_t event)
{
}
static rt_device_t lcd_device = 0;
static struct rt_device_graphic_info info;
static void lcd_cleanscreen(void)
{
if (info.framebuffer != RT_NULL)
{
if (rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, (void *)info.framebuffer) == RT_EOK)
{
/* Sync-type LCD panel, will fill to VRAM directly. */
rt_memset(info.framebuffer, 0, (info.pitch * info.height));
}
else
{
/* MPU-type LCD panel, fill to shadow RAM, then flush. */
struct rt_device_rect_info rectinfo;
int filled_line_num = 0;
int i32LineBufNum = info.smem_len / info.pitch;
int i32RemainLineNum = info.height;
i32LineBufNum = (i32LineBufNum > info.height) ? info.height : i32LineBufNum;
while (i32RemainLineNum > 0)
{
int pixel_count;
rectinfo.x = 0;
rectinfo.y = filled_line_num;
rectinfo.width = info.width;
rectinfo.height = (i32RemainLineNum > i32LineBufNum) ? i32LineBufNum : i32RemainLineNum ;
pixel_count = info.width * rectinfo.height;
rt_uint16_t *pu16ShadowBuf = (rt_uint16_t *)info.framebuffer;
while (pixel_count > 0)
{
*pu16ShadowBuf++ = 0;
pixel_count--;
}
rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rectinfo);
filled_line_num += i32LineBufNum;
i32RemainLineNum -= rectinfo.height;
}
}
}
else
{
// TODO
}
}
#define DEF_DOT_NUMBER 9
#define DOTS_NUMBER (DEF_DOT_NUMBER*DEF_DOT_NUMBER)
static void nu_draw_bots(int x, int y)
{
if (info.framebuffer != RT_NULL)
{
/* Rendering */
struct rt_device_rect_info rect;
int i, j;
int start_x = x - (DEF_DOT_NUMBER / 2);
int start_y = y - (DEF_DOT_NUMBER / 2);
rt_bool_t bDrawDirect;
if (rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, (void *)info.framebuffer) == RT_EOK)
{
/* Sync-type LCD panel, will draw to VRAM directly. */
bDrawDirect = RT_TRUE;
}
else
{
/* MPU-type LCD panel, draw to shadow RAM, then flush. */
bDrawDirect = RT_FALSE;
}
if (info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
{
uint16_t *pu16Start = (bDrawDirect == RT_TRUE) ? (uint16_t *)((uint32_t)info.framebuffer + (start_y) * info.pitch + (start_x * 2)) : (uint16_t *)info.framebuffer;
for (i = 0; i < DEF_DOT_NUMBER; i++)
{
for (j = 0; j < DEF_DOT_NUMBER; j++)
{
*pu16Start = 0x07E0; //Green, RGB565
pu16Start++;
}
if (bDrawDirect)
pu16Start += (info.width - DEF_DOT_NUMBER);
}
}
else if (info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
{
uint32_t *pu32Start = (bDrawDirect == RT_TRUE) ? (uint32_t *)((uint32_t)info.framebuffer + (start_y) * info.pitch + (start_x * 4)) : (uint32_t *)info.framebuffer;
for (i = 0; i < DEF_DOT_NUMBER; i++)
{
for (j = 0; j < DEF_DOT_NUMBER; j++)
{
*pu32Start = 0xff00ff00; //Green, ARGB888
pu32Start++;
}
if (bDrawDirect)
pu32Start += (info.width - DEF_DOT_NUMBER);
}
}
else
{
//Not supported
return;
}
if (!bDrawDirect)
{
/* Region updating */
rect.x = start_x;
rect.y = start_y;
rect.width = DEF_DOT_NUMBER;
rect.height = DEF_DOT_NUMBER;
rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
}
}
else
{
// TODO
}
return;
}
#if (DEF_CAL_POINT_NUM==3)
const S_COORDINATE_POINT sDispPoints[DEF_CAL_POINT_NUM] =
{
{BSP_LCD_WIDTH / 4, BSP_LCD_HEIGHT / 2},
{BSP_LCD_WIDTH - BSP_LCD_WIDTH / 4, BSP_LCD_HEIGHT / 4},
{BSP_LCD_WIDTH / 2, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / 4}
};
#elif (DEF_CAL_POINT_NUM==5)
const static S_COORDINATE_POINT sDispPoints[DEF_CAL_POINT_NUM] =
{
#define DEF_CUT_PIECES 8
{BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT / DEF_CUT_PIECES},
{BSP_LCD_WIDTH - BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT / DEF_CUT_PIECES},
{BSP_LCD_WIDTH - BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / DEF_CUT_PIECES},
{BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / DEF_CUT_PIECES},
{BSP_LCD_WIDTH / 2, BSP_LCD_HEIGHT / 2}
};
#endif
static int nu_adc_touch_readfile(void)
{
int fd;
S_CALIBRATION_MATRIX sCalMat;
if ((fd = open(PATH_CALIBRATION_FILE, O_RDONLY, 0)) < 0)
{
goto exit_nu_adc_touch_readfile;
}
else if (read(fd, &sCalMat, sizeof(S_CALIBRATION_MATRIX)) == sizeof(S_CALIBRATION_MATRIX))
{
rt_kprintf("[%s] %s\n", __func__, PATH_CALIBRATION_FILE);
}
close(fd);
nu_adc_touch_update_calmat(&sCalMat);
return 0;
exit_nu_adc_touch_readfile:
return -1;
}
static int nu_adc_touch_writefile(void *buf, int buf_len)
{
int fd;
if ((fd = open(PATH_CALIBRATION_FILE, O_WRONLY | O_CREAT, 0)) < 0)
{
goto exit_nu_adc_touch_writefile;
}
else if (write(fd, buf, buf_len) == buf_len)
{
rt_kprintf("[%s] %s\n", __func__, PATH_CALIBRATION_FILE);
}
close(fd);
return 0;
exit_nu_adc_touch_writefile:
return -1;
}
static void nu_touch_do_calibration(rt_device_t pdev)
{
int i;
rt_err_t result;
S_CALIBRATION_MATRIX sCalMat;
S_COORDINATE_POINT sADCPoints[DEF_CAL_POINT_NUM];
lcd_device = rt_device_find("lcd");
if (!lcd_device)
{
rt_kprintf("Not supported graphics ops\n");
return;
}
result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
if (result != RT_EOK)
{
rt_kprintf("error!");
return;
}
result = rt_device_open(lcd_device, 0);
if (result != RT_EOK)
{
rt_kprintf("opened?");
}
rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, info.framebuffer);
rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
for (i = 0; i < DEF_CAL_POINT_NUM; i++)
{
struct rt_touch_data sTouchPoint;
int count = 0;
lcd_cleanscreen();
/* Drain RX queue before doing calibrate. */
while (adc_request_point(pdev, &sTouchPoint) == RT_EOK);
rt_thread_mdelay(100);
/* Ready to calibrate */
nu_draw_bots(sDispPoints[i].x, sDispPoints[i].y);
#define DEF_MAX_GET_POINT_NUM 5
sADCPoints[i].x = 0;
sADCPoints[i].y = 0;
while (count < DEF_MAX_GET_POINT_NUM)
{
if (adc_request_point(pdev, &sTouchPoint) == RT_EOK)
{
sADCPoints[i].x += (int32_t)sTouchPoint.x_coordinate;
sADCPoints[i].y += (int32_t)sTouchPoint.y_coordinate;
rt_kprintf("[%d %d] - Disp:[%d, %d] -> ADC:[%d, %d]\n", i, count, sDispPoints[i].x, sDispPoints[i].y, sADCPoints[i].x, sADCPoints[i].y);
count++;
}
}
sADCPoints[i].x = (int32_t)((float)sADCPoints[i].x / DEF_MAX_GET_POINT_NUM);
sADCPoints[i].y = (int32_t)((float)sADCPoints[i].y / DEF_MAX_GET_POINT_NUM);
rt_kprintf("[%d] - Disp:[%d, %d], ADC:[%d, %d]\n", i, sDispPoints[i].x, sDispPoints[i].y, sADCPoints[i].x, sADCPoints[i].y);
rt_thread_mdelay(300);
}
lcd_cleanscreen();
/* Get calibration matrix. */
if (nu_adc_cal_mat_get(&sDispPoints[0], &sADCPoints[0], &sCalMat) == 0)
{
/* Finally, update calibration matrix to drivers. */
nu_adc_touch_update_calmat(&sCalMat);
nu_adc_touch_writefile(&sCalMat, sizeof(sCalMat));
for (i = 0; i < DEF_CAL_POINT_NUM; i++)
{
rt_kprintf("[%d] - Disp:[%d, %d], ADC:[%d, %d]\n", i, sDispPoints[i].x, sDispPoints[i].y, sADCPoints[i].x, sADCPoints[i].y);
}
}
else
{
rt_kprintf("Failed to calibrate.\n");
}
rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWEROFF, RT_NULL);
rt_device_close(lcd_device);
return;
}
static void adc_touch_entry(void *parameter)
{
struct rt_touch_data touch_point;
rt_err_t result;
rt_device_t pdev;
int max_range;
adc_touch_sem = rt_sem_create("adc_touch_sem", 0, RT_IPC_FLAG_FIFO);
RT_ASSERT(adc_touch_sem != RT_NULL);
pdev = rt_device_find("adc_touch");
if (!pdev)
{
rt_kprintf("Not found\n");
return ;
}
if (rt_memcmp((void *)&g_sCalMat, (void *)&g_sCalZero, sizeof(S_CALIBRATION_MATRIX)) != 0)
g_u32Calibrated = 1;
nu_adc_touch_readfile();
result = rt_device_open(pdev, RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(result == RT_EOK);
result = rt_device_set_rx_indicate(pdev, adc_touch_rx_callback);
RT_ASSERT(result == RT_EOK);
max_range = BSP_LCD_WIDTH;
result = rt_device_control(pdev, RT_TOUCH_CTRL_SET_X_RANGE, (void *)&max_range);
RT_ASSERT(result == RT_EOK);
max_range = BSP_LCD_HEIGHT;
result = rt_device_control(pdev, RT_TOUCH_CTRL_SET_Y_RANGE, (void *)&max_range);
RT_ASSERT(result == RT_EOK);
result = rt_device_control(pdev, RT_TOUCH_CTRL_POWER_ON, RT_NULL);
RT_ASSERT(result == RT_EOK);
while (adc_touch_worker_run)
{
if (!g_u32Calibrated)
{
rt_kprintf("Start ADC touching calibration.\n");
nu_touch_do_calibration(pdev);
rt_kprintf("Stop ADC touching calibration.\n");
continue;
}
if (adc_request_point(pdev, &touch_point) == RT_EOK)
{
if (touch_point.event == RT_TOUCH_EVENT_DOWN
|| touch_point.event == RT_TOUCH_EVENT_UP
|| touch_point.event == RT_TOUCH_EVENT_MOVE)
{
nu_touch_inputevent_cb(touch_point.x_coordinate, touch_point.y_coordinate, touch_point.event);
rt_kprintf("x=%d y=%d event=%s%s%s\n",
touch_point.x_coordinate,
touch_point.y_coordinate,
(touch_point.event == RT_TOUCH_EVENT_DOWN) ? "DOWN" : "",
(touch_point.event == RT_TOUCH_EVENT_UP) ? "UP" : "",
(touch_point.event == RT_TOUCH_EVENT_MOVE) ? "MOVE" : "");
}
}
}
result = rt_device_control(pdev, RT_TOUCH_CTRL_POWER_OFF, RT_NULL);
RT_ASSERT(result == RT_EOK);
result = rt_device_close(pdev);
RT_ASSERT(result == RT_EOK);
}
/* Support "nu_touch_start" command line in msh mode */
static rt_err_t nu_touch_start(int argc, char **argv)
{
if (adc_touch_thread == RT_NULL)
{
adc_touch_thread = rt_thread_create("adc_touch_thread",
adc_touch_entry,
RT_NULL,
2048,
5,
5);
adc_touch_worker_run = 1;
if (adc_touch_thread != RT_NULL)
rt_thread_startup(adc_touch_thread);
}
return 0;
}
MSH_CMD_EXPORT(nu_touch_start, e.g: start adc touch);
/* Support "nu_touch_stop" command line in msh mode */
static rt_err_t nu_touch_stop(int argc, char **argv)
{
adc_touch_worker_run = 0;
adc_touch_thread = RT_NULL;
return 0;
}
MSH_CMD_EXPORT(nu_touch_stop, e.g: stop adc touch);
static int nu_touch_autostart(void)
{
return nu_touch_start(0, RT_NULL);
}
INIT_APP_EXPORT(nu_touch_autostart);
static rt_err_t nu_touch_calibration(int argc, char **argv)
{
/* Clean calibration matrix data for getting raw adc value. */
nu_adc_touch_reset_calmat();
return 0;
}
MSH_CMD_EXPORT(nu_touch_calibration, for adc touch);
#endif //#if defined(NU_PKG_USING_ADC_TOUCH)
|