app_step.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_imu.h"
  14. #include "ble_comm.h"
  15. static uint8_t realStepMode = 0; //是否为实时计步模式
  16. static uint32_t realStepCur_L = 0; //左鞋步数记录点
  17. static uint16_t realStepAdd_R = 0; //右鞋步数
  18. static uint32_t realStepTimCnt = 0; //连接右鞋计时
  19. /********************** 函数声明区 *************************/
  20. uint32_t app_step_GetStep_L(void)
  21. {
  22. return mFlash.mStep.stepCur[0];
  23. }
  24. uint32_t app_step_GetStep_R(void)
  25. {
  26. return mFlash.mStep.stepCur[1];
  27. }
  28. //左鞋实时计步发送
  29. void app_step_RealSendProcess(void)
  30. {
  31. if(realStepMode==BLE_Client_R_REALTIMESTEP_ENTER && slave_isconnect()>0){
  32. uint8_t buf[8];
  33. uint8_t L = 0;
  34. buf[L++] = BLE_Client_R_REALTIMESTEP_ENTER;
  35. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  36. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  37. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  38. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  39. BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
  40. }
  41. }
  42. //右鞋实时计步连接管理
  43. void app_step_RealConnectProcess(void)
  44. {
  45. if(realStepMode==BLE_Client_R_REALTIMESTEP_ENTER){
  46. if(slave_isconnect()==0){//与手机断开连接
  47. if(realStepTimCnt>0){ realStepTimCnt = 0; //从机在发步数
  48. uint8_t mod = BLE_Client_R_REALTIMESTEP_HANDUP;
  49. IMU_SetRealTimeStepMode(0);
  50. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //挂起从机实时步数
  51. }
  52. }else{
  53. if(realStepTimCnt>0) realStepTimCnt--;
  54. if(realStepTimCnt==0) BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  55. }
  56. }else{
  57. if(realStepTimCnt>0){ realStepTimCnt = 0;
  58. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  59. }
  60. }
  61. }
  62. //接收右鞋实时步数
  63. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  64. {
  65. BLE_Client_Rx_t* target = handle;
  66. #if DEBUG_STEP
  67. extern step_count_t mIstep_count_t_right;
  68. mIstep_count_t_right.F_val_now = ((uint32_t)target->pDat[1]<<8)|((uint32_t)target->pDat[2]<<0);
  69. mIstep_count_t_right.F_val_now_B = ((uint32_t)target->pDat[3]<<8)|((uint32_t)target->pDat[4]<<0);
  70. mIstep_count_t_right.F_filter_val = ((uint32_t)target->pDat[5]<<8)|((uint32_t)target->pDat[6]<<0);
  71. mIstep_count_t_right.F_flag_step = ((uint32_t)target->pDat[7]<<8)|((uint32_t)target->pDat[8]<<0);
  72. hal_step_matlib_Test(0x01);
  73. if(mIstep_count_t_right.F_flag_step > 0)mIstep_count_t_right.F_flag_step =0;
  74. #else
  75. if(target->pDat[0]==BLE_Client_R_REALTIMESTEP_ENTER){
  76. realStepAdd_R = ((uint16_t)target->pDat[1]<<8) | ((uint16_t)target->pDat[2]<<0);
  77. realStepTimCnt = 3;
  78. }
  79. #endif
  80. }
  81. //接收手机命令
  82. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  83. {
  84. BLE_Client_Rx_t* target = handle;
  85. uint8_t cmd = target->pDat[0];
  86. if(cmd==BLE_Client_R_REALTIMESTEP_ENTER)IMU_SetRealTimeStepMode(1);
  87. else if(cmd==BLE_Client_R_REALTIMESTEP_EXIT)IMU_SetRealTimeStepMode(0);
  88. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&cmd,1);
  89. if(cmd==BLE_Client_R_REALTIMESTEP_ENTER && realStepMode==BLE_Client_R_REALTIMESTEP_EXIT)
  90. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  91. realStepMode = cmd;
  92. }
  93. void app_step_Init(void)
  94. {
  95. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  96. Process_Start(1000,"step_RealConnect",app_step_RealConnectProcess);
  97. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  98. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  99. }