app_game.c 3.4 KB

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