app_client_step.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 uint8_t isScan = 0;
  14. uint8_t app_client_step_GetIsScan(void)
  15. {
  16. return isScan;
  17. }
  18. void app_client_step_SetIsScan(void)
  19. {
  20. isScan =1;
  21. }
  22. //一小时保存一次步数
  23. static void app_client_step_Process(void)
  24. {
  25. static uint8_t state =0;
  26. static uint32_t tim =0;
  27. switch(state){
  28. case 0:
  29. if(1 == isScan){
  30. state =1;
  31. tim = TIME_GetTicks();
  32. }
  33. break;
  34. case 1:
  35. if(TIME_GetTicks() - tim >= 10000)state =2;
  36. else app_host_GetClientData(1);
  37. break;
  38. case 2:
  39. if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_DATEStep,"save step fail");
  40. isScan = 0;
  41. state =0;
  42. DEBUG_LOG("Flash_SaveInfomation:%d,mFlash.mStep.stepCur[1]:%d\r\n",TIME_GetTicks(),mFlash.mStep.stepCur[1]);
  43. break;
  44. default:state =0;isScan = 0;break;
  45. }
  46. }
  47. /************************ 删除步数 ***************************/
  48. void app_client_DeleteStep(BLE_Client_Rx_t* target)
  49. {
  50. for(uint8_t i=0;i<8;i++) mFlash.mStep.startTime[i] = target->pDat[i+1];
  51. if(0 != mFlash.mStep.num){mFlash.mStep.num = 0;
  52. flash_SetClearStepFlag();
  53. }
  54. }
  55. /************************ 发送步数 ***************************/
  56. #define PACK_STEP_LEN 40 //发送 PACK_STEP_LEN*4 个字节
  57. static uint8_t isSendAll = 0;
  58. static uint16_t packDex = 0;
  59. static uint16_t packNum = 0;
  60. static uint16_t lastNum = 0;
  61. BLE_Client_Tx_t mSendStep = {
  62. .n = 1,
  63. .t = 1,
  64. .cb = cb_BLE_Client_T_UPDATE_STEPNUM,
  65. };
  66. void app_client_SendStepAll(uint16_t n)
  67. {
  68. static uint8_t buf[250];
  69. uint8_t L=0;
  70. uint32_t addr = (Flash_GetStepZoneStartAddr()+PACK_STEP_LEN*4*(n-1));
  71. buf[L++] = BLE_UPDATE_STEPNUM;
  72. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  73. buf[L++] = (uint8_t)(packNum>>8);
  74. buf[L++] = (uint8_t)(packNum>>0);
  75. buf[L++] = (uint8_t)(n>>8);
  76. buf[L++] = (uint8_t)(n>>0);
  77. if(n==0){
  78. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R();
  79. DEBUG_LOG("====>step=%d,app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",step,app_step_GetStep_L(),app_step_GetStep_R());
  80. buf[L++] = (uint8_t)(step>>24);
  81. buf[L++] = (uint8_t)(step>>16);
  82. buf[L++] = (uint8_t)(step>>8);
  83. buf[L++] = (uint8_t)(step>>0);
  84. }else if(n==packNum){
  85. Zone_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  86. }else{
  87. Zone_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  88. }
  89. DEBUG_LOG("====>lastNum=%d,PACK_STEP_LEN=%d\n",lastNum,PACK_STEP_LEN);
  90. DEBUG_LOG("app_client_SendStepAll:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
  91. BLE_Client_Tx_Send(&mSendStep,BLE_UPDATE,buf,L);
  92. }
  93. void app_client_SendStepN(uint16_t n)
  94. {
  95. uint8_t buf[250];
  96. uint8_t L=0;
  97. uint32_t addr = (Flash_GetStepZoneStartAddr()+PACK_STEP_LEN*4*(n-1));
  98. buf[L++] = BLE_UPDATE_STEPNUM;
  99. for(int i=0;i<8;i++){buf[L++] = mFlash.mStep.startTime[i];}
  100. buf[L++] = (uint8_t)(packNum>>8);
  101. buf[L++] = (uint8_t)(packNum>>0);
  102. buf[L++] = (uint8_t)(n>>8);
  103. buf[L++] = (uint8_t)(n>>0);
  104. if(n == packNum){
  105. Zone_Read(addr,(uint32_t*)&buf[L],lastNum*4); L+=lastNum*4;
  106. }else{
  107. Zone_Read(addr,(uint32_t*)&buf[L],PACK_STEP_LEN*4); L+=PACK_STEP_LEN*4;
  108. }
  109. DEBUG_LOG("app_client_SendStepN:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
  110. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  111. }
  112. void app_client_SendStep(BLE_Client_Rx_t* target)
  113. {
  114. uint16_t n = ((uint16_t)target->pDat[1]<<8)|((uint16_t)target->pDat[2]<<0);
  115. if(mFlash.mStep.num > 0){
  116. packNum = ((mFlash.mStep.num-1)/PACK_STEP_LEN)+1;
  117. lastNum = ((mFlash.mStep.num-1)%PACK_STEP_LEN)+1;
  118. }else{
  119. packNum = 0;
  120. lastNum = 0;
  121. }
  122. // DEBUG_LOG("====>mFlash.mStep.num=%d,packNum=%d,lastNum=%d,nnnn=%d\n",mFlash.mStep.num,packNum,lastNum,n);
  123. if(n==0){
  124. isSendAll = 1;
  125. packDex = 0;
  126. app_client_SendStepAll(packDex);
  127. }else{
  128. app_client_SendStepN(n);
  129. }
  130. }
  131. static void cb_BLE_Client_T_UPDATE_STEPNUM(void* handle)
  132. {
  133. if(isSendAll==0) return;
  134. if(packDex<packNum){
  135. if(++packDex>=packNum) isSendAll = 0;
  136. app_client_SendStepAll(packDex);
  137. }
  138. }
  139. /************************ 初始化 ***************************/
  140. void app_client_step_Initialize(void)
  141. {
  142. Process_Start(1000,"app_client_step",app_client_step_Process);
  143. if(mFlash.mStep.stepCur[0]<mFlash.mStep.step[0])
  144. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  145. DEBUG_LOG("mFlash.mStep.num=%d\n",mFlash.mStep.num);
  146. DEBUG_LOG("mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
  147. }