app_game.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if GAMEDATA_TO_RAWDATA
  17. uint8_t gamedata_to_rawdata = 0;
  18. #endif
  19. /********************** 函数声明区 *************************/
  20. typedef struct game_manager{
  21. uint8_t clientCnt; //右鞋游戏模式的标志位
  22. uint8_t right_discnt; //右鞋断开连接计数器
  23. uint8_t client_discnt; //主机连接断开计数器
  24. uint8_t isGameMode; //游戏模式标志位
  25. uint8_t GameModeHeartCnt;//游戏模式心跳维护,左鞋
  26. }game_manager_t;
  27. static game_manager_t game_manager ={0};
  28. void app_game_SetClientGameMode(void)
  29. {
  30. if(game_manager.clientCnt != 3) game_manager.clientCnt = 3;
  31. }
  32. void cb_BLE_Client_R_GAMEMODE(void* handle)
  33. {
  34. BLE_Client_Rx_t* target = handle;
  35. game_manager.isGameMode = target->pDat[0];
  36. if(game_manager.isGameMode){
  37. hal_mode_set(HAL_MODE_GAME);
  38. game_manager.GameModeHeartCnt =0;
  39. }else{
  40. hal_mode_set(HAL_MODE_NORMAL);
  41. }
  42. #if GAMEDATA_TO_RAWDATA
  43. if(game_manager.isGameMode == 2){
  44. gamedata_to_rawdata = 1;
  45. }else if(game_manager.isGameMode == 0){
  46. gamedata_to_rawdata = 0;
  47. }
  48. #endif
  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
  67. static uint8_t errCnt = 0;
  68. uint8_t clientMode = 0;
  69. if(hal_mode_get() != HAL_MODE_GAME){
  70. if(1 == game_manager.isGameMode){
  71. hal_mode_set(HAL_MODE_GAME);
  72. errCnt = 0;
  73. }
  74. if(game_manager.clientCnt >0 && mFlash.isHost){
  75. clientMode = 0;
  76. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  77. }
  78. }
  79. else{//游戏模式
  80. if(mFlash.isHost){//管理右鞋状态
  81. if(host_isconnect()){
  82. if(0 == game_manager.clientCnt){
  83. clientMode = 1;
  84. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  85. if(++errCnt >= 10){
  86. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  87. app_err_Set(ERR_NUM_GAME,1);
  88. }
  89. }
  90. else if(errCnt>0) errCnt = 0;
  91. game_manager.right_discnt =0;
  92. }
  93. else{
  94. if(game_manager.right_discnt++ >10 && gamedata_to_rawdata == 0){//与右鞋断开10秒后,退出游戏模式
  95. game_manager.isGameMode = 0;
  96. hal_mode_set(HAL_MODE_NORMAL);
  97. }
  98. }
  99. }
  100. if(0 == game_manager.isGameMode){
  101. hal_mode_set(HAL_MODE_NORMAL);//收到退出游戏指令,退出游戏模式
  102. if(mFlash.isHost){
  103. clientMode = 0;
  104. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  105. }
  106. }
  107. //与主机断开5秒后,退出游戏模式
  108. if(!slave_isconnect()){
  109. if(game_manager.client_discnt++ >5){
  110. game_manager.isGameMode = 0;
  111. hal_mode_set(HAL_MODE_NORMAL);
  112. }
  113. }else game_manager.client_discnt =0;
  114. }
  115. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  116. #endif
  117. }
  118. void app_game_Init(void)
  119. {
  120. Process_Start(1000,"app_game",app_game_Process);
  121. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  122. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  123. }