app_connect_manage.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. switch(state){
  99. case 0:{
  100. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  101. SEGGER_RTT_printf(0,"=======>scan_start\n");
  102. Process_SetHoldOn(app_connect_Process,1);
  103. scan_start();
  104. cnt = 0;
  105. state = 1;
  106. }
  107. break;}
  108. case 1:{
  109. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  110. SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
  111. nrf_ble_scan_stop();
  112. Process_SetHoldOn(app_connect_Process,0);
  113. state = 0;
  114. }else if(host_isconnect()){
  115. SEGGER_RTT_printf(0,"=======>host_isconnect\n");
  116. Process_SetHoldOn(app_connect_Process,0);
  117. if(mFlash.isHost){
  118. update_temp =0;
  119. ble_con_int = 0;
  120. }
  121. state =2;
  122. }
  123. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  124. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  125. nrf_ble_scan_stop();
  126. Process_SetHoldOn(app_connect_Process,0);
  127. state = 0;
  128. }
  129. break;}
  130. case 2:{
  131. if(mFlash.isHost){
  132. if(hal_mode_get() == HAL_MODE_GAME){//游戏模式下更新连接间隔
  133. if(ble_con_int !=2 && update_temp <= 60){
  134. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  135. ble_con_int =2;
  136. update_temp =0;
  137. }
  138. else ++update_temp;
  139. }
  140. }else{
  141. if(ble_con_int !=1 && update_temp <= 60){
  142. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  143. ble_con_int =1;
  144. update_temp =0;
  145. }
  146. else update_temp++;
  147. }
  148. }
  149. }
  150. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  151. if(host_isconnect()){host_disconnect();
  152. }
  153. }
  154. if(!host_isconnect()){
  155. SEGGER_RTT_printf(0,"=======>host_disconnect\n");
  156. Process_SetHoldOn(app_connect_Process,0);
  157. state = 0;
  158. }
  159. break;}
  160. default:state=0;break;
  161. }
  162. }
  163. void app_connect_manage_Init(void)
  164. {
  165. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  166. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  167. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  168. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  169. #if BLE_LED_TEST_ENANBLE
  170. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  171. #endif
  172. }