app_game.c 4.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 "exception.h"
  15. #include "hal_ser_imu_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_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  36. game_manager.GameModeHeartCnt =0;
  37. }else{
  38. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  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_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1)){
  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_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  58. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
  59. }
  60. }
  61. }
  62. static void app_game_notify_host(uint8_t mode){
  63. uint8_t clientMode = mode;
  64. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  65. }
  66. static void app_game_Process(void)
  67. {
  68. static uint8_t errCnt = 0;
  69. static uint8_t state_imu_gamemode = 0;
  70. if(game_manager.clientCnt>0) game_manager.clientCnt--;
  71. switch(state_imu_gamemode){
  72. case 0:
  73. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) == -1){
  74. if(1 == game_manager.isGameMode){
  75. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  76. if(++errCnt >= 20){
  77. errCnt =0;
  78. Except_TxError(EXCEPT_GAME,"shoes into game mode fail");
  79. }
  80. }
  81. if(game_manager.clientCnt >0 && mFlash.isHost) app_game_notify_host(0);
  82. }
  83. else state_imu_gamemode =1;
  84. break;
  85. case 1:
  86. if(mFlash.isHost){//管理右鞋状态
  87. if(host_isconnect()){
  88. if(0 == game_manager.clientCnt){
  89. app_game_notify_host(1);
  90. if(++errCnt >= 10){
  91. errCnt =0;
  92. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  93. Except_TxError(EXCEPT_GAME,"no get right shoes data");
  94. }
  95. }
  96. else if(errCnt>0) errCnt = 0;
  97. game_manager.right_discnt =0;
  98. }
  99. else{
  100. if(game_manager.right_discnt++ >10){//与右鞋断开10秒后,退出游戏模式
  101. game_manager.isGameMode = 0;
  102. errCnt =0;
  103. state_imu_gamemode =0;
  104. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  105. Except_TxError(EXCEPT_GAME,"In game mode,right shoes disconnted long time");
  106. }
  107. }
  108. }
  109. if(0 == game_manager.isGameMode){
  110. errCnt =0;
  111. state_imu_gamemode =0;
  112. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);//收到退出游戏指令,退出游戏模式
  113. if(mFlash.isHost)app_game_notify_host(0);
  114. }
  115. //与主机断开5秒后,退出游戏模式
  116. if(!slave_isconnect()){
  117. if(game_manager.client_discnt++ >5){
  118. game_manager.isGameMode = 0;
  119. errCnt =0;
  120. state_imu_gamemode =0;
  121. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_GAME, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  122. Except_TxError(EXCEPT_GAME,"In game mode,client disconnted long time");
  123. }
  124. }
  125. else game_manager.client_discnt =0;
  126. break;
  127. }
  128. }
  129. void app_game_Init(void)
  130. {
  131. Process_Start(1000,"app_game",app_game_Process);
  132. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  133. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  134. }