press_down_detect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "press_down_detect.h"
  2. int left_press_data[10];
  3. int right_press_data[10];
  4. int left_up_count;
  5. int right_up_count;
  6. int mean_press_data[PRESS_LEGTH];
  7. int mean_min_val = 20000000;
  8. int mean_max_val = 0;
  9. int mean_up = 0;
  10. int mean_up_true = 0;
  11. int last_mean_min_val = 0;
  12. int right_min_val = 20000000;
  13. int right_max_val = 0;
  14. int left_min_val = 20000000;
  15. int left_max_val = 0;
  16. int maybe_down = 0;
  17. int tail_count = 0;
  18. int last_down_tag = 0;
  19. int left_press_min_val = 20000000;
  20. int right_press_min_val = 20000000;
  21. int left_press_max_val = 0;
  22. int right_press_max_val = 0;
  23. int left_press_sum = 0;
  24. int right_press_sum = 0;
  25. int press_count = 0;
  26. int left_press_low = LEFT_PRESS_LOW;
  27. int right_press_low = RIGHT_PRESS_LOW;
  28. uint32_t jump_count;
  29. uint32_t down_count;
  30. int last_jump_stage;
  31. uint32_t get_jump_count(void)
  32. {
  33. return jump_count;
  34. }
  35. uint32_t get_down_count(void)
  36. {
  37. return down_count;
  38. }
  39. int mid_data(int* press_data, int length)
  40. {
  41. //ðÅÝÅÅÐò
  42. int temp = press_data[0];
  43. for (int i = 0; i < length; i++)
  44. for (int j = i; j < length; j++)
  45. {
  46. if (press_data[j] < press_data[i])
  47. {
  48. temp = press_data[i];
  49. press_data[i] = press_data[j];
  50. press_data[j] = temp;
  51. }
  52. }
  53. return press_data[length / 2 - 1];
  54. }
  55. int min_press_window(int *window, int length)
  56. {
  57. int min_window_val = window[0];
  58. for (int i = 0; i < length; i++)
  59. {
  60. if (min_window_val > window[i])
  61. {
  62. min_window_val = window[i];
  63. }
  64. }
  65. return min_window_val;
  66. }
  67. int max_press_window(int *window, int length)
  68. {
  69. int max_window_val = window[0];
  70. for (int i = 0; i < length; i++)
  71. {
  72. if (max_window_val < window[i])
  73. {
  74. max_window_val = window[i];
  75. }
  76. }
  77. return max_window_val;
  78. }
  79. int poloar_trend(int index, int *window, int window_size, int *stage, int *last_top,
  80. int *last_down, int *now_top, int *now_down,int *count, int *min_count)
  81. {
  82. int max_val = window[(index - 1) % window_size];
  83. int max_index = index;
  84. int min_val = max_val;
  85. int min_index = index;
  86. for (int i = 0; i < window_size; i++)
  87. {
  88. int temp = window[(index - i - 1) % window_size];
  89. if (max_val < temp)
  90. {
  91. max_val = temp;
  92. max_index = index - i;
  93. }
  94. if (min_val > temp)
  95. {
  96. min_val = temp;
  97. min_index = index - i;
  98. }
  99. }
  100. if (max_index < min_index)
  101. {
  102. if (*stage == 2)
  103. {
  104. *last_down = 200000000;
  105. *last_top = 0;
  106. }
  107. if (*last_down > min_val)
  108. {
  109. *last_down = min_val;
  110. }
  111. if (*last_top < max_val)
  112. {
  113. *last_top = max_val;
  114. }
  115. *stage = 1;
  116. *count = 0;
  117. *min_count = 0;
  118. }
  119. else if (max_index > min_index)
  120. {
  121. if (*stage == 1)
  122. {
  123. *now_down = 200000000;
  124. *now_top = 0;
  125. }
  126. if (*now_down > min_val)
  127. {
  128. *now_down = min_val;
  129. }
  130. if (*now_top < max_val)
  131. {
  132. *now_top = max_val;
  133. }
  134. *stage = 2;
  135. *count = *count + 1;
  136. if (window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] < 80000 &&
  137. window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] >0)
  138. {
  139. *min_count = *min_count + 1;
  140. }
  141. }
  142. return max_index == index;
  143. }
  144. int press_down_detect_new(int index, int left_press, int right_press, int left_high, int right_high)
  145. {
  146. static int left_data_press[30];
  147. static int right_data_press[30];
  148. static int press_count = 0;
  149. if(left_high > 1 || right_high > 1)
  150. {
  151. press_count = 0;
  152. }
  153. left_data_press[press_count % 30] = left_press;
  154. right_data_press[press_count % 30] = right_press;
  155. if(press_count >= PRESS_COUNT_MAX)
  156. {
  157. int left_max_val = left_press;
  158. int left_min_val = left_press;
  159. int left_min_index = press_count;
  160. int left_max_index = press_count;
  161. int right_max_val = right_press;
  162. int right_min_val = right_press;
  163. int right_min_index = press_count;
  164. int right_max_index = press_count;
  165. for(int i = press_count; i > press_count - PRESS_COUNT_MAX; i--)
  166. {
  167. if(left_data_press[i % PRESS_COUNT_MAX] > left_max_val)
  168. {
  169. left_max_val = left_data_press[i % PRESS_COUNT_MAX];
  170. left_max_index = i;
  171. }
  172. if(left_data_press[i % PRESS_COUNT_MAX] < left_min_val)
  173. {
  174. left_min_val = left_data_press[i % PRESS_COUNT_MAX];
  175. left_min_index = i;
  176. }
  177. if(right_data_press[i % PRESS_COUNT_MAX] > right_max_val)
  178. {
  179. right_max_val = right_data_press[i % PRESS_COUNT_MAX];
  180. right_max_index = i;
  181. }
  182. if(right_data_press[i % PRESS_COUNT_MAX] < right_min_val)
  183. {
  184. right_min_val = right_data_press[i % PRESS_COUNT_MAX];
  185. right_min_index = i;
  186. }
  187. }
  188. if(left_max_index == press_count && left_max_index - left_min_index > TIME_INTERVAL && left_max_val - left_min_val > PRESS_INTER
  189. && right_max_index == press_count && right_max_index - right_min_index > TIME_INTERVAL && right_max_val - right_min_val > PRESS_INTER)
  190. {
  191. press_count = press_count + 1;
  192. return 1;
  193. }
  194. }
  195. press_count = press_count + 1;
  196. return 0;
  197. }
  198. short press_jump_detect(short *h_pos, short *s_pos)
  199. {
  200. if(h_pos[2] > 0)
  201. {
  202. left_up_count = left_up_count + 1;
  203. }
  204. else
  205. {
  206. left_up_count = 0;
  207. }
  208. if(s_pos[2] > 0)
  209. {
  210. right_up_count = right_up_count + 1;
  211. }
  212. else
  213. {
  214. right_up_count = 0;
  215. }
  216. if(right_up_count > 0 && left_up_count > 0)
  217. {
  218. if(last_jump_stage == 0 && right_up_count < 15 && left_up_count < 15)
  219. {
  220. jump_count = jump_count + 1;
  221. last_jump_stage = 1;
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. last_jump_stage = 0;
  227. return 0;
  228. }
  229. void press_minimum_eval(int left_press, int right_press, short h_pos_z, short s_pos_z)
  230. {
  231. if(right_press > right_press_max_val)
  232. {
  233. right_press_max_val = right_press;
  234. }
  235. if(right_press < right_press_min_val)
  236. {
  237. right_press_min_val = right_press;
  238. }
  239. if(left_press > left_press_max_val)
  240. {
  241. left_press_max_val = left_press;
  242. }
  243. if(left_press < left_press_min_val)
  244. {
  245. left_press_min_val = left_press;
  246. }
  247. if(left_press_max_val - left_press_min_val < 10 && right_press_max_val - right_press_min_val < 10 )
  248. {
  249. press_count = press_count + 1;
  250. right_press_sum = right_press_sum + right_press;
  251. left_press_sum = left_press_sum + left_press;
  252. }
  253. else
  254. {
  255. press_count = 0;
  256. right_press_sum = 0;
  257. left_press_sum = 0;
  258. left_press_min_val = 20000000;
  259. right_press_min_val = 20000000;
  260. left_press_max_val = 0;
  261. right_press_max_val = 0;
  262. }
  263. if(press_count == 1000)
  264. {
  265. left_press_low = left_press_sum / press_count * 256;
  266. right_press_low = right_press_sum/ press_count * 256;
  267. press_count = 0;
  268. right_press_sum = 0;
  269. left_press_sum = 0;
  270. left_press_min_val = 20000000;
  271. right_press_min_val = 20000000;
  272. left_press_max_val = 0;
  273. right_press_max_val = 0;
  274. // gpio_mt_run(500);
  275. }
  276. }