app_client_step.c 5.2 KB

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