app_connect_manage.c 5.4 KB

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