app_connect_manage.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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){
  78. ResportCs_Flag = 0;
  79. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  80. }
  81. }
  82. else if(ResportCs_Flag>0){
  83. ResportCs_Flag = 0;
  84. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  85. }
  86. }
  87. void cb_Slave_Connect(void)
  88. {
  89. SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
  90. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  91. }
  92. void cb_Host_Connect(void)
  93. {
  94. SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
  95. // else slave_update_conn_interval_request(100,100);
  96. // Process_Start(200," ",cb_Slave_Connect_LED);
  97. }
  98. void app_connect_Process(void)
  99. {
  100. static uint8_t state = 0;
  101. static uint32_t cnt = 0;
  102. static uint16_t update_temp = 0;
  103. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  104. switch(state){
  105. case 0:{
  106. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  107. SEGGER_RTT_printf(0,"=======>scan_start\n");
  108. Process_SetHoldOn(app_connect_Process,1);
  109. scan_start();
  110. cnt = 0;
  111. state = 1;
  112. }
  113. break;}
  114. case 1:{
  115. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  116. SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
  117. nrf_ble_scan_stop();
  118. Process_SetHoldOn(app_connect_Process,0);
  119. state = 0;
  120. }else if(host_isconnect()){
  121. SEGGER_RTT_printf(0,"=======>host_isconnect\n");
  122. Process_SetHoldOn(app_connect_Process,0);
  123. if(mFlash.isHost){
  124. update_temp =0;
  125. ble_con_int = 0;
  126. }
  127. state =2;
  128. }
  129. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  130. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  131. nrf_ble_scan_stop();
  132. Process_SetHoldOn(app_connect_Process,0);
  133. state = 0;
  134. }
  135. break;}
  136. case 2:{
  137. if(mFlash.isHost){
  138. if(hal_mode_get() == HAL_MODE_GAME){//游戏模式下更新连接间隔
  139. if(ble_con_int !=2 && update_temp <= 60){
  140. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  141. ble_con_int =2;
  142. update_temp =0;
  143. }
  144. else ++update_temp;
  145. }
  146. }else{
  147. if(ble_con_int !=1 && update_temp <= 60){
  148. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  149. ble_con_int =1;
  150. update_temp =0;
  151. }
  152. else update_temp++;
  153. }
  154. }
  155. }
  156. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  157. if(host_isconnect()){host_disconnect();
  158. }
  159. }
  160. if(!host_isconnect()){
  161. SEGGER_RTT_printf(0,"=======>host_disconnect\n");
  162. Process_SetHoldOn(app_connect_Process,0);
  163. state = 0;
  164. }
  165. break;}
  166. default:state=0;break;
  167. }
  168. }
  169. void app_connect_manage_Init(void)
  170. {
  171. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  172. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  173. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  174. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  175. #if BLE_LED_TEST_ENANBLE
  176. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  177. #endif
  178. }