app_game.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "hal_imu.h"
  14. #include "ble_comm.h"
  15. #include "app_err.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. IMU_SetGameMode(isGameMode);
  29. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&isGameMode,1);
  30. if(1 == target->pDat[0])GameModeHeartCnt =0;
  31. }
  32. static void app_AutoOutgame_Process(void){
  33. if(mFlash.isHost && IMU_GetSensorEnable()){
  34. SEGGER_RTT_printf(0,"GameModeHeartCnt:%d\r\n",GameModeHeartCnt);
  35. if(GameModeHeartCnt++ >= 5){
  36. isGameMode = 0;
  37. IMU_SetGameMode(isGameMode);
  38. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&isGameMode,1);
  39. }
  40. }
  41. }
  42. static void app_game_Process(void)
  43. {
  44. #if GAME_ENANBLE
  45. isGameMode = 1;
  46. IMU_SetGameMode(isGameMode);
  47. #else
  48. if(slave_isconnect()==0){
  49. if(isGameMode>0){ isGameMode = 0;
  50. IMU_SetGameMode(isGameMode);
  51. }
  52. }
  53. #endif
  54. if(mFlash.isHost){
  55. if(isGameMode>0){static uint8_t errCnt = 0;
  56. if(clientCnt==0){
  57. uint8_t clientMode = 1;
  58. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  59. if(++errCnt>=10) {
  60. SEGGER_RTT_printf(0,"ERR_NUM_GAME app_game_Process\r\n");
  61. app_err_Set(ERR_NUM_GAME,1);
  62. }
  63. }else{
  64. if(errCnt>0) errCnt = 0;
  65. }
  66. }else{
  67. if(clientCnt>0){
  68. uint8_t clientMode = 0;
  69. BLE_Host_Tx_Send(0,BLE_GAMEMODE,&clientMode,1);
  70. }
  71. }
  72. if(clientCnt>0) clientCnt--;
  73. }
  74. }
  75. void app_game_Init(void)
  76. {
  77. Process_Start(1000,"app_game",app_game_Process);
  78. BLE_Client_Rx_Regist(BLE_GAMEMODE,cb_BLE_Client_R_GAMEMODE);
  79. Process_Start(60000,"app_AutoOutgame",app_AutoOutgame_Process);
  80. }