app_step.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = 0; //是否为实时计步模式
  17. static uint32_t realStepCur_L = 0; //左鞋步数记录点
  18. static uint32_t realStepAdd_R = 0; //右鞋步数
  19. static uint8_t realStepTimCnt = 0; //连接右鞋计时
  20. static uint8_t realStepHeartCnt = 0; //游戏模式心跳计时
  21. /********************** 函数声明区 *************************/
  22. uint32_t app_step_GetStep_L(void)
  23. {
  24. return mFlash.mStep.stepCur[0];
  25. }
  26. uint32_t app_step_GetStep_R(void)
  27. {
  28. return mFlash.mStep.stepCur[1];
  29. }
  30. //右鞋实时计步发送
  31. static void app_step_RealSendProcess(void)
  32. {
  33. if(-1 != hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP)){
  34. uint8_t buf[8];
  35. uint8_t L = 0;
  36. buf[L++] = BLE_REALTIMESTEP_ENTER;
  37. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>24);
  38. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>16);
  39. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  40. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  41. buf[L++] = (uint8_t)(realStepAdd_R>>24);
  42. buf[L++] = (uint8_t)(realStepAdd_R>>16);
  43. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  44. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  45. // SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]-realStepCur_L:%d,realStepAdd_R:%d\n",(mFlash.mStep.stepCur[0]-realStepCur_L),realStepAdd_R);
  46. BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
  47. }
  48. }
  49. //右鞋实时计步连接管理
  50. void app_step_RealConnectProcess(void)
  51. {
  52. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_REALSTEP) != -1){
  53. if(realStepHeartCnt < 255)realStepHeartCnt++;
  54. if(realStepHeartCnt >= 60){//与手机断开连接
  55. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  56. if(realStepTimCnt > 0){ realStepTimCnt = 0; //从机在发步数
  57. uint8_t mod = BLE_REALTIMESTEP_HANDUP;
  58. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //挂起从机实时步数
  59. }
  60. }else{
  61. if(realStepTimCnt>0) realStepTimCnt--;
  62. if(realStepTimCnt==0) BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  63. }
  64. }else{
  65. if(realStepTimCnt>0){ realStepTimCnt = 0;
  66. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  67. }
  68. }
  69. }
  70. //接收右鞋实时步数并发送到手机
  71. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  72. {
  73. BLE_Client_Rx_t* target = handle;
  74. #if DEBUG_STEP
  75. extern step_count_t mIstep_count_t_right;
  76. mIstep_count_t_right.F_val_now = ((uint32_t)target->pDat[1]<<8)|((uint32_t)target->pDat[2]<<0);
  77. mIstep_count_t_right.F_val_now_B = ((uint32_t)target->pDat[3]<<8)|((uint32_t)target->pDat[4]<<0);
  78. mIstep_count_t_right.F_filter_val = ((uint32_t)target->pDat[5]<<8)|((uint32_t)target->pDat[6]<<0);
  79. mIstep_count_t_right.F_flag_step = ((uint32_t)target->pDat[7]<<8)|((uint32_t)target->pDat[8]<<0);
  80. hal_step_matlib_Test(0x01);
  81. if(mIstep_count_t_right.F_flag_step > 0)mIstep_count_t_right.F_flag_step =0;
  82. #else
  83. if(target->pDat[0]==BLE_REALTIMESTEP_ENTER){
  84. 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);
  85. app_step_RealSendProcess();
  86. // SEGGER_RTT_printf(0,">>>>>>realStepAdd_R:%ld\n",realStepAdd_R);
  87. realStepTimCnt = 3;
  88. }
  89. #endif
  90. }
  91. //接收手机命令
  92. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  93. {
  94. BLE_Client_Rx_t* target = handle;
  95. uint8_t cmd = target->pDat[0];
  96. if(cmd==BLE_REALTIMESTEP_ENTER){
  97. realStepHeartCnt = 0;
  98. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);
  99. }
  100. else if(cmd==BLE_REALTIMESTEP_EXIT){
  101. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_REALSTEP, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);
  102. }
  103. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&cmd,1);
  104. if(cmd==BLE_REALTIMESTEP_ENTER && realStepMode==BLE_REALTIMESTEP_EXIT){
  105. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  106. realStepAdd_R = 0;
  107. }
  108. realStepMode = cmd;
  109. }
  110. void app_step_Init(void)
  111. {
  112. if(0 == mFlash.isHost)Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  113. Process_Start(1000,"step_RealConnect",app_step_RealConnectProcess);
  114. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  115. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  116. }