app_step.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "hal_flash.h"
  13. #include "hal_imu.h"
  14. extern uint8_t slave_isconnect(void);
  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. uint16_t app_step_GetStep_L(void)
  21. {
  22. uint16_t st = 0;
  23. if(mFlash.mStep.stepCur[0]>mFlash.mStep.step[0])
  24. st = mFlash.mStep.stepCur[0] - mFlash.mStep.step[0];
  25. return st;
  26. }
  27. uint16_t app_step_GetStep_R(void)
  28. {
  29. uint16_t st = 0;
  30. if(mFlash.mStep.stepCur[1]>mFlash.mStep.step[1])
  31. st = mFlash.mStep.stepCur[1] - mFlash.mStep.step[1];
  32. return st;
  33. }
  34. //左鞋实时计步发送
  35. void app_step_RealSendProcess(void)
  36. {
  37. if(realStepMode==BLE_Client_R_REALTIMESTEP_ENTER && slave_isconnect()>0){
  38. uint8_t buf[8];
  39. uint8_t L = 0;
  40. buf[L++] = BLE_Client_R_REALTIMESTEP_ENTER;
  41. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>8);
  42. buf[L++] = (uint8_t)((mFlash.mStep.stepCur[0]-realStepCur_L)>>0);
  43. buf[L++] = (uint8_t)(realStepAdd_R>>8);
  44. buf[L++] = (uint8_t)(realStepAdd_R>>0);
  45. BLE_Client_Tx_Send(0,BLE_Client_T_REALTIMESTEP,buf,L);
  46. }
  47. }
  48. //右鞋实时计步连接管理
  49. void app_step_RealConnectProcess(void)
  50. {
  51. if(realStepMode==BLE_Client_R_REALTIMESTEP_ENTER){
  52. if(slave_isconnect()==0){//与手机断开连接
  53. if(realStepTimCnt>0){ realStepTimCnt = 0; //从机在发步数
  54. uint8_t mod = BLE_Client_R_REALTIMESTEP_HANDUP;
  55. BLE_Host_Tx_Send(0,BLE_Host_T_REALTIMESTEP,&mod,1); //挂起从机实时步数
  56. }
  57. }else{
  58. if(realStepTimCnt>0) realStepTimCnt--;
  59. if(realStepTimCnt==0) BLE_Host_Tx_Send(0,BLE_Host_T_REALTIMESTEP,&realStepMode,1);
  60. }
  61. }else{
  62. if(realStepTimCnt>0){ realStepTimCnt = 0;
  63. BLE_Host_Tx_Send(0,BLE_Host_T_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(target->pDat[0]==BLE_Client_R_REALTIMESTEP_ENTER){
  72. realStepAdd_R = ((uint16_t)target->pDat[1]<<8) | ((uint16_t)target->pDat[2]<<0);
  73. realStepTimCnt = 3;
  74. }
  75. }
  76. //接收手机命令
  77. void cb_BLE_Client_R_REALTIMESTEP(void* handle)
  78. {
  79. BLE_Client_Rx_t* target = handle;
  80. uint8_t cmd = target->pDat[0];
  81. BLE_Host_Tx_Send(0,BLE_Host_T_REALTIMESTEP,&cmd,1);
  82. if(cmd==BLE_Client_R_REALTIMESTEP_ENTER && realStepMode==BLE_Client_R_REALTIMESTEP_EXIT)
  83. realStepCur_L = mFlash.mStep.stepCur[0]; //重新计数
  84. realStepMode = cmd;
  85. }
  86. void app_step_Init(void)
  87. {
  88. Process_Start(100,"step_RealSend",app_step_RealSendProcess);
  89. Process_Start(1000,"step_RealConnect",app_step_RealConnectProcess);
  90. BLE_Client_Rx_Regist(BLE_Client_R_REALTIMESTEP,cb_BLE_Client_R_REALTIMESTEP);
  91. BLE_Host_Rx_Regist(BLE_Host_R_REALTIMESTEP,cb_BLE_Host_R_REALTIMESTEP);
  92. }