dtalige.c 7.6 KB

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