app_connect_manage.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #if BLE_LED_TEST_ENANBLE
  17. static uint8_t connect_LED_FlashFlag =0;
  18. BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
  19. .n = 10,
  20. .t = 500,
  21. .cb = 0,
  22. };
  23. static void app_connect_LED_Flash(void)
  24. {
  25. static uint8_t cnt = 0;
  26. static uint8_t state = 0;
  27. // SEGGER_RTT_printf(0,"=======>app_connect_LED_Flash:%d\n",TIME_GetTicks());
  28. switch(state){
  29. case 0:
  30. Process_SetHoldOn(app_connect_LED_Flash,1);
  31. LED_Start(LED_CONNECT,COLOR_BLUE);
  32. state = 1;
  33. break;
  34. case 1:
  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. else LED_Start(LED_CONNECT,COLOR_BLACK);
  44. break;
  45. default:state=0;break;
  46. }
  47. }
  48. void cb_BLE_Client_R_CONNET_LED(void* handle)
  49. {
  50. if(0 == connect_LED_FlashFlag){
  51. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  52. SEGGER_RTT_printf(0,"====================>cb_BLE_Client_R_CONNET_LED\n");
  53. connect_LED_FlashFlag =1;
  54. }
  55. }
  56. static BLE_Host_Tx_t mBLE_Host_T_CONNET_R = {
  57. .n = 3,
  58. .t = 100,
  59. .cb = 0,
  60. };
  61. void app_connect_LED_Process(void)
  62. {
  63. static uint8_t flashLED = 0;
  64. if(slave_isconnect()){
  65. if(host_isconnect()&&flashLED==0){
  66. flashLED = 1;
  67. BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&flashLED,1);
  68. BLE_Host_Tx_Send(&mBLE_Host_T_CONNET_R,BLE_CONNET_LED,&flashLED,1);
  69. Process_Start(200,"connect_LED",app_connect_LED_Flash);
  70. SEGGER_RTT_printf(0,"====================>BLE_Host_T_CONNET_LED\n");
  71. }
  72. }else{
  73. if(flashLED>0) {
  74. flashLED = 0;
  75. }
  76. }
  77. }
  78. #endif
  79. void cb_Slave_Connect(void)
  80. {
  81. SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
  82. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  83. }
  84. void cb_Host_Connect(void)
  85. {
  86. SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
  87. // else slave_update_conn_interval_request(100,100);
  88. // Process_Start(200," ",cb_Slave_Connect_LED);
  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. switch(state){
  97. case 0:{
  98. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  99. SEGGER_RTT_printf(0,"=======>scan_start\n");
  100. Process_SetHoldOn(app_connect_Process,1);
  101. scan_start();
  102. cnt = 0;
  103. state = 1;
  104. }
  105. break;}
  106. case 1:{
  107. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  108. SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
  109. nrf_ble_scan_stop();
  110. Process_SetHoldOn(app_connect_Process,0);
  111. state = 0;
  112. }else if(host_isconnect()){
  113. SEGGER_RTT_printf(0,"=======>host_isconnect\n");
  114. Process_SetHoldOn(app_connect_Process,0);
  115. if(mFlash.isHost){
  116. update_temp =0;
  117. ble_con_int = 0;
  118. }
  119. state =2;
  120. }
  121. if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
  122. SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
  123. nrf_ble_scan_stop();
  124. Process_SetHoldOn(app_connect_Process,0);
  125. state = 0;
  126. }
  127. break;}
  128. case 2:{
  129. if(mFlash.isHost){
  130. if(hal_mode_get() == HAL_MODE_GAME){//游戏模式下更新连接间隔
  131. if(ble_con_int !=2 && update_temp <= 60){
  132. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  133. ble_con_int =2;
  134. update_temp =0;
  135. }
  136. else ++update_temp;
  137. }
  138. }else{
  139. if(ble_con_int !=1 && update_temp <= 60){
  140. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  141. ble_con_int =1;
  142. update_temp =0;
  143. }
  144. else update_temp++;
  145. }
  146. }
  147. }
  148. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
  149. if(host_isconnect()){host_disconnect();
  150. }
  151. }
  152. if(!host_isconnect()){
  153. SEGGER_RTT_printf(0,"=======>host_disconnect\n");
  154. Process_SetHoldOn(app_connect_Process,0);
  155. state = 0;
  156. }
  157. break;}
  158. default:state=0;break;
  159. }
  160. }
  161. void app_connect_manage_Init(void)
  162. {
  163. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  164. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  165. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  166. #if BLE_LED_TEST_ENANBLE
  167. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  168. BLE_Client_Rx_Regist(BLE_CONNET_LED,cb_BLE_Client_R_CONNET_LED);
  169. #endif
  170. }