app_detectIsHost.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ble_comm.h"
  5. #include "app_detectIsHost.h"
  6. #include "hal_ble_client.h"
  7. #include "app_flash.h"
  8. #include "hal_qma.h"
  9. #include "hal_mt.h"
  10. #include "tool.h"
  11. #include "bll_imu.h"
  12. #define DETECT_LR_TIMEOUT 10
  13. static DETECT_LR_e ob_detect_LR = DETECT_LR_INIT;
  14. static uint8_t SetDeviceNameFlag = 0;
  15. static void app_SetDeviceName_Porcess(void){
  16. static uint8_t state =0;
  17. char buf[16];
  18. memset(buf,0,16);
  19. switch(state){
  20. case 0:
  21. if(1 == SetDeviceNameFlag){
  22. state =1;
  23. }
  24. break;
  25. case 1:
  26. if(host_isconnect()){
  27. host_disconnect();
  28. }
  29. else{
  30. if(slave_isconnect())
  31. slave_disconnect();
  32. else{
  33. advertising_stop();
  34. ST_scan_stop();
  35. state =2;
  36. }
  37. }
  38. break;
  39. case 2:
  40. if(app_Get_isHost()){ //
  41. #if BleNameHoldOn_ENANBLE
  42. slave_set_adv_name((char *) LEFT_NAME,sizeof(LEFT_NAME));
  43. DEBUG_LOG("AdvName(%d):%s\n",sizeof(LEFT_NAME),LEFT_NAME);
  44. host_set_scan_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  45. DEBUG_LOG("scanName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  46. #else
  47. if(mFlash.mClient.isConfig == 'C'){
  48. memset(buf,0,16);
  49. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  50. slave_set_adv_name(buf,strlen(buf));
  51. }
  52. #endif
  53. }else{ //
  54. #if BleNameHoldOn_ENANBLE
  55. slave_set_adv_name((char *)RIGHT_NAME,sizeof(RIGHT_NAME));
  56. DEBUG_LOG("AdvName(%d):%s\n",sizeof(RIGHT_NAME),RIGHT_NAME);
  57. #else
  58. if(mFlash.mClient.isConfig=='C'){ //
  59. sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
  60. DEBUG_LOG("advName(%d):%s\n",strlen(buf),buf);
  61. slave_set_adv_name(buf,strlen(buf));
  62. }
  63. host_set_scan_name((char *)"12321321312",sizeof("12321321312"));
  64. #endif
  65. }
  66. slave_adv_init();
  67. advertising_start();
  68. state =0;
  69. SetDeviceNameFlag = 0;
  70. break;
  71. default:state =0;SetDeviceNameFlag = 0;break;
  72. }
  73. }
  74. static void app_detect_LR_Porcess(void)
  75. {
  76. int consZ=0;
  77. int ret =-1;
  78. static uint8_t counter = 0;
  79. bll_imu_data_t f_data = {0};
  80. qma_data_t qma_data={0};
  81. static DETECT_LR_e direct_detect_LR = DETECT_LR_INIT;
  82. uint8_t direct_detect_host = 0;
  83. #define Bufflength 6
  84. static int32_t buff[Bufflength]={0};
  85. int32_t LvBobuff[Bufflength]={0};
  86. uint8_t i=0;
  87. int32_t acc_norm =0;
  88. int32_t temp =0;
  89. static uint8_t firtRunflag =0;
  90. //获取中间加速度值
  91. ret = drv_qma_get_acc_data(&qma_data);
  92. if(-1 == ret || (0 == qma_data.acc[0] && 0 == qma_data.acc[1] && 0== qma_data.acc[2]))return;
  93. // DEBUG_LOG("qma_data.acc:%d,%d,%d\r\n",qma_data.acc[0],qma_data.acc[1],qma_data.acc[2]);
  94. acc_norm = sqrt(qma_data.acc[0] * qma_data.acc[0] +qma_data.acc[1] * qma_data.acc[1] + qma_data.acc[2] * qma_data.acc[2]);
  95. //获取前脚IMU的值
  96. if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 0){
  97. if(-1 == bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &f_data))
  98. return;
  99. }
  100. //第一次启动给所有的BUFF赋值
  101. if( 0 == firtRunflag) {firtRunflag =1;
  102. for(i=0;i< Bufflength;i++){
  103. buff[i]=acc_norm;
  104. }
  105. }
  106. for(i=0;i<(Bufflength-1);i++){
  107. buff[i]=buff[i+1];
  108. }
  109. buff[Bufflength-1] = acc_norm;
  110. //数据排序
  111. memcpy(LvBobuff,buff,sizeof(buff));
  112. for(uint8_t d=0;d<(Bufflength-1);d++){
  113. for(uint8_t b=0;b<(Bufflength-1-d);b++){
  114. if(LvBobuff[b]>LvBobuff[b+1]){
  115. temp=LvBobuff[b+1];
  116. LvBobuff[b+1]=LvBobuff[b];
  117. LvBobuff[b]=temp;
  118. }
  119. }
  120. }
  121. char printfbuff[100]={0};
  122. memset(printfbuff,0,sizeof(printfbuff));
  123. sprintf(printfbuff,"f_data.acc[2]:%6d,qma_data.acc[2]:%6d\r\n",f_data.acc[2],qma_data.acc[2]);
  124. Except_TxError(EXCEPT_Power,(const char*)printfbuff);
  125. // DEBUG_LOG("LvBobuff:%d,%d\r\n",LvBobuff[Bufflength-1],LvBobuff[0]);
  126. if((LvBobuff[Bufflength-1] - LvBobuff[0]) < 500){
  127. // DEBUG_LOG("f_data.acc[0]:%6d,qma_data.acc[0]:%6d\r\n",f_data.acc[0],qma_data.acc[0]);
  128. // DEBUG_LOG("f_data.acc[1]:%6d,qma_data.acc[1]:%6d\r\n",f_data.acc[1],qma_data.acc[1]);
  129. // DEBUG_LOG("f_data.acc[2]:%6d,qma_data.acc[2]:%6d\r\n",f_data.acc[2],qma_data.acc[2]);
  130. consZ=f_data.acc[2]*qma_data.acc[2];
  131. // DEBUG_LOG("f_data.acc[2]:%6d\r\n",f_data.acc[2]);
  132. if(f_data.acc[2] < -1800 && f_data.acc[2] > -2200 ){//平放的时候才判断左右鞋
  133. if(qma_data.acc[2] > -3000 && qma_data.acc[2] < -900){//同向
  134. #if _SAME_DIRECTION
  135. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  136. else counter =0;
  137. direct_detect_LR = DETECT_LR_IS_LEFT;
  138. #else
  139. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  140. else counter =0;
  141. direct_detect_LR = DETECT_LR_IS_RIGHT;
  142. #endif
  143. }
  144. else if(qma_data.acc[2] > 3000 && qma_data.acc[2] < 7000) {//反向
  145. #if _SAME_DIRECTION
  146. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  147. else counter =0;
  148. direct_detect_LR = DETECT_LR_IS_RIGHT;
  149. #else
  150. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  151. else counter =0;
  152. direct_detect_LR = DETECT_LR_IS_LEFT;
  153. #endif
  154. }
  155. else counter =0;
  156. }
  157. }
  158. else counter =0;
  159. if(counter >= DETECT_LR_TIMEOUT){counter = 0;
  160. if(direct_detect_LR != ob_detect_LR){ob_detect_LR = direct_detect_LR;
  161. if(ob_detect_LR == DETECT_LR_IS_LEFT)
  162. direct_detect_host = 1;
  163. else
  164. direct_detect_host = 0;
  165. if(direct_detect_host != mFlash.isHost){
  166. mFlash.isHost = direct_detect_host;
  167. Flash_SaveInfomation();
  168. SetDeviceNameFlag =1;
  169. MT_Run(500);
  170. }
  171. direct_detect_LR = DETECT_LR_INIT;
  172. }
  173. }
  174. }
  175. /**
  176. @brief 返回主机标志位
  177. @param 无
  178. @return 主机标志位
  179. */
  180. uint8_t app_Get_isHost(void)
  181. {
  182. return mFlash.isHost;
  183. }
  184. void app_detect_Init(void)
  185. {
  186. SetDeviceNameFlag =1;
  187. Process_Start(1000,"app_detect_LR_Init_Porcess",app_detect_LR_Porcess);
  188. Process_Start(100,"app_SetDeviceName_Porcess",app_SetDeviceName_Porcess);
  189. if(mFlash.isHost){
  190. DEBUG_LOG("======= Left shooe ======= \n");
  191. }else{
  192. DEBUG_LOG("======= Right shooe ======= \n");
  193. }
  194. }