ble.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "includes.h"
  2. const char BLE_BASE_01[4]={0x01,0xE0,0xFC,0x01};
  3. const char BLE_BASE_02[4]={0x01,0xE0,0xFC,0x02};
  4. const char Ble_IO2AT[5]={0x01,0xE0,0xFC,0x01,0xDB}; //03 succ 4 faile
  5. const char Ble_AT_ASK[]={0x04,0x0e}; //单个字节 0x01
  6. const char Ble_CLEAR_LIST[5]={0x01,0xE0,0xFC,0x01,0xA3};
  7. const char BLE_BREAK_BASECONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdc};
  8. const char BLE_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xde};
  9. const char BLE_BREAK_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdf};
  10. Sut_BLE Sut_ble;
  11. static LSAPI_Device_t * BT_POWER_Gpio = NULL;
  12. static LSAPI_Device_t * BT_CTL_Gpio = NULL;
  13. static LSAPI_Device_t * BLE_CTL_Gpio = NULL;
  14. #define BT_POWER_ID 21
  15. #define BT_CTL_ID 5 //5
  16. #define BLE_CTL_ID 4
  17. #define BlE_TIMER_TICK 10
  18. void gpioBleConfigOutput(void){
  19. bool write_value = false;
  20. LSAPI_GpioConfig_t gpioConfig = {0};
  21. gpioConfig.mode = LS_GPIO_OUTPUT;
  22. gpioConfig.out_level=true;
  23. gpioConfig.id = BT_POWER_ID;
  24. BT_POWER_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  25. gpioConfig.id = BT_CTL_ID;
  26. BT_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  27. gpioConfig.id = BLE_CTL_ID;
  28. BLE_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  29. LSAPI_Device_Open(BT_POWER_Gpio);
  30. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  31. LSAPI_Device_Open(BT_CTL_Gpio);
  32. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);
  33. LSAPI_Device_Open(BLE_CTL_Gpio);
  34. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);
  35. }
  36. LSAPI_OSI_Thread_t *threadid_Ble=NULL;
  37. LSAPI_OSI_Timer_t *BleTimer_t=NULL;
  38. void bleCb(void *param){
  39. static bool fanzhuan=true;
  40. bool lastValue=false;
  41. static short MC_cnt=0;
  42. if(Sut_ble.BleRingInit){
  43. fanzhuan=!fanzhuan;
  44. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&fanzhuan, 1);
  45. //MSG_INFO(1,"BLE_CTL_Gpio fanzhuan=%d",fanzhuan);
  46. LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  47. if(MC_cnt++>6000/BlE_TIMER_TICK){
  48. Sut_ble.BleRingInit=0;
  49. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&lastValue, 1);
  50. MSG_INFO(1,"BLE_CTL_Gpio Over set high=%d",fanzhuan);
  51. MC_cnt=0;
  52. //LSAPI_OSI_ThreadExit();
  53. //LSAPI_OSI_ThreadDelete();
  54. }
  55. }
  56. }
  57. void BLETimerEntry(void *param)
  58. {
  59. if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL);
  60. if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed");
  61. LSAPI_OSI_Event_t event = {};
  62. for(;;)
  63. {
  64. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  65. LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  66. }
  67. }
  68. void Send_BLE_EVEN(){
  69. if(threadid_Ble!=NULL){
  70. Sut_ble.BleRingInit=1;
  71. threadSendEvent(threadid_Ble,OHPOC_EVENT_BLERING,NULL,NULL,NULL);
  72. }
  73. }
  74. void start_ringClearList(){
  75. //Sut_ble.BleRingInit=1;
  76. if(NULL==threadid_Ble) threadid_Ble=LSAPI_OSI_ThreadCreate("bletimer", BLETimerEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,1024,4);
  77. if(NULL==threadid_Ble){
  78. MSG_WARN(1,"create thread failed");
  79. }
  80. }
  81. void BLE_Open(){
  82. bool write_value = true;
  83. char write_low=false;
  84. // if(threadid_Bletimer==0)MSG_WARN(1,"create threadid_Bletimer fail");
  85. // Ble_Info.BleRingInit=1;
  86. // LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  87. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1); //需要先置高
  88. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  89. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_low, 1);//正常为低
  90. }
  91. void BT_Match_IOctr(bool ctr){//
  92. bool write_value;
  93. write_value=ctr;
  94. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);//正常为低
  95. if(write_value)MSG_INFO(1,"=====SET HIGH");
  96. else MSG_INFO(1,"=====SET LOW");
  97. }
  98. void Ble_Match_IOCTR(bool ctr){//
  99. bool write_value=true ;
  100. write_value=ctr;
  101. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);//正常为低
  102. if(write_value)MSG_INFO(1,"BLE_CTR=====SET HIGH");
  103. else MSG_INFO(1,"BLE_CTR=====SET LOW");
  104. }
  105. void BLE_Close(){
  106. bool write_value = false;
  107. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  108. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);
  109. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);
  110. }
  111. void BLE_at_process(char *r_buffer,int read_size){
  112. if(memcmp(r_buffer,Ble_AT_ASK,sizeof(Ble_AT_ASK)))return;
  113. newPara.BleEnable=1;
  114. MSG_INFO(1,"Ble_result===%02x",r_buffer[read_size-1]);
  115. switch(r_buffer[read_size-1]){
  116. case BLE_PTT_PRESS:
  117. //澶勫湪 鎸囩幆閰嶅�搴旇�鍛婄煡鎸囩幆閰嶅�鎴愬姛
  118. if(sutUIstatus.Status==UIS_MENU_RING_MATCH){
  119. sutApp.ringlink=1;
  120. }else micPttHandler(1);
  121. break;
  122. case BLE_PTT_FREE:
  123. micPttHandler(0);
  124. break;
  125. case BLE_CONNECT_SUC:
  126. Sut_ble.Base_Conected=1;
  127. break;
  128. case BLE_CONNECT_FAIL:
  129. Sut_ble.Base_Conected=0;
  130. Sut_ble.Audio_Conected=0;
  131. ModemSendAt("AT+AUDCH=0,0\r\n");
  132. break;
  133. case BLE_AUDCONNECT_SUC:
  134. Sut_ble.Audio_Conected=1;
  135. Ble_SendATCMD(BLE_BASE_02,sizeof(BLE_BASE_02),BLE_SETSPK_SUC,newPara.BleSpkLv*10);
  136. LSAPI_OSI_ThreadSleep(200);
  137. Ble_SendATCMD(BLE_BASE_02,sizeof(BLE_BASE_02),BLE_SETMIC_SUC,newPara.BleMicLv);
  138. ModemSendAt("AT+AUDCH=1,3\r\n");
  139. break;
  140. case BLE_BREAK_AUDCONNECT_SUC:
  141. Sut_ble.Audio_Conected=0;
  142. break;
  143. case BLE_SETMIC_SUC:
  144. break;
  145. case BLE_SETSPK_SUC:
  146. break;
  147. }
  148. }
  149. void Ble_handler(unsigned int interval){
  150. static unsigned short cnt=0,lcnt=0;
  151. if(sutPocStatus.mic)return;//璇烽害鏃跺€欎笉鎵�弿锛岃�閬垮紩鍏ユ潅闊�
  152. if(!newPara.BleEnable)return;
  153. if(Sut_ble.Base_Conected&&Sut_ble.Audio_Conected)return;
  154. if(++cnt<(1000/interval)) return;//1秒读一次ADC即可
  155. cnt=0;
  156. if(++lcnt<2)return;
  157. lcnt=0;
  158. if(Sut_ble.Base_Conected==0){
  159. Ble_SendAT(Ble_IO2AT,sizeof(Ble_IO2AT));
  160. return;
  161. }
  162. if(Sut_ble.Audio_Conected==0){
  163. Ble_SendAT(BLE_AUDCONNECT_ASK,sizeof(BLE_AUDCONNECT_ASK));
  164. return;
  165. }
  166. }
  167. void Ble_SendATCMD(char * data, int len,BLE_Result cmd,char value){
  168. char buf[6]={0};
  169. char tmp[3]={0};
  170. char Tbuf[30]={0};
  171. char i=0;
  172. memcpy(buf,data,len);
  173. buf[4]=cmd;
  174. buf[5]=value;
  175. for(i=0;i<len+2;i++){
  176. snprintf(tmp, sizeof(tmp),"%02x",buf[i]);
  177. strcat(Tbuf, tmp);
  178. }
  179. MSG_INFO(1,"Tbuf===%s",Tbuf);
  180. Ble_SendAT(buf,sizeof(buf));
  181. }