app_connect_manage.c 5.3 KB

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