app_connect_manage.c 5.2 KB

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