adc_touch.c 22 KB

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