dtalige.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "ble_comm.h"
  2. #include "vl53l1.h"
  3. #include "tool.h"
  4. #include "app_game.h"
  5. //WAITRIGHT==1 时,无论右鞋数据包是否丢失,左鞋数据包都上传,不等待
  6. #define WAITRIGHT 1
  7. typedef struct {
  8. unsigned char imubuff[72];
  9. unsigned char imubuff_len;
  10. uint16_t ts;
  11. }IMU_Hal_t;
  12. typedef struct {
  13. IMU_Hal_t h;
  14. IMU_Hal_t s;
  15. }IMU_Hal_RL_t;
  16. typedef void (*process_motion_t)(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  17. extern void get_game_package(uint8_t* buff_address, uint8_t * buff_len);
  18. extern void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  19. extern void get_foot_data(uint8_t *buf, uint8_t *buff_len);
  20. IMU_Hal_t rev_s={.ts=0};
  21. IMU_Hal_t rev={.ts=0};
  22. IMU_Hal_t rev_io;
  23. IMU_Hal_RL_t temp_IMU_DAT;
  24. QUEUE_DEF(IMU_Hal_t, IMU_DAT_H_queue, size_8, QUEUE_MODE_OVERFLOW);
  25. QUEUE_DEF(IMU_Hal_t, IMU_DAT_S_queue, size_8, QUEUE_MODE_OVERFLOW);
  26. #define UNION_PACK {\
  27. queue_out(ph, &outp->h); \
  28. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len); \
  29. get_game_package(rev_io.imubuff, &rev_io.imubuff_len); \
  30. rev_io.ts++; \
  31. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts; \
  32. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8; \
  33. if(rev_io.ts%2==0) \
  34. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len); \
  35. }
  36. extern uint16_t lose_pack_all;
  37. void IMU_dtalige(IMU_Hal_RL_t *outp, queue_t *ph, queue_t *ps, process_motion_t process_p)
  38. {
  39. static char sta = 0;
  40. static unsigned short R_timestamp_re = 0;
  41. switch (sta)
  42. {
  43. case 0:
  44. if (ps->element_count > 0)
  45. {
  46. BLE_PRINT( "IMU_dtalige init.\r\n");
  47. sta = 1;
  48. queue_reset(ps);
  49. queue_reset(ph);
  50. }
  51. else if(ph->element_count > 0)
  52. {
  53. #if GAMEDATA_TO_RAWDATA
  54. if(gamedata_to_rawdata){
  55. UNION_PACK
  56. }else{
  57. queue_out(ph, &outp->h);
  58. }
  59. #else
  60. queue_out(ph, &outp->h);
  61. #endif
  62. }
  63. break;
  64. case 1:
  65. while (ps->element_count > ph->element_count)
  66. {
  67. queue_out(ps, &outp->s);
  68. R_timestamp_re = outp->s.ts;
  69. }
  70. if (ps->element_count > 0)
  71. {
  72. queue_out(ps, &outp->s);
  73. R_timestamp_re++;
  74. if (R_timestamp_re != outp->s.ts)
  75. {
  76. do
  77. {
  78. BLE_PRINT( "...............................lose packet %d\r\n", R_timestamp_re);
  79. // #if LOSSPACK_ENANBLE
  80. // extern uint16_t lose_pack_all;
  81. // lose_pack_all++;
  82. // #endif
  83. R_timestamp_re++;
  84. // #if WAITRIGHT //右鞋数据包丢失,左鞋数据包存在,发送左鞋数据包和右鞋(上一个相同的数据包)
  85. // UNION_PACK
  86. // #else
  87. // queue_out(ph, &outp->h);
  88. // #endif
  89. #if GAMEDATA_TO_RAWDATA
  90. if(gamedata_to_rawdata){
  91. UNION_PACK
  92. }else{
  93. queue_out(ph, &outp->h);
  94. }
  95. #else
  96. queue_out(ph, &outp->h);
  97. #endif
  98. if (R_timestamp_re == outp->h.ts)
  99. {
  100. UNION_PACK
  101. break;
  102. }
  103. if (ph->element_count <= 0)
  104. {
  105. sta = 0;
  106. break;
  107. }
  108. } while (1);
  109. }
  110. else
  111. {
  112. UNION_PACK
  113. }
  114. }
  115. break;
  116. }
  117. }
  118. void IMU_Dtalige(void)
  119. {
  120. IMU_dtalige(&temp_IMU_DAT, &IMU_DAT_H_queue, &IMU_DAT_S_queue, integrate_left_right);
  121. }
  122. void IMU_Rec_data(uint8_t* pdat,uint8_t len)
  123. {
  124. if(pdat[3]==4)
  125. {
  126. memcpy(rev_s.imubuff,&pdat[4],pdat[1]);
  127. rev_s.imubuff_len=pdat[1]-7;
  128. rev_s.ts=((uint16_t)pdat[pdat[1]-3]<<8) |((uint16_t)pdat[pdat[1]-2]<<0);;
  129. queue_in(&IMU_DAT_S_queue,&rev_s);
  130. }
  131. }
  132. 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)
  133. {
  134. process_motion(time_stamp, _acc, _gry, front_mag, back_mag, _rssi);
  135. get_foot_data(rev.imubuff,&rev.imubuff_len);
  136. rev.ts++;
  137. if(IS_HOST)
  138. {
  139. queue_in(&IMU_DAT_H_queue,&rev);
  140. }
  141. else
  142. {
  143. rev.imubuff[rev.imubuff_len+1]=rev.ts;
  144. rev.imubuff[rev.imubuff_len]=rev.ts>>8;
  145. send_protocol(0,4,rev.imubuff,rev.imubuff_len+2);
  146. }
  147. }