app_client_step.c 4.6 KB

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