press_down_detect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. extern void gpio_mt_run(uint32_t tim);
  5. #define PRESS_LEGTH 15
  6. #define DETECT_DOWN_SIZE 2
  7. //#define LEFT_PRESS_LOW 10400000
  8. //#define RIGHT_PRESS_LOW 10050000
  9. #define LEFT_PRESS_LOW 9860000
  10. #define RIGHT_PRESS_LOW 10570000
  11. int left_press_data[10];
  12. int right_press_data[10];
  13. int left_up_count;
  14. int right_up_count;
  15. int mean_press_data[PRESS_LEGTH];
  16. int mean_min_val = 20000000;
  17. int mean_max_val = 0;
  18. int mean_up = 0;
  19. int mean_up_true = 0;
  20. int last_mean_min_val = 0;
  21. int right_min_val = 20000000;
  22. int right_max_val = 0;
  23. int left_min_val = 20000000;
  24. int left_max_val = 0;
  25. int maybe_down = 0;
  26. int tail_count = 0;
  27. int last_down_tag = 0;
  28. int left_press_min_val = 20000000;
  29. int right_press_min_val = 20000000;
  30. int left_press_max_val = 0;
  31. int right_press_max_val = 0;
  32. int left_press_sum = 0;
  33. int right_press_sum = 0;
  34. int press_count = 0;
  35. int left_press_low = LEFT_PRESS_LOW;
  36. int right_press_low = RIGHT_PRESS_LOW;
  37. uint32_t jump_count;
  38. uint32_t down_count;
  39. int last_jump_stage;
  40. uint32_t get_jump_count(void)
  41. {
  42. return jump_count;
  43. }
  44. uint32_t get_down_count(void)
  45. {
  46. return down_count;
  47. }
  48. int mid_data(int* press_data, int length)
  49. {
  50. //ðÅÝÅÅÐò
  51. int temp = press_data[0];
  52. for (int i = 0; i < length; i++)
  53. for (int j = i; j < length; j++)
  54. {
  55. if (press_data[j] < press_data[i])
  56. {
  57. temp = press_data[i];
  58. press_data[i] = press_data[j];
  59. press_data[j] = temp;
  60. }
  61. }
  62. return press_data[length / 2 - 1];
  63. }
  64. int min_press_window(int *window, int length)
  65. {
  66. int min_window_val = window[0];
  67. for (int i = 0; i < length; i++)
  68. {
  69. if (min_window_val > window[i])
  70. {
  71. min_window_val = window[i];
  72. }
  73. }
  74. return min_window_val;
  75. }
  76. int max_press_window(int *window, int length)
  77. {
  78. int max_window_val = window[0];
  79. for (int i = 0; i < length; i++)
  80. {
  81. if (max_window_val < window[i])
  82. {
  83. max_window_val = window[i];
  84. }
  85. }
  86. return max_window_val;
  87. }
  88. int poloar_trend(int index, int *window, int window_size, int *stage, int *last_top,
  89. int *last_down, int *now_top, int *now_down,int *count, int *min_count)
  90. {
  91. int max_val = window[(index - 1) % window_size];
  92. int max_index = index;
  93. int min_val = max_val;
  94. int min_index = index;
  95. for (int i = 0; i < window_size; i++)
  96. {
  97. int temp = window[(index - i - 1) % window_size];
  98. if (max_val < temp)
  99. {
  100. max_val = temp;
  101. max_index = index - i;
  102. }
  103. if (min_val > temp)
  104. {
  105. min_val = temp;
  106. min_index = index - i;
  107. }
  108. }
  109. if (max_index < min_index)
  110. {
  111. if (*stage == 2)
  112. {
  113. *last_down = 200000000;
  114. *last_top = 0;
  115. }
  116. if (*last_down > min_val)
  117. {
  118. *last_down = min_val;
  119. }
  120. if (*last_top < max_val)
  121. {
  122. *last_top = max_val;
  123. }
  124. *stage = 1;
  125. *count = 0;
  126. *min_count = 0;
  127. }
  128. else if (max_index > min_index)
  129. {
  130. if (*stage == 1)
  131. {
  132. *now_down = 200000000;
  133. *now_top = 0;
  134. }
  135. if (*now_down > min_val)
  136. {
  137. *now_down = min_val;
  138. }
  139. if (*now_top < max_val)
  140. {
  141. *now_top = max_val;
  142. }
  143. *stage = 2;
  144. *count = *count + 1;
  145. if (window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] < 80000 &&
  146. window[((index - 1) % PRESS_LEGTH)] - window[((index - 2) % PRESS_LEGTH)] >0)
  147. {
  148. *min_count = *min_count + 1;
  149. }
  150. }
  151. return max_index == index;
  152. }
  153. /*
  154. int press_down_detect_new(int index, int left_press, int right_press)
  155. {
  156. left_press_data[(index - 1) % PRESS_LEGTH] = left_press;
  157. right_press_data[(index - 1) % PRESS_LEGTH] = right_press;
  158. mean_press_data[(index - 1) % PRESS_LEGTH] = (left_press + right_press) / 2;
  159. int is_down = 0;
  160. if (index > 10)
  161. {
  162. int now_mean_min_val = min_press_window(mean_press_data, PRESS_LEGTH);
  163. if (now_mean_min_val >= last_mean_min_val)
  164. {
  165. mean_up = mean_up + 1;
  166. if(now_mean_min_val > last_mean_min_val)
  167. {
  168. mean_up_true = mean_up_true + 1;
  169. }
  170. if (now_mean_min_val < mean_min_val)
  171. {
  172. mean_min_val = now_mean_min_val;
  173. }
  174. if (now_mean_min_val > mean_max_val)
  175. {
  176. mean_max_val = now_mean_min_val;
  177. }
  178. if(left_press > left_max_val)
  179. {
  180. left_max_val = left_press;
  181. }
  182. if(left_press < left_min_val)
  183. {
  184. left_min_val = left_press;
  185. }
  186. if(right_press > right_max_val)
  187. {
  188. right_max_val = right_press;
  189. }
  190. if(right_press < right_min_val)
  191. {
  192. right_min_val = right_press;
  193. }
  194. }
  195. if (now_mean_min_val < last_mean_min_val || left_press < left_press_low + 200000 || right_press < right_press_low + 200000)
  196. {
  197. mean_up = 0;
  198. mean_up_true = 0;
  199. mean_min_val = 20000000;
  200. mean_max_val = 0;
  201. right_min_val = 20000000;
  202. right_max_val = 0;
  203. left_min_val = 20000000;
  204. left_max_val = 0;
  205. }
  206. //if(mean_up_true > 20 && mean_max_val - mean_min_val > 500000)
  207. if(mean_up_true > 15 &&
  208. (right_max_val - right_min_val > 600000 && left_max_val - left_min_val > 600000)
  209. )
  210. {
  211. is_down = 1;
  212. }
  213. //if (mean_up > 20 && mean_max_val - mean_min_val > 500000)
  214. if(mean_up > 20 &&
  215. (right_max_val - right_min_val > 750000 || left_max_val - left_min_val > 750000)
  216. && mean_max_val - mean_min_val > 200000)
  217. {
  218. maybe_down = 1;
  219. }
  220. else if(maybe_down == 1)
  221. {
  222. maybe_down = 0;
  223. tail_count = 5;
  224. }
  225. if(tail_count > 0)
  226. {
  227. tail_count = tail_count -1;
  228. if(tail_count == 0 && left_press - left_press_data[(index - 7) % PRESS_LEGTH] > -500000
  229. && right_press - right_press_data[(index - 7) % PRESS_LEGTH] > -500000)
  230. {
  231. down_count = down_count + 1;
  232. is_down = 1;
  233. }
  234. }
  235. last_mean_min_val = now_mean_min_val;
  236. }
  237. else
  238. {
  239. last_mean_min_val = (left_press + right_press) / 2;
  240. }
  241. if(last_down_tag == 0 && is_down == 1)
  242. {
  243. last_down_tag = is_down;
  244. return 1;
  245. }
  246. last_down_tag = is_down;
  247. return 0;
  248. }
  249. */
  250. int press_down_detect_new(int index, int left_press, int right_press)
  251. {
  252. static int left_data_press[30];
  253. static int right_data_press[30];
  254. static int press_count = 0;
  255. left_data_press[press_count % 30] = left_press;
  256. right_data_press[press_count % 30] = right_press;
  257. if(press_count >= 30)
  258. {
  259. int left_max_val = left_press;
  260. int left_min_val = left_press;
  261. int left_min_index = press_count;
  262. int left_max_index = press_count;
  263. int right_max_val = left_press;
  264. int right_min_val = left_press;
  265. int right_min_index = press_count;
  266. int right_max_index = press_count;
  267. for(int i = press_count; i > press_count - 30; i--)
  268. {
  269. if(left_data_press[i % 30] > left_max_val)
  270. {
  271. left_max_val = left_data_press[i % 30];
  272. left_max_index = i;
  273. }
  274. if(left_data_press[i % 30] < left_min_val)
  275. {
  276. left_min_val = left_data_press[i % 30];
  277. left_min_index = i;
  278. }
  279. if(right_data_press[i % 30] > right_max_val)
  280. {
  281. right_max_val = right_data_press[i % 30];
  282. right_max_index = i;
  283. }
  284. if(right_data_press[i % 30] < right_min_val)
  285. {
  286. right_min_val = right_data_press[i % 30];
  287. right_min_index = i;
  288. }
  289. }
  290. if(left_max_index == press_count && left_max_index - left_min_index > 20 && left_max_val - left_min_val > 500000
  291. && right_max_index == press_count && right_max_index - right_min_index > 20 && right_max_val - right_min_val > 500000)
  292. {
  293. press_count = press_count + 1;
  294. return 1;
  295. }
  296. }
  297. press_count = press_count + 1;
  298. return 0;
  299. }
  300. short press_jump_detect(short *h_pos, short *s_pos)
  301. {
  302. if(h_pos[2] > 0)
  303. {
  304. left_up_count = left_up_count + 1;
  305. }
  306. else
  307. {
  308. left_up_count = 0;
  309. }
  310. if(s_pos[2] > 0)
  311. {
  312. right_up_count = right_up_count + 1;
  313. }
  314. else
  315. {
  316. right_up_count = 0;
  317. }
  318. if(right_up_count > 0 && left_up_count > 0)
  319. {
  320. if(last_jump_stage == 0 && right_up_count < 15 && left_up_count < 15)
  321. {
  322. jump_count = jump_count + 1;
  323. last_jump_stage = 1;
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. last_jump_stage = 0;
  329. return 0;
  330. }
  331. //short press_jump_detect(int index)
  332. //{
  333. // if(left_press_data[(index - 1) % PRESS_LEGTH] < left_press_low + 100000)
  334. // {
  335. // left_up_count = left_up_count + 1;
  336. // }
  337. // else
  338. // {
  339. // left_up_count = 0;
  340. // }
  341. // if(right_press_data[(index - 1) % PRESS_LEGTH] < right_press_low + 100000)
  342. // {
  343. // right_up_count = right_up_count + 1;
  344. // }
  345. // else
  346. // {
  347. // right_up_count = 0;
  348. // }
  349. //
  350. //
  351. // if(index >= 10)
  352. // {
  353. // int max_left_press = max_press_window(left_press_data, PRESS_LEGTH);
  354. // int max_right_press = max_press_window(right_press_data, PRESS_LEGTH);
  355. // if( left_up_count> 4 && right_up_count > 4
  356. //// (right_press_data[(index - 1) % PRESS_LEGTH] - max_right_press < -1000000
  357. //// || left_press_data[(index - 1) % PRESS_LEGTH] - max_left_press < -1000000)
  358. // )
  359. // {
  360. // if(last_jump_stage == 0)
  361. // {
  362. // jump_count = jump_count + 1;
  363. //
  364. // last_jump_stage = 1;
  365. //
  366. // return 1;
  367. // }
  368. //
  369. // return 0;
  370. //
  371. // }
  372. //
  373. // }
  374. //
  375. // last_jump_stage = 0;
  376. //
  377. // return 0;
  378. //}
  379. void press_minimum_eval(int left_press, int right_press, short h_pos_z, short s_pos_z)
  380. {
  381. if(right_press > right_press_max_val)
  382. {
  383. right_press_max_val = right_press;
  384. }
  385. if(right_press < right_press_min_val)
  386. {
  387. right_press_min_val = right_press;
  388. }
  389. if(left_press > left_press_max_val)
  390. {
  391. left_press_max_val = left_press;
  392. }
  393. if(left_press < left_press_min_val)
  394. {
  395. left_press_min_val = left_press;
  396. }
  397. if(left_press_max_val - left_press_min_val < 10 && right_press_max_val - right_press_min_val < 10 )
  398. {
  399. press_count = press_count + 1;
  400. right_press_sum = right_press_sum + right_press;
  401. left_press_sum = left_press_sum + left_press;
  402. }
  403. else
  404. {
  405. press_count = 0;
  406. right_press_sum = 0;
  407. left_press_sum = 0;
  408. left_press_min_val = 20000000;
  409. right_press_min_val = 20000000;
  410. left_press_max_val = 0;
  411. right_press_max_val = 0;
  412. }
  413. if(press_count == 1000)
  414. {
  415. left_press_low = left_press_sum / press_count * 256;
  416. right_press_low = right_press_sum/ press_count * 256;
  417. press_count = 0;
  418. right_press_sum = 0;
  419. left_press_sum = 0;
  420. left_press_min_val = 20000000;
  421. right_press_min_val = 20000000;
  422. left_press_max_val = 0;
  423. right_press_max_val = 0;
  424. // gpio_mt_run(500);
  425. }
  426. }