app_connect_manage.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "hal_charge.h"
  7. #include "app_ota.h"
  8. #include "app_game.h"
  9. #include "exception.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_ble_host.h"
  12. #include "hal_led.h"
  13. #include "app_self_checking.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. static uint8_t hostclientswitch =0;
  113. if(!mFlash.isHost){
  114. if(1 == hostclientswitch){hostclientswitch =0;
  115. state = 0;
  116. cnt = 0;
  117. update_temp = 0;
  118. ble_con_int = 0;
  119. Process_SetHoldOn(app_connect_Process,0);
  120. }
  121. return;
  122. }
  123. if( 1 != hostclientswitch)hostclientswitch =1;
  124. switch(state){
  125. case 0:{
  126. if((app_client_step_GetIsScan()||slave_isconnect()||(hal_charge_Getstate()!=BLE_CHARGE_PULLOUT) ) && 0 == app_ota_host_state()){
  127. DEBUG_LOG("=======>scan_start:%d,%d\n",app_client_step_GetIsScan(),slave_isconnect());
  128. Process_SetHoldOn(app_connect_Process,1);
  129. scan_start();
  130. cnt = 0;
  131. state = 1;
  132. }
  133. if(mFlash.isHost && 0 == slave_isconnect()){
  134. if(host_isconnect())host_disconnect();
  135. }
  136. break;}
  137. case 1:{
  138. if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  139. Except_TxError(EXCEPT_DFU,"adv disconnted over time");
  140. DEBUG_LOG("=======>nrf_ble_scan_stop\n");
  141. nrf_ble_scan_stop();
  142. Process_SetHoldOn(app_connect_Process,0);
  143. state = 0;
  144. }else if(host_isconnect()){
  145. DEBUG_LOG("=======>host_isconnect\n");
  146. Process_SetHoldOn(app_connect_Process,0);
  147. if(mFlash.isHost){
  148. update_temp =0;
  149. ble_con_int = 0;
  150. }
  151. state =2;
  152. }
  153. if(++cnt>=100){ if(0 == app_client_step_GetIsScan() && 'C' == mFlash.mClient.isConfig)Except_TxError(EXCEPT_CONNET,"scan right shoes over time");
  154. DEBUG_LOG("=======>app_err_Set nrf_ble_scan_stop\n");
  155. nrf_ble_scan_stop();
  156. Process_SetHoldOn(app_connect_Process,0);
  157. state = 0;
  158. }
  159. break;}
  160. case 2:{
  161. if(app_game_GetGameMode()){//游戏模式下更新连接间隔
  162. if(ble_con_int !=2 && update_temp <= Ble_update_connted_Error){
  163. if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5)){
  164. ble_con_int =2;
  165. update_temp =0;
  166. }
  167. else {
  168. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 7.5ms fail");
  169. ++update_temp;
  170. }
  171. }
  172. }else{
  173. if(ble_con_int !=1 && update_temp <= Ble_update_connted_Error){
  174. if(APP_SUCCESS == Ble_update_conn_interval(100,100)){
  175. ble_con_int =1;
  176. update_temp =0;
  177. }
  178. else {
  179. if(Ble_update_connted_Error == update_temp)Except_TxError(EXCEPT_CONNET,"update Ble conn interval 100ms fail");
  180. update_temp++;
  181. }
  182. }
  183. }
  184. if(app_client_step_GetIsScan()==0 && slave_isconnect()==0 && (hal_charge_Getstate()==BLE_CHARGE_PULLOUT)){
  185. if(host_isconnect())host_disconnect();
  186. }
  187. if(!host_isconnect()){
  188. DEBUG_LOG("=======>host_disconnect\n");
  189. Process_SetHoldOn(app_connect_Process,0);
  190. state = 0;
  191. }
  192. break;}
  193. default:state=0;break;
  194. }
  195. }
  196. static void cb_BLE_BLE_CONNET_R(void* handle)
  197. {
  198. uint8_t host_state=BLE_RIGHT_DISCONNTED;
  199. host_state =app_conneted_getRight();
  200. // DEBUG_LOG("!!!!app_conneted_getRight:%d\n",host_state);
  201. BLE_Client_Tx_Send(0,BLE_CONNET_R,&host_state,1);
  202. }
  203. void app_connect_manage_Init(void)
  204. {
  205. Process_Start(100,"app_connect",app_connect_Process);
  206. Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
  207. Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
  208. Process_Start(100,"app_connect_led",app_connect_LED_Process);
  209. BLE_Client_Rx_Regist(BLE_CONNET_R,cb_BLE_BLE_CONNET_R);
  210. BLE_Client_Rx_Regist(BLE_BLINK_LED,cb_BLE_BLINK_LED);
  211. }