app_game.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. static volatile uint8_t clientCnt;
  18. static uint8_t isGameMode = 0;
  19. static uint8_t GameModeHeartCnt = 0;
  20. void app_game_SetClientGameMode(void)
  21. {
  22. if(clientCnt!=3) clientCnt = 3;
  23. }
  24. void cb_BLE_Client_R_GAMEMODE(void* handle)
  25. {
  26. BLE_Client_Rx_t* target = handle;
  27. isGameMode = target->pDat[0];
  28. if(isGameMode){
  29. hal_mode_set(HAL_MODE_GAME);
  30. }else{
  31. hal_mode_set(HAL_MODE_NORMAL);
  32. }
  33. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&isGameMode,1);
  34. if(1 == isGameMode)GameModeHeartCnt =0;
  35. }
  36. static void app_AutoOutgame_Process(void){
  37. if(mFlash.isHost && hal_mode_get() == HAL_MODE_GAME){
  38. SEGGER_RTT_printf(0,"GameModeHeartCnt:%d\r\n",GameModeHeartCnt);
  39. if(GameModeHeartCnt++ >= 5){
  40. isGameMode = 0;
  41. hal_mode_set(HAL_MODE_NORMAL);
  42. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&isGameMode,1);
  43. }
  44. }
  45. }
  46. static void app_game_Process(void)
  47. {
  48. #if GAME_ENANBLE
  49. isGameMode = 1;
  50. IMU_SetGameMode(isGameMode);
  51. #else
  52. if(slave_isconnect()==0){
  53. if(isGameMode>0){ isGameMode = 0;
  54. hal_mode_set(HAL_MODE_NORMAL);
  55. }
  56. }
  57. #endif
  58. if(mFlash.isHost){
  59. if(isGameMode>0){static uint8_t errCnt = 0;
  60. if(clientCnt==0){
  61. uint8_t clientMode = 1;
  62. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  63. if(++errCnt>=10) {
  64. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  65. app_err_Set(ERR_NUM_GAME,1);
  66. }
  67. }else{
  68. if(errCnt>0) errCnt = 0;
  69. }
  70. }else{
  71. if(clientCnt>0){
  72. uint8_t clientMode = 0;
  73. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  74. }
  75. }
  76. if(clientCnt>0) clientCnt--;
  77. }
  78. }
  79. void app_game_Init(void)
  80. {
  81. Process_Start(1000,"app_game",app_game_Process);
  82. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  83. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  84. }