app_connect_manage.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\n");
  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. // else slave_update_conn_interval_request(100,100);
  52. // Process_Start(200," ",cb_Slave_Connect_LED);
  53. }
  54. void cb_Host_Connect(void)
  55. {
  56. SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
  57. // else slave_update_conn_interval_request(100,100);
  58. // Process_Start(200," ",cb_Slave_Connect_LED);
  59. }
  60. void cb_BLE_Client_R_CONNET_LED(void* handle)
  61. {
  62. if(0 == connect_LED_FlashFlag){
  63. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  64. SEGGER_RTT_printf(0,"====================>cb_BLE_Client_R_CONNET_LED\n");
  65. connect_LED_FlashFlag =1;
  66. }
  67. }
  68. static BLE_Host_Tx_t mBLE_Host_T_CONNET_R = {
  69. .n = 3,
  70. .t = 100,
  71. .cb = 0,
  72. };
  73. void app_connect_LED_Process(void)
  74. {
  75. static uint8_t flashLED = 0;
  76. if(slave_isconnect()){
  77. if(host_isconnect()&&flashLED==0){
  78. flashLED = 1;
  79. BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&flashLED,1);
  80. BLE_Host_Tx_Send(&mBLE_Host_T_CONNET_R,BLE_CONNET_LED,&flashLED,1);
  81. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  82. SEGGER_RTT_printf(0,"====================>BLE_Host_T_CONNET_LED\n");
  83. }
  84. }else{
  85. if(flashLED>0) {
  86. flashLED = 0;
  87. }
  88. }
  89. }
  90. void app_connect_Process(void)
  91. {
  92. static uint8_t state = 0;
  93. static uint32_t cnt = 0;
  94. static uint16_t update_temp = 0;
  95. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  96. // static uint16_t tim1=0;
  97. // tim1++
  98. // if(tim1 >= 10){ tim1 = 0;
  99. // SEGGER_RTT_printf(0,"=======>app_client_step_GetIsScan()=%d\n",app_client_step_GetIsScan());
  100. // SEGGER_RTT_printf(0,"=======>slave_isconnect()=%d\n",slave_isconnect());
  101. // SEGGER_RTT_printf(0,"=======>host_isconnect()=%d\n",host_isconnect());
  102. // SEGGER_RTT_printf(0,"=======>app_charge_Getstate()=%d\n",app_charge_Getstate());
  103. // }
  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)update_temp =0;
  124. state =2;
  125. }
  126. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  127. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  128. nrf_ble_scan_stop();
  129. Process_SetHoldOn(app_connect_Process,0);
  130. state = 0;
  131. }
  132. break;}
  133. case 2:{
  134. static uint8_t gamemode_chang =0;
  135. if(mFlash.isHost){
  136. if(hal_mode_get() == HAL_MODE_GAME){//游戏模式下更新连接间隔
  137. if(gamemode_chang !=1){
  138. gamemode_chang =1;
  139. update_temp =0;
  140. }
  141. if(ble_con_int !=2 && update_temp <= 60){
  142. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5))ble_con_int =2;
  143. else ++update_temp;
  144. }
  145. }else{
  146. if(gamemode_chang != 2){
  147. gamemode_chang = 2;
  148. update_temp =0;
  149. }
  150. if(ble_con_int !=1 && update_temp <= 60){
  151. if(APP_SUCCESS == Ble_update_conn_interval(100,100))ble_con_int =1;
  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. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  173. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  174. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  175. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  176. }