luat_tp_jd9261t_inited.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_gpio.h"
  4. #include "luat_mem.h"
  5. #include "luat_rtos.h"
  6. #include "luat_tp.h"
  7. #define LUAT_LOG_TAG "jd9261t-tp"
  8. #include "luat_log.h"
  9. typedef struct
  10. {
  11. luat_tp_config_t* config;
  12. luat_rtos_timer_t scan_timer;
  13. uint8_t is_inited;
  14. uint8_t scan_time;
  15. uint8_t is_scan;
  16. }jd9261t_tp_ctrl_t;
  17. static jd9261t_tp_ctrl_t jd9261t_tp;
  18. static int tp_i2c_send(luat_tp_config_t* luat_tp_config, int addr, void* buff, size_t len, uint8_t stop)
  19. {
  20. if (luat_tp_config->soft_i2c != NULL){
  21. return i2c_soft_send(luat_tp_config->soft_i2c, addr, (char*)buff, len, stop);
  22. }else{
  23. return luat_i2c_send(luat_tp_config->i2c_id, addr, (char*)buff, len, stop);
  24. }
  25. }
  26. static int tp_i2c_recv(luat_tp_config_t* luat_tp_config, int addr, void* buff, size_t len)
  27. {
  28. if (luat_tp_config->soft_i2c != NULL){
  29. return i2c_soft_recv(luat_tp_config->soft_i2c, addr, (char*)buff, len);
  30. }else{
  31. return luat_i2c_recv(luat_tp_config->i2c_id, addr, (char*)buff, len);
  32. }
  33. }
  34. static int tp_i2c_xfer(luat_tp_config_t* luat_tp_config, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len)
  35. {
  36. if (luat_tp_config->soft_i2c != NULL){
  37. i2c_soft_send(luat_tp_config->soft_i2c, addr, (char*)reg, reg_len, 0);
  38. return i2c_soft_recv(luat_tp_config->soft_i2c, addr, (char*)buff, len);
  39. }else{
  40. return luat_i2c_transfer(luat_tp_config->i2c_id, addr, reg, reg_len, buff, len);
  41. }
  42. }
  43. static int tp_jd9261t_read(luat_tp_config_t* luat_tp_config, luat_tp_data_t *luat_tp_data)
  44. {
  45. uint16_t tp_x, tp_y;
  46. int res;
  47. uint8_t pressed = 0;
  48. uint8_t buff[60];
  49. buff[0] = 0x20;
  50. buff[1] = 0x01;
  51. buff[2] = 0x11;
  52. buff[3] = 0x20;
  53. res = tp_i2c_send(luat_tp_config, 0x68, buff, 4, 1);
  54. if (res)
  55. {
  56. res = tp_i2c_send(luat_tp_config, 0x68, buff, 4, 1);
  57. if (res)
  58. {
  59. LLOGE("TP read point failed");
  60. return -1;
  61. }
  62. }
  63. luat_rtos_task_sleep(1);
  64. res = tp_i2c_recv(luat_tp_config, 0x68, buff, 60);
  65. if (res)
  66. {
  67. res = tp_i2c_recv(luat_tp_config, 0x68, buff, 60);
  68. if (res)
  69. {
  70. LLOGE("TP read point failed");
  71. return -1;
  72. }
  73. }
  74. memset(luat_tp_data, 0, sizeof(luat_tp_data_t) * LUAT_TP_TOUCH_MAX);
  75. if (!buff[0])
  76. {
  77. luat_stop_rtos_timer(jd9261t_tp.scan_timer);
  78. jd9261t_tp.is_scan = 0;
  79. return 0;
  80. }
  81. for (uint8_t i = 0; i < 10; i++)
  82. {
  83. if (buff[i * 5 + 3] != 0xff)
  84. {
  85. //LLOGI("%d, %x %x %x %x", i * 5 + 3, buff[i * 5 + 3], buff[i * 5 + 4], buff[i * 5 + 5], buff[i * 5 + 6]);
  86. tp_x = buff[i * 5 + 3];
  87. tp_x = (tp_x << 8) | buff[i * 5 + 4];
  88. tp_y = buff[i * 5 + 5];
  89. tp_y = (tp_y << 8) | buff[i * 5 + 6];
  90. if (tp_x < luat_tp_config->w && tp_y < luat_tp_config->h)
  91. {
  92. //LLOGI("TP point %d x %d y %d", i+1, tp_x, tp_y);
  93. luat_tp_data[pressed].event = TP_EVENT_TYPE_DOWN;
  94. luat_tp_data[pressed].x_coordinate = tp_x;
  95. luat_tp_data[pressed].y_coordinate = tp_y;
  96. pressed++;
  97. if (pressed >= LUAT_TP_TOUCH_MAX)
  98. {
  99. goto DONE;
  100. }
  101. }
  102. else
  103. {
  104. goto DONE;
  105. }
  106. }
  107. else
  108. {
  109. goto DONE;
  110. }
  111. }
  112. DONE:
  113. return pressed;
  114. }
  115. static LUAT_RT_RET_TYPE jd9261t_scan_once(LUAT_RT_CB_PARAM)
  116. {
  117. luat_tp_config_t* luat_tp_config = (luat_tp_config_t*)param;
  118. luat_rtos_message_send(luat_tp_config->task_handle, 1, luat_tp_config);
  119. }
  120. static int jd9261t_irq_cb(int pin, void* args)
  121. {
  122. luat_tp_config_t* luat_tp_config = (luat_tp_config_t*)args;
  123. if (!jd9261t_tp.is_scan)
  124. {
  125. jd9261t_tp.is_scan = 1;
  126. //luat_tp_irq_enable(luat_tp_config, 0);
  127. luat_rtos_message_send(luat_tp_config->task_handle, 1, luat_tp_config);
  128. //luat_start_rtos_timer(jd9261t_tp.scan_timer, jd9261t_tp.scan_time, 1);
  129. }
  130. return 0;
  131. }
  132. static int tp_jd9261t_inited_init(luat_tp_config_t* luat_tp_config)
  133. {
  134. if (jd9261t_tp.is_inited) return -1;
  135. uint8_t ID[4];
  136. ID[0] = 0x40;
  137. ID[1] = 0x00;
  138. ID[2] = 0x80;
  139. ID[3] = 0x76;
  140. if (luat_tp_config->soft_i2c != NULL){
  141. i2c_soft_setup(luat_tp_config->soft_i2c);
  142. }else{
  143. luat_i2c_setup(luat_tp_config->i2c_id, I2C_SPEED_SLOW);
  144. }
  145. if (tp_i2c_xfer(luat_tp_config, 0x68, ID, 4, ID, 2))
  146. {
  147. LLOGE("TP not detect");
  148. }
  149. else
  150. {
  151. if (luat_tp_config->refresh_rate < 10)
  152. {
  153. luat_tp_config->refresh_rate = 10;
  154. }
  155. if (luat_tp_config->refresh_rate > 100)
  156. {
  157. luat_tp_config->refresh_rate = 100;
  158. }
  159. jd9261t_tp.scan_time = (1000 / luat_tp_config->refresh_rate);
  160. // LLOGI("TP detect %02x%02x, refresh time %dms", ID[1], ID[0], jd9261t_tp.scan_time);
  161. jd9261t_tp.config = luat_tp_config;
  162. jd9261t_tp.is_scan = 0;
  163. jd9261t_tp.scan_timer = luat_create_rtos_timer(jd9261t_scan_once, jd9261t_tp.config, NULL);
  164. luat_gpio_t gpio = {0};
  165. gpio.pin = luat_tp_config->pin_int;
  166. gpio.mode = LUAT_GPIO_IRQ;
  167. gpio.alt_func = -1;
  168. gpio.pull = (luat_tp_config->int_type == LUAT_GPIO_FALLING_IRQ)?LUAT_GPIO_PULLUP:LUAT_GPIO_PULLDOWN;
  169. gpio.irq = luat_tp_config->int_type;
  170. gpio.irq_cb = jd9261t_irq_cb;
  171. gpio.irq_args = jd9261t_tp.config;
  172. luat_gpio_setup(&gpio);
  173. jd9261t_tp.is_inited = 1;
  174. }
  175. return 0;
  176. }
  177. static int tp_jd9261t_deinit(luat_tp_config_t* luat_tp_config){
  178. jd9261t_tp.is_inited = 0;
  179. if (luat_tp_config->pin_int != LUAT_GPIO_NONE){
  180. luat_gpio_close(luat_tp_config->pin_int);
  181. }
  182. if (luat_tp_config->pin_rst != LUAT_GPIO_NONE){
  183. luat_gpio_close(luat_tp_config->pin_rst);
  184. }
  185. return 0;
  186. }
  187. static void tp_jd9261t_read_done(luat_tp_config_t * luat_tp_config)
  188. {
  189. jd9261t_tp.is_scan = 0;
  190. //luat_tp_irq_enable(luat_tp_config, 1);
  191. }
  192. luat_tp_opts_t tp_config_jd9261t_inited = {
  193. .name = "jd9261t_inited",
  194. .init = tp_jd9261t_inited_init,
  195. .deinit = tp_jd9261t_deinit,
  196. .read = tp_jd9261t_read,
  197. .read_done = tp_jd9261t_read_done,
  198. };