app_client_step.c 4.3 KB

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