app_game.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "hal_ser_imu_mode_manage.h"
  15. /********************** 函数声明区 *************************/
  16. typedef struct game_manager{
  17. uint8_t clientCnt; //右鞋游戏模式的标志位
  18. uint8_t right_discnt; //右鞋断开连接计数器
  19. uint8_t client_discnt; //主机连接断开计数器
  20. uint8_t Signal; //收到主机的游戏指令
  21. uint8_t HeartCnt; //游戏模式心跳维护,左鞋
  22. }game_manager_t;
  23. static game_manager_t game_manager ={0};
  24. void app_game_SetClientGameMode(void)
  25. {
  26. if(game_manager.clientCnt != 3) game_manager.clientCnt = 3;
  27. }
  28. static void cb_BLE_Client_R_GAMEMODE(void* handle)
  29. {
  30. BLE_Client_Rx_t* target = handle;
  31. game_manager.Signal = target->pDat[0];
  32. if(game_manager.Signal){
  33. game_manager.HeartCnt =0;
  34. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  35. //是否上传原始数据
  36. if(1 == game_manager.Signal)IMU_Dtalige_Rowdata_OFF();
  37. else if(2 == game_manager.Signal)IMU_Dtalige_Rowdata_ON();
  38. }else{
  39. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  40. }
  41. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1);
  42. DEBUG_LOG(">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.Signal);
  43. }
  44. static void app_game_notify_host(uint8_t mode){
  45. uint8_t clientMode = mode;
  46. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  47. }
  48. static void app_game_Process(void)
  49. {
  50. static uint8_t errCnt = 0;
  51. static uint8_t state = 0;
  52. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  53. switch(state){
  54. case 0:
  55. if(game_manager.Signal){
  56. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) == -1){
  57. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  58. if(++errCnt >= 20){errCnt =0;
  59. game_manager.Signal =0;
  60. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  61. Except_TxError(EXCEPT_GAME,"shoes into game mode fail");
  62. }
  63. }
  64. else state =1;
  65. if(0 != game_manager.HeartCnt)game_manager.HeartCnt =0;
  66. }
  67. if(game_manager.clientCnt >0 && mFlash.isHost) app_game_notify_host(0);
  68. break;
  69. case 1:
  70. if(mFlash.isHost){//管理右鞋状态
  71. if(host_isconnect()){
  72. if(0 == game_manager.clientCnt){
  73. app_game_notify_host(1);
  74. if(++errCnt >= 10){errCnt =0;
  75. DEBUG_LOG("ERR_NUM_GAME app_game_Process\r\n");
  76. Except_TxError(EXCEPT_GAME,"no get right shoes data");
  77. }
  78. }
  79. else if(errCnt>0) errCnt = 0;
  80. if(0 != game_manager.right_discnt)game_manager.right_discnt =0;
  81. }
  82. else game_manager.right_discnt++;
  83. }
  84. if(0 == slave_isconnect())game_manager.client_discnt++;
  85. else if(0 != game_manager.client_discnt)game_manager.client_discnt =0;
  86. if(0 == game_manager.Signal || //收到退出游戏的指令
  87. game_manager.client_discnt > 5 || //与主机断开5秒后,退出游戏模式
  88. game_manager.right_discnt > 10 //与右鞋断开10秒后,退出游戏模式
  89. ){
  90. if(game_manager.right_discnt > 10)Except_TxError(EXCEPT_GAME,"In game mode,right shoes disconnted long time");
  91. if(game_manager.client_discnt > 5)Except_TxError(EXCEPT_GAME,"In game mode,client disconnted long time");
  92. errCnt =0;
  93. state =0;
  94. game_manager.Signal = 0;
  95. game_manager.client_discnt =0;
  96. game_manager.right_discnt =0;
  97. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  98. if(mFlash.isHost)app_game_notify_host(0);
  99. }
  100. break;
  101. default: state =0;game_manager.Signal = 0;game_manager.client_discnt =0;break;
  102. }
  103. }
  104. static void app_AutoOutgame_Process(void){
  105. if(mFlash.isHost && (hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1)){
  106. DEBUG_LOG("game_manager.GameModeHeartCnt:%d\r\n",game_manager.HeartCnt);
  107. if(game_manager.HeartCnt++ >= 5){
  108. game_manager.Signal = 0;
  109. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  110. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.Signal,1);
  111. }
  112. }
  113. }
  114. void app_game_Init(void)
  115. {
  116. Process_Start(1000,"app_game",app_game_Process);
  117. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  118. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  119. }