app_connect_manage.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "exception.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. #include "exception.h"
  15. #include "hal_mt.h"
  16. #include "hal_battery.h"
  17. /********************** 变量区 *************************/
  18. #define Ble_update_connted_Error 60//一次最多更新蓝牙连接间隔 次数
  19. /********************** 函数声明区 *************************/
  20. extern uint8_t app_client_step_GetIsScan(void);
  21. BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
  22. .n = 10,
  23. .t = 500,
  24. .cb = 0,
  25. };
  26. static uint16_t LedPlaytime =0;
  27. static uint32_t LedBlinktim =0;
  28. static uint8_t app_conneted_getRight(void){
  29. uint8_t state =BLE_RIGHT_DISCONNTED;
  30. if('C' != mFlash.mClient.isConfig)
  31. state =BLE_RIGHT_NOCONFIG;
  32. else if(host_isconnect())
  33. state =BLE_RIGHT_CONNTED;
  34. #if BleNameHoldOn_ENANBLE
  35. if(host_isconnect())
  36. state =BLE_RIGHT_CONNTED;
  37. #endif
  38. return state;
  39. }
  40. void app_connect_LED_Process(void)
  41. {
  42. static uint8_t ResportCs_Flag = 0;
  43. static uint8_t state =0;
  44. static uint8_t host_state=0;
  45. switch(state){
  46. case 0:
  47. if(slave_isconnect()){
  48. state =1;
  49. ResportCs_Flag = 0;
  50. }
  51. break;
  52. case 1:
  53. if(host_isconnect()){
  54. if(0 == ResportCs_Flag){ResportCs_Flag = 1;
  55. host_state = app_conneted_getRight();
  56. BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_CONNET_R,&host_state,1);
  57. DEBUG_LOG("====================>BLE_Host_T_CONNET_LED:%d\n",TIME_GetTicks());
  58. }
  59. }
  60. else if(ResportCs_Flag > 0) {ResportCs_Flag = 0;
  61. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  62. }
  63. if(0 != LedPlaytime){
  64. if(TIME_GetTicks() - LedBlinktim >=LedPlaytime){LedPlaytime =0;
  65. LED_Stop(LED_OVERTURN);
  66. Process_SetHoldOn(app_connect_LED_Process,0);
  67. }
  68. }
  69. if(!slave_isconnect()){
  70. state =0;
  71. LedPlaytime =0;
  72. Process_SetHoldOn(app_connect_LED_Process,0);
  73. LED_Stop(LED_OVERTURN);
  74. BLE_Client_Tx_Clear(&mBLE_Client_T_CONNET_R);
  75. }
  76. break;
  77. default:state =0;LedPlaytime =0;LED_Stop(LED_OVERTURN);break;
  78. }
  79. }
  80. static void cb_BLE_BLINK_LED(void* handle)
  81. {
  82. BLE_Client_Rx_t* target = handle;
  83. uint8_t buf[2]={0};
  84. buf[0] = target->pDat[0];
  85. buf[1] = target->pDat[1];
  86. LedPlaytime = ((target->pDat[0]<<8) + target->pDat[1]);
  87. DEBUG_LOG("=======>cb_BLE_BLINK_LED,displaytime:%d\n",LedPlaytime);
  88. BLE_Host_Tx_Send(0,BLE_BLINK_LED,buf,2);
  89. Process_SetHoldOn(app_connect_LED_Process,1);
  90. LedBlinktim = TIME_GetTicks();
  91. if(GetBatteryPersent()>20){
  92. LED_Start(LED_OVERTURN,COLOR_GREEN);
  93. }else{
  94. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  95. }
  96. }
  97. void cb_Slave_Connect(void)
  98. {
  99. DEBUG_LOG("=======>cb_Slave_Connect\n");
  100. if(mFlash.isHost) slave_update_conn_interval_request(30,30);
  101. }
  102. void cb_Host_Connect(void)
  103. {
  104. DEBUG_LOG("=======>cb_Host_Connect\n");
  105. }
  106. void app_connect_Process(void)
  107. {
  108. static uint8_t state = 0;
  109. static uint32_t cnt = 0;
  110. static uint16_t update_temp = 0;
  111. static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
  112. switch(state){
  113. case 0:{
  114. if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
  115. DEBUG_LOG("=======>scan_start:%d,%d\n",app_client_step_GetIsScan(),slave_isconnect());
  116. Process_SetHoldOn(app_connect_Process,1);
  117. scan_start();
  118. cnt = 0;
  119. state = 1;
  120. }
  121. if(mFlash.isHost && 0 == slave_isconnect()){
  122. if(host_isconnect())host_disconnect();
  123. }
  124. break;}
  125. case 1:{
  126. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  127. Except_TxError(EXCEPT_DFU,"adv disconnted over time");
  128. DEBUG_LOG("=======>nrf_ble_scan_stop\n");
  129. nrf_ble_scan_stop();
  130. Process_SetHoldOn(app_connect_Process,0);
  131. state = 0;
  132. }else if(host_isconnect()){
  133. DEBUG_LOG("=======>host_isconnect\n");
  134. Process_SetHoldOn(app_connect_Process,0);
  135. if(mFlash.isHost){
  136. update_temp =0;
  137. ble_con_int = 0;
  138. }
  139. state =2;
  140. }
  141. if(++cnt>=100){ if(0 == app_client_step_GetIsScan() && 'C' == mFlash.mClient.isConfig)Except_TxError(EXCEPT_CONNET,"scan right shoes over time");
  142. DEBUG_LOG("=======>app_err_Set nrf_ble_scan_stop\n");
  143. nrf_ble_scan_stop();
  144. Process_SetHoldOn(app_connect_Process,0);
  145. state = 0;
  146. }
  147. break;}
  148. case 2:{
  149. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_GAME) != -1){//游戏模式下更新连接间隔
  150. if(ble_con_int !=2 && update_temp <= Ble_update_connted_Error){
  151. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  152. ble_con_int =2;
  153. update_temp =0;
  154. }
  155. else {
  156. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 7.5ms fail");
  157. ++update_temp;
  158. }
  159. }
  160. }else{
  161. if(ble_con_int !=1 && update_temp <= Ble_update_connted_Error){
  162. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  163. ble_con_int =1;
  164. update_temp =0;
  165. }
  166. else {
  167. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 100ms fail");
  168. update_temp++;
  169. }
  170. }
  171. }
  172. if(app_client_step_GetIsScan()==0 && slave_isconnect()==0 && (app_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  173. DEBUG_LOG("app_charge_Getstate()==BLE_CHARGE_PULLOUT\n");
  174. if(host_isconnect())host_disconnect();
  175. }
  176. if(!host_isconnect()){
  177. DEBUG_LOG("=======>host_disconnect\n");
  178. Process_SetHoldOn(app_connect_Process,0);
  179. state = 0;
  180. }
  181. break;}
  182. default:state=0;break;
  183. }
  184. }
  185. static void cb_BLE_BLE_CONNET_R(void* handle)
  186. {
  187. uint8_t host_state=BLE_RIGHT_DISCONNTED;
  188. host_state =app_conneted_getRight();
  189. // DEBUG_LOG("!!!!app_conneted_getRight:%d\n",host_state);
  190. BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  191. }
  192. void app_connect_manage_Init(void)
  193. {
  194. if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
  195. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  196. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  197. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  198. BLE_Client_Rx_Regist(BLE_CONNET_R,cb_BLE_BLE_CONNET_R);
  199. BLE_Client_Rx_Regist(BLE_BLINK_LED,cb_BLE_BLINK_LED);
  200. }