app_game.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "app_game.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "hal_battery.h"
  8. #include "app_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_ble_host.h"
  11. #include "nrf_delay.h"
  12. #include "app_flash.h"
  13. #include "ble_comm.h"
  14. #include "app_err.h"
  15. #include "hal_mode_manage.h"
  16. #include "ble_comm.h"
  17. /********************** 函数声明区 *************************/
  18. typedef struct game_manager{
  19. uint8_t clientCnt; //右鞋游戏模式的标志位
  20. uint8_t right_discnt; //右鞋断开连接计数器
  21. uint8_t client_discnt; //主机连接断开计数器
  22. uint8_t isGameMode; //游戏模式标志位
  23. uint8_t GameModeHeartCnt;//游戏模式心跳维护,左鞋
  24. }game_manager_t;
  25. static game_manager_t game_manager ={0};
  26. void app_game_SetClientGameMode(void)
  27. {
  28. if(game_manager.clientCnt != 3) game_manager.clientCnt = 3;
  29. }
  30. void cb_BLE_Client_R_GAMEMODE(void* handle)
  31. {
  32. BLE_Client_Rx_t* target = handle;
  33. game_manager.isGameMode = target->pDat[0];
  34. if(game_manager.isGameMode){
  35. hal_mode_set(HAL_MODE_GAME);
  36. game_manager.GameModeHeartCnt =0;
  37. }else{
  38. if(hal_mode_get() != HAL_MODE_SELF_CHECK)hal_mode_set(HAL_MODE_NORMAL);
  39. }
  40. switch(game_manager.isGameMode)
  41. {
  42. case 1:
  43. IMU_Dtalige_Rowdata_OFF();
  44. break;
  45. case 2:
  46. IMU_Dtalige_Rowdata_ON();
  47. break;
  48. }
  49. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
  50. SEGGER_RTT_printf(0,">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.isGameMode);
  51. }
  52. static void app_AutoOutgame_Process(void){
  53. if(mFlash.isHost && hal_mode_get() == HAL_MODE_GAME){
  54. SEGGER_RTT_printf(0,"game_manager.GameModeHeartCnt:%d\r\n",game_manager.GameModeHeartCnt);
  55. if(game_manager.GameModeHeartCnt++ >= 5){
  56. game_manager.isGameMode = 0;
  57. hal_mode_set(HAL_MODE_NORMAL);
  58. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
  59. }
  60. }
  61. }
  62. static void app_game_Process(void)
  63. {
  64. #if GAME_ENANBLE
  65. if(slave_isconnect())hal_mode_set(HAL_MODE_GAME);
  66. else hal_mode_set(HAL_MODE_NORMAL);
  67. #else
  68. static uint8_t errCnt = 0;
  69. uint8_t clientMode = 0;
  70. if(hal_mode_get() != HAL_MODE_GAME){
  71. if(1 == game_manager.isGameMode){
  72. hal_mode_set(HAL_MODE_GAME);
  73. errCnt = 0;
  74. }
  75. if(game_manager.clientCnt >0 && mFlash.isHost){
  76. clientMode = 0;
  77. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  78. }
  79. }
  80. else{//游戏模式
  81. if(mFlash.isHost){//管理右鞋状态
  82. if(host_isconnect()){
  83. if(0 == game_manager.clientCnt){
  84. clientMode = 1;
  85. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  86. if(++errCnt >= 10){
  87. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  88. app_err_Set(ERR_NUM_GAME,1);
  89. }
  90. }
  91. else if(errCnt>0) errCnt = 0;
  92. game_manager.right_discnt =0;
  93. }
  94. else{
  95. if(game_manager.right_discnt++ >10){//与右鞋断开10秒后,退出游戏模式
  96. game_manager.isGameMode = 0;
  97. hal_mode_set(HAL_MODE_NORMAL);
  98. }
  99. }
  100. }
  101. if(0 == game_manager.isGameMode){
  102. hal_mode_set(HAL_MODE_NORMAL);//收到退出游戏指令,退出游戏模式
  103. if(mFlash.isHost){
  104. clientMode = 0;
  105. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  106. }
  107. }
  108. //与主机断开5秒后,退出游戏模式
  109. if(!slave_isconnect()){
  110. if(game_manager.client_discnt++ >5){
  111. game_manager.isGameMode = 0;
  112. hal_mode_set(HAL_MODE_NORMAL);
  113. }
  114. }else game_manager.client_discnt =0;
  115. }
  116. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  117. #endif
  118. }
  119. void app_game_Init(void)
  120. {
  121. Process_Start(1000,"app_game",app_game_Process);
  122. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  123. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  124. }