app_game.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "app_game.h"
  2. #include "usr_config.h"
  3. #include "bsp_time.h"
  4. #include "system.h"
  5. #include "hal_mt.h"
  6. #include "hal_battery.h"
  7. #include "app_charge.h"
  8. #include "hal_ble_client.h"
  9. #include "hal_ble_host.h"
  10. #include "nrf_delay.h"
  11. #include "app_flash.h"
  12. #include "ble_comm.h"
  13. #include "exception.h"
  14. #include "bll_imu.h"
  15. #include "hal_led.h"
  16. #include "pdrStatus.h"
  17. /********************** 函数声明区 *************************/
  18. //游戏模式
  19. static const bll_imu_one_way_param_t game_front_param={
  20. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  21. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  22. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  23. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  24. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  25. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  26. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  27. .acc_odr = FML_IMU_ACC_ODR_416HZ, //前脚 - 加速度采样频率 - 104HZ
  28. .gry_odr = FML_IMU_GRY_ODR_416HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  29. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  30. .fifo_odr = FML_IMU_FIFO_ODR_416HZ,
  31. };
  32. static const bll_imu_one_way_param_t game_back_param={
  33. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  34. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  35. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  36. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  37. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  38. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  39. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  40. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  41. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  42. .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  43. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  44. };
  45. static const bll_imu_param_t game_bll_imu_param_t={
  46. .config_param[FML_IMU_DIR_FRONT] = &game_front_param,
  47. .config_param[FML_IMU_DIR_BACK] = &game_back_param,
  48. };
  49. typedef struct game_manager{
  50. uint8_t clientCnt; //右鞋游戏模式的标志位
  51. uint8_t right_discnt; //右鞋断开连接计数器
  52. uint8_t client_discnt; //主机连接断开计数器
  53. uint8_t Signal; //收到主机的游戏指令
  54. uint8_t HeartCnt; //游戏模式心跳维护,左鞋
  55. uint8_t GameMode; //是否在游戏
  56. }game_manager_t;
  57. static game_manager_t game_manager ={0};
  58. #define IMU_MAX_GROUP_NUM 10
  59. static int16_t acc_front[IMU_MAX_GROUP_NUM][3];
  60. static int16_t gry_front[IMU_MAX_GROUP_NUM][3];
  61. static int16_t mag6310_front[IMU_MAX_GROUP_NUM][3];
  62. static int16_t mag6310_back[3];
  63. static int32_t timestamp_front[IMU_MAX_GROUP_NUM];
  64. static uint8_t rssi;
  65. static uint8_t IMU_STATUS; //记录状态用来重新记录时间戳
  66. static uint32_t timestamp;
  67. static int32_t last_timestamp;
  68. static void app_game_Process(void);
  69. static void process_imu_data_front(int front_index)
  70. {
  71. if(IMU_STATUS != 1){
  72. IMU_STATUS = 1;
  73. last_timestamp = timestamp_front[0];
  74. timestamp = 0;
  75. }
  76. if(front_index >IMU_MAX_GROUP_NUM)return;
  77. for(int i = 0; i < front_index; i++)
  78. {
  79. int32_t dt = timestamp_front[i] - last_timestamp + 100;
  80. last_timestamp = timestamp_front[i];
  81. DEBUG_LOG("dt : %d\n", dt);
  82. if(dt == 0)
  83. {
  84. DEBUG_LOG("as dt == 0, return\n", dt);
  85. continue;
  86. }
  87. if(dt < 0)
  88. {
  89. dt = 2425;
  90. }
  91. timestamp += dt;
  92. DEBUG_LOG("timestamp : %ud\n", timestamp);
  93. DEBUG_LOG("timestamp_front[i] : %d\n", timestamp_front[i]);
  94. IMU_Process_motion_queue(mFlash.isHost, timestamp, acc_front[i],
  95. gry_front[i],mag6310_front[i], mag6310_back, rssi);
  96. }
  97. }
  98. void app_game_SetClientGameMode(void)
  99. {
  100. if(game_manager.clientCnt != 30) game_manager.clientCnt = 30;
  101. }
  102. uint8_t app_game_GetGameMode(void)
  103. {
  104. return game_manager.GameMode;
  105. }
  106. static void cb_BLE_Client_R_GAMEMODE(void* handle)
  107. {
  108. BLE_Client_Rx_t* target = handle;
  109. game_manager.Signal = target->pDat[0];
  110. if(game_manager.Signal){
  111. game_manager.HeartCnt =0;
  112. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  113. //是否上传原始数据
  114. if(1 == game_manager.Signal)IMU_Dtalige_Rowdata_OFF();
  115. else if(2 == game_manager.Signal)IMU_Dtalige_Rowdata_ON();
  116. Process_SetHoldOn(app_game_Process,1);
  117. }else{
  118. Process_SetHoldOn(app_game_Process,0);
  119. bll_imu_Resume_unregister_config_param(&game_bll_imu_param_t);
  120. }
  121. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1);
  122. DEBUG_LOG(">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.Signal);
  123. }
  124. //通知右鞋进入或者退出游戏模式
  125. static void app_game_notify_host(uint8_t mode){
  126. uint8_t clientMode = mode;
  127. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  128. }
  129. ////测试使用,游戏模式下 1S亮绿灯20ms.
  130. //static void app_game_led(uint8_t gamemode){
  131. // static uint8_t led_state =0;
  132. // static uint32_t tim =0;
  133. // if(gamemode){
  134. // if(TIME_GetTicks() -tim >= 1000){tim = TIME_GetTicks();
  135. // if(led_state){led_state =0;
  136. // LED_Start(LED_RUN,COLOR_GREEN);
  137. // }
  138. // else {led_state =1;
  139. // LED_Start(LED_RUN,COLOR_BLACK);
  140. // }
  141. // }
  142. // }else{
  143. // LED_Stop(LED_RUN);
  144. // led_state =0;
  145. // }
  146. //}
  147. //IMU数据回调
  148. static void gamemode_data_notify_cb(uint32_t dir_bit)
  149. {
  150. int16_t group_num = 0;
  151. bll_imu_data_t data={0};
  152. if(0 == app_game_GetGameMode())return;
  153. if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01){
  154. rssi = 0-host_get_rssi();
  155. group_num = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  156. if(group_num >IMU_MAX_GROUP_NUM)return;
  157. for(int i=0;i<group_num;i++){
  158. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &data);
  159. gry_front[i][0] = data.gry[0];gry_front[i][1] = data.gry[1];gry_front[i][2] = data.gry[2];
  160. acc_front[i][0] = data.acc[0];acc_front[i][1] = data.acc[1];acc_front[i][2] = data.acc[2];
  161. mag6310_front[i][0] = data.mag[0];mag6310_front[i][1] = data.mag[1];mag6310_front[i][2] = data.mag[2];
  162. timestamp_front[i] = data.fifo_timestamp;
  163. }
  164. if(bll_imu_get_data_num(BLL_IMU_DIR_BACK) >= 1){
  165. bll_imu_get_data(BLL_IMU_DIR_BACK, 0, &data);
  166. mag6310_back[0] = data.mag[0];mag6310_back[1] = data.mag[1];mag6310_back[2] = data.mag[2];
  167. }
  168. if(mFlash.isHost){
  169. process_imu_data_front(group_num);
  170. }else if(Slave_Get7_5ms_interval()){
  171. process_imu_data_front(group_num);
  172. }
  173. // app_game_led(1);
  174. }
  175. }
  176. static void app_game_Process(void)
  177. {
  178. static uint8_t configcnt = 0;
  179. static uint8_t state = 0;
  180. uint8_t front_CS =0,back_CS =0;
  181. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  182. switch(state){
  183. case 0:{
  184. if(game_manager.Signal){
  185. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&game_bll_imu_param_t);
  186. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&game_bll_imu_param_t);
  187. DEBUG_LOG("front_CS:%d back_CS:%d\r\n",front_CS,back_CS);
  188. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  189. state =1;
  190. game_manager.GameMode =1;
  191. }
  192. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  193. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  194. if(++configcnt >= 200){configcnt =0;
  195. game_manager.Signal =0;
  196. bll_imu_Resume_unregister_config_param(&game_bll_imu_param_t);
  197. Except_TxError(EXCEPT_GAME,"shoes into game mode fail");
  198. Process_SetHoldOn(app_game_Process,0);
  199. }
  200. }
  201. if(0 != game_manager.HeartCnt)game_manager.HeartCnt =0;
  202. }
  203. if(game_manager.clientCnt >0 && mFlash.isHost) app_game_notify_host(0);
  204. break;
  205. }
  206. case 1:{
  207. if(mFlash.isHost){//管理右鞋状态
  208. if(host_isconnect()){
  209. if(0 == game_manager.clientCnt){
  210. app_game_notify_host(1);
  211. if(++configcnt >= 100){configcnt =0;
  212. DEBUG_LOG("ERR_NUM_GAME app_game_Process\r\n");
  213. Except_TxError(EXCEPT_GAME,"no get right shoes data");
  214. }
  215. }
  216. else if(configcnt>0) configcnt = 0;
  217. if(0 != game_manager.right_discnt)game_manager.right_discnt =0;
  218. }
  219. else game_manager.right_discnt++;
  220. }
  221. if(0 == slave_isconnect())game_manager.client_discnt++;
  222. else if(0 != game_manager.client_discnt)game_manager.client_discnt =0;
  223. if(0 == game_manager.Signal || //收到退出游戏的指令
  224. game_manager.client_discnt > 50 || //与主机断开5秒后,退出游戏模式
  225. game_manager.right_discnt > 100 //与右鞋断开10秒后,退出游戏模式
  226. ){
  227. if(game_manager.client_discnt > 50)Except_TxError(EXCEPT_GAME,"In game mode,client disconnted long time");
  228. if(game_manager.right_discnt > 100)Except_TxError(EXCEPT_GAME,"In game mode,right shoes disconnted long time");
  229. configcnt =0;
  230. state =0;
  231. game_manager.Signal = 0;
  232. game_manager.client_discnt =0;
  233. game_manager.right_discnt =0;
  234. game_manager.GameMode =0;
  235. bll_imu_Resume_unregister_config_param(&game_bll_imu_param_t);
  236. if(mFlash.isHost)app_game_notify_host(0);
  237. IMU_STATUS = 0;
  238. set_pdr_status();
  239. // app_game_led(0);
  240. Process_SetHoldOn(app_game_Process,0);
  241. }
  242. break;
  243. }
  244. default: state =0;game_manager.Signal = 0;game_manager.client_discnt =0;Process_SetHoldOn(app_game_Process,0);break;
  245. }
  246. }
  247. static void app_AutoOutgame_Process(void){
  248. if(mFlash.isHost && app_game_GetGameMode()){
  249. DEBUG_LOG("game_manager.GameModeHeartCnt:%d\r\n",game_manager.HeartCnt);
  250. if(game_manager.HeartCnt++ >= 5){
  251. game_manager.Signal = 0;
  252. bll_imu_Resume_unregister_config_param(&game_bll_imu_param_t);
  253. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1);
  254. }
  255. }
  256. }
  257. void app_game_Init(void)
  258. {
  259. Process_Start(100,"app_game",app_game_Process);
  260. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  261. Process_Start(60000,"AutoOutgame",app_AutoOutgame_Process);
  262. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, gamemode_data_notify_cb);
  263. }