tp_demo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_tp.h"
  17. #include "ql_lcd.h"
  18. #include "ql_gpio.h"
  19. #include "ql_adc.h"
  20. #include "ql_pwm.h"
  21. #include "ql_log.h"
  22. #include "tp_demo.h"
  23. #define QL_TP_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  24. #define QL_TP_DEMO_LOG(msg, ...) QL_LOG(QL_TP_DEMO_LOG_LEVEL, "ql_TP_DEMO", msg, ##__VA_ARGS__)
  25. #define QL_TP_DEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_TP_DEMO", msg, ##__VA_ARGS__)
  26. ql_task_t tp_demo_task = NULL;
  27. #define QL_TP_LCD_CALIB_WIDTH 20 //在LCD屏幕坐标显示校准+的宽度
  28. #define QL_TP_LCD_CALIB_HEIGHT 4 //在LCD屏幕坐标显示校准+的高度
  29. #define QL_TP_LCD_WIDTH 240 //LCD屏幕宽度,需要改成实际屏的宽度
  30. #define QL_TP_LCD_HIGH 320 //LCD屏幕高度,需要改成实际屏的高度
  31. //注意,3个校准点不能在一条直线上
  32. #define QL_TP_CALIB_POINT_X1 (0) //左上角
  33. #define QL_TP_CALIB_POINT_Y1 (0)
  34. #define QL_TP_CALIB_POINT_X2 (QL_TP_LCD_WIDTH-QL_TP_LCD_CALIB_WIDTH-QL_TP_LCD_CALIB_HEIGHT) //右上角
  35. #define QL_TP_CALIB_POINT_Y2 (0)
  36. #define QL_TP_CALIB_POINT_X3 ((QL_TP_LCD_WIDTH-QL_TP_LCD_CALIB_WIDTH-QL_TP_LCD_CALIB_HEIGHT)/2) //屏幕中间
  37. #define QL_TP_CALIB_POINT_Y3 ((QL_TP_LCD_HIGH-QL_TP_LCD_CALIB_WIDTH-QL_TP_LCD_CALIB_HEIGHT)/2)
  38. void ql_tp_demo_calib_cb(QL_TP_CALIB_STATUS_E calib_status)
  39. {
  40. QL_TP_DEMO_LOG("calib_status=%d", calib_status);
  41. }
  42. static void ql_tp_demo_touch_cb(ql_tp_point_t point, QL_TP_PRESS_STATE_E state)
  43. {
  44. uint16_t draw_data[64];
  45. int i;
  46. for (i=0; i<64; i++)
  47. {
  48. draw_data[i] = QL_TP_LCD_BLACK;
  49. }
  50. QL_TP_DEMO_LOG("LCD x=%d,y=%d,state=%d", point.x, point.y, state);
  51. if (point.x > QL_TP_LCD_WIDTH - 3)
  52. {
  53. point.x = QL_TP_LCD_WIDTH - 3;
  54. }
  55. if (point.y > QL_TP_LCD_HIGH - 3)
  56. {
  57. point.y = QL_TP_LCD_HIGH - 3;
  58. }
  59. ql_lcd_write(draw_data, point.x, point.y, point.x+2, point.y+2);
  60. }
  61. static void ql_tp_demo_task_pthread(void *ctx)
  62. {
  63. QlOSStatus err = 0;
  64. ql_tp_ctg_t tp_cfg;
  65. ql_errcode_tp_e ret;
  66. ql_tp_calib_ctg_t calib_cfg = {0};
  67. ql_tp_touch_adc_ctg_t adc_cfg = {0};
  68. //控制背光,根据实际样机修改
  69. ql_pin_set_func(122, 2);
  70. ql_gpio_deinit(GPIO_29);
  71. ql_gpio_init(GPIO_29, GPIO_OUTPUT, PULL_NONE, LVL_HIGH);
  72. ql_rtos_task_sleep_s(2);
  73. if( ql_lcd_init() != QL_LCD_SUCCESS )
  74. {
  75. QL_TP_DEMO_LOG("LCD init failed");
  76. goto exit;
  77. }
  78. //控制背光,根据实际样机修改
  79. //ql_pwm_open(PWM_PWT);
  80. //ql_pwm_pwt_enable(50, 2000, 1000);
  81. //必须要先将对应的引脚设置为GPIO功能
  82. //XP使能脚 GPIO16
  83. ql_pin_set_func(29, 4);
  84. //XN使能脚 GPIO22
  85. ql_pin_set_func(32, 0);
  86. //YP使能脚 GPIO13
  87. ql_pin_set_func(53, 0);
  88. //YN使能脚 GPIO5
  89. ql_pin_set_func(27, 1);
  90. tp_cfg.xp_adc_channel = QL_ADC2_CHANNEL;
  91. tp_cfg.yp_adc_channel = QL_ADC1_CHANNEL;
  92. tp_cfg.xp = GPIO_16;
  93. tp_cfg.xn = GPIO_22;
  94. tp_cfg.yp = GPIO_13;
  95. tp_cfg.yn = GPIO_5;
  96. adc_cfg.timeout = 3000;
  97. adc_cfg.adc_diff = QL_TP_ADC_DIFF;
  98. adc_cfg.adc_num = QL_TP_ADC_VALUE_TIMES;
  99. ret = ql_tp_init(&tp_cfg, &adc_cfg);
  100. if (ret != QL_TP_SUCCESS)
  101. {
  102. QL_TP_DEMO_LOG("tp init failed");
  103. goto exit;
  104. }
  105. ql_tp_point_t point[QL_TP_CALIB_POINT_NUM] = {{QL_TP_CALIB_POINT_X1, QL_TP_CALIB_POINT_Y1},{QL_TP_CALIB_POINT_X2, QL_TP_CALIB_POINT_Y2},{QL_TP_CALIB_POINT_X3, QL_TP_CALIB_POINT_Y3}};
  106. calib_cfg.calib = QL_TP_NEED_CALIB;
  107. memcpy(&calib_cfg.point, point, sizeof(point));
  108. calib_cfg.calib_width = QL_TP_LCD_CALIB_WIDTH;
  109. calib_cfg.calib_height = QL_TP_LCD_CALIB_HEIGHT;
  110. ret = ql_tp_do_calib(&calib_cfg, ql_tp_demo_calib_cb);
  111. if (ret != QL_TP_SUCCESS)
  112. {
  113. QL_TP_DEMO_LOG("tp init failed");
  114. goto exit;
  115. }
  116. ret = ql_tp_open(ql_tp_demo_touch_cb);
  117. if (ret != QL_TP_SUCCESS)
  118. {
  119. QL_TP_DEMO_LOG("tp touch failed");
  120. goto exit;
  121. }
  122. //只是为了测试ql_tp_close,实际不需要关闭TP功能
  123. ql_rtos_task_sleep_s(30);
  124. ql_tp_close();
  125. ql_rtos_task_sleep_s(5);
  126. ret = ql_tp_open(ql_tp_demo_touch_cb);
  127. if (ret != QL_TP_SUCCESS)
  128. {
  129. QL_TP_DEMO_LOG("tp touch failed");
  130. goto exit;
  131. }
  132. while(1)
  133. {
  134. ql_rtos_task_sleep_s(10);
  135. }
  136. QL_TP_DEMO_LOG("ql_rtos_task_delete");
  137. exit:
  138. err = ql_rtos_task_delete(NULL);
  139. if(err != QL_OSI_SUCCESS)
  140. {
  141. QL_TP_DEMO_LOG("task deleted failed");
  142. }
  143. }
  144. QlOSStatus ql_tp_demo_init(void)
  145. {
  146. QlOSStatus err = QL_OSI_SUCCESS;
  147. err = ql_rtos_task_create(&tp_demo_task, TP_DEMO_TASK_STACK_SIZE, TP_DEMO_TASK_PRIO, "ql_tp_demo", ql_tp_demo_task_pthread, NULL, TP_DEMO_TASK_EVENT_CNT);
  148. if(err != QL_OSI_SUCCESS)
  149. {
  150. QL_TP_DEMO_LOG("demo_task created failed");
  151. return err;
  152. }
  153. return err;
  154. }