app_connect_manage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "app_connect_manage.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "ble_comm.h"
  5. #include "app_flash.h"
  6. #include "app_charge.h"
  7. #include "app_ota.h"
  8. #include "app_err.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_ble_host.h"
  11. #include "hal_led.h"
  12. #include "app_self_checking.h"
  13. #include "hal_ser_imu_mode_manage.h"
  14. /********************** 变量区 *************************/
  15. /********************** 函数声明区 *************************/
  16. extern uint8_t app_client_step_GetIsScan(void);
  17. BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
  18. .n = 10,
  19. .t = 500,
  20. .cb = 0,
  21. };
  22. #if BLE_LED_TEST_ENANBLE
  23. static uint8_t connect_LED_FlashFlag =0;
  24. static BLE_Host_Tx_t mBLE_Host_T_CONNET_R = {
  25. .n = 3,
  26. .t = 100,
  27. .cb = 0,
  28. };
  29. static void app_connect_LED_Flash(void)
  30. {
  31. static uint8_t cnt = 0;
  32. static uint8_t state = 0;
  33. // SEGGER_RTT_printf(0,"=======>app_connect_LED_Flash:%d\n",TIME_GetTicks());
  34. switch(state){
  35. case 0:
  36. Process_SetHoldOn(app_connect_LED_Flash,1);
  37. LED_Start(LED_CONNECT,COLOR_BLUE);
  38. state = 1;
  39. break;
  40. case 1:
  41. state = 0;
  42. if(++cnt >= 3){ cnt = 0;
  43. LED_Stop(LED_CONNECT);
  44. Process_SetHoldOn(app_connect_LED_Flash,0);
  45. Process_Stop(app_connect_LED_Flash);
  46. connect_LED_FlashFlag =0;
  47. // SEGGER_RTT_printf(0,"=======>out app_connect_LED_Flash\n");
  48. }
  49. else LED_Start(LED_CONNECT,COLOR_BLACK);
  50. break;
  51. default:state=0;break;
  52. }
  53. }
  54. void cb_BLE_Client_R_CONNET_LED(void* handle)
  55. {
  56. if(0 == connect_LED_FlashFlag){
  57. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  58. SEGGER_RTT_printf(0,"====================>cb_BLE_Client_R_CONNET_LED\n");
  59. connect_LED_FlashFlag =1;
  60. }
  61. }
  62. #endif
  63. void app_connect_LED_Process(void)
  64. {
  65. static uint8_t ResportCs_Flag = 0;
  66. if(slave_isconnect()){
  67. if(host_isconnect()){
  68. if(0 == ResportCs_Flag){
  69. ResportCs_Flag = 1;
  70. BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&ResportCs_Flag,1);
  71. #if BLE_LED_TEST_ENANBLE
  72. BLE_Host_Tx_Send(&mBLE_Host_T_CONNET_R,BLE_CONNET_LED,&flashLED,1);
  73. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  74. #endif
  75. SEGGER_RTT_printf(0,"====================>BLE_Host_T_CONNET_LED:%d\n",TIME_GetTicks());
  76. }
  77. }
  78. else if(ResportCs_Flag > 0) {
  79. ResportCs_Flag = 0;
  80. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  81. }
  82. }
  83. else if(ResportCs_Flag>0) {
  84. ResportCs_Flag = 0;
  85. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  86. }
  87. }
  88. void cb_Slave_Connect(void)
  89. {
  90. SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
  91. gpio_mt_run(500);
  92. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  93. }
  94. void cb_Host_Connect(void)
  95. {
  96. SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
  97. gpio_mt_run(500);
  98. // else slave_update_conn_interval_request(100,100);
  99. // Process_Start(200," ",cb_Slave_Connect_LED);
  100. }
  101. void app_connect_Process(void)
  102. {
  103. static uint8_t state = 0;
  104. static uint32_t cnt = 0;
  105. static uint16_t update_temp = 0;
  106. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  107. switch(state){
  108. case 0:{
  109. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  110. SEGGER_RTT_printf(0,"=======>scan_start\n");
  111. Process_SetHoldOn(app_connect_Process,1);
  112. scan_start();
  113. cnt = 0;
  114. state = 1;
  115. }
  116. break;}
  117. case 1:{
  118. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  119. SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
  120. nrf_ble_scan_stop();
  121. Process_SetHoldOn(app_connect_Process,0);
  122. state = 0;
  123. }else if(host_isconnect()){
  124. SEGGER_RTT_printf(0,"=======>host_isconnect\n");
  125. Process_SetHoldOn(app_connect_Process,0);
  126. if(mFlash.isHost){
  127. update_temp =0;
  128. ble_con_int = 0;
  129. }
  130. state =2;
  131. }
  132. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  133. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  134. nrf_ble_scan_stop();
  135. Process_SetHoldOn(app_connect_Process,0);
  136. state = 0;
  137. }
  138. break;}
  139. case 2:{
  140. if(mFlash.isHost){
  141. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1){//游戏模式下更新连接间隔
  142. if(ble_con_int !=2 && update_temp <= 60){
  143. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  144. ble_con_int =2;
  145. update_temp =0;
  146. }
  147. else ++update_temp;
  148. }
  149. }else{
  150. if(ble_con_int !=1 && update_temp <= 60){
  151. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  152. ble_con_int =1;
  153. update_temp =0;
  154. }
  155. else update_temp++;
  156. }
  157. }
  158. }
  159. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  160. if(host_isconnect()){host_disconnect();
  161. }
  162. }
  163. if(!host_isconnect()){
  164. SEGGER_RTT_printf(0,"=======>host_disconnect\n");
  165. Process_SetHoldOn(app_connect_Process,0);
  166. state = 0;
  167. }
  168. break;}
  169. default:state=0;break;
  170. }
  171. }
  172. void app_connect_self_check_trigger(void)
  173. {
  174. static bool first_time = false;
  175. if(app_self_checking_get_scan_trigger_timeout() != 0 && app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)
  176. {
  177. ST_scan_start();
  178. first_time = true;
  179. }
  180. else
  181. {
  182. if(first_time)
  183. {
  184. ST_scan_stop();
  185. first_time = false;
  186. }
  187. }
  188. }
  189. void app_connect_manage_Init(void)
  190. {
  191. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  192. Process_Start(100,"app_connect_self_check_trigger",app_connect_self_check_trigger);
  193. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  194. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  195. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  196. #if BLE_LED_TEST_ENANBLE
  197. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  198. #endif
  199. }