123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include "app_connect_manage.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_imu.h"
- #include "ble_comm.h"
- #include "hal_flash.h"
- #include "app_charge.h"
- #include "app_ota.h"
- #include "app_err.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "hal_led.h"
- /********************** 变量区 *************************/
- BLE_Client_Tx_t mBLE_Client_T_CONNET_R = {
- .n = 10,
- .t = 100,
- .cb = 0,
- };
- /********************** 函数声明区 *************************/
- extern uint8_t app_client_step_GetIsScan(void);
- void app_connect_LED_Flash(void)
- {
- static uint8_t cnt = 0;
- static uint8_t state = 0;
- switch(state){
- case 0:
- Process_SetHoldOn(app_connect_LED_Flash,1);
- LED_SetColor(LED_CONNECT,COLOR_BLUE);
- LED_Start(LED_CONNECT);
- state = 1;
- break;
- case 1:
- LED_SetColor(LED_CONNECT,COLOR_BLACK);
- state = 0;
- if(++cnt>3){ cnt = 0;
- LED_Stop(LED_CONNECT);
- Process_SetHoldOn(app_connect_LED_Flash,0);
- Process_Stop(app_connect_LED_Flash);
- }
- break;
- default:state=0;break;
- }
-
- }
- void cb_Slave_Connect(void)
- {
- SEGGER_RTT_printf(0,"=======>cb_Slave_Connect\n");
- if(mFlash.isHost) slave_update_conn_interval_request(30,30);
- // else slave_update_conn_interval_request(100,100);
- // Process_Start(200," ",cb_Slave_Connect_LED);
- }
- void cb_Host_Connect(void)
- {
- SEGGER_RTT_printf(0,"=======>cb_Host_Connect\n");
- // else slave_update_conn_interval_request(100,100);
- // Process_Start(200," ",cb_Slave_Connect_LED);
- }
- void cb_BLE_Host_R_CONNET_LED(void* handle)
- {
- SEGGER_RTT_printf(0,"====================>cb_BLE_Host_R_CONNET_LED\n");
- Process_Start(200,"connect_LED",app_connect_LED_Flash);
- }
- void app_connect_LED_Process(void)
- {
- static uint8_t flashLED = 0;
- if(slave_isconnect()){
- if(host_isconnect()&&flashLED==0){ flashLED = 1;
- BLE_Client_Tx_Send(&mBLE_Client_T_CONNET_R,BLE_Client_T_CONNET_R,&flashLED,1);
- BLE_Host_Tx_Send(0,BLE_Host_T_CONNET_LED,&flashLED,1);
- Process_Start(200,"connect_LED",app_connect_LED_Flash);
- }
- }else{
- if(flashLED>0) flashLED = 0;
- }
- }
- #if DEBUG_GAME_LED
- static void Conneted_Ledupdate(uint8_t t){
- static uint16_t led_test =0;
- static uint8_t flag =0;
- led_test += t;
- if(led_test >=500){led_test =0;
- if(flag ==0){
- LED_SetColor(LED_GAME,COLOR_BLUE);
- LED_Start(LED_GAME);
- }
- else flag = (~flag);
- }
- }
- #endif
- void app_connect_Process(void)
- {
- static uint8_t state = 0;
- static uint32_t cnt = 0;
- static uint16_t update_temp = 0;
- static uint8_t ble_con_int = 0;//左右鞋蓝牙之间的连接间隔
-
- // static uint16_t tim1=0;
- // tim1++
- // if(tim1 >= 10){ tim1 = 0;
- // SEGGER_RTT_printf(0,"=======>app_client_step_GetIsScan()=%d\n",app_client_step_GetIsScan());
- // SEGGER_RTT_printf(0,"=======>slave_isconnect()=%d\n",slave_isconnect());
- // SEGGER_RTT_printf(0,"=======>host_isconnect()=%d\n",host_isconnect());
- // SEGGER_RTT_printf(0,"=======>app_charge_Getstate()=%d\n",app_charge_Getstate());
- // }
- switch(state){
- case 0:{
- if((app_client_step_GetIsScan()||slave_isconnect()||(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT)) && 0 == app_ota_host_state()){
- SEGGER_RTT_printf(0,"=======>scan_start\n");
- Process_SetHoldOn(app_connect_Process,1);
- scan_start();
- cnt = 0;
- state = 1;
- }
- break;}
- case 1:{
- if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
- SEGGER_RTT_printf(0,"=======>nrf_ble_scan_stop\n");
- nrf_ble_scan_stop();
- Process_SetHoldOn(app_connect_Process,0);
- state = 0;
- }else if(host_isconnect()){
- SEGGER_RTT_printf(0,"=======>host_isconnect\n");
- Process_SetHoldOn(app_connect_Process,0);
- if(mFlash.isHost)update_temp =0;
- state =2;
- }
- if(++cnt>=100){ app_err_Set(ERR_NUM_CONNET,1);
- SEGGER_RTT_printf(0,"=======>app_err_Set nrf_ble_scan_stop\n");
- nrf_ble_scan_stop();
- Process_SetHoldOn(app_connect_Process,0);
- state = 0;
- }
- break;}
- case 2:{
- static uint8_t gamemode_chang =0;
- if(mFlash.isHost){
- if(IMU_GetGameMode()){//游戏模式下更新连接间隔
- if(gamemode_chang !=1){
- gamemode_chang =1;
- update_temp =0;
- }
- if(ble_con_int !=2 && update_temp <= 60){
- #if DEBUG_GAME_LED
- Conneted_Ledupdate(100);
- #endif
- if(APP_SUCCESS == Ble_update_conn_interval(7.5,7.5))ble_con_int =2;
- else ++update_temp;
- }
- }else{
- if(gamemode_chang != 2){
- gamemode_chang = 2;
- update_temp =0;
- }
- if(ble_con_int !=1 && update_temp <= 60){
- if(APP_SUCCESS == Ble_update_conn_interval(100,100))ble_con_int =1;
- else update_temp++;
- }
- }
- }
-
- if(app_client_step_GetIsScan()==0&&slave_isconnect()==0&&(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT)){
- if(host_isconnect()){host_disconnect();
-
- }
- }
- if(!host_isconnect()){
- SEGGER_RTT_printf(0,"=======>host_disconnect\n");
- Process_SetHoldOn(app_connect_Process,0);
- state = 0;
- }
-
- break;}
- default:state=0;break;
- }
- }
- void app_connect_manage_Init(void)
- {
- if(mFlash.isHost) Process_Start(100,"app_connect",app_connect_Process);
- Process_Start(100,"app_connect_led",app_connect_LED_Process);
- Ble_Slave_Connectd_Evt_Regist(cb_Slave_Connect);
- Ble_Host_Connectd_Evt_Regist(cb_Host_Connect);
- BLE_Client_Rx_Regist(BLE_Host_R_CONNET_LED,cb_BLE_Host_R_CONNET_LED);
- }
|