app_client_step.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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,"====>lastNum=%d,PACK_STEP_LEN=%d\n",lastNum,PACK_STEP_LEN);
  81. 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");
  82. BLE_Client_Tx_Send(&mSendStep,BLE_UPDATE,buf,L);
  83. }
  84. void app_client_SendStepN(uint16_t n)
  85. {
  86. uint8_t buf[250];
  87. uint8_t L=0;
  88. uint32_t addr = (Flash_GetStepZoneStartAddr()+PACK_STEP_LEN*4*(n-1));
  89. buf[L++] = BLE_Client_T_UPDATE_STEPNUM;
  90. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  91. buf[L++] = (uint8_t)(packNum>>8);
  92. buf[L++] = (uint8_t)(packNum>>0);
  93. buf[L++] = (uint8_t)(n>>8);
  94. buf[L++] = (uint8_t)(n>>0);
  95. if(n==packNum){
  96. Zone_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  97. }else{
  98. Zone_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  99. }
  100. 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");
  101. BLE_Client_Tx_Send(0,BLE_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. }