app_step.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_mode_manage.h"
  14. #include "ble_comm.h"
  15. static uint8_t realStepMode = 0; //是否为实时计步模式
  16. static uint32_t realStepCur_L = 0; //左鞋步数记录点
  17. static uint32_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(HAL_MODE_REALSTEP == hal_mode_get() && 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)>>24);
  36. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>16);
  37. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  38. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  39. buf[L++] = (uint8_t)(realStepAdd_R>>24);
  40. buf[L++] = (uint8_t)(realStepAdd_R>>16);
  41. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  42. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  43. // SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]:%d,realStepCur_L:%d,realStepAdd_R:%d\n",mFlash.mStep.stepCur[0],realStepCur_L,realStepAdd_R);
  44. BLE_Client_Tx_Send(0,BLE_REALTIMESTEP,buf,L);
  45. }
  46. }
  47. //右鞋实时计步连接管理
  48. void app_step_RealConnectProcess(void)
  49. {
  50. if(HAL_MODE_REALSTEP == hal_mode_get()){
  51. if(slave_isconnect()==0){//与手机断开连接
  52. if(realStepTimCnt>0){ realStepTimCnt = 0; //从机在发步数
  53. uint8_t mod = BLE_Client_R_REALTIMESTEP_HANDUP;
  54. hal_mode_set(HAL_MODE_NORMAL);
  55. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&mod,1); //挂起从机实时步数
  56. }
  57. }else{
  58. if(realStepTimCnt>0) realStepTimCnt--;
  59. if(realStepTimCnt==0) BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  60. }
  61. }else{
  62. if(realStepTimCnt>0){ realStepTimCnt = 0;
  63. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&realStepMode,1);
  64. }
  65. }
  66. }
  67. //接收右鞋实时步数
  68. void cb_BLE_Host_R_REALTIMESTEP(void* handle)
  69. {
  70. BLE_Client_Rx_t* target = handle;
  71. #if DEBUG_STEP
  72. extern step_count_t mIstep_count_t_right;
  73. mIstep_count_t_right.F_val_now = ((uint32_t)target->pDat[1]<<8)|((uint32_t)target->pDat[2]<<0);
  74. mIstep_count_t_right.F_val_now_B = ((uint32_t)target->pDat[3]<<8)|((uint32_t)target->pDat[4]<<0);
  75. mIstep_count_t_right.F_filter_val = ((uint32_t)target->pDat[5]<<8)|((uint32_t)target->pDat[6]<<0);
  76. mIstep_count_t_right.F_flag_step = ((uint32_t)target->pDat[7]<<8)|((uint32_t)target->pDat[8]<<0);
  77. hal_step_matlib_Test(0x01);
  78. if(mIstep_count_t_right.F_flag_step > 0)mIstep_count_t_right.F_flag_step =0;
  79. #else
  80. if(target->pDat[0]==BLE_Client_R_REALTIMESTEP_ENTER){
  81. 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);
  82. // SEGGER_RTT_printf(0,">>>>>>realStepAdd_R:%ld\n",realStepAdd_R);
  83. realStepTimCnt = 3;
  84. }
  85. #endif
  86. }
  87. //接收手机命令
  88. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  89. {
  90. BLE_Client_Rx_t* target = handle;
  91. uint8_t cmd = target->pDat[0];
  92. if(cmd==BLE_Client_R_REALTIMESTEP_ENTER)hal_mode_set(HAL_MODE_REALSTEP);
  93. else if(cmd==BLE_Client_R_REALTIMESTEP_EXIT)hal_mode_set(HAL_MODE_NORMAL);
  94. BLE_Host_Tx_Send(0,BLE_REALTIMESTEP,&cmd,1);
  95. if(cmd==BLE_Client_R_REALTIMESTEP_ENTER && realStepMode==BLE_Client_R_REALTIMESTEP_EXIT)
  96. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  97. realStepMode = cmd;
  98. }
  99. void app_step_Init(void)
  100. {
  101. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  102. Process_Start(1000,"step_RealConnect",app_step_RealConnectProcess);
  103. BLE_Client_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  104. BLE_Host_Rx_Regist(BLE_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  105. }