dtalige.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "ble_comm.h"
  2. #include "vl53l1.h"
  3. #include "tool.h"
  4. typedef struct {
  5. unsigned char imubuff[82];
  6. unsigned char imubuff_len;
  7. uint16_t ts;
  8. }IMU_Hal_t;
  9. typedef struct {
  10. IMU_Hal_t h;
  11. IMU_Hal_t s;
  12. }IMU_Hal_RL_t;
  13. typedef void (*process_motion_t)(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  14. extern void get_game_package(uint8_t* buff_address, uint8_t * buff_len);
  15. extern void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len);
  16. extern void get_foot_data(uint8_t *buf, uint8_t *buff_len);
  17. IMU_Hal_t rev_s={.ts=0};
  18. IMU_Hal_t rev={.ts=0};
  19. IMU_Hal_t rev_io;
  20. IMU_Hal_RL_t temp_IMU_DAT;
  21. QUEUE_DEF(IMU_Hal_t, IMU_DAT_H_queue, size_8, QUEUE_MODE_OVERFLOW);
  22. QUEUE_DEF(IMU_Hal_t, IMU_DAT_S_queue, size_8, QUEUE_MODE_OVERFLOW);
  23. extern uint16_t lose_pack_all;
  24. void IMU_dtalige(IMU_Hal_RL_t *outp, queue_t *ph, queue_t *ps, process_motion_t process_p)
  25. {
  26. static char sta = 0;
  27. static unsigned short R_timestamp_re = 0;
  28. switch (sta)
  29. {
  30. case 0:
  31. if (ps->element_count > 0)
  32. {
  33. BLE_PRINT( "IMU_dtalige init.\r\n");
  34. sta = 1;
  35. queue_reset(ps);
  36. queue_reset(ph);
  37. }
  38. break;
  39. case 1:
  40. while (ps->element_count > ph->element_count)
  41. {
  42. queue_out(ps, &outp->s);
  43. R_timestamp_re = outp->s.ts;
  44. }
  45. if (ps->element_count > 0)
  46. {
  47. queue_out(ps, &outp->s);
  48. R_timestamp_re++;
  49. if (R_timestamp_re != outp->s.ts)
  50. {
  51. do
  52. {
  53. BLE_PRINT( "lose packet %d\r\n", R_timestamp_re);
  54. R_timestamp_re++;
  55. #if LOSSPACK_ENANBLE
  56. extern uint16_t lose_pack_all;
  57. lose_pack_all++;
  58. #endif
  59. queue_out(ph, &outp->h);
  60. if (R_timestamp_re == outp->h.ts)
  61. {
  62. queue_out(ph, &outp->h);
  63. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  64. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  65. rev_io.ts++;
  66. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  67. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  68. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  69. break;
  70. }
  71. if (ph->element_count <= 0)
  72. {
  73. sta = 0;
  74. break;
  75. }
  76. } while (1);
  77. }
  78. else
  79. {
  80. queue_out(ph, &outp->h);
  81. process_p(outp->h.imubuff, outp->h.imubuff_len, outp->s.imubuff, outp->s.imubuff_len);
  82. get_game_package(rev_io.imubuff, &rev_io.imubuff_len);
  83. rev_io.ts++;
  84. rev_io.imubuff[rev_io.imubuff_len + 1] = rev_io.ts;
  85. rev_io.imubuff[rev_io.imubuff_len] = rev_io.ts >> 8;
  86. send_protocol(0, 4, rev_io.imubuff, rev_io.imubuff_len);
  87. }
  88. }
  89. break;
  90. }
  91. }
  92. void IMU_Dtalige(void)
  93. {
  94. IMU_dtalige(&temp_IMU_DAT, &IMU_DAT_H_queue, &IMU_DAT_S_queue, integrate_left_right);
  95. }
  96. void IMU_Rec_data(uint8_t* pdat,uint8_t len)
  97. {
  98. if(pdat[3]==4)
  99. {
  100. memcpy(rev_s.imubuff,&pdat[4],pdat[1]);
  101. rev_s.imubuff_len=pdat[1]-7;
  102. rev_s.ts=((uint16_t)pdat[pdat[1]-3]<<8) |((uint16_t)pdat[pdat[1]-2]<<0);;
  103. queue_in(&IMU_DAT_S_queue,&rev_s);
  104. }
  105. }
  106. 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)
  107. {
  108. process_motion(time_stamp, _acc, _gry, front_mag, back_mag, _rssi);
  109. get_foot_data(rev.imubuff,&rev.imubuff_len);
  110. rev.ts++;
  111. if(IS_HOST)
  112. {
  113. queue_in(&IMU_DAT_H_queue,&rev);
  114. }
  115. else
  116. {
  117. rev.imubuff[rev.imubuff_len+1]=rev.ts;
  118. rev.imubuff[rev.imubuff_len]=rev.ts>>8;
  119. send_protocol(0,4,rev.imubuff,rev.imubuff_len+2);
  120. }
  121. }