app_game.c 2.1 KB

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