app_step.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "app_step.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_mt.h"
  7. #include "app_host.h"
  8. #include "app_charge.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_ble_host.h"
  11. #include "nrf_delay.h"
  12. #include "app_flash.h"
  13. #include "hal_ser_imu_mode_manage.h"
  14. #include "ble_comm.h"
  15. #include "exception.h"
  16. static uint8_t realStepMode = BLE_REALTIMESTEP_EXIT; //是否为实时计步模式
  17. static uint32_t realStepCur_L = 0; //左鞋步数记录点
  18. static uint32_t realStepAdd_R = 0; //右鞋步数
  19. static uint8_t realStepTimCnt = 0; //右鞋实时计步的心跳
  20. static uint16_t realStepHeartCnt = 0; //实时计步模式心跳计时
  21. static uint8_t RealStepFlag =BLE_REALTIMESTEP_EXIT; //实时计步的信号。
  22. /********************** 函数声明区 *************************/
  23. uint32_t app_step_GetStep_L(void)
  24. {
  25. return mFlash.mStep.stepCur[0];
  26. }
  27. uint32_t app_step_GetStep_R(void)
  28. {
  29. return mFlash.mStep.stepCur[1];
  30. }
  31. //右鞋实时计步发送
  32. static void app_step_RealSendProcess(void)
  33. {
  34. if(realStepMode == BLE_REALTIMESTEP_ENTER){
  35. uint8_t buf[8]={0};
  36. uint8_t L = 0;
  37. buf[L++] = BLE_REALTIMESTEP_ENTER;
  38. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>24);
  39. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>16);
  40. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  41. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  42. buf[L++] = (uint8_t)(realStepAdd_R>>24);
  43. buf[L++] = (uint8_t)(realStepAdd_R>>16);
  44. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  45. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  46. // DEBUG_LOG("mFlash.mStep.stepCur[0]:%d,realStepCur_L:%d,realStepAdd_R:%d\n",mFlash.mStep.stepCur[0],realStepCur_L,realStepAdd_R);
  47. BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
  48. }
  49. }
  50. //右鞋实时计步连接管理
  51. void app_step_RealConnectProcess(void)
  52. {
  53. static uint8_t state =0;
  54. static uint32_t tim =0;
  55. uint8_t mod =0;
  56. switch(state){
  57. case 0://空闲
  58. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  59. state =1;
  60. tim = TIME_GetTicks();
  61. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  62. }else{
  63. if(realStepTimCnt>0){ realStepTimCnt = 0;
  64. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  65. }
  66. }
  67. break;
  68. case 1://配置IMU
  69. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) == 0 ){
  70. state =2;
  71. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  72. realStepAdd_R = 0;
  73. realStepHeartCnt = 0;
  74. realStepMode = BLE_REALTIMESTEP_ENTER;
  75. }
  76. else{
  77. if(TIME_GetTicks()-tim >= 10000){tim = TIME_GetTicks();
  78. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  79. state =0;
  80. RealStepFlag = BLE_REALTIMESTEP_EXIT;
  81. realStepMode = BLE_REALTIMESTEP_EXIT;
  82. }
  83. else hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  84. }
  85. break;
  86. case 2://进入实时计步
  87. realStepHeartCnt++;
  88. if(realStepHeartCnt >= 600){realStepHeartCnt=0;
  89. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  90. mod = BLE_REALTIMESTEP_EXIT;
  91. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //退出从机实时步数
  92. state =3;
  93. tim = TIME_GetTicks();
  94. }else{
  95. if(realStepTimCnt > 0) realStepTimCnt--;
  96. if(realStepTimCnt==0) {
  97. mod = BLE_REALTIMESTEP_ENTER;
  98. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1);
  99. }
  100. }
  101. if(RealStepFlag == BLE_REALTIMESTEP_EXIT){
  102. state =3;
  103. realStepMode = BLE_REALTIMESTEP_EXIT;
  104. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  105. }
  106. break;
  107. case 3://退出实时计步模式
  108. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != 0 || TIME_GetTicks()-tim >= 10000){
  109. state =0;
  110. realStepMode = BLE_REALTIMESTEP_EXIT;
  111. }
  112. else hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  113. break;
  114. default:RealStepFlag = BLE_REALTIMESTEP_EXIT;realStepMode = BLE_REALTIMESTEP_EXIT;state =0;break;
  115. }
  116. }
  117. //接收右鞋实时步数并发送到手机
  118. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  119. {
  120. BLE_Client_Rx_t* target = handle;
  121. if(target->pDat[0]==BLE_REALTIMESTEP_ENTER){
  122. if(realStepMode == BLE_REALTIMESTEP_ENTER){
  123. realStepAdd_R = ((uint32_t)target->pDat[1]<<24) | (uint32_t)(target->pDat[2]<<16) | (uint32_t)(target->pDat[3]<<8) | ((uint32_t)target->pDat[4]<<0);
  124. app_step_RealSendProcess();
  125. // DEBUG_LOG(">>>>>>realStepCur_L:%ld,realStepAdd_R:%ld,\n",realStepCur_L,realStepAdd_R);
  126. }
  127. realStepTimCnt = 3;
  128. }
  129. }
  130. //接收手机命令
  131. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  132. {
  133. BLE_Client_Rx_t* target = handle;
  134. RealStepFlag = target->pDat[0];
  135. if(RealStepFlag == BLE_REALTIMESTEP_ENTER){
  136. realStepHeartCnt = 0;
  137. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  138. }
  139. else{
  140. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  141. }
  142. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&RealStepFlag,1);
  143. }
  144. void app_step_Init(void)
  145. {
  146. if(0 == mFlash.isHost){
  147. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  148. }
  149. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  150. Process_Start(100,"step_RealConnect",app_step_RealConnectProcess);
  151. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  152. }