app_client_step.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "system.h"
  2. #include "app_client_step.h"
  3. #include "app_flash.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "app_host.h"
  7. #include "app_step.h"
  8. #include "ble_comm.h"
  9. #include "app_flash.h"
  10. /************************ 函数定义 ***************************/
  11. void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle);
  12. /************************ 获取步数 ***************************/
  13. static uint32_t timeCNT = 0;
  14. static uint8_t isScan = 0;
  15. uint8_t app_client_step_GetIsScan(void)
  16. {
  17. return isScan;
  18. }
  19. static void app_client_step_Process(void)
  20. {
  21. uint32_t sec = 0;
  22. static uint32_t cnt_b=0;
  23. uint32_t cnt = NRF_RTC0->COUNTER;
  24. if(cnt<cnt_b) cnt += 16777216;
  25. timeCNT += cnt - cnt_b;
  26. sec = timeCNT/32768;
  27. if(sec >= 130000 )SEGGER_RTT_printf(0,"!!!!!!!!!!!!!!! sec over,%d,%d,%d\n",cnt,cnt_b,sec);
  28. if(cnt >16777216)cnt_b = cnt - 16777216;
  29. else cnt_b = cnt;
  30. // SEGGER_RTT_printf(0,"timeCNT(%d)(%d)\n",timeCNT,sec);
  31. if(sec>=60*60-10) isScan = 1;
  32. if(sec>=60*60){
  33. SEGGER_RTT_printf(0,"====>1 hour:%d,%d,%d\n",timeCNT,sec,TIME_GetTicks()/1000);
  34. timeCNT = 0;
  35. Flash_SaveStep();
  36. Flash_SaveInfomation();
  37. isScan = 0;
  38. }
  39. }
  40. /************************ 删除步数 ***************************/
  41. void app_client_DeleteStep(BLE_Client_Rx_t* target)
  42. {
  43. for(uint8_t i=0;i<8;i++) mFlash.mStep.startTime[i] = target->pDat[i+1];
  44. Flash_DeleteAllStep();
  45. }
  46. /************************ 发送步数 ***************************/
  47. #define PACK_STEP_LEN 40 //发送 PACK_STEP_LEN*4 个字节
  48. static uint8_t isSendAll = 0;
  49. static uint16_t packDex = 0;
  50. static uint16_t packNum = 0;
  51. static uint16_t lastNum = 0;
  52. BLE_Client_Tx_t mSendStep = {
  53. .n = 1,
  54. .t = 1,
  55. .cb = cb_BLE_Client_T_UPDATE_STEPNUM,
  56. };
  57. void app_client_SendStepAll(uint16_t n)
  58. {
  59. static uint8_t buf[250];
  60. uint8_t L=0;
  61. uint32_t addr = (Flash_GetStepZoneStartAddr()+PACK_STEP_LEN*4*(n-1));
  62. buf[L++] = BLE_Client_T_UPDATE_STEPNUM;
  63. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  64. buf[L++] = (uint8_t)(packNum>>8);
  65. buf[L++] = (uint8_t)(packNum>>0);
  66. buf[L++] = (uint8_t)(n>>8);
  67. buf[L++] = (uint8_t)(n>>0);
  68. if(n==0){
  69. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R();
  70. //SEGGER_RTT_printf(0,"====>step=%d,app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",step,app_step_GetStep_L(),app_step_GetStep_R());
  71. buf[L++] = (uint8_t)(step>>24);
  72. buf[L++] = (uint8_t)(step>>16);
  73. buf[L++] = (uint8_t)(step>>8);
  74. buf[L++] = (uint8_t)(step>>0);
  75. }else if(n==packNum){
  76. Zone_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  77. }else{
  78. Zone_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  79. }
  80. // SEGGER_RTT_printf(0,"app_client_SendStepAll:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  81. BLE_Client_Tx_Send(&mSendStep,BLE_UPDATE,buf,L);
  82. }
  83. void app_client_SendStepN(uint16_t n)
  84. {
  85. uint8_t buf[250];
  86. uint8_t L=0;
  87. uint32_t addr = (Flash_GetStepZoneStartAddr()+PACK_STEP_LEN*4*(n-1));
  88. buf[L++] = BLE_Client_T_UPDATE_STEPNUM;
  89. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  90. buf[L++] = (uint8_t)(packNum>>8);
  91. buf[L++] = (uint8_t)(packNum>>0);
  92. buf[L++] = (uint8_t)(n>>8);
  93. buf[L++] = (uint8_t)(n>>0);
  94. if(n==packNum){
  95. Zone_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  96. }else{
  97. Zone_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  98. }
  99. // SEGGER_RTT_printf(0,"app_client_SendStepN:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  100. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  101. }
  102. void app_client_SendStep(BLE_Client_Rx_t* target)
  103. {
  104. uint16_t n = ((uint16_t)target->pDat[1]<<8)|((uint16_t)target->pDat[2]<<0);
  105. if(mFlash.mStep.num>0){
  106. packNum = ((mFlash.mStep.num-1)/PACK_STEP_LEN)+1;
  107. lastNum = ((mFlash.mStep.num-1)%PACK_STEP_LEN)+1;
  108. }else{
  109. packNum = 0;
  110. lastNum = 0;
  111. }
  112. //SEGGER_RTT_printf(0,"====>mFlash.mStep.num=%d,packNum=%d,lastNum=%d\n",mFlash.mStep.num,packNum,lastNum);
  113. if(n==0){
  114. isSendAll = 1;
  115. packDex = 0;
  116. app_client_SendStepAll(packDex);
  117. }else{
  118. app_client_SendStepN(n);
  119. }
  120. }
  121. static void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle)
  122. {
  123. if(isSendAll==0) return;
  124. if(packDex<packNum){
  125. if(++packDex>=packNum) isSendAll = 0;
  126. app_client_SendStepAll(packDex);
  127. }
  128. }
  129. /************************ 初始化 ***************************/
  130. void app_client_step_Initialize(void)
  131. {
  132. Process_Start(1000,"app_client_step",app_client_step_Process);
  133. }