123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include "app_game.h"
- #include "nrf_gpio.h"
- #include "usr_config.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_mt.h"
- #include "hal_battery.h"
- #include "app_charge.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "nrf_delay.h"
- #include "app_flash.h"
- #include "ble_comm.h"
- #include "app_err.h"
- #include "hal_mode_manage.h"
- /********************** 函数声明区 *************************/
- typedef struct game_manager{
- uint8_t clientCnt; //右鞋游戏模式的标志位
- uint8_t right_discnt; //右鞋断开连接计数器
- uint8_t client_discnt; //主机连接断开计数器
- uint8_t isGameMode; //游戏模式标志位
- uint8_t GameModeHeartCnt;//游戏模式心跳维护,左鞋
- }game_manager_t;
- static game_manager_t game_manager ={0};
- void app_game_SetClientGameMode(void)
- {
- if(game_manager.clientCnt != 3) game_manager.clientCnt = 3;
- }
- void cb_BLE_Client_R_GAMEMODE(void* handle)
- {
- BLE_Client_Rx_t* target = handle;
- game_manager.isGameMode = target->pDat[0];
- if(game_manager.isGameMode){
- hal_mode_set(HAL_MODE_GAME);
- game_manager.GameModeHeartCnt =0;
- }else{
- hal_mode_set(HAL_MODE_NORMAL);
- }
- BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
- // SEGGER_RTT_printf(0,">>>>>>>>>>cb_BLE_Client_R_GAMEMODE:%d\r\n",game_manager.isGameMode);
- }
- static void app_AutoOutgame_Process(void){
- if(mFlash.isHost && hal_mode_get() == HAL_MODE_GAME){
- SEGGER_RTT_printf(0,"game_manager.GameModeHeartCnt:%d\r\n",game_manager.GameModeHeartCnt);
- if(game_manager.GameModeHeartCnt++ >= 5){
- game_manager.isGameMode = 0;
- hal_mode_set(HAL_MODE_NORMAL);
- BLE_Host_Tx_Send(0,BLE_GAMEMODE,&game_manager.isGameMode,1);
- }
- }
- }
- static void app_game_Process(void)
- {
- #if GAME_ENANBLE
-
- if(slave_isconnect())hal_mode_set(HAL_MODE_GAME);
- #else
-
- static uint8_t errCnt = 0;
- uint8_t clientMode = 0;
-
- if(hal_mode_get() != HAL_MODE_GAME){
- if(1 == game_manager.isGameMode){
- hal_mode_set(HAL_MODE_GAME);
- errCnt = 0;
- }
- if(game_manager.clientCnt >0 && mFlash.isHost){
- clientMode = 0;
- BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
- }
- }
- else{//游戏模式
-
- if(mFlash.isHost){//管理右鞋状态
- if(host_isconnect()){
- if(0 == game_manager.clientCnt){
- clientMode = 1;
- BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
- if(++errCnt >= 10){
- SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
- app_err_Set(ERR_NUM_GAME,1);
- }
- }
- else if(errCnt>0) errCnt = 0;
- game_manager.right_discnt =0;
- }
- else{
- if(game_manager.right_discnt++ >10){//与右鞋断开10秒后,退出游戏模式
- game_manager.isGameMode = 0;
- hal_mode_set(HAL_MODE_NORMAL);
- }
- }
-
- }
-
- if(0 == game_manager.isGameMode){
- hal_mode_set(HAL_MODE_NORMAL);//收到退出游戏指令,退出游戏模式
- if(mFlash.isHost){
- clientMode = 0;
- BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
- }
- }
-
- //与主机断开5秒后,退出游戏模式
- if(!slave_isconnect()){
- if(game_manager.client_discnt++ >5){
- game_manager.isGameMode = 0;
- hal_mode_set(HAL_MODE_NORMAL);
- }
- }else game_manager.client_discnt =0;
-
- }
- if(game_manager.clientCnt>0) game_manager.clientCnt--;
-
- #endif
-
- }
- void app_game_Init(void)
- {
- Process_Start(1000,"app_game",app_game_Process);
- BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
- Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
- }
|