app_detectIsHost.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 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]);
  49. DEBUG_LOG("scanName(%d):%s\n",strlen(buf),buf);
  50. host_set_scan_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. int consZ=(int)sqrt((float)(qma_data.acc[0]*qma_data.acc[0]+qma_data.acc[1]*qma_data.acc[1]+qma_data.acc[2]*qma_data.acc[2]));
  124. DEBUG_LOG("f acc:%6d,%6d,%6d,qma acc:%6d,%6d,%6d %d\r\n",f_data.acc[0],f_data.acc[1],f_data.acc[2],qma_data.acc[0],qma_data.acc[1],qma_data.acc[2],consZ);
  125. // Except_TxError(EXCEPT_Power,(const char*)printfbuff);
  126. //
  127. // DEBUG_LOG("LvBobuff:%d,%d\r\n",LvBobuff[Bufflength-1],LvBobuff[0]);
  128. if((LvBobuff[Bufflength-1] - LvBobuff[0]) < 500){
  129. // DEBUG_LOG("f_data.acc[0]:%6d,qma_data.acc[0]:%6d\r\n",f_data.acc[0],qma_data.acc[0]);
  130. // DEBUG_LOG("f_data.acc[1]:%6d,qma_data.acc[1]:%6d\r\n",f_data.acc[1],qma_data.acc[1]);
  131. // DEBUG_LOG("f_data.acc[2]:%6d,qma_data.acc[2]:%6d\r\n",f_data.acc[2],qma_data.acc[2]);
  132. // consZ=f_data.acc[2]*qma_data.acc[2];
  133. // DEBUG_LOG("f_data.acc[2]:%6d\r\n",f_data.acc[2]);
  134. if(f_data.acc[2] < -1850 && f_data.acc[2] > -2050 ){//平放的时候才判断左右鞋
  135. if(qma_data.acc[2] > 500){//同向
  136. #if _SAME_DIRECTION
  137. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  138. else counter =0;
  139. direct_detect_LR = DETECT_LR_IS_LEFT;
  140. #else
  141. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  142. else counter =0;
  143. direct_detect_LR = DETECT_LR_IS_RIGHT;
  144. #endif
  145. }
  146. else {//反向
  147. #if _SAME_DIRECTION
  148. if(DETECT_LR_IS_RIGHT == direct_detect_LR)counter++;
  149. else counter =0;
  150. direct_detect_LR = DETECT_LR_IS_RIGHT;
  151. #else
  152. if(DETECT_LR_IS_LEFT == direct_detect_LR)counter++;
  153. else counter =0;
  154. direct_detect_LR = DETECT_LR_IS_LEFT;
  155. #endif
  156. }
  157. }
  158. }
  159. else counter =0;
  160. if(counter >= DETECT_LR_TIMEOUT){counter = 0;
  161. if(direct_detect_LR != ob_detect_LR){ob_detect_LR = direct_detect_LR;
  162. if(ob_detect_LR == DETECT_LR_IS_LEFT)
  163. direct_detect_host = 1;
  164. else
  165. direct_detect_host = 0;
  166. if(direct_detect_host != mFlash.isHost){
  167. mFlash.isHost = direct_detect_host;
  168. Flash_SaveInfomation();
  169. SetDeviceNameFlag =1;
  170. MT_Run(500);
  171. }
  172. direct_detect_LR = DETECT_LR_INIT;
  173. }
  174. }
  175. }
  176. /**
  177. @brief 返回主机标志位
  178. @param 无
  179. @return 主机标志位
  180. */
  181. uint8_t app_Get_isHost(void)
  182. {
  183. return mFlash.isHost;
  184. }
  185. void app_detect_Init(void)
  186. {
  187. SetDeviceNameFlag =1;
  188. Process_Start(100,"app_SetDeviceName_Porcess",app_SetDeviceName_Porcess);
  189. Process_Start(1000,"app_detect_LR_Init_Porcess",app_detect_LR_Porcess);
  190. if(mFlash.isHost){
  191. DEBUG_LOG("======= Left shooe ======= \n");
  192. }else{
  193. DEBUG_LOG("======= Right shooe ======= \n");
  194. }
  195. }