luat_tp_gt911.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #include "luat_base.h"
  2. #include "luat_tp.h"
  3. #include "luat_gpio.h"
  4. #include "luat_mem.h"
  5. #include "luat_rtos.h"
  6. #include "luat_tp_reg.h"
  7. #define LUAT_LOG_TAG "gt911"
  8. #include "luat_log.h"
  9. #define GT911_ADDRESS0 (0x5D)
  10. #define GT911_ADDRESS1 (0x14)
  11. #define GT911_PRODUCT_ID_CODE (0x00313139) // "911" ASCII code
  12. #define GT911_COMMAND_REG (0x8040)
  13. #define GT911_CONFIG_REG (0x8047)
  14. #define GT911_CONFIG_VERSION (0x8047)
  15. #define GT911_X_OUTPUT_MAX (0x8048)
  16. #define GT911_Y_OUTPUT_MAX (0x804A)
  17. #define GT911_TOUCH_NUMBER (0x804C)
  18. #define GT911_MODULE_SWITCH1 (0x804D)
  19. #define GT911_MODULE_SWITCH2 (0x804E)
  20. #define GT911_SHAKE_COUNT (0x804F)
  21. #define GT911_FILTER (0x8050)
  22. #define GT911_LARGE_TOUCH (0x8051)
  23. #define GT911_NOISE_REDUCTION (0x8052)
  24. #define GT911_SCREEN_TOUCH_LEVEL (0x8053)
  25. #define GT911_SCREEN_LEVEL_TOUCH (0x8054)
  26. #define GT911_LOW_POWER_CONTROL (0x8055)
  27. #define GT911_REFRESH_RATE (0x8056)
  28. #define GT911_X_THRESHOLD (0x8057)
  29. #define GT911_Y_THRESHOLD (0x8058)
  30. #define GT911_X_SPEED_LIMIT (0x8059)
  31. #define GT911_Y_SPEED_LIMIT (0x805A)
  32. #define GT911_SPACE (0x805B)
  33. #define GT911_STRETCH_RATE (0x805D)
  34. #define GT911_STRETCH_R0 (0x805E)
  35. #define GT911_STRETCH_R1 (0x805F)
  36. #define GT911_STRETCH_R2 (0x8060)
  37. #define GT911_STRETCH_RM (0x8061)
  38. #define GT911_DRV_GROUPA_NUM (0x8062)
  39. #define GT911_DRV_GROUPB_NUM (0x8063)
  40. #define GT911_SENSOR_NUM (0x8064)
  41. #define GT911_FREQA_FACTOR (0x8065)
  42. #define GT911_FREQB_FACTOR (0x8066)
  43. #define GT911_PANNEL_BITFREQL (0x8067)
  44. #define GT911_PANNEL_BITFREQH (0x8068)
  45. #define GT911_PANNEL_SENSOR_TIMEL (0x8069)
  46. #define GT911_PANNEL_SENSOR_TIMEH (0x806A)
  47. #define GT911_PANNEL_TX_GAIN (0x806B)
  48. #define GT911_PANNEL_RX_GAIN (0x806C)
  49. #define GT911_CHECK_SUM (0x80FF)
  50. #define GT911_CONFIG_FRESH (0x8100)
  51. #define GT911_PRODUCT_ID (0x8140)
  52. #define GT911_FIRMWARE_VERSION (0x8144)
  53. #define GT911_X_RESOLUTION (0x8146)
  54. #define GT911_Y_RESOLUTION (0x8148)
  55. #define GT911_VENDOR_ID (0x814A)
  56. #define GT911_STATUS (0x814E)
  57. #define GT911_POINT1_REG (0x814F)
  58. #define GT911_POINT2_REG (0x8157)
  59. #define GT911_POINT3_REG (0x815F)
  60. #define GT911_POINT4_REG (0x8167)
  61. #define GT911_POINT5_REG (0x816F)
  62. #define GT911_CONFIG_SIZE (GT911_CONFIG_FRESH - GT911_CONFIG_REG + 1)
  63. #define GT911_POINT_INFO_NUM (8)
  64. #define GT911_TOUCH_NUMBER_MIN (1)
  65. #define GT911_TOUCH_NUMBER_MAX (5)
  66. #define GT911_REFRESH_RATE_MIN (5)
  67. #define GT911_REFRESH_RATE_MAX (20)
  68. typedef struct luat_touch_info{
  69. union{
  70. struct {
  71. uint64_t version:8;
  72. uint64_t x_max:16;
  73. uint64_t y_max:16;
  74. uint64_t touch_num:4;
  75. uint64_t :4;
  76. uint64_t int_type:2;
  77. uint64_t :1;
  78. uint64_t x2y:1;
  79. uint64_t stretch_rank:2;
  80. uint64_t :2;
  81. uint64_t :8;
  82. };
  83. uint64_t info;
  84. };
  85. }luat_tp_info_t;
  86. static uint8_t gt911_init_state = 0;
  87. // static uint8_t gt911_cfg_table[GT911_CONFIG_SIZE] ={
  88. // // #if 1
  89. // // 0x41,0x20,0x03,0xe0,0x01,0x05,0x3d,0x00,0x01,0x08,0x28,0x05,0x50,0x32,0x03,0x05,
  90. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x1a,0x1f,0x14,0x8c,0x24,0x0a,0x1b,0x19,
  91. // // 0xf4,0x0a,0x00,0x00,0x00,0x21,0x04,0x1d,0x00,0x00,0x00,0x00,0x00,0x03,0x64,0x32,
  92. // // 0x00,0x00,0x00,0x11,0xb2,0x94,0xc5,0x02,0x07,0x00,0x00,0x04,0x8e,0x16,0x00,0x5d,
  93. // // 0x23,0x00,0x3d,0x38,0x00,0x2a,0x5a,0x00,0x22,0x90,0x00,0x22,0x00,0x00,0x00,0x00,
  94. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  95. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  96. // // 0x14,0x12,0x10,0x0e,0x0c,0x0a,0x08,0x06,0x04,0x02,0xff,0xff,0xff,0xff,0x00,0x00,
  97. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1d,0x1c,
  98. // // 0x18,0x16,0x14,0x13,0x12,0x10,0x0f,0x0c,0x0a,0x08,0x06,0x04,0x02,0x00,0xff,0xff,
  99. // // 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  100. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2a,0x00
  101. // // #else
  102. // // 0x42,0x20,0x03,0xE0,0x01,0x01,0x3D,0x00,0x01,0x08,0x28,0x05,0x50,0x32,0x03,0x05,
  103. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x1A,0x1F,0x14,0x8C,0x24,0x0A,0x1B,0x19,
  104. // // 0xF4,0x0A,0x00,0x00,0x00,0x20,0x04,0x1C,0x00,0x00,0x00,0x00,0x00,0x03,0x64,0x32,
  105. // // 0x00,0x00,0x00,0x11,0xB2,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x8E,0x16,0x00,0x5D,
  106. // // 0x23,0x00,0x3D,0x38,0x00,0x2A,0x5A,0x00,0x22,0x90,0x00,0x22,0x00,0x00,0x00,0x00,
  107. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  108. // // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  109. // // 0x14,0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  110. // // 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1D,0x1C,
  111. // // 0x18,0x16,0x14,0x13,0x12,0x10,0x0F,0x0C,0x0A,0x08,0x06,0x04,0x02,0x00,0xFF,0xFF,
  112. // // 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  113. // // 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x4F,0x00
  114. // // #endif
  115. // 0x6b,0x00,0x04,0x58,0x02,0x05,0x0d,0x00,0x01,0x0f,0x28,0x0f,0x50,0x32,0x03,0x05,
  116. // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x8a,0x2a,0x0c,0x45,0x47,
  117. // 0x0c,0x08,0x00,0x00,0x00,0x40,0x03,0x2c,0x00,0x01,0x00,0x00,0x00,0x03,0x64,0x32,
  118. // 0x00,0x00,0x00,0x28,0x64,0x94,0xd5,0x02,0x07,0x00,0x00,0x04,0x95,0x2c,0x00,0x8b,
  119. // 0x34,0x00,0x82,0x3f,0x00,0x7d,0x4c,0x00,0x7a,0x5b,0x00,0x7a,0x00,0x00,0x00,0x00,
  120. // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  121. // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  122. // 0x18,0x16,0x14,0x12,0x10,0x0e,0x0c,0x0a,0x08,0x06,0x04,0x02,0xff,0xff,0x00,0x00,
  123. // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x16,0x18,
  124. // 0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x24,0x13,0x12,0x10,0x0f,0x0a,0x08,0x06,0x04,
  125. // 0x02,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  126. // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x79,0x01,
  127. // };
  128. static int gt911_obtain_config(luat_tp_config_t* luat_tp_config, uint8_t *config, uint8_t size){
  129. if (tp_i2c_read_reg16(luat_tp_config, GT911_CONFIG_REG, config, size, 1)){
  130. LLOGE("obtain config regs fail!");
  131. return -1;
  132. }
  133. uint8_t check_sum = 0;
  134. for (uint8_t index=0; index<size-2; index++){
  135. check_sum += config[index];
  136. }
  137. check_sum = (~check_sum) + 1;
  138. if (config[GT911_CHECK_SUM - GT911_CONFIG_REG] != check_sum){
  139. LLOGE("check sum 0X%02x and 0X%02x is not equal!", config[GT911_CHECK_SUM - GT911_CONFIG_REG], check_sum);
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. static int gt911_update_config(luat_tp_config_t* luat_tp_config, uint8_t *config, uint8_t size){
  145. uint8_t check_sum = 0;
  146. for (uint8_t index=0; index<size-2; index++){
  147. check_sum += config[index];
  148. }
  149. check_sum = (~check_sum) + 1;
  150. config[GT911_CHECK_SUM - GT911_CONFIG_REG] = check_sum;
  151. config[GT911_CONFIG_FRESH - GT911_CONFIG_REG] = 1;
  152. if (tp_i2c_write_reg16(luat_tp_config, GT911_CONFIG_REG, config, size)){
  153. LLOGE("write config regs fail!");
  154. return -1;
  155. }
  156. return 0;
  157. }
  158. int tp_gt911_read_status(luat_tp_config_t* luat_tp_config, uint8_t *status){
  159. if (tp_i2c_read_reg16(luat_tp_config, GT911_STATUS, status, 1, 1)){
  160. LLOGE("read status reg fail!\r\n");
  161. return -1;
  162. }
  163. // LLOGD("status=0x%02X\r\n", *status); // 调试需要看!!!
  164. return 0;
  165. }
  166. int tp_gt911_clear_status(luat_tp_config_t* luat_tp_config){
  167. if (tp_i2c_write_reg16(luat_tp_config, GT911_STATUS, (uint8_t[]){0x00}, 1)){
  168. LLOGE("write status reg fail!");
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. static int tp_gt911_detect(luat_tp_config_t* luat_tp_config){
  174. uint32_t product_id = 0;
  175. luat_tp_config->address = GT911_ADDRESS0;
  176. tp_i2c_read_reg16(luat_tp_config, GT911_PRODUCT_ID, &product_id, sizeof(product_id), 1);
  177. if (product_id == GT911_PRODUCT_ID_CODE){
  178. LLOGI("TP find device GT911 ,address:0x%02X",luat_tp_config->address);
  179. return 0;
  180. }else{
  181. luat_tp_config->address = GT911_ADDRESS1;
  182. tp_i2c_read_reg16(luat_tp_config, GT911_PRODUCT_ID, &product_id, sizeof(product_id), 1);
  183. if (product_id == GT911_PRODUCT_ID_CODE){
  184. LLOGI("TP find device GT911 ,address:0x%02X",luat_tp_config->address);
  185. return 0;
  186. }else{
  187. return -1;
  188. }
  189. }
  190. }
  191. static int luat_tp_irq_cb(int pin, void *args){
  192. if (gt911_init_state == 0){
  193. return -1;
  194. }
  195. luat_tp_config_t* luat_tp_config = (luat_tp_config_t*)args;
  196. luat_tp_irq_enable(luat_tp_config, 0);
  197. luat_rtos_message_send(luat_tp_config->task_handle, 1, args);
  198. return 0;
  199. }
  200. static int tp_gt911_gpio_init(luat_tp_config_t* luat_tp_config){
  201. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  202. luat_gpio_mode(luat_tp_config->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW);
  203. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_LOW);
  204. }
  205. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  206. luat_gpio_mode(luat_tp_config->pin_int, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW);
  207. luat_gpio_set(luat_tp_config->pin_int, Luat_GPIO_LOW);
  208. }
  209. // /* 0x14 */
  210. // luat_gpio_set(luat_tp_config->pin_int, Luat_GPIO_HIGH);
  211. /* 0x5D */
  212. luat_gpio_set(luat_tp_config->pin_int, Luat_GPIO_LOW);
  213. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  214. luat_rtos_task_sleep(1);
  215. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  216. luat_rtos_task_sleep(5);
  217. luat_rtos_task_sleep(2);
  218. luat_gpio_set(luat_tp_config->pin_rst, Luat_GPIO_HIGH);
  219. }
  220. return 0;
  221. }
  222. static int tp_gt911_soft_reset(luat_tp_config_t* luat_tp_config){
  223. return tp_i2c_write_reg16(luat_tp_config, GT911_COMMAND_REG, (uint8_t[]){0x02}, 1);
  224. }
  225. static int tp_gt911_init(luat_tp_config_t* luat_tp_config){
  226. int ret = 0;
  227. luat_rtos_task_sleep(100);
  228. tp_gt911_gpio_init(luat_tp_config);
  229. ret = tp_gt911_detect(luat_tp_config);
  230. if (ret){
  231. LLOGE("tp detect fail!");
  232. return ret;
  233. }
  234. uint8_t cfg_table[GT911_CONFIG_SIZE] = {0};
  235. gt911_obtain_config(luat_tp_config, cfg_table, GT911_CONFIG_SIZE);
  236. // renew config parameters.
  237. memcpy((uint8_t*)(&cfg_table[GT911_X_OUTPUT_MAX - GT911_CONFIG_REG]), (uint8_t*)(&luat_tp_config->w), 2);
  238. memcpy((uint8_t*)(&cfg_table[GT911_Y_OUTPUT_MAX - GT911_CONFIG_REG]), (uint8_t*)(&luat_tp_config->h), 2);
  239. if (LUAT_GPIO_RISING_IRQ == luat_tp_config->int_type){
  240. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] &= 0xFC;
  241. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] |= 0x00;
  242. }else if (LUAT_GPIO_FALLING_IRQ == luat_tp_config->int_type){
  243. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] &= 0xFC;
  244. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] |= 0x01;
  245. }
  246. // xy cordinate swap.
  247. if (luat_tp_config->swap_xy){
  248. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] &= 0xF7;
  249. cfg_table[GT911_MODULE_SWITCH1 - GT911_CONFIG_REG] |= 0x08;
  250. }
  251. // refresh rate.
  252. if (luat_tp_config->refresh_rate > GT911_REFRESH_RATE_MAX || luat_tp_config->refresh_rate == 0){
  253. cfg_table[GT911_REFRESH_RATE - GT911_CONFIG_REG] = GT911_REFRESH_RATE_MAX;
  254. }else if (luat_tp_config->refresh_rate < GT911_REFRESH_RATE_MIN){
  255. cfg_table[GT911_REFRESH_RATE - GT911_CONFIG_REG] = GT911_REFRESH_RATE_MIN;
  256. }else{
  257. cfg_table[GT911_REFRESH_RATE - GT911_CONFIG_REG] = luat_tp_config->refresh_rate - GT911_REFRESH_RATE_MIN;
  258. }
  259. // touch number.
  260. if (luat_tp_config->tp_num > GT911_TOUCH_NUMBER_MAX || luat_tp_config->tp_num == 0){
  261. cfg_table[GT911_TOUCH_NUMBER - GT911_CONFIG_REG] = GT911_TOUCH_NUMBER_MAX;
  262. }else if (luat_tp_config->tp_num < GT911_TOUCH_NUMBER_MIN){
  263. cfg_table[GT911_TOUCH_NUMBER - GT911_CONFIG_REG] = GT911_TOUCH_NUMBER_MIN;
  264. }else{
  265. cfg_table[GT911_TOUCH_NUMBER - GT911_CONFIG_REG] = luat_tp_config->tp_num;
  266. }
  267. gt911_update_config(luat_tp_config, cfg_table, GT911_CONFIG_SIZE);
  268. // gt911_update_config(luat_tp_config, gt911_cfg_table, GT911_CONFIG_SIZE);
  269. // luat_rtos_task_sleep(5);
  270. // tp_gt911_soft_reset(luat_tp_config);
  271. luat_rtos_task_sleep(20);
  272. // tp_i2c_write_reg16(luat_tp_config, GT911_COMMAND_REG, (uint8_t[]){0x00}, 1);
  273. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  274. luat_gpio_t gpio = {0};
  275. gpio.pin = luat_tp_config->pin_int;
  276. gpio.mode = Luat_GPIO_IRQ;
  277. gpio.pull = Luat_GPIO_DEFAULT;
  278. gpio.irq = luat_tp_config->int_type;
  279. gpio.irq_cb = luat_tp_irq_cb;
  280. gpio.irq_args = luat_tp_config;
  281. luat_gpio_setup(&gpio);
  282. }
  283. gt911_init_state = 1;
  284. return ret;
  285. }
  286. static int tp_gt911_deinit(luat_tp_config_t* luat_tp_config){
  287. gt911_init_state = 0;
  288. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  289. luat_gpio_close(luat_tp_config->pin_int);
  290. }
  291. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  292. luat_gpio_close(luat_tp_config->pin_rst);
  293. }
  294. return 0;
  295. }
  296. static int tp_gt911_get_info(luat_tp_config_t* luat_tp_config, luat_tp_info_t *luat_touch_info){
  297. return tp_i2c_read_reg16(luat_tp_config, GT911_CONFIG_REG, luat_touch_info, sizeof(luat_tp_info_t), 1);
  298. }
  299. static void tp_gt911_read_done(luat_tp_config_t * luat_tp_config)
  300. {
  301. luat_tp_irq_enable(luat_tp_config, 1);
  302. }
  303. // gt911 get tp info.
  304. static int16_t pre_x[GT911_TOUCH_NUMBER_MAX] = {-1, -1, -1, -1, -1};
  305. static int16_t pre_y[GT911_TOUCH_NUMBER_MAX] = {-1, -1, -1, -1, -1};
  306. static int16_t pre_w[GT911_TOUCH_NUMBER_MAX] = {-1, -1, -1, -1, -1};
  307. static uint8_t s_tp_down[GT911_TOUCH_NUMBER_MAX];
  308. static uint8_t read_buff[GT911_POINT_INFO_NUM * GT911_TOUCH_NUMBER_MAX];
  309. void gt911_touch_up(void *buf, int8_t id){
  310. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  311. if(s_tp_down[id] == 1){
  312. s_tp_down[id] = 0;
  313. read_data[id].event = TP_EVENT_TYPE_UP;
  314. }else{
  315. read_data[id].event = TP_EVENT_TYPE_NONE;
  316. }
  317. read_data[id].timestamp = luat_mcu_ticks();
  318. read_data[id].width = pre_w[id];
  319. read_data[id].x_coordinate = pre_x[id];
  320. read_data[id].y_coordinate = pre_y[id];
  321. read_data[id].track_id = id;
  322. pre_x[id] = -1; /* last point is none */
  323. pre_y[id] = -1;
  324. pre_w[id] = -1;
  325. }
  326. void gt911_touch_down(void *buf, int8_t id, int16_t x, int16_t y, int16_t w){
  327. luat_tp_data_t *read_data = (luat_tp_data_t *)buf;
  328. if (s_tp_down[id] == 1){
  329. read_data[id].event = TP_EVENT_TYPE_MOVE;
  330. }else{
  331. read_data[id].event = TP_EVENT_TYPE_DOWN;
  332. s_tp_down[id] = 1;
  333. }
  334. read_data[id].timestamp = luat_mcu_ticks();
  335. read_data[id].width = w;
  336. read_data[id].x_coordinate = x;
  337. read_data[id].y_coordinate = y;
  338. read_data[id].track_id = id;
  339. pre_x[id] = x; /* save last point */
  340. pre_y[id] = y;
  341. pre_w[id] = w;
  342. }
  343. void gt911_read_point(uint8_t *input_buff, void *buf, uint8_t touch_num){
  344. uint8_t *read_buf = input_buff;
  345. uint8_t read_index;
  346. int8_t read_id = 0;
  347. int16_t input_x = 0;
  348. int16_t input_y = 0;
  349. int16_t input_w = 0;
  350. static uint8_t pre_touch = 0;
  351. static int8_t pre_id[GT911_TOUCH_NUMBER_MAX] = {0};
  352. if (pre_touch > touch_num){ /* point up */
  353. for (read_index = 0; read_index < pre_touch; read_index++){
  354. uint8_t j;
  355. for (j = 0; j < touch_num; j++){ /* this time touch num */
  356. read_id = read_buf[j * 8] & 0x0F;
  357. if (read_id >= GT911_POINT_INFO_NUM){
  358. LLOGE("%s, touch ID %d is out range!\r\n", __func__, read_id);
  359. return;
  360. }
  361. if (pre_id[read_index] == read_id) /* this id is not free */
  362. break;
  363. if (j >= touch_num - 1){
  364. uint8_t up_id;
  365. up_id = pre_id[read_index];
  366. gt911_touch_up(buf, up_id);
  367. }
  368. }
  369. }
  370. }
  371. if (touch_num){ /* point down */
  372. uint8_t off_set;
  373. for (read_index = 0; read_index < touch_num; read_index++){
  374. off_set = read_index * 8;
  375. read_id = read_buf[off_set] & 0x0F;
  376. if (read_id >= GT911_POINT_INFO_NUM){
  377. LLOGE("%s, touch ID %d is out range!\r\n", __func__, read_id);
  378. return;
  379. }
  380. pre_id[read_index] = read_id;
  381. input_x = read_buf[off_set + 1] | (read_buf[off_set + 2] << 8); /* x */
  382. input_y = read_buf[off_set + 3] | (read_buf[off_set + 4] << 8); /* y */
  383. input_w = read_buf[off_set + 5] | (read_buf[off_set + 6] << 8); /* size */
  384. gt911_touch_down(buf, read_id, input_x, input_y, input_w);
  385. }
  386. }else if (pre_touch){
  387. for(read_index = 0; read_index < pre_touch; read_index++){
  388. gt911_touch_up(buf, pre_id[read_index]);
  389. }
  390. }
  391. pre_touch = touch_num;
  392. }
  393. static int tp_gt911_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_tp_data){
  394. uint8_t touch_num=0, point_status=0;
  395. // luat_tp_info_t luat_touch_info = {0};
  396. // tp_gt911_get_info(luat_tp_config, &luat_touch_info);
  397. // uint8_t read_num = luat_touch_info.touch_num;
  398. // LLOGD("tp_gt911_read read_num:%d",read_num);
  399. tp_gt911_read_status(luat_tp_config, &point_status);
  400. if (point_status == 0){ /* no data */
  401. goto exit_;
  402. }
  403. if ((point_status & 0x80) == 0){ /* data is not ready */
  404. goto exit_;
  405. }
  406. touch_num = point_status & 0x0F; /* get point num */
  407. if (touch_num > GT911_TOUCH_NUMBER_MAX) {/* point num is not correct */
  408. touch_num = 0;
  409. goto exit_;
  410. }
  411. // LLOGD("tp_gt911_read touch_num:%d",touch_num);
  412. memset(read_buff, 0x00, sizeof(read_buff));
  413. tp_i2c_read_reg16(luat_tp_config, GT911_POINT1_REG, read_buff, touch_num * GT911_POINT_INFO_NUM, 1);
  414. gt911_read_point(read_buff, luat_tp_data, touch_num);
  415. exit_:
  416. tp_gt911_clear_status(luat_tp_config);
  417. return touch_num;
  418. }
  419. luat_tp_opts_t tp_config_gt911 = {
  420. .name = "gt911",
  421. .init = tp_gt911_init,
  422. .deinit = tp_gt911_deinit,
  423. .read = tp_gt911_read,
  424. .read_done = tp_gt911_read_done,
  425. };