tpkey_acts_opt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2017 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief TP Keyboard driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <kernel.h>
  12. #include <string.h>
  13. #include <init.h>
  14. #include <irq.h>
  15. #include <drivers/adc.h>
  16. #include <drivers/input/input_dev.h>
  17. #include <sys/util.h>
  18. #include <sys/byteorder.h>
  19. #include <board.h>
  20. #include <soc_pmu.h>
  21. #include <logging/log.h>
  22. #include <device.h>
  23. #include <drivers/gpio.h>
  24. #include <soc.h>
  25. #include <string.h>
  26. #include <drivers/i2c.h>
  27. #include <board_cfg.h>
  28. #include <stdlib.h>
  29. //#define CONFIG_DEBUG_TP 1
  30. #define MAX_CACHE_KEY_NUM 5
  31. #define COOR_COEF0 1
  32. #define COOR_COEF1 2
  33. #define COOR_COEF2 3
  34. #define COOR_COEF3 10
  35. /** key action type */
  36. enum GESTURE_TYPE
  37. {
  38. /** gesture drop down */
  39. GESTURE_DROP_DOWN = 1,
  40. /** gesture drop up */
  41. GESTURE_DROP_UP,
  42. /** gesture drop left */
  43. GESTURE_DROP_LEFT,
  44. /** gesture drop right */
  45. GESTURE_DROP_RIGHT,
  46. };
  47. #define COOR_COEF_TOTAL (COOR_COEF0 + COOR_COEF1 + COOR_COEF2 + COOR_COEF3)
  48. struct acts_tpkey_context {
  49. struct input_value key_value[MAX_CACHE_KEY_NUM];
  50. struct input_value next_point;
  51. uint32_t tp_point_duration;
  52. uint32_t last_timestamp;
  53. uint32_t next_timestamp;
  54. uint8_t key_index;
  55. uint8_t key_num;
  56. uint8_t gesture;
  57. uint8_t fixed_gesture;
  58. uint8_t gesture_speed;
  59. };
  60. static struct acts_tpkey_context key_context;
  61. static int gesture_fixed(int gesture, int scroll_off_x, int scroll_off_y)
  62. {
  63. int fixed_gesture = gesture;
  64. static const uint8_t fixed_gesture_table[] = {
  65. 0, GESTURE_DROP_UP, GESTURE_DROP_DOWN,
  66. GESTURE_DROP_RIGHT, GESTURE_DROP_LEFT,
  67. };
  68. if ((gesture == GESTURE_DROP_DOWN && scroll_off_y < -2)
  69. || (gesture == GESTURE_DROP_UP && scroll_off_y > 2)) {
  70. fixed_gesture = fixed_gesture_table[gesture];
  71. }
  72. if ((gesture == GESTURE_DROP_LEFT && scroll_off_x > 2)
  73. || (gesture == GESTURE_DROP_RIGHT && scroll_off_x < -2)) {
  74. fixed_gesture = fixed_gesture_table[gesture];
  75. }
  76. key_context.fixed_gesture = fixed_gesture;
  77. return fixed_gesture;
  78. }
  79. static int _tpkey_forecast_next_point(void)
  80. {
  81. uint8_t start_index = (key_context.key_index + 1) % MAX_CACHE_KEY_NUM;
  82. short x_coor_change[MAX_CACHE_KEY_NUM];
  83. short y_coor_change[MAX_CACHE_KEY_NUM];
  84. //uint16_t x_coor = 0;
  85. //uint16_t y_coor = 0;
  86. int i = 0;
  87. for (i = 0 ; i < key_context.key_num; i++) {
  88. uint8_t current_index = (start_index + i) % MAX_CACHE_KEY_NUM;
  89. uint8_t next_index = (start_index + i + 1) % MAX_CACHE_KEY_NUM;
  90. if (i == 4) {
  91. x_coor_change[i] = key_context.key_value[current_index].point.loc_x - key_context.key_value[next_index].point.loc_x;
  92. y_coor_change[i] = key_context.key_value[current_index].point.loc_y - key_context.key_value[next_index].point.loc_y;
  93. } else {
  94. x_coor_change[i] = key_context.key_value[next_index].point.loc_x - key_context.key_value[current_index].point.loc_x;
  95. y_coor_change[i] = key_context.key_value[next_index].point.loc_y - key_context.key_value[current_index].point.loc_y;
  96. }
  97. }
  98. if (x_coor_change[0] * x_coor_change[1] * x_coor_change[2] * x_coor_change[3] > 0) {
  99. x_coor_change[4] = (x_coor_change[0] * COOR_COEF0 + x_coor_change[1] * COOR_COEF1
  100. + x_coor_change[2] * COOR_COEF2 + x_coor_change[3] * COOR_COEF3) / COOR_COEF_TOTAL;
  101. #ifdef CONFIG_DEBUG_TP
  102. printk("x1 %d x1 %d x1 %d x1 %d COOR_COEF_TOTAL %d \n",x_coor_change[0], x_coor_change[1], x_coor_change[2], x_coor_change[3],COOR_COEF_TOTAL);
  103. #endif
  104. } else {
  105. x_coor_change[4] = x_coor_change[3];
  106. }
  107. if (y_coor_change[0] * y_coor_change[1] * y_coor_change[2] * y_coor_change[3] > 0) {
  108. y_coor_change[4] = (y_coor_change[0] * COOR_COEF0 + y_coor_change[1] * COOR_COEF1
  109. + y_coor_change[2] * COOR_COEF2 + y_coor_change[3] * COOR_COEF3) / COOR_COEF_TOTAL;
  110. y_coor_change[4] = (y_coor_change[0] * COOR_COEF0 + y_coor_change[1] * COOR_COEF1
  111. + y_coor_change[2] * COOR_COEF2 + y_coor_change[3] * COOR_COEF3) / COOR_COEF_TOTAL;
  112. #ifdef CONFIG_DEBUG_TP
  113. printk("y1 %d y2 %d y3 %d y4 %d COOR_COEF_TOTAL %d \n",y_coor_change[0], y_coor_change[1],y_coor_change[2], y_coor_change[3],COOR_COEF_TOTAL);
  114. #endif
  115. } else {
  116. y_coor_change[4] = y_coor_change[3];
  117. }
  118. #ifdef CONFIG_DEBUG_TP
  119. printk("gesture %d xcoor %d ycoor %d \n",key_context.key_value[key_context.key_index].point.gesture,abs(x_coor_change[4]), abs(y_coor_change[4]));
  120. #endif
  121. if (!key_context.fixed_gesture) {
  122. if (abs(x_coor_change[4]) > abs(y_coor_change[4])
  123. || key_context.key_value[key_context.key_index].point.loc_x < 50) {
  124. if (x_coor_change[4] > 0) {
  125. key_context.fixed_gesture = 4;
  126. } else {
  127. key_context.fixed_gesture = 3;
  128. }
  129. } else {
  130. key_context.fixed_gesture = 0;
  131. }
  132. }
  133. #ifdef CONFIG_DEBUG_TP
  134. printk("fixed_gesture %d\n",key_context.fixed_gesture);
  135. #endif
  136. if (key_context.fixed_gesture) {
  137. key_context.key_value[key_context.key_index].point.gesture = key_context.fixed_gesture;
  138. }
  139. key_context.next_point.point.loc_x = key_context.key_value[key_context.key_index].point.loc_x + x_coor_change[4];
  140. key_context.next_point.point.loc_y = key_context.key_value[key_context.key_index].point.loc_y + y_coor_change[4];
  141. key_context.next_point.point.gesture = gesture_fixed(key_context.key_value[key_context.key_index].point.gesture, x_coor_change[3], y_coor_change[3]);
  142. #ifdef CONFIG_DEBUG_TP
  143. printk(" x :");
  144. for (i = 0 ; i < key_context.key_num; i++) {
  145. printk(" %d ",x_coor_change[i]);
  146. }
  147. printk("\n");
  148. printk(" y :");
  149. for (i = 0 ; i < key_context.key_num; i++) {
  150. printk(" %d ",y_coor_change[i]);
  151. }
  152. printk("\n");
  153. #endif
  154. return 0;
  155. }
  156. int tpkey_put_point(struct input_value *value, uint32_t timestamp)
  157. {
  158. struct input_value *current_value = NULL;
  159. if (!value->point.pessure_value) {
  160. key_context.gesture = 0;
  161. key_context.key_num = 0;
  162. key_context.key_index = 0;
  163. key_context.fixed_gesture = 0;
  164. } else {
  165. if (key_context.gesture != value->point.gesture) {
  166. key_context.gesture = value->point.gesture;
  167. key_context.key_index = 0;
  168. key_context.key_num = 0;
  169. } else {
  170. key_context.key_index++;
  171. if (key_context.key_index >= MAX_CACHE_KEY_NUM) {
  172. key_context.key_index = 0;
  173. }
  174. }
  175. }
  176. current_value = &key_context.key_value[key_context.key_index];
  177. memcpy(current_value, value, sizeof(struct input_value));
  178. key_context.key_num++;
  179. if (key_context.key_num >= MAX_CACHE_KEY_NUM) {
  180. key_context.key_num = MAX_CACHE_KEY_NUM;
  181. }
  182. #ifdef CONFIG_DEBUG_TP
  183. printk("put gesture = %d,pessure_value = %d local:(%d,%d),index %d num %d duration %d\n"
  184. ,value->point.gesture,value->point.pessure_value,value->point.loc_x, value->point.loc_y,
  185. key_context.key_index, key_context.key_num,
  186. k_cyc_to_us_floor32(timestamp - key_context.last_timestamp));
  187. #endif
  188. key_context.tp_point_duration = k_cyc_to_us_floor32(timestamp - key_context.last_timestamp);
  189. key_context.next_timestamp = timestamp - key_context.last_timestamp;
  190. key_context.last_timestamp = timestamp;
  191. key_context.next_timestamp += key_context.last_timestamp;
  192. if (key_context.gesture && key_context.key_num == MAX_CACHE_KEY_NUM) {
  193. _tpkey_forecast_next_point();
  194. }
  195. return 0;
  196. }
  197. int tpkey_get_last_point(struct input_value *value, uint32_t timestamp)
  198. {
  199. struct input_value *current_value = NULL;
  200. struct input_value *next_value = NULL;
  201. uint32_t current_duration = k_cyc_to_us_floor32(timestamp - key_context.last_timestamp) + 6000;
  202. if (current_duration > key_context.tp_point_duration) {
  203. current_duration = key_context.tp_point_duration;
  204. }
  205. #ifdef CONFIG_DEBUG_TP
  206. printk("current_duration %d \n",current_duration);
  207. #endif
  208. if (k_cyc_to_us_floor32(timestamp - key_context.last_timestamp) > 20000) {
  209. return 0;
  210. }
  211. if (!key_context.gesture) {
  212. current_value = &key_context.key_value[0];
  213. memcpy(value, current_value, sizeof(struct input_value));
  214. } else {
  215. current_value = &key_context.key_value[key_context.key_index];
  216. next_value = &key_context.next_point;
  217. if (key_context.key_num == MAX_CACHE_KEY_NUM) {
  218. value->point.loc_x = (next_value->point.loc_x * current_duration
  219. + current_value->point.loc_x * (key_context.tp_point_duration - current_duration))
  220. / key_context.tp_point_duration;
  221. value->point.loc_y = (next_value->point.loc_y * current_duration
  222. + current_value->point.loc_y * (key_context.tp_point_duration - current_duration))
  223. / key_context.tp_point_duration;
  224. value->point.gesture = current_value->point.gesture;
  225. value->point.pessure_value = current_value->point.pessure_value;
  226. } else {
  227. memcpy(value, current_value, sizeof(struct input_value));
  228. }
  229. #ifdef CONFIG_DEBUG_TP
  230. printk("current(%d %d ) next(%d %d ) report(%d %d ) pessure_value %d\n",
  231. current_value->point.loc_x,current_value->point.loc_y,
  232. next_value->point.loc_x,next_value->point.loc_y,
  233. value->point.loc_x,value->point.loc_y,value->point.pessure_value);
  234. #endif
  235. }
  236. return 0;
  237. }