app_connect_manage.c 6.7 KB

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