1
0

touch.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * File : touch.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-30 Sundm75 first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <stdbool.h>
  27. #include <drivers/spi.h>
  28. #include "ls1c.h"
  29. #include "ls1c_gpio.h"
  30. #include "ls1c_spi.h"
  31. #include "drv_spi.h"
  32. #include "touch.h"
  33. #ifdef RT_USING_RTGUI
  34. #include <rtgui/calibration.h>
  35. #include <rtgui/event.h>
  36. #include <rtgui/kbddef.h>
  37. #include <rtgui/rtgui_server.h>
  38. #include <rtgui/rtgui_system.h>
  39. //竖屏幕 不需要 _ILI_HORIZONTAL_DIRECTION_
  40. //横屏幕 需要 _ILI_HORIZONTAL_DIRECTION_
  41. //#define _ILI_HORIZONTAL_DIRECTION_
  42. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  43. #define X_WIDTH 272
  44. #define Y_WIDTH 480
  45. #else
  46. #define X_WIDTH 480
  47. #define Y_WIDTH 272
  48. #endif
  49. /*
  50. TOUCH INT: 84
  51. */
  52. #define IS_TOUCH_UP() gpio_get(TOUCH_INT_PIN)
  53. #define led_gpio 52 // led1指示
  54. #define DUMMY 0x00
  55. /*
  56. 7 6 - 4 3 2 1-0
  57. s A2-A0 MODE SER/DFR PD1-PD0
  58. */
  59. /* bit[1:0] power-down */
  60. #define POWER_MODE0 (0) /* Power-Down Between Conversions. When */
  61. /* each conversion is finished, the converter */
  62. /* enters a low-power mode. At the start of the */
  63. /* next conversion, the device instantly powers up */
  64. /* to full power. There is no need for additional */
  65. /* delays to ensure full operation, and the very first */
  66. /* conversion is valid. The Y? switch is on when in */
  67. /* power-down.*/
  68. #define POWER_MODE1 (1) /* Reference is off and ADC is on. */
  69. #define POWER_MODE2 (2) /* Reference is on and ADC is off. */
  70. #define POWER_MODE3 (3) /* Device is always powered. Reference is on and */
  71. /* ADC is on. */
  72. /* bit[2] SER/DFR */
  73. #define DIFFERENTIAL (0<<2)
  74. #define SINGLE_ENDED (1<<2)
  75. /* bit[3] mode */
  76. #define MODE_12BIT (0<<3)
  77. #define MODE_8BIT (1<<3)
  78. /* bit[6:4] differential mode */
  79. #define MEASURE_X (((1<<2) | (0<<1) | (1<<0))<<4)
  80. #define MEASURE_Y (((0<<2) | (0<<1) | (1<<0))<<4)
  81. #define MEASURE_Z1 (((0<<2) | (1<<1) | (1<<0))<<4)
  82. #define MEASURE_Z2 (((1<<2) | (0<<1) | (0<<0))<<4)
  83. /* bit[7] start */
  84. #define START (1<<7)
  85. /* X Y change. */
  86. #define TOUCH_MSR_X (START | MEASURE_X | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
  87. #define TOUCH_MSR_Y (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
  88. /* 以下定义XPT2046 的触摸屏位置*/
  89. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  90. #define MIN_X_DEFAULT 2047
  91. #define MAX_X_DEFAULT 47
  92. #define MIN_Y_DEFAULT 102
  93. #define MAX_Y_DEFAULT 1939
  94. #else
  95. #define MIN_X_DEFAULT 47
  96. #define MAX_X_DEFAULT 2047
  97. #define MIN_Y_DEFAULT 1939
  98. #define MAX_Y_DEFAULT 102
  99. #endif
  100. #define SAMP_CNT 8 //the adc array size
  101. #define SAMP_CNT_DIV2 4 //the middle of the adc array
  102. #define SH 10 // Valve value
  103. /*宏定义 */
  104. #define TOUCH_SPI_X SPI1
  105. #define TOUCH_INT_PIN 84
  106. #define TOUCH_CS_PIN 49
  107. #define TOUCH_SCK_PIN 46
  108. #define TOUCH_MISO_PIN 47
  109. #define TOUCH_MOSI_PIN 48
  110. /*创建结构体将需要用到的东西进行打包*/
  111. struct rtgui_touch_device
  112. {
  113. struct rt_device parent; /* 用于注册设备*/
  114. rt_uint16_t x, y; /* 记录读取到的位置值 */
  115. rt_bool_t calibrating; /* 触摸校准标志 */
  116. rt_touch_calibration_func_t calibration_func;/* 触摸函数 函数指针 */
  117. rt_uint16_t min_x, max_x; /* 校准后 X 方向最小 最大值 */
  118. rt_uint16_t min_y, max_y; /* 校准后 Y 方向最小 最大值 */
  119. struct rt_spi_device * spi_device; /* SPI 设备 用于通信 */
  120. struct rt_event event; /* 事件同步,用于“笔中断” */
  121. };
  122. static struct rtgui_touch_device *touch = RT_NULL;
  123. static rt_err_t touch_send_then_recv(struct rt_spi_device *device,
  124. const void *send_buf,
  125. rt_size_t send_length,
  126. void *recv_buf,
  127. rt_size_t recv_length)
  128. {
  129. rt_err_t result;
  130. struct rt_spi_message message;
  131. rt_uint8_t dummy[128] ;
  132. rt_memset(dummy, DUMMY, sizeof(dummy));
  133. RT_ASSERT(device != RT_NULL);
  134. RT_ASSERT(device->bus != RT_NULL);
  135. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  136. if (result == RT_EOK)
  137. {
  138. if (device->bus->owner != device)
  139. {
  140. /* not the same owner as current, re-configure SPI bus */
  141. result = device->bus->ops->configure(device, &device->config);
  142. if (result == RT_EOK)
  143. {
  144. /* set SPI bus owner */
  145. device->bus->owner = device;
  146. }
  147. else
  148. {
  149. /* configure SPI bus failed */
  150. result = -RT_EIO;
  151. goto __exit;
  152. }
  153. }
  154. /* send data */
  155. message.send_buf = send_buf;
  156. message.recv_buf = RT_NULL;
  157. message.length = send_length;
  158. message.cs_take = 1;
  159. message.cs_release = 0;
  160. message.next = RT_NULL;
  161. result = device->bus->ops->xfer(device, &message);
  162. if (result == 0)
  163. {
  164. result = -RT_EIO;
  165. goto __exit;
  166. }
  167. /* recv data */
  168. message.send_buf = dummy;
  169. message.recv_buf = recv_buf;
  170. message.length = recv_length;
  171. message.cs_take = 0;
  172. message.cs_release = 1;
  173. message.next = RT_NULL;
  174. result = device->bus->ops->xfer(device, &message);
  175. if (result == 0)
  176. {
  177. result = -RT_EIO;
  178. goto __exit;
  179. }
  180. result = RT_EOK;
  181. }
  182. else
  183. {
  184. return -RT_EIO;
  185. }
  186. __exit:
  187. rt_mutex_release(&(device->bus->lock));
  188. return result;
  189. }
  190. static void rtgui_touch_calculate(void)
  191. {
  192. if (touch != RT_NULL)
  193. {
  194. /* read touch */
  195. {
  196. rt_uint8_t i, j, k, min;
  197. rt_uint16_t temp;
  198. rt_uint16_t tmpxy[2][SAMP_CNT];
  199. rt_uint8_t send_buffer[1];
  200. rt_uint8_t recv_buffer[2];
  201. for(i=0; i<SAMP_CNT; i++)
  202. {
  203. send_buffer[0] = TOUCH_MSR_X;
  204. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  205. rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4);
  206. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  207. tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  208. tmpxy[1][i] >>= 4;
  209. #else
  210. tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  211. tmpxy[0][i] >>=4;
  212. #endif
  213. send_buffer[0] = TOUCH_MSR_Y;
  214. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  215. rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4);
  216. #if defined(_ILI_HORIZONTAL_DIRECTION_)
  217. tmpxy[0][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  218. tmpxy[0][i] >>= 4;
  219. #else
  220. tmpxy[1][i] = (recv_buffer[0]<<8)|recv_buffer[1] ;
  221. tmpxy[1][i] >>= 4;
  222. #endif
  223. }
  224. /*再次打开触摸中断*/
  225. send_buffer[0] = 1 << 7;
  226. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  227. touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
  228. /* calculate average */
  229. {
  230. rt_uint32_t total_x = 0;
  231. rt_uint32_t total_y = 0;
  232. for(k=0; k<2; k++)
  233. {
  234. // sorting the ADC value
  235. for(i=0; i<SAMP_CNT-1; i++)
  236. {
  237. min=i;
  238. for (j=i+1; j<SAMP_CNT; j++)
  239. {
  240. if (tmpxy[k][min] > tmpxy[k][j])
  241. min=j;
  242. }
  243. temp = tmpxy[k][i];
  244. tmpxy[k][i] = tmpxy[k][min];
  245. tmpxy[k][min] = temp;
  246. }
  247. //check value for Valve value
  248. if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH)
  249. {
  250. return;
  251. }
  252. }
  253. total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1];
  254. total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1];
  255. //calculate average value
  256. touch->x=total_x>>2;
  257. touch->y=total_y>>2;
  258. rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y);
  259. } /* calculate average */
  260. } /* read touch */
  261. /* if it's not in calibration status */
  262. /*触摸值缩放*/
  263. if (touch->calibrating != RT_TRUE)
  264. {
  265. if (touch->max_x > touch->min_x)
  266. {
  267. touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
  268. }
  269. else
  270. {
  271. touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
  272. }
  273. if (touch->max_y > touch->min_y)
  274. {
  275. touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
  276. }
  277. else
  278. {
  279. touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
  280. }
  281. }
  282. }
  283. }
  284. #include "ls1c_regs.h"
  285. #define TOUCH_INT_EN __REG32(LS1C_INT4_EN)
  286. rt_inline void touch_int_cmd(rt_bool_t NewState)
  287. {
  288. if(NewState == RT_TRUE)
  289. {
  290. //TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64));
  291. reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
  292. }
  293. else
  294. {
  295. //TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64)));
  296. reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
  297. }
  298. }
  299. void ls1c_touch_irqhandler(void) /* TouchScreen */
  300. {
  301. if(gpio_get(TOUCH_INT_PIN)==0)
  302. {
  303. /* 触摸屏按下后操作 */
  304. if (gpio_level_low == gpio_get(led_gpio))
  305. gpio_set(led_gpio, gpio_level_high);
  306. else
  307. gpio_set(led_gpio, gpio_level_low);
  308. touch_int_cmd(RT_FALSE);
  309. rt_event_send(&touch->event, 1);
  310. }
  311. }
  312. /*管脚初始化,配置中断打开SPI1 CS0 设备*/
  313. rt_inline void touch_init(void)
  314. {
  315. unsigned int touch_int_gpio = TOUCH_INT_PIN; // 触摸屏中断
  316. int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio);
  317. // 初始化按键中断
  318. gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING);
  319. rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch");
  320. rt_hw_interrupt_umask(touch_irq);
  321. gpio_init(touch_int_gpio, gpio_mode_input);
  322. // 初始化led
  323. gpio_init(led_gpio, gpio_mode_output);
  324. gpio_set(led_gpio, gpio_level_high);
  325. }
  326. /* RT-Thread Device Interface */
  327. static rt_err_t rtgui_touch_init (rt_device_t dev)
  328. {
  329. rt_uint8_t send;
  330. rt_uint8_t recv_buffer[2];
  331. struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev;
  332. touch_init();
  333. rt_kprintf("touch_init ...\n");
  334. send = START | DIFFERENTIAL | POWER_MODE0;
  335. touch_send_then_recv(touch->spi_device, &send, 1, recv_buffer, 2);
  336. return RT_EOK;
  337. }
  338. static rt_err_t rtgui_touch_control (rt_device_t dev, int cmd, void *args)
  339. {
  340. switch (cmd)
  341. {
  342. case RT_TOUCH_CALIBRATION:
  343. touch->calibrating = RT_TRUE;
  344. touch->calibration_func = (rt_touch_calibration_func_t)args;
  345. break;
  346. case RT_TOUCH_NORMAL:
  347. touch->calibrating = RT_FALSE;
  348. break;
  349. case RT_TOUCH_CALIBRATION_DATA:
  350. {
  351. struct calibration_data* data;
  352. data = (struct calibration_data*) args;
  353. //update
  354. touch->min_x = data->min_x;
  355. touch->max_x = data->max_x;
  356. touch->min_y = data->min_y;
  357. touch->max_y = data->max_y;
  358. }
  359. break;
  360. }
  361. return RT_EOK;
  362. }
  363. void _set_mouse_position(rt_uint32_t X, rt_uint32_t Y)
  364. {}
  365. static void touch_thread_entry(void *parameter)
  366. {
  367. rt_bool_t touch_down = RT_FALSE;
  368. rt_uint32_t event_value;
  369. struct rtgui_event_mouse emouse;
  370. static struct _touch_previous
  371. {
  372. rt_uint32_t x;
  373. rt_uint32_t y;
  374. } touch_previous;
  375. RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  376. emouse.wid = RT_NULL;
  377. while(1)
  378. {
  379. /* 接收到触摸中断事件 */
  380. if(rt_event_recv(&touch->event,
  381. 1,
  382. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  383. 100,
  384. &event_value)
  385. == RT_EOK)
  386. {
  387. while(1)
  388. {
  389. if (IS_TOUCH_UP())
  390. {
  391. /* 触摸笔抬起 */
  392. /* touch up */
  393. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);
  394. /* use old value */
  395. emouse.x = touch->x;
  396. emouse.y = touch->y;
  397. if(touch_down != RT_TRUE)
  398. {
  399. touch_int_cmd(RT_TRUE);
  400. break;
  401. }
  402. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  403. {
  404. /* 触摸校准处理 */
  405. /* callback function */
  406. touch->calibration_func(emouse.x, emouse.y);
  407. }
  408. else
  409. {
  410. /* 向ui发送触摸坐标 */
  411. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  412. }
  413. rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);
  414. /* clean */
  415. touch_previous.x = touch_previous.y = 0;
  416. touch_down = RT_FALSE;
  417. touch_int_cmd(RT_TRUE);
  418. break;
  419. } /* touch up */
  420. else /* touch down or move */
  421. {
  422. if(touch_down == RT_FALSE)
  423. {
  424. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  425. }
  426. else
  427. {
  428. rt_thread_delay(5);
  429. }
  430. if(IS_TOUCH_UP()) continue;
  431. /* calculation */
  432. rtgui_touch_calculate();
  433. /* send mouse event */
  434. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  435. emouse.parent.sender = RT_NULL;
  436. emouse.x = touch->x;
  437. emouse.y = touch->y;
  438. _set_mouse_position(emouse.x, emouse.y);
  439. /* 光标跟随 */
  440. /* init mouse button */
  441. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);
  442. /* send event to server */
  443. if (touch->calibrating != RT_TRUE)
  444. {
  445. #define previous_keep 8
  446. /* filter. */
  447. if((touch_previous.x > touch->x + previous_keep)
  448. || (touch_previous.x < touch->x - previous_keep)
  449. || (touch_previous.y > touch->y + previous_keep)
  450. || (touch_previous.y < touch->y - previous_keep))
  451. {
  452. touch_previous.x = touch->x;
  453. touch_previous.y = touch->y;
  454. /* 向ui发送触摸坐标 */
  455. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  456. if(touch_down == RT_FALSE)
  457. {
  458. touch_down = RT_TRUE;
  459. rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
  460. }
  461. else
  462. {
  463. rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
  464. }
  465. }
  466. }
  467. else
  468. {
  469. touch_down = RT_TRUE;
  470. }
  471. } /* touch down or move */
  472. } /* read touch */
  473. } /* event recv */
  474. } /* thread while(1) */
  475. }
  476. rt_err_t rtgui_touch_hw_init(const char * spi_device_name)
  477. {
  478. rt_uint32_t arg[2];
  479. struct rt_device * spi_device;
  480. struct rt_thread * touch_thread;
  481. rt_err_t err;
  482. rt_kprintf("spi1 cs0 start...\n");
  483. spi_device = rt_device_find("spi10");
  484. if(spi_device == RT_NULL)
  485. {
  486. rt_kprintf("Did not find spi1, exit thread....\n");
  487. return;
  488. }
  489. err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR);
  490. if(err != RT_EOK)
  491. {
  492. rt_kprintf("Open spi1 failed %08X, exit thread....\n", err);
  493. return;
  494. }
  495. /* config spi */
  496. {
  497. struct rt_spi_configuration cfg;
  498. cfg.data_width = 8;
  499. cfg.mode = RT_SPI_MODE_0;
  500. cfg.max_hz = 200 * 1000; /* 200K */
  501. rt_spi_configure((struct rt_spi_device *)spi_device, &cfg);
  502. }
  503. touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
  504. if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */
  505. /* clear device structure */
  506. rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
  507. rt_event_init(&touch->event, "touch", RT_IPC_FLAG_FIFO);
  508. touch->spi_device = (struct rt_spi_device *)spi_device;
  509. touch->calibrating = false;
  510. touch->min_x = MIN_X_DEFAULT;
  511. touch->max_x = MAX_X_DEFAULT;
  512. touch->min_y = MIN_Y_DEFAULT;
  513. touch->max_y = MAX_Y_DEFAULT;
  514. /* init device structure */
  515. touch->parent.type = RT_Device_Class_Miscellaneous;
  516. touch->parent.init = rtgui_touch_init;
  517. touch->parent.control = rtgui_touch_control;
  518. touch->parent.user_data = RT_NULL;
  519. /* register touch device to RT-Thread */
  520. rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
  521. touch_thread = rt_thread_create("touch_thread",
  522. touch_thread_entry, RT_NULL,
  523. 4096, RTGUI_SVR_THREAD_PRIORITY-1, 1);
  524. if (touch_thread != RT_NULL) rt_thread_startup(touch_thread);
  525. rt_device_init((rt_device_t)touch);
  526. return RT_EOK;
  527. }
  528. #endif