#include "includes.h" const char BLE_BASE_01[4]={0x01,0xE0,0xFC,0x01}; const char BLE_BASE_02[4]={0x01,0xE0,0xFC,0x02}; const char Ble_IO2AT[5]={0x01,0xE0,0xFC,0x01,0xDB}; //03 succ 4 faile const char Ble_AT_ASK[]={0x04,0x0e}; //单个字节 0x01 const char Ble_CLEAR_LIST[5]={0x01,0xE0,0xFC,0x01,0xA3}; const char BLE_BREAK_BASECONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdc}; const char BLE_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xde}; const char BLE_BREAK_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdf}; Sut_BLE Sut_ble; static LSAPI_Device_t * BT_POWER_Gpio = NULL; static LSAPI_Device_t * BT_CTL_Gpio = NULL; static LSAPI_Device_t * BLE_CTL_Gpio = NULL; #define BT_POWER_ID 21 #define BT_CTL_ID 5 //5 #define BLE_CTL_ID 4 #define BlE_TIMER_TICK 10 void gpioBleConfigOutput(void){ bool write_value = false; LSAPI_GpioConfig_t gpioConfig = {0}; gpioConfig.mode = LS_GPIO_OUTPUT; gpioConfig.out_level=true; gpioConfig.id = BT_POWER_ID; BT_POWER_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig); gpioConfig.id = BT_CTL_ID; BT_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig); gpioConfig.id = BLE_CTL_ID; BLE_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig); LSAPI_Device_Open(BT_POWER_Gpio); LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1); LSAPI_Device_Open(BT_CTL_Gpio); LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1); LSAPI_Device_Open(BLE_CTL_Gpio); LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1); } LSAPI_OSI_Thread_t *threadid_Ble=NULL; LSAPI_OSI_Timer_t *BleTimer_t=NULL; void bleCb(void *param){ static bool fanzhuan=true; bool lastValue=false; static short MC_cnt=0; if(Sut_ble.BleRingInit){ fanzhuan=!fanzhuan; LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&fanzhuan, 1); //MSG_INFO(1,"BLE_CTL_Gpio fanzhuan=%d",fanzhuan); LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK); if(MC_cnt++>6000/BlE_TIMER_TICK){ Sut_ble.BleRingInit=0; LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&lastValue, 1); MSG_INFO(1,"BLE_CTL_Gpio Over set high=%d",fanzhuan); MC_cnt=0; //LSAPI_OSI_ThreadExit(); //LSAPI_OSI_ThreadDelete(); } } } void BLETimerEntry(void *param) { if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL); if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed"); LSAPI_OSI_Event_t event = {}; for(;;) { LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event); LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK); } } void Send_BLE_EVEN(){ if(threadid_Ble!=NULL){ Sut_ble.BleRingInit=1; threadSendEvent(threadid_Ble,OHPOC_EVENT_BLERING,NULL,NULL,NULL); } } void start_ringClearList(){ //Sut_ble.BleRingInit=1; if(NULL==threadid_Ble) threadid_Ble=LSAPI_OSI_ThreadCreate("bletimer", BLETimerEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,1024,4); if(NULL==threadid_Ble){ MSG_WARN(1,"create thread failed"); } } void BLE_Open(){ bool write_value = true; char write_low=false; // if(threadid_Bletimer==0)MSG_WARN(1,"create threadid_Bletimer fail"); // Ble_Info.BleRingInit=1; // LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK); LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1); //需要先置高 LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1); LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_low, 1);//正常为低 } void BT_Match_IOctr(bool ctr){// bool write_value; write_value=ctr; LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);//正常为低 if(write_value)MSG_INFO(1,"=====SET HIGH"); else MSG_INFO(1,"=====SET LOW"); } void Ble_Match_IOCTR(bool ctr){// bool write_value=true ; write_value=ctr; LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);//正常为低 if(write_value)MSG_INFO(1,"BLE_CTR=====SET HIGH"); else MSG_INFO(1,"BLE_CTR=====SET LOW"); } void BLE_Close(){ bool write_value = false; LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1); LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1); LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1); } void BLE_at_process(char *r_buffer,int read_size){ if(memcmp(r_buffer,Ble_AT_ASK,sizeof(Ble_AT_ASK)))return; newPara.BleEnable=1; MSG_INFO(1,"Ble_result===%02x",r_buffer[read_size-1]); switch(r_buffer[read_size-1]){ case BLE_PTT_PRESS: //澶勫湪 鎸囩幆閰嶅搴旇鍛婄煡鎸囩幆閰嶅鎴愬姛 if(sutUIstatus.Status==UIS_MENU_RING_MATCH){ sutApp.ringlink=1; }else micPttHandler(1); break; case BLE_PTT_FREE: micPttHandler(0); break; case BLE_CONNECT_SUC: Sut_ble.Base_Conected=1; break; case BLE_CONNECT_FAIL: Sut_ble.Base_Conected=0; Sut_ble.Audio_Conected=0; ModemSendAt("AT+AUDCH=0,0\r\n"); break; case BLE_AUDCONNECT_SUC: Sut_ble.Audio_Conected=1; Ble_SendATCMD(BLE_BASE_02,sizeof(BLE_BASE_02),BLE_SETSPK_SUC,newPara.BleSpkLv*10); LSAPI_OSI_ThreadSleep(200); Ble_SendATCMD(BLE_BASE_02,sizeof(BLE_BASE_02),BLE_SETMIC_SUC,newPara.BleMicLv); ModemSendAt("AT+AUDCH=1,3\r\n"); break; case BLE_BREAK_AUDCONNECT_SUC: Sut_ble.Audio_Conected=0; break; case BLE_SETMIC_SUC: break; case BLE_SETSPK_SUC: break; } } void Ble_handler(unsigned int interval){ static unsigned short cnt=0,lcnt=0; if(sutPocStatus.mic)return;//璇烽害鏃跺欎笉鎵弿锛岃閬垮紩鍏ユ潅闊 if(!newPara.BleEnable)return; if(Sut_ble.Base_Conected&&Sut_ble.Audio_Conected)return; if(++cnt<(1000/interval)) return;//1秒读一次ADC即可 cnt=0; if(++lcnt<2)return; lcnt=0; if(Sut_ble.Base_Conected==0){ Ble_SendAT(Ble_IO2AT,sizeof(Ble_IO2AT)); return; } if(Sut_ble.Audio_Conected==0){ Ble_SendAT(BLE_AUDCONNECT_ASK,sizeof(BLE_AUDCONNECT_ASK)); return; } } void Ble_SendATCMD(char * data, int len,BLE_Result cmd,char value){ char buf[6]={0}; char tmp[3]={0}; char Tbuf[30]={0}; char i=0; memcpy(buf,data,len); buf[4]=cmd; buf[5]=value; for(i=0;i