123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #include "includes.h"
- typedef enum
- {
- BLE_PTT_PRESS=0x01,
- BLE_PTT_FREE=0x02,
- BLE_CONNECT_SUC=0x03,
- BLE_CONNECT_FAIL=0x04,
- BLE_AUDCONNECT_SUC=0x05,
- BLE_BREAK_AUDCONNECT_SUC=0x06,
- BLE_SETMIC_SUC=0XE0,
- BLE_SETSPK_SUC=0XE1,
- }BLE_Result;
- 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_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
- #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_Bletimer=NULL;
- LSAPI_OSI_Timer_t *BleTimer_t=NULL;
- void bleCb(void *param){
- static bool fanzhuan=true;
- bool lastValue=true;
- 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;
- }
-
- }
- }
- void Ble_Init(){
- static int timer=0;
- }
- void BLETimerEntry(void *param)
- {
- Sut_ble.BleRingInit=1;
- 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_TimerStart(BleTimer_t,BlE_TIMER_TICK);
- LSAPI_OSI_Event_t event = {};
-
- for(;;)
- {
- LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
- }
- }
- void BLE_Open(){
- bool write_value = true;
- char write_low=false;
- // threadid_Bletimer = LSAPI_OSI_ThreadCreate("ostimer", BLETimerEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,1024,4);
- // 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);//正常为低
- newPara.BleEnable=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,"%s=====SET HIGH",*BT_Match_IOctr);
- else MSG_INFO(1,"%s=====SET LOW",*BT_Match_IOctr);
- }
- 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(sutApp.authReady==0) return;
- 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:
- 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;
- ModemSendAt("AT+AUDCH=0,0\r\n");
- break;
- case BLE_AUDCONNECT_SUC:
- Sut_ble.Audio_Conected=1;
- 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(!newPara.BleEnable)return;
- if(Sut_ble.Base_Conected&&Sut_ble.Audio_Conected)return;
- if(++cnt<(2000/interval)) return;//1秒读一次ADC即可
- cnt=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;
- }
- }
|