dtalige.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "ble_comm.h"
  2. #include "vl53l1.h"
  3. #include "tool.h"
  4. #define GAMEUSEDTALIGE 1
  5. static unsigned char USEdtalige = 1;
  6. void IMU_Dtalige_Rowdata_ON(void)
  7. {
  8. USEdtalige = 0;
  9. }
  10. void IMU_Dtalige_Rowdata_OFF(void)
  11. {
  12. USEdtalige = 1;
  13. }
  14. //.....................重写运动库接口.............................
  15. unsigned char left_right_buff[60];
  16. unsigned char left_right_data_len;
  17. static void d_integrate_left_right(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
  18. {
  19. unsigned char L=0;
  20. for(int i=0;i<left_len;i++)
  21. {
  22. left_right_buff[L++]=left_buff[i];
  23. }
  24. for(int i=0;i<right_len;i++)
  25. {
  26. left_right_buff[L++]=right_buff[i];
  27. }
  28. left_right_data_len=L;
  29. }
  30. static void d_get_game_package(uint8_t* buff_address, uint8_t * buff_len)
  31. {
  32. for(int i=0;i<left_right_data_len;i++)
  33. {
  34. buff_address[i]=left_right_buff[i];
  35. }
  36. *buff_len=left_right_data_len;
  37. }
  38. unsigned char mofoot_buff[30];
  39. unsigned char mofoot_data_len;
  40. static void d_process_motion(int32_t time_stamp, int16_t _acc[3], int16_t _gry[3], int16_t front_mag[3], int16_t back_mag[3], uint8_t _rssi)
  41. {
  42. int L=0;
  43. mofoot_buff[L++]=_acc[0]>>8;
  44. mofoot_buff[L++]=_acc[0];
  45. mofoot_buff[L++]=_acc[1]>>8;
  46. mofoot_buff[L++]=_acc[1];
  47. mofoot_buff[L++]=_acc[2]>>8;
  48. mofoot_buff[L++]=_acc[2];
  49. mofoot_buff[L++]=_gry[0]>>8;
  50. mofoot_buff[L++]=_gry[0];
  51. mofoot_buff[L++]=_gry[1]>>8;
  52. mofoot_buff[L++]=_gry[1];
  53. mofoot_buff[L++]=_gry[2]>>8;
  54. mofoot_buff[L++]=_gry[2];
  55. mofoot_buff[L++]=front_mag[0]>>8;
  56. mofoot_buff[L++]=front_mag[0];
  57. mofoot_buff[L++]=front_mag[1]>>8;
  58. mofoot_buff[L++]=front_mag[1];
  59. mofoot_buff[L++]=front_mag[2]>>8;
  60. mofoot_buff[L++]=front_mag[2];
  61. mofoot_buff[L++]=back_mag[0]>>8;
  62. mofoot_buff[L++]=back_mag[0];
  63. mofoot_buff[L++]=back_mag[1]>>8;
  64. mofoot_buff[L++]=back_mag[1];
  65. mofoot_buff[L++]=back_mag[2]>>8;
  66. mofoot_buff[L++]=back_mag[2];
  67. // mofoot_buff[L++]=_rssi;
  68. //
  69. // mofoot_buff[L++]=time_stamp>>24;
  70. // mofoot_buff[L++]=time_stamp>>16;
  71. // mofoot_buff[L++]=time_stamp>>8;
  72. // mofoot_buff[L++]=time_stamp;
  73. mofoot_data_len=L;
  74. }
  75. static void d_get_foot_data(uint8_t *buf, uint8_t *buff_len)
  76. {
  77. for(int i=0;i<mofoot_data_len;i++)
  78. {
  79. buf[i]=mofoot_buff[i];
  80. }
  81. *buff_len=mofoot_data_len;
  82. }
  83. //.....................重写运动库接口.............................end
  84. typedef struct {
  85. unsigned char imubuff[72];
  86. unsigned char imubuff_len;
  87. uint16_t ts;
  88. }IMU_Hal_t;
  89. typedef struct {
  90. IMU_Hal_t h;
  91. IMU_Hal_t s;
  92. }IMU_Hal_RL_t;
  93. typedef void (*process_motion_t)(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  94. extern void get_game_package(uint8_t* buff_address, uint8_t * buff_len);
  95. extern void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  96. extern void get_foot_data(uint8_t *buf, uint8_t *buff_len);
  97. IMU_Hal_t rev_s={.ts=0};
  98. IMU_Hal_t rev={.ts=0};
  99. IMU_Hal_t rev_io;
  100. IMU_Hal_RL_t temp_IMU_DAT;
  101. QUEUE_DEF(IMU_Hal_t, IMU_DAT_H_queue, size_4, QUEUE_MODE_OVERFLOW);
  102. QUEUE_DEF(IMU_Hal_t, IMU_DAT_S_queue, size_4, QUEUE_MODE_OVERFLOW);
  103. extern uint16_t lose_pack_all;
  104. void IMU_dtalige(IMU_Hal_RL_t *outp, queue_t *ph, queue_t *ps, process_motion_t process_p)
  105. {
  106. static char sta = 0;
  107. static unsigned short R_timestamp_re = 0;
  108. switch (sta)
  109. {
  110. case 0:
  111. if (ps->element_count > 0)
  112. {
  113. BLE_PRINT( "IMU_dtalige init.\r\n");
  114. sta = 1;
  115. queue_reset(ps);
  116. queue_reset(ph);
  117. }
  118. break;
  119. case 1:
  120. while (ps->element_count > ph->element_count)
  121. {
  122. queue_out(ps, &outp->s);
  123. R_timestamp_re = outp->s.ts;
  124. }
  125. if (ps->element_count > 0)
  126. {
  127. queue_out(ps, &outp->s);
  128. R_timestamp_re++;
  129. if (R_timestamp_re != outp->s.ts)
  130. {
  131. do
  132. {
  133. BLE_PRINT( "lose packet %d\r\n", R_timestamp_re);
  134. #if LOSSPACK_ENANBLE
  135. extern uint16_t lose_pack_all;
  136. lose_pack_all++;
  137. #endif
  138. R_timestamp_re++;
  139. queue_out(ph, &outp->h);
  140. if (R_timestamp_re == outp->h.ts)
  141. {
  142. queue_out(ph, &outp->h);
  143. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  144. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  145. rev_io.ts++;
  146. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  147. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  148. if(rev_io.ts%2==0)
  149. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  150. break;
  151. }
  152. if (ph->element_count <= 0)
  153. {
  154. sta = 0;
  155. break;
  156. }
  157. } while (1);
  158. }
  159. else
  160. {
  161. queue_out(ph, &outp->h);
  162. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  163. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  164. rev_io.ts++;
  165. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  166. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  167. if(rev_io.ts%2==0)
  168. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  169. }
  170. }
  171. break;
  172. }
  173. // SEGGER_RTT_printf(0, "%d,%d\r\n",ph->element_count,ps->element_count);
  174. }
  175. void IMU_Dtalige(void)
  176. {
  177. if( USEdtalige )
  178. {
  179. #if GAMEUSEDTALIGE
  180. IMU_dtalige(&temp_IMU_DAT, &IMU_DAT_H_queue, &IMU_DAT_S_queue, integrate_left_right);
  181. #endif
  182. }
  183. }
  184. void IMU_Rec_data(uint8_t* pdat,uint8_t len)
  185. {
  186. if(pdat[3]==4)
  187. {
  188. memcpy(rev_s.imubuff,&pdat[4],pdat[1]);
  189. rev_s.imubuff_len=pdat[1]-7;
  190. rev_s.ts=((uint16_t)pdat[pdat[1]-3]<<8) |((uint16_t)pdat[pdat[1]-2]<<0);
  191. if( USEdtalige )
  192. {
  193. #if GAMEUSEDTALIGE
  194. queue_in(&IMU_DAT_S_queue,&rev_s);
  195. #endif
  196. }
  197. }
  198. }
  199. void IMU_Process_motion_queue(uint8_t IS_HOST, int32_t time_stamp, int16_t* _acc,int16_t* _gry, int16_t* front_mag, int16_t* back_mag, uint8_t _rssi)
  200. {
  201. if( USEdtalige )
  202. {
  203. process_motion(time_stamp, _acc, _gry, front_mag, back_mag, _rssi);
  204. get_foot_data(rev.imubuff,&rev.imubuff_len);
  205. }
  206. else
  207. {
  208. d_process_motion(time_stamp, _acc, _gry, front_mag, back_mag, _rssi);
  209. d_get_foot_data(rev.imubuff,&rev.imubuff_len);
  210. }
  211. rev.ts++;
  212. if(IS_HOST)
  213. {
  214. if( USEdtalige )
  215. {
  216. #if GAMEUSEDTALIGE
  217. queue_in(&IMU_DAT_H_queue,&rev);
  218. #else
  219. integrate_left_right(rev.imubuff, rev.imubuff_len, rev_s.imubuff, rev_s.imubuff_len);
  220. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  221. rev_io.ts++;
  222. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  223. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  224. if(rev_io.ts%2==0)
  225. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  226. #endif
  227. }
  228. else
  229. {
  230. d_integrate_left_right(rev.imubuff, rev.imubuff_len, rev_s.imubuff, rev_s.imubuff_len);
  231. d_get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  232. rev_io.ts++;
  233. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  234. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  235. if(rev_io.ts%2==0)
  236. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  237. }
  238. }
  239. else
  240. {
  241. rev.imubuff[rev.imubuff_len+1]=rev.ts;
  242. rev.imubuff[rev.imubuff_len]=rev.ts>>8;
  243. send_protocol(0,4,rev.imubuff,rev.imubuff_len+2);
  244. }
  245. }