adc_touch.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /**************************************************************************//**
  2. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-20 Wayne First version
  9. *
  10. ******************************************************************************/
  11. #include <rtconfig.h>
  12. #if defined(NU_PKG_USING_ADC_TOUCH)
  13. #include "NuMicro.h"
  14. #include <rtdevice.h>
  15. #include <dfs_file.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <sys/stat.h>
  19. #include <sys/statfs.h>
  20. #include "touch.h"
  21. //#include "drv_adc.h"
  22. #include "adc_touch.h"
  23. #if !defined(PATH_CALIBRATION_FILE)
  24. #define PATH_CALIBRATION_FILE "/mnt/filesystem/ts_calibration"
  25. #endif
  26. rt_err_t nu_adc_touch_disable(void);
  27. rt_err_t nu_adc_touch_enable(rt_touch_t psRtTouch);
  28. void nu_adc_touch_detect(rt_bool_t bStartDetect);
  29. int32_t nu_adc_touch_read_xyz(uint32_t *bufX, uint32_t *bufY, uint32_t *bufZ0, uint32_t *bufZ1, int32_t dataCnt);
  30. typedef struct
  31. {
  32. struct rt_touch_device dev;
  33. rt_uint32_t x_range;
  34. rt_uint32_t y_range;
  35. } nu_adc_touch;
  36. typedef nu_adc_touch *nu_adc_touch_t;
  37. static nu_adc_touch s_NuAdcTouch = {0};
  38. /* User can define ADC touch calibration matrix in board_dev.c. */
  39. rt_weak S_CALIBRATION_MATRIX g_sCalMat = { 1, 0, 0, 0, 1, 0, 1 };
  40. static volatile uint32_t g_u32Calibrated = 0;
  41. static int nu_adc_touch_readfile(void);
  42. static const S_CALIBRATION_MATRIX g_sCalZero = { 1, 0, 0, 0, 1, 0, 1 };
  43. static int nu_adc_cal_mat_get(const S_COORDINATE_POINT *psDispCP, S_COORDINATE_POINT *psADCCP, S_CALIBRATION_MATRIX *psCM)
  44. {
  45. #if (DEF_CAL_POINT_NUM==3)
  46. psCM->div = ((psADCCP[0].x - psADCCP[2].x) * (psADCCP[1].y - psADCCP[2].y)) -
  47. ((psADCCP[1].x - psADCCP[2].x) * (psADCCP[0].y - psADCCP[2].y)) ;
  48. if (psCM->div == 0)
  49. {
  50. return -1;
  51. }
  52. else
  53. {
  54. psCM->a = ((psDispCP[0].x - psDispCP[2].x) * (psADCCP[1].y - psADCCP[2].y)) -
  55. ((psDispCP[1].x - psDispCP[2].x) * (psADCCP[0].y - psADCCP[2].y)) ;
  56. psCM->b = ((psADCCP[0].x - psADCCP[2].x) * (psDispCP[1].x - psDispCP[2].x)) -
  57. ((psDispCP[0].x - psDispCP[2].x) * (psADCCP[1].x - psADCCP[2].x)) ;
  58. psCM->c = (psADCCP[2].x * psDispCP[1].x - psADCCP[1].x * psDispCP[2].x) * psADCCP[0].y +
  59. (psADCCP[0].x * psDispCP[2].x - psADCCP[2].x * psDispCP[0].x) * psADCCP[1].y +
  60. (psADCCP[1].x * psDispCP[0].x - psADCCP[0].x * psDispCP[1].x) * psADCCP[2].y ;
  61. psCM->d = ((psDispCP[0].y - psDispCP[2].y) * (psADCCP[1].y - psADCCP[2].y)) -
  62. ((psDispCP[1].y - psDispCP[2].y) * (psADCCP[0].y - psADCCP[2].y)) ;
  63. psCM->e = ((psADCCP[0].x - psADCCP[2].x) * (psDispCP[1].y - psDispCP[2].y)) -
  64. ((psDispCP[0].y - psDispCP[2].y) * (psADCCP[1].x - psADCCP[2].x)) ;
  65. psCM->f = (psADCCP[2].x * psDispCP[1].y - psADCCP[1].x * psDispCP[2].y) * psADCCP[0].y +
  66. (psADCCP[0].x * psDispCP[2].y - psADCCP[2].x * psDispCP[0].y) * psADCCP[1].y +
  67. (psADCCP[1].x * psDispCP[0].y - psADCCP[0].x * psDispCP[1].y) * psADCCP[2].y ;
  68. }
  69. #elif (DEF_CAL_POINT_NUM==5)
  70. int i;
  71. float n, x, y, xx, yy, xy, z, zx, zy;
  72. float a, b, c, d, e, f, g;
  73. float scaling = 65536.0f;
  74. n = x = y = xx = yy = xy = 0;
  75. for (i = 0; i < DEF_CAL_POINT_NUM; i++)
  76. {
  77. n += (float)1.0;
  78. x += (float)psADCCP[i].x;
  79. y += (float)psADCCP[i].y;
  80. xx += (float)psADCCP[i].x * psADCCP[i].x;
  81. yy += (float)psADCCP[i].y * psADCCP[i].y;
  82. xy += (float)psADCCP[i].x * psADCCP[i].y;
  83. }
  84. d = n * (xx * yy - xy * xy) + x * (xy * y - x * yy) + y * (x * xy - y * xx);
  85. if (d < (float)0.1 && d > (float) -0.1)
  86. {
  87. return -1;
  88. }
  89. a = (xx * yy - xy * xy) / d;
  90. b = (xy * y - x * yy) / d;
  91. c = (x * xy - y * xx) / d;
  92. e = (n * yy - y * y) / d;
  93. f = (x * y - n * xy) / d;
  94. g = (n * xx - x * x) / d;
  95. z = zx = zy = 0;
  96. for (i = 0; i < DEF_CAL_POINT_NUM; i++)
  97. {
  98. z += (float)psDispCP[i].x;
  99. zx += (float)psDispCP[i].x * psADCCP[i].x;
  100. zy += (float)psDispCP[i].x * psADCCP[i].y;
  101. }
  102. psCM->c = (int32_t)((a * z + b * zx + c * zy) * scaling);
  103. psCM->a = (int32_t)((b * z + e * zx + f * zy) * scaling);
  104. psCM->b = (int32_t)((c * z + f * zx + g * zy) * scaling);
  105. z = zx = zy = 0;
  106. for (i = 0; i < DEF_CAL_POINT_NUM; i++)
  107. {
  108. z += (float)psDispCP[i].y;
  109. zx += (float)psDispCP[i].y * psADCCP[i].x;
  110. zy += (float)psDispCP[i].y * psADCCP[i].y;
  111. }
  112. psCM->f = (int32_t)((a * z + b * zx + c * zy) * scaling);
  113. psCM->d = (int32_t)((b * z + e * zx + f * zy) * scaling);
  114. psCM->e = (int32_t)((c * z + f * zx + g * zy) * scaling);
  115. psCM->div = (int32_t)scaling;
  116. #else
  117. #error "Not supported calibration method"
  118. #endif
  119. return 0;
  120. }
  121. static void nu_adc_touch_cal(int32_t *sumx, int32_t *sumy)
  122. {
  123. int32_t xtemp, ytemp;
  124. xtemp = *sumx;
  125. ytemp = *sumy;
  126. *sumx = (g_sCalMat.c +
  127. g_sCalMat.a * xtemp +
  128. g_sCalMat.b * ytemp) / g_sCalMat.div;
  129. *sumy = (g_sCalMat.f +
  130. g_sCalMat.d * xtemp +
  131. g_sCalMat.e * ytemp) / g_sCalMat.div;
  132. }
  133. static rt_size_t nu_adc_touch_readpoint(struct rt_touch_device *device, void *buf, rt_size_t read_num)
  134. {
  135. static uint32_t last_report_x = 0, last_report_y = 0;
  136. struct rt_touch_data *pPoint = (struct rt_touch_data *)buf;
  137. nu_adc_touch_t psNuAdcTouch = (nu_adc_touch_t)device;
  138. RT_ASSERT(device != RT_NULL);
  139. RT_ASSERT(buf != RT_NULL);
  140. int i;
  141. for (i = 0; i < read_num; i++)
  142. {
  143. uint32_t bufZ0 = 0, bufZ1 = 0;
  144. int32_t sumx = 0, sumy = 0;
  145. pPoint[i].timestamp = rt_touch_get_ts();
  146. pPoint[i].track_id = 0;
  147. if (nu_adc_touch_read_xyz((uint32_t *)&sumx, (uint32_t *)&sumy, &bufZ0, &bufZ1, 1) != 1)
  148. break;
  149. if (bufZ0 == 0)
  150. {
  151. /* Workaround: In this case, x, y values are unstable. so, report last point's coordinate.*/
  152. pPoint[i].event = RT_TOUCH_EVENT_UP;
  153. pPoint[i].x_coordinate = (uint16_t)last_report_x;
  154. pPoint[i].y_coordinate = (uint16_t)last_report_y;
  155. }
  156. else
  157. {
  158. if (g_u32Calibrated)
  159. {
  160. nu_adc_touch_cal(&sumx, &sumy);
  161. }
  162. last_report_x = sumx;
  163. last_report_y = sumy;
  164. pPoint[i].event = RT_TOUCH_EVENT_DOWN;
  165. pPoint[i].x_coordinate = (uint16_t)sumx;
  166. pPoint[i].y_coordinate = (uint16_t)sumy;
  167. }
  168. if (g_u32Calibrated)
  169. {
  170. bufZ0 = bufZ0 >> 3;
  171. pPoint[i].width = (bufZ0 > 255) ? 255 : bufZ0;
  172. //Limit max x, y coordinate if value is over its range.
  173. pPoint[i].x_coordinate = (pPoint[i].x_coordinate > psNuAdcTouch->x_range) ? psNuAdcTouch->x_range : pPoint[i].x_coordinate;
  174. pPoint[i].y_coordinate = (pPoint[i].y_coordinate > psNuAdcTouch->y_range) ? psNuAdcTouch->y_range : pPoint[i].y_coordinate;
  175. }
  176. }
  177. return (rt_size_t)i;
  178. }
  179. static rt_err_t nu_adc_touch_control(struct rt_touch_device *device, int cmd, void *data)
  180. {
  181. nu_adc_touch_t psNuAdcTouch = (nu_adc_touch_t)device;
  182. RT_ASSERT(psNuAdcTouch != RT_NULL);
  183. switch (cmd)
  184. {
  185. case RT_TOUCH_CTRL_SET_X_RANGE: /* set x range */
  186. psNuAdcTouch->x_range = *((rt_int32_t *)data);
  187. break;
  188. case RT_TOUCH_CTRL_SET_Y_RANGE: /* set y range */
  189. psNuAdcTouch->y_range = *((rt_int32_t *)data);
  190. break;
  191. case RT_TOUCH_CTRL_ENABLE_INT: /* enable pen_down interrupt */
  192. nu_adc_touch_detect(RT_TRUE);
  193. break;
  194. case RT_TOUCH_CTRL_DISABLE_INT: /* disable pen_down interrupt */
  195. nu_adc_touch_detect(RT_FALSE);
  196. break;
  197. case RT_TOUCH_CTRL_POWER_ON: /* Touch Power On */
  198. return nu_adc_touch_enable(device);
  199. case RT_TOUCH_CTRL_POWER_OFF: /* Touch Power Off */
  200. return nu_adc_touch_disable();
  201. default:
  202. return -RT_ERROR;
  203. }
  204. return RT_EOK;
  205. }
  206. static struct rt_touch_ops touch_ops =
  207. {
  208. .touch_readpoint = nu_adc_touch_readpoint,
  209. .touch_control = nu_adc_touch_control,
  210. };
  211. static void nu_adc_touch_update_calmat(S_CALIBRATION_MATRIX *psNewCalMat)
  212. {
  213. if (psNewCalMat &&
  214. psNewCalMat->div != 0)
  215. {
  216. rt_memcpy(&g_sCalMat, psNewCalMat, sizeof(S_CALIBRATION_MATRIX));
  217. g_u32Calibrated = 1;
  218. rt_kprintf("Applied calibration data: %d, %d, %d, %d, %d, %d, %d\n",
  219. g_sCalMat.a,
  220. g_sCalMat.b,
  221. g_sCalMat.c,
  222. g_sCalMat.d,
  223. g_sCalMat.e,
  224. g_sCalMat.f,
  225. g_sCalMat.div);
  226. }
  227. }
  228. static void nu_adc_touch_reset_calmat(void)
  229. {
  230. rt_memcpy(&g_sCalMat, &g_sCalZero, sizeof(S_CALIBRATION_MATRIX));
  231. g_u32Calibrated = 0;
  232. }
  233. int rt_hw_adc_touch_init(void)
  234. {
  235. /* Register touch device */
  236. s_NuAdcTouch.dev.info.type = RT_TOUCH_TYPE_RESISTANCE;
  237. s_NuAdcTouch.dev.info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
  238. s_NuAdcTouch.dev.info.point_num = 1;
  239. s_NuAdcTouch.dev.info.range_x = BSP_LCD_WIDTH;
  240. s_NuAdcTouch.dev.info.range_y = BSP_LCD_HEIGHT;
  241. s_NuAdcTouch.dev.ops = &touch_ops;
  242. return (int)rt_hw_touch_register(&s_NuAdcTouch.dev, "adc_touch", RT_DEVICE_FLAG_INT_RX, RT_NULL);
  243. }
  244. INIT_DEVICE_EXPORT(rt_hw_adc_touch_init);
  245. static rt_thread_t adc_touch_thread = RT_NULL;
  246. static rt_sem_t adc_touch_sem = RT_NULL;
  247. static int adc_touch_worker_run = 0;
  248. static rt_err_t adc_touch_rx_callback(rt_device_t dev, rt_size_t size)
  249. {
  250. //rt_kprintf("[%s %d] %d\n", __func__, __LINE__, size);
  251. return rt_sem_release(adc_touch_sem);
  252. }
  253. static rt_err_t adc_request_point(rt_device_t pdev, struct rt_touch_data *psTouchPoint)
  254. {
  255. rt_err_t ret = -RT_ERROR;
  256. if ((ret = rt_sem_take(adc_touch_sem, rt_tick_from_millisecond(500))) == RT_EOK)
  257. {
  258. rt_memset(psTouchPoint, 0, sizeof(struct rt_touch_data));
  259. if (rt_device_read(pdev, 0, psTouchPoint, s_NuAdcTouch.dev.info.point_num) == s_NuAdcTouch.dev.info.point_num)
  260. {
  261. ret = RT_EOK;
  262. }
  263. }
  264. return ret;
  265. }
  266. rt_weak void nu_touch_inputevent_cb(rt_int16_t x, rt_int16_t y, rt_uint8_t event)
  267. {
  268. }
  269. static rt_device_t lcd_device = 0;
  270. static struct rt_device_graphic_info info;
  271. static void lcd_cleanscreen(void)
  272. {
  273. if (info.framebuffer != RT_NULL)
  274. {
  275. if (rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, (void *)info.framebuffer) == RT_EOK)
  276. {
  277. /* Sync-type LCD panel, will fill to VRAM directly. */
  278. rt_memset(info.framebuffer, 0, (info.pitch * info.height));
  279. }
  280. else
  281. {
  282. /* MPU-type LCD panel, fill to shadow RAM, then flush. */
  283. struct rt_device_rect_info rectinfo;
  284. int filled_line_num = 0;
  285. int i32LineBufNum = info.smem_len / info.pitch;
  286. int i32RemainLineNum = info.height;
  287. i32LineBufNum = (i32LineBufNum > info.height) ? info.height : i32LineBufNum;
  288. while (i32RemainLineNum > 0)
  289. {
  290. int pixel_count;
  291. rectinfo.x = 0;
  292. rectinfo.y = filled_line_num;
  293. rectinfo.width = info.width;
  294. rectinfo.height = (i32RemainLineNum > i32LineBufNum) ? i32LineBufNum : i32RemainLineNum ;
  295. pixel_count = info.width * rectinfo.height;
  296. rt_uint16_t *pu16ShadowBuf = (rt_uint16_t *)info.framebuffer;
  297. while (pixel_count > 0)
  298. {
  299. *pu16ShadowBuf++ = 0;
  300. pixel_count--;
  301. }
  302. rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rectinfo);
  303. filled_line_num += i32LineBufNum;
  304. i32RemainLineNum -= rectinfo.height;
  305. }
  306. }
  307. }
  308. else
  309. {
  310. // TODO
  311. }
  312. }
  313. #define DEF_DOT_NUMBER 9
  314. #define DOTS_NUMBER (DEF_DOT_NUMBER*DEF_DOT_NUMBER)
  315. static void nu_draw_bots(int x, int y)
  316. {
  317. if (info.framebuffer != RT_NULL)
  318. {
  319. /* Rendering */
  320. struct rt_device_rect_info rect;
  321. int i, j;
  322. int start_x = x - (DEF_DOT_NUMBER / 2);
  323. int start_y = y - (DEF_DOT_NUMBER / 2);
  324. rt_bool_t bDrawDirect;
  325. if (rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, (void *)info.framebuffer) == RT_EOK)
  326. {
  327. /* Sync-type LCD panel, will draw to VRAM directly. */
  328. bDrawDirect = RT_TRUE;
  329. }
  330. else
  331. {
  332. /* MPU-type LCD panel, draw to shadow RAM, then flush. */
  333. bDrawDirect = RT_FALSE;
  334. }
  335. if (info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_RGB565)
  336. {
  337. uint16_t *pu16Start = (bDrawDirect == RT_TRUE) ? (uint16_t *)((uint32_t)info.framebuffer + (start_y) * info.pitch + (start_x * 2)) : (uint16_t *)info.framebuffer;
  338. for (i = 0; i < DEF_DOT_NUMBER; i++)
  339. {
  340. for (j = 0; j < DEF_DOT_NUMBER; j++)
  341. {
  342. *pu16Start = 0x07E0; //Green, RGB565
  343. pu16Start++;
  344. }
  345. if (bDrawDirect)
  346. pu16Start += (info.width - DEF_DOT_NUMBER);
  347. }
  348. }
  349. else if (info.pixel_format == RTGRAPHIC_PIXEL_FORMAT_ARGB888)
  350. {
  351. uint32_t *pu32Start = (bDrawDirect == RT_TRUE) ? (uint32_t *)((uint32_t)info.framebuffer + (start_y) * info.pitch + (start_x * 4)) : (uint32_t *)info.framebuffer;
  352. for (i = 0; i < DEF_DOT_NUMBER; i++)
  353. {
  354. for (j = 0; j < DEF_DOT_NUMBER; j++)
  355. {
  356. *pu32Start = 0xff00ff00; //Green, ARGB888
  357. pu32Start++;
  358. }
  359. if (bDrawDirect)
  360. pu32Start += (info.width - DEF_DOT_NUMBER);
  361. }
  362. }
  363. else
  364. {
  365. //Not supported
  366. return;
  367. }
  368. if (!bDrawDirect)
  369. {
  370. /* Region updating */
  371. rect.x = start_x;
  372. rect.y = start_y;
  373. rect.width = DEF_DOT_NUMBER;
  374. rect.height = DEF_DOT_NUMBER;
  375. rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
  376. }
  377. }
  378. else
  379. {
  380. // TODO
  381. }
  382. return;
  383. }
  384. #if (DEF_CAL_POINT_NUM==3)
  385. const S_COORDINATE_POINT sDispPoints[DEF_CAL_POINT_NUM] =
  386. {
  387. {BSP_LCD_WIDTH / 4, BSP_LCD_HEIGHT / 2},
  388. {BSP_LCD_WIDTH - BSP_LCD_WIDTH / 4, BSP_LCD_HEIGHT / 4},
  389. {BSP_LCD_WIDTH / 2, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / 4}
  390. };
  391. #elif (DEF_CAL_POINT_NUM==5)
  392. const static S_COORDINATE_POINT sDispPoints[DEF_CAL_POINT_NUM] =
  393. {
  394. #define DEF_CUT_PIECES 8
  395. {BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT / DEF_CUT_PIECES},
  396. {BSP_LCD_WIDTH - BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT / DEF_CUT_PIECES},
  397. {BSP_LCD_WIDTH - BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / DEF_CUT_PIECES},
  398. {BSP_LCD_WIDTH / DEF_CUT_PIECES, BSP_LCD_HEIGHT - BSP_LCD_HEIGHT / DEF_CUT_PIECES},
  399. {BSP_LCD_WIDTH / 2, BSP_LCD_HEIGHT / 2}
  400. };
  401. #endif
  402. static int nu_adc_touch_readfile(void)
  403. {
  404. int fd;
  405. S_CALIBRATION_MATRIX sCalMat;
  406. if ((fd = open(PATH_CALIBRATION_FILE, O_RDONLY, 0)) < 0)
  407. {
  408. goto exit_nu_adc_touch_readfile;
  409. }
  410. else if (read(fd, &sCalMat, sizeof(S_CALIBRATION_MATRIX)) == sizeof(S_CALIBRATION_MATRIX))
  411. {
  412. rt_kprintf("[%s] %s\n", __func__, PATH_CALIBRATION_FILE);
  413. }
  414. close(fd);
  415. nu_adc_touch_update_calmat(&sCalMat);
  416. return 0;
  417. exit_nu_adc_touch_readfile:
  418. return -1;
  419. }
  420. static int nu_adc_touch_writefile(void *buf, int buf_len)
  421. {
  422. int fd;
  423. if ((fd = open(PATH_CALIBRATION_FILE, O_WRONLY | O_CREAT, 0)) < 0)
  424. {
  425. goto exit_nu_adc_touch_writefile;
  426. }
  427. else if (write(fd, buf, buf_len) == buf_len)
  428. {
  429. rt_kprintf("[%s] %s\n", __func__, PATH_CALIBRATION_FILE);
  430. }
  431. close(fd);
  432. return 0;
  433. exit_nu_adc_touch_writefile:
  434. return -1;
  435. }
  436. static void nu_touch_do_calibration(rt_device_t pdev)
  437. {
  438. int i;
  439. rt_err_t result;
  440. S_CALIBRATION_MATRIX sCalMat;
  441. S_COORDINATE_POINT sADCPoints[DEF_CAL_POINT_NUM];
  442. lcd_device = rt_device_find("lcd");
  443. if (!lcd_device)
  444. {
  445. rt_kprintf("Not supported graphics ops\n");
  446. return;
  447. }
  448. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  449. if (result != RT_EOK)
  450. {
  451. rt_kprintf("error!");
  452. return;
  453. }
  454. result = rt_device_open(lcd_device, 0);
  455. if (result != RT_EOK)
  456. {
  457. rt_kprintf("opened?");
  458. }
  459. rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, info.framebuffer);
  460. rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
  461. for (i = 0; i < DEF_CAL_POINT_NUM; i++)
  462. {
  463. struct rt_touch_data sTouchPoint;
  464. int count = 0;
  465. lcd_cleanscreen();
  466. /* Drain RX queue before doing calibrate. */
  467. while (adc_request_point(pdev, &sTouchPoint) == RT_EOK);
  468. rt_thread_mdelay(100);
  469. /* Ready to calibrate */
  470. nu_draw_bots(sDispPoints[i].x, sDispPoints[i].y);
  471. #define DEF_MAX_GET_POINT_NUM 5
  472. sADCPoints[i].x = 0;
  473. sADCPoints[i].y = 0;
  474. while (count < DEF_MAX_GET_POINT_NUM)
  475. {
  476. if (adc_request_point(pdev, &sTouchPoint) == RT_EOK)
  477. {
  478. sADCPoints[i].x += (int32_t)sTouchPoint.x_coordinate;
  479. sADCPoints[i].y += (int32_t)sTouchPoint.y_coordinate;
  480. 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);
  481. count++;
  482. }
  483. }
  484. sADCPoints[i].x = (int32_t)((float)sADCPoints[i].x / DEF_MAX_GET_POINT_NUM);
  485. sADCPoints[i].y = (int32_t)((float)sADCPoints[i].y / DEF_MAX_GET_POINT_NUM);
  486. rt_kprintf("[%d] - Disp:[%d, %d], ADC:[%d, %d]\n", i, sDispPoints[i].x, sDispPoints[i].y, sADCPoints[i].x, sADCPoints[i].y);
  487. rt_thread_mdelay(300);
  488. }
  489. lcd_cleanscreen();
  490. /* Get calibration matrix. */
  491. if (nu_adc_cal_mat_get(&sDispPoints[0], &sADCPoints[0], &sCalMat) == 0)
  492. {
  493. /* Finally, update calibration matrix to drivers. */
  494. nu_adc_touch_update_calmat(&sCalMat);
  495. nu_adc_touch_writefile(&sCalMat, sizeof(sCalMat));
  496. for (i = 0; i < DEF_CAL_POINT_NUM; i++)
  497. {
  498. rt_kprintf("[%d] - Disp:[%d, %d], ADC:[%d, %d]\n", i, sDispPoints[i].x, sDispPoints[i].y, sADCPoints[i].x, sADCPoints[i].y);
  499. }
  500. }
  501. else
  502. {
  503. rt_kprintf("Failed to calibrate.\n");
  504. }
  505. rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWEROFF, RT_NULL);
  506. rt_device_close(lcd_device);
  507. return;
  508. }
  509. static void adc_touch_entry(void *parameter)
  510. {
  511. struct rt_touch_data touch_point;
  512. rt_err_t result;
  513. rt_device_t pdev;
  514. int max_range;
  515. adc_touch_sem = rt_sem_create("adc_touch_sem", 0, RT_IPC_FLAG_FIFO);
  516. RT_ASSERT(adc_touch_sem != RT_NULL);
  517. pdev = rt_device_find("adc_touch");
  518. if (!pdev)
  519. {
  520. rt_kprintf("Not found\n");
  521. return ;
  522. }
  523. if (rt_memcmp((void *)&g_sCalMat, (void *)&g_sCalZero, sizeof(S_CALIBRATION_MATRIX)) != 0)
  524. g_u32Calibrated = 1;
  525. nu_adc_touch_readfile();
  526. result = rt_device_open(pdev, RT_DEVICE_FLAG_INT_RX);
  527. RT_ASSERT(result == RT_EOK);
  528. result = rt_device_set_rx_indicate(pdev, adc_touch_rx_callback);
  529. RT_ASSERT(result == RT_EOK);
  530. max_range = BSP_LCD_WIDTH;
  531. result = rt_device_control(pdev, RT_TOUCH_CTRL_SET_X_RANGE, (void *)&max_range);
  532. RT_ASSERT(result == RT_EOK);
  533. max_range = BSP_LCD_HEIGHT;
  534. result = rt_device_control(pdev, RT_TOUCH_CTRL_SET_Y_RANGE, (void *)&max_range);
  535. RT_ASSERT(result == RT_EOK);
  536. result = rt_device_control(pdev, RT_TOUCH_CTRL_POWER_ON, RT_NULL);
  537. RT_ASSERT(result == RT_EOK);
  538. while (adc_touch_worker_run)
  539. {
  540. if (!g_u32Calibrated)
  541. {
  542. rt_kprintf("Start ADC touching calibration.\n");
  543. nu_touch_do_calibration(pdev);
  544. rt_kprintf("Stop ADC touching calibration.\n");
  545. continue;
  546. }
  547. if (adc_request_point(pdev, &touch_point) == RT_EOK)
  548. {
  549. if (touch_point.event == RT_TOUCH_EVENT_DOWN
  550. || touch_point.event == RT_TOUCH_EVENT_UP
  551. || touch_point.event == RT_TOUCH_EVENT_MOVE)
  552. {
  553. nu_touch_inputevent_cb(touch_point.x_coordinate, touch_point.y_coordinate, touch_point.event);
  554. rt_kprintf("x=%d y=%d event=%s%s%s\n",
  555. touch_point.x_coordinate,
  556. touch_point.y_coordinate,
  557. (touch_point.event == RT_TOUCH_EVENT_DOWN) ? "DOWN" : "",
  558. (touch_point.event == RT_TOUCH_EVENT_UP) ? "UP" : "",
  559. (touch_point.event == RT_TOUCH_EVENT_MOVE) ? "MOVE" : "");
  560. }
  561. }
  562. }
  563. result = rt_device_control(pdev, RT_TOUCH_CTRL_POWER_OFF, RT_NULL);
  564. RT_ASSERT(result == RT_EOK);
  565. result = rt_device_close(pdev);
  566. RT_ASSERT(result == RT_EOK);
  567. }
  568. /* Support "nu_touch_start" command line in msh mode */
  569. static rt_err_t nu_touch_start(int argc, char **argv)
  570. {
  571. if (adc_touch_thread == RT_NULL)
  572. {
  573. adc_touch_thread = rt_thread_create("adc_touch_thread",
  574. adc_touch_entry,
  575. RT_NULL,
  576. 2048,
  577. 5,
  578. 5);
  579. adc_touch_worker_run = 1;
  580. if (adc_touch_thread != RT_NULL)
  581. rt_thread_startup(adc_touch_thread);
  582. }
  583. return 0;
  584. }
  585. MSH_CMD_EXPORT(nu_touch_start, e.g: start adc touch);
  586. /* Support "nu_touch_stop" command line in msh mode */
  587. static rt_err_t nu_touch_stop(int argc, char **argv)
  588. {
  589. adc_touch_worker_run = 0;
  590. adc_touch_thread = RT_NULL;
  591. return 0;
  592. }
  593. MSH_CMD_EXPORT(nu_touch_stop, e.g: stop adc touch);
  594. static int nu_touch_autostart(void)
  595. {
  596. return nu_touch_start(0, RT_NULL);
  597. }
  598. INIT_APP_EXPORT(nu_touch_autostart);
  599. static rt_err_t nu_touch_calibration(int argc, char **argv)
  600. {
  601. /* Clean calibration matrix data for getting raw adc value. */
  602. nu_adc_touch_reset_calmat();
  603. return 0;
  604. }
  605. MSH_CMD_EXPORT(nu_touch_calibration, for adc touch);
  606. #endif //#if defined(NU_PKG_USING_ADC_TOUCH)