dtalige.c 6.5 KB

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