app_game.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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
  57. static uint8_t errCnt = 0;
  58. uint8_t clientMode = 0;
  59. if(hal_mode_get() != HAL_MODE_GAME){
  60. if(1 == game_manager.isGameMode){
  61. hal_mode_set(HAL_MODE_GAME);
  62. errCnt = 0;
  63. }
  64. if(game_manager.clientCnt >0 && mFlash.isHost){
  65. clientMode = 0;
  66. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  67. }
  68. }
  69. else{//游戏模式
  70. if(mFlash.isHost){//管理右鞋状态
  71. if(host_isconnect()){
  72. if(0 == game_manager.clientCnt){
  73. clientMode = 1;
  74. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  75. if(++errCnt >= 10){
  76. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  77. app_err_Set(ERR_NUM_GAME,1);
  78. }
  79. }
  80. else if(errCnt>0) errCnt = 0;
  81. game_manager.right_discnt =0;
  82. }
  83. else{
  84. if(game_manager.right_discnt++ >10){//与右鞋断开10秒后,退出游戏模式
  85. game_manager.isGameMode = 0;
  86. hal_mode_set(HAL_MODE_NORMAL);
  87. }
  88. }
  89. }
  90. if(0 == game_manager.isGameMode){
  91. hal_mode_set(HAL_MODE_NORMAL);//收到退出游戏指令,退出游戏模式
  92. if(mFlash.isHost){
  93. clientMode = 0;
  94. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  95. }
  96. }
  97. //与主机断开5秒后,退出游戏模式
  98. if(!slave_isconnect()){
  99. if(game_manager.client_discnt++ >5){
  100. game_manager.isGameMode = 0;
  101. hal_mode_set(HAL_MODE_NORMAL);
  102. }
  103. }else game_manager.client_discnt =0;
  104. }
  105. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  106. #endif
  107. }
  108. void app_game_Init(void)
  109. {
  110. Process_Start(1000,"app_game",app_game_Process);
  111. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  112. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  113. }