app_client_step.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. isScan = 0;
  37. }
  38. }
  39. /************************ 删除步数 ***************************/
  40. void app_client_DeleteStep(BLE_Client_Rx_t* target)
  41. {
  42. for(uint8_t i=0;i<8;i++) mFlash.mStep.startTime[i] = target->pDat[i+1];
  43. uint8_t startMin = target->pDat[9]; //距离上一个整点的分钟数
  44. timeCNT = (startMin)*32768;
  45. uint32_t tem = timeCNT/32768;
  46. SEGGER_RTT_printf(0,"====>startMin=%d,timeCNT=%d(%ds)\n",startMin,timeCNT,tem);
  47. Flash_DeleteAllStep();
  48. mFlash.mStep.num = 0;
  49. }
  50. /************************ 发送步数 ***************************/
  51. #define PACK_STEP_LEN 40 //发送 PACK_STEP_LEN*4 个字节
  52. static uint8_t isSendAll = 0;
  53. static uint16_t packDex = 0;
  54. static uint16_t packNum = 0;
  55. static uint16_t lastNum = 0;
  56. BLE_Client_Tx_t mSendStep = {
  57. .n = 1,
  58. .t = 1,
  59. .cb = cb_BLE_Client_T_UPDATE_STEPNUM,
  60. };
  61. void app_client_SendStepAll(uint16_t n)
  62. {
  63. static uint8_t buf[250];
  64. uint8_t L=0;
  65. uint32_t addr = FLASH_ADDR_STEP+PACK_STEP_LEN*4*(n-1);
  66. buf[L++] = BLE_Client_T_UPDATE_STEPNUM;
  67. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  68. buf[L++] = (uint8_t)(packNum>>8);
  69. buf[L++] = (uint8_t)(packNum>>0);
  70. buf[L++] = (uint8_t)(n>>8);
  71. buf[L++] = (uint8_t)(n>>0);
  72. if(n==0){
  73. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R();
  74. //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());
  75. buf[L++] = (uint8_t)(step>>24);
  76. buf[L++] = (uint8_t)(step>>16);
  77. buf[L++] = (uint8_t)(step>>8);
  78. buf[L++] = (uint8_t)(step>>0);
  79. }else if(n==packNum){
  80. Read_N_Byte_flash(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  81. }else{
  82. Read_N_Byte_flash(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  83. }
  84. 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");
  85. BLE_Client_Tx_Send(&mSendStep,BLE_Client_T_UPDATE,buf,L);
  86. }
  87. void app_client_SendStepN(uint16_t n)
  88. {
  89. uint8_t buf[250];
  90. uint8_t L=0;
  91. uint32_t addr = FLASH_ADDR_STEP+PACK_STEP_LEN*4*(n-1);
  92. buf[L++] = BLE_Client_T_UPDATE_STEPNUM;
  93. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  94. buf[L++] = (uint8_t)(packNum>>8);
  95. buf[L++] = (uint8_t)(packNum>>0);
  96. buf[L++] = (uint8_t)(n>>8);
  97. buf[L++] = (uint8_t)(n>>0);
  98. if(n==packNum){
  99. Read_N_Byte_flash(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  100. }else{
  101. Read_N_Byte_flash(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  102. }
  103. 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");
  104. BLE_Client_Tx_Send(0,BLE_Client_T_UPDATE,buf,L);
  105. }
  106. void app_client_SendStep(BLE_Client_Rx_t* target)
  107. {
  108. uint16_t n = ((uint16_t)target->pDat[1]<<8)|((uint16_t)target->pDat[2]<<0);
  109. if(mFlash.mStep.num>0){
  110. packNum = ((mFlash.mStep.num-1)/PACK_STEP_LEN)+1;
  111. lastNum = ((mFlash.mStep.num-1)%PACK_STEP_LEN)+1;
  112. }else{
  113. packNum = 0;
  114. lastNum = 0;
  115. }
  116. //SEGGER_RTT_printf(0,"====>mFlash.mStep.num=%d,packNum=%d,lastNum=%d\n",mFlash.mStep.num,packNum,lastNum);
  117. if(n==0){
  118. isSendAll = 1;
  119. packDex = 0;
  120. app_client_SendStepAll(packDex);
  121. }else{
  122. app_client_SendStepN(n);
  123. }
  124. }
  125. static void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle)
  126. {
  127. if(isSendAll==0) return;
  128. if(packDex<packNum){
  129. if(++packDex>=packNum) isSendAll = 0;
  130. app_client_SendStepAll(packDex);
  131. }
  132. }
  133. /************************ 初始化 ***************************/
  134. void app_client_step_Initialize(void)
  135. {
  136. Process_Start(1000,"app_client_step",app_client_step_Process);
  137. }