app_connect_manage.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "app_connect_manage.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "hal_mode_manage.h"
  5. #include "ble_comm.h"
  6. #include "app_flash.h"
  7. #include "app_charge.h"
  8. #include "app_ota.h"
  9. #include "app_err.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "hal_led.h"
  13. /********************** 变量区 *************************/
  14. BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
  15. .n = 10,
  16. .t = 500,
  17. .cb = 0,
  18. };
  19. /********************** 函数声明区 *************************/
  20. extern uint8_t app_client_step_GetIsScan(void);
  21. static uint8_t connect_LED_FlashFlag =0;
  22. static void app_connect_LED_Flash(void)
  23. {
  24. static uint8_t cnt = 0;
  25. static uint8_t state = 0;
  26. SEGGER_RTT_printf(0,"====================>app_connect_LED_Flash:%d\n",TIME_GetTicks());
  27. switch(state){
  28. case 0:
  29. Process_SetHoldOn(app_connect_LED_Flash,1);
  30. LED_Start(LED_CONNECT,COLOR_BLUE);
  31. state = 1;
  32. break;
  33. case 1:
  34. LED_Start(LED_CONNECT,COLOR_BLACK);
  35. state = 0;
  36. if(++cnt>3){ cnt = 0;
  37. LED_Stop(LED_CONNECT);
  38. Process_SetHoldOn(app_connect_LED_Flash,0);
  39. Process_Stop(app_connect_LED_Flash);
  40. connect_LED_FlashFlag =0;
  41. // SEGGER_RTT_printf(0,"=======>out app_connect_LED_Flash\n");
  42. }
  43. break;
  44. default:state=0;break;
  45. }
  46. }
  47. void cb_Slave_Connect(void)
  48. {
  49. SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
  50. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  51. }
  52. void cb_Host_Connect(void)
  53. {
  54. SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
  55. // else slave_update_conn_interval_request(100,100);
  56. // Process_Start(200," ",cb_Slave_Connect_LED);
  57. }
  58. void cb_BLE_Client_R_CONNET_LED(void* handle)
  59. {
  60. if(0 == connect_LED_FlashFlag){
  61. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  62. SEGGER_RTT_printf(0,"====================>cb_BLE_Client_R_CONNET_LED\n");
  63. connect_LED_FlashFlag =1;
  64. }
  65. }
  66. static BLE_Host_Tx_t mBLE_Host_T_CONNET_R = {
  67. .n = 3,
  68. .t = 100,
  69. .cb = 0,
  70. };
  71. void app_connect_LED_Process(void)
  72. {
  73. static uint8_t flashLED = 0;
  74. if(slave_isconnect()){
  75. if(host_isconnect()&&flashLED==0){
  76. flashLED = 1;
  77. BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&flashLED,1);
  78. BLE_Host_Tx_Send(&mBLE_Host_T_CONNET_R,BLE_CONNET_LED,&flashLED,1);
  79. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  80. SEGGER_RTT_printf(0,"====================>BLE_Host_T_CONNET_LED\n");
  81. }
  82. }else{
  83. if(flashLED>0) {
  84. flashLED = 0;
  85. }
  86. }
  87. }
  88. void app_connect_Process(void)
  89. {
  90. static uint8_t state = 0;
  91. static uint32_t cnt = 0;
  92. static uint16_t update_temp = 0;
  93. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  94. switch(state){
  95. case 0:{
  96. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  97. SEGGER_RTT_printf(0,"=======>scan_start\n");
  98. Process_SetHoldOn(app_connect_Process,1);
  99. scan_start();
  100. cnt = 0;
  101. state = 1;
  102. }
  103. break;}
  104. case 1:{
  105. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  106. SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
  107. nrf_ble_scan_stop();
  108. Process_SetHoldOn(app_connect_Process,0);
  109. state = 0;
  110. }else if(host_isconnect()){
  111. SEGGER_RTT_printf(0,"=======>host_isconnect\n");
  112. Process_SetHoldOn(app_connect_Process,0);
  113. if(mFlash.isHost)update_temp =0;
  114. state =2;
  115. }
  116. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  117. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  118. nrf_ble_scan_stop();
  119. Process_SetHoldOn(app_connect_Process,0);
  120. state = 0;
  121. }
  122. break;}
  123. case 2:{
  124. static uint8_t gamemode_chang =0;
  125. if(mFlash.isHost){
  126. if(hal_mode_get() == HAL_MODE_GAME){//游戏模式下更新连接间隔
  127. if(gamemode_chang !=1){
  128. gamemode_chang =1;
  129. update_temp =0;
  130. }
  131. if(ble_con_int !=2 && update_temp <= 60){
  132. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5))ble_con_int =2;
  133. else ++update_temp;
  134. }
  135. }else{
  136. if(gamemode_chang != 2){
  137. gamemode_chang = 2;
  138. update_temp =0;
  139. }
  140. if(ble_con_int !=1 && update_temp <= 60){
  141. if(APP_SUCCESS == Ble_update_conn_interval(100,100))ble_con_int =1;
  142. else update_temp++;
  143. }
  144. }
  145. }
  146. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  147. if(host_isconnect()){host_disconnect();
  148. }
  149. }
  150. if(!host_isconnect()){
  151. SEGGER_RTT_printf(0,"=======>host_disconnect\n");
  152. Process_SetHoldOn(app_connect_Process,0);
  153. state = 0;
  154. }
  155. break;}
  156. default:state=0;break;
  157. }
  158. }
  159. void app_connect_manage_Init(void)
  160. {
  161. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  162. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  163. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  164. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  165. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  166. }