优化计步
authorluozheng <1732036298@qq.com>
Mon, 25 May 2026 07:56:25 +0000 (15:56 +0800)
committerluozheng <1732036298@qq.com>
Mon, 25 May 2026 07:56:25 +0000 (15:56 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/goal/src/goalmodel.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_card/src/TraingCardPresenter.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/training_card/src/TraingCardView.cpp
code/sdk/application/wearable/fd272/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h
code/sdk/application/wearable/fd272/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp
code/sdk/zhihui/drivers/cywee/dml/cywee_data.c
code/sdk/zhihui/drivers/cywee/dml/cywee_dml.c
code/sdk/zhihui/drivers/cywee/dml/cywee_hal.h

index 4e7c0e0..33149f4 100644 (file)
@@ -306,6 +306,10 @@ namespace OHOS
       uapi_get_YYMMDD_Time_extend(timeday, '_',0);
 #ifdef SUPPORT_CYWEE_SERVICE
       sprintf_s(path_,64,"%s%s.bin",CWM_HISTORY_DATA_PATH,timeday);
+#if STEP_DEBUG
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+         "[步数调试] 读取历史步数 当天日期后缀=%s 文件=%s", timeday, path_);
+#endif
 #endif
       FILE * fp = fopen(path_,"rb+");
       uint32_t size=0;
@@ -327,10 +331,18 @@ namespace OHOS
       }
       else
       {
+#if STEP_DEBUG
+         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+            "[步数调试] 历史步数文件打开失败 文件=%s", path_);
+#endif
          return 0;
       }
       if (size == 0 || (size % 8) != 0)
       {
+#if STEP_DEBUG
+         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+            "[步数调试] 历史步数文件为空或格式错 文件=%s 大小=%u", path_, size);
+#endif
          fclose(fp);
          return 0;
       }
@@ -347,6 +359,10 @@ namespace OHOS
          free(date);
          return 0;
       }
+#if STEP_DEBUG
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+         "[步数调试] 历史步数文件读取成功 文件=%s 记录条数=%u", path_, size / 8);
+#endif
       int32_t sing_date=0;
       int32_t timetemp=0;
       
@@ -355,8 +371,14 @@ namespace OHOS
          // uint8_t
          memcpy(&timetemp,date+i*8,4);
          memcpy(&sing_date,date+i*8+4,4);
+#if !STEP_DEBUG
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "time: %d", timetemp);
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "data: %d", sing_date);
+#endif
+#if STEP_DEBUG
+         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+            "[步数调试] 历史记录[%d] unix时间=%d 本小时步数=%d", i, timetemp, sing_date);
+#endif
 
          time_t ts = static_cast<time_t>(timetemp);
          struct tm *timeinfo = localtime(&ts);
@@ -618,6 +640,17 @@ namespace OHOS
       toal_date=show_step;
 #ifdef SUPPORT_CYWEE_SERVICE
       cywee_get_now_hour_data(&value,STEP);
+#if STEP_DEBUG
+      {
+         uint32_t hist = cywee_get_sport_data(HISTORY_STEP);
+         uint32_t sc = cywee_get_sport_data(STEP);
+         uint32_t hist_ago = cywee_get_sport_data(HISTORY_STEP_);
+         uint32_t total = toal_date + (value > 0 ? (uint32_t)value : 0);
+         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+            "[步数调试] 活动界面 合计=%u(文件%u+本小时%d) 历史=%u 当前=%u 一小时前=%u",
+            total, show_step, value, hist, sc, hist_ago);
+      }
+#endif
 #endif
       if(value>0)
       toal_date+=value;
index cbad205..e11af31 100644 (file)
@@ -854,8 +854,11 @@ void TraingModel::SetSportdata(SportData *data) {
  #endif
  
    } else if (currentState == ExerciseState::End && !Endstate) {
+#if STEP_DEBUG
+     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 结束运动-训练状态置0并停数据采集");
+#endif
      sport_stop_data_timer(); //停止数据获取定时器
- 
+
      // 设置训练 runtime 为 0 训练结束
      uapi_dev_runtime_set_train_status(0);
      if (Openingway) {
index 7ca3a2d..f9e5d2b 100644 (file)
@@ -36,6 +36,8 @@
  #include <thread>
  #include <unordered_map>
  #include "custom_runtime.h"
+ #include "wearable_log.h"
+ #include "cywee_hal.h"
  
  extern "C" void do_cywee_service_check_msg(int cmd, int data[]);
  
@@ -227,6 +229,9 @@ bool TraingCardPresenter::OnDragEnd(UIView &view, const DragEvent &event) {
    }
    // 根据不同的运动项目进行不同的运动
    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingCardPresenter::OpenSensor!!");
+ #if STEP_DEBUG
+   WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 开始运动 切换算法模式 type=%d", (int)type);
+ #endif
  #ifdef SUPPORT_CYWEE_SERVICE
    switch (type) {
    case ExerciseClass::NORMAL:
@@ -258,10 +263,16 @@ bool TraingCardPresenter::OnDragEnd(UIView &view, const DragEvent &event) {
  
  /*控制cywee*/
  bool TraingCardPresenter::ControlSensor(ExerciseState state) {
+ #if STEP_DEBUG
+   WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 控制运动传感器 state=%d(3=结束)", (int)state);
+ #endif
  #ifdef SUPPORT_PPG_SERVICE
    switch (state) {
    case ExerciseState::End:
      // 退出进入普通模式
+#if STEP_DEBUG
+     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 结束运动-即将切回普通模式1001");
+#endif
      do_cywee_service_check_msg(CYWEE_SERVICE_CMD_NORMAL_MODE, NULL);
      opensensorstuats = false; // 恢复标志位
      StopAndResetTimer();      // 关闭计时器
@@ -606,6 +617,9 @@ bool TraingCardPresenter::OnDragEnd(UIView &view, const DragEvent &event) {
  #endif
  /*训练结束*/
  void TraingCardPresenter::handleEndState() {
+#if STEP_DEBUG
+   WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 结束运动-流程开始");
+#endif
    TraingModel::GetInstance()->SetSportState(ExerciseState::End);
    ControlSensor(ExerciseState::End);
    uapi_set_screen_on_api(); // 亮屏
index c48cbf2..2770bb2 100755 (executable)
@@ -20,6 +20,8 @@
 #include "languagecode/LanguageInit.h"\r
 #include "traing/include/TraingModel.h"\r
 #include "uicomponents/UIComponents.h"\r
+#include "wearable_log.h"\r
+#include "cywee_hal.h"\r
 \r
 #include "common/screen.h"\r
 #include "engines/gfx/gfx_engine_manager.h"\r
@@ -576,6 +578,9 @@ TraingCardView::~TraingCardView() {
 \r
   g_pTraingCardView = nullptr;\r
   if (TraingModel::GetInstance()->GetSportState() == ExerciseState::End) {\r
+#if STEP_DEBUG\r
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[步数调试] 结束运动-训练页销毁写结束时间");\r
+#endif\r
     StopAndResetTimer(); // 关闭计时器\r
     TraingModel::GetInstance()->SetTimestamp();\r
   } else {\r
index ddc157b..f28f42d 100644 (file)
@@ -44,6 +44,7 @@ namespace OHOS
         std::string get_current_vote_status(void);
         std::string get_offline_log_status(void);
         std::string get_hp4570_register_dump(void);
+        std::string get_step_debug_info(void);
 
 
       private:
index bab0c58..fa71006 100644 (file)
@@ -28,6 +28,8 @@
 #include <vector>
 #include <deque>
 #include <cerrno>
+#include <cstdio>
+#include <ctime>
 
 #include "bluetooth/include/BluetoothModel.h"
 #include <cstring>
@@ -47,6 +49,7 @@
 #include "bluetooth/include/BluetoothModel.h"
 #ifdef SUPPORT_CYWEE_SERVICE
 #include "cywee_hal.h"
+#include "cywee_data.h"
 #endif
 
 #ifdef SUPPORT_BLE_SERVICE
@@ -125,6 +128,44 @@ namespace OHOS
             return labelTime; // 成功获取时间
         }
     }
+
+    /* 步数历史:2026/05/23 14:00 */
+    static std::string formatStepHistoryTime(uint32_t unixTime)
+    {
+        char labelTime[24] = {0};
+        if (uapi_get_TIME_STR(unixTime, labelTime, '/', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC) {
+            return "--";
+        }
+        return labelTime;
+    }
+
+    /* 快照时间:5/23 9:00 */
+    static std::string formatSnapshotTimeShort(uint32_t unixTime)
+    {
+        time_t raw = static_cast<time_t>(unixTime);
+        struct tm tm_info;
+        struct tm *ti = localtime_r(&raw, &tm_info);
+        if (ti == NULL) {
+            return "--";
+        }
+        char buf[16] = {0};
+        sprintf_s(buf, sizeof(buf), "%d/%d %d:%02d",
+            ti->tm_mon + 1, ti->tm_mday, ti->tm_hour, ti->tm_min);
+        return buf;
+    }
+
+    static uint32_t getCurrentHourBoundaryTime(void)
+    {
+        time_t now = time(nullptr);
+        struct tm tm_info;
+        struct tm *ti = localtime_r(&now, &tm_info);
+        if (ti == NULL) {
+            return static_cast<uint32_t>(now);
+        }
+        tm_info.tm_min = 0;
+        tm_info.tm_sec = 0;
+        return static_cast<uint32_t>(mktime(&tm_info));
+    }
     /**************************************************************************************************** */
 
     struct DebugOutputViewMapper
@@ -198,6 +239,7 @@ namespace OHOS
     
         debugOutputItemInfo[index++] = {index, "CWM日志开关"};
         debugOutputItemInfo[index++] = {index, "充电iC寄存器的值"};
+        debugOutputItemInfo[index++] = {index, "步数重要信息"};
     
         usedLen = index;
         /* -------------------------------- 添加数据 -------------------------------- */
@@ -871,6 +913,97 @@ namespace OHOS
 #endif
     }
 
+    std::string DebugOutputViewEventListener::get_step_debug_info(void)
+    {
+#ifdef SUPPORT_CYWEE_SERVICE
+        std::ostringstream oss;
+        uint32_t sc = cywee_get_sport_data(STEP);
+        uint32_t hist = cywee_get_sport_data(HISTORY_STEP);
+        uint32_t hist_ago = cywee_get_sport_data(HISTORY_STEP_);
+        uint32_t mem_total = hist + sc;
+        int32_t hour_steps = (int32_t)mem_total - (int32_t)hist_ago;
+        if (hour_steps < 0) {
+            hour_steps = 0;
+        }
+
+        char timeday[12] = {0};
+        char history_path[64] = {0};
+        uapi_get_YYMMDD_Time_extend(timeday, '_', 0);
+        sprintf_s(history_path, sizeof(history_path), "%s%s.bin", CWM_HISTORY_DATA_PATH, timeday);
+
+        uint32_t file_sum = 0;
+        int record_idx = 0;
+        std::ostringstream hour_records;
+        FILE *fp = fopen(history_path, "rb");
+        if (fp == NULL) {
+            hour_records << "历史文件: 不存在或打开失败\n";
+        } else {
+            fseek(fp, 0, SEEK_END);
+            long size = ftell(fp);
+            fseek(fp, 0, SEEK_SET);
+            if (size <= 0 || (size % 8) != 0) {
+                hour_records << "历史文件: 空或格式错误\n";
+            } else {
+                hour_records << "【每小时保存】\n";
+                time_t currtime = time(nullptr);
+                struct tm *timeinfo_now = localtime(&currtime);
+                uint8_t mon_now = timeinfo_now != nullptr ? (uint8_t)timeinfo_now->tm_mon : 0;
+                uint8_t day_now = timeinfo_now != nullptr ? (uint8_t)timeinfo_now->tm_mday : 0;
+                for (long i = 0; i < size / 8; i++) {
+                    int32_t timetemp = 0;
+                    int32_t sing_date = 0;
+                    if (fread(&timetemp, sizeof(timetemp), 1, fp) != 1 ||
+                        fread(&sing_date, sizeof(sing_date), 1, fp) != 1) {
+                        break;
+                    }
+                    time_t ts = static_cast<time_t>(timetemp);
+                    struct tm *timeinfo = localtime(&ts);
+                    if (timeinfo == nullptr ||
+                        timeinfo->tm_mday != day_now || timeinfo->tm_mon != mon_now) {
+                        continue;
+                    }
+                    if (sing_date <= 0) {
+                        continue;
+                    }
+                    file_sum += (uint32_t)sing_date;
+                    hour_records << "[" << record_idx << "]="
+                        << formatStepHistoryTime((uint32_t)timetemp)
+                        << " step=" << sing_date << "\n";
+                    record_idx++;
+                }
+                if (record_idx == 0) {
+                    hour_records << "今日暂无已保存记录\n";
+                }
+                hour_records << "文件各小时合计:" << file_sum << "\n";
+            }
+            fclose(fp);
+        }
+
+        uint32_t ui_total = file_sum;
+        if (hour_steps > 0) {
+            ui_total += (uint32_t)hour_steps;
+        }
+
+        oss << "【UI今日步数】\n";
+        oss << "UI今日总数(文件+本小时):" << ui_total << "\n";
+        oss << "  文件各小时合计:" << file_sum << "\n";
+        oss << "  本小时步数:" << hour_steps << "\n";
+
+        oss << "【cywee内存(算本小时)】\n";
+        oss << "历史步数:" << hist << "\n";
+        oss << "当前步数:" << sc << "\n";
+        oss << "整点快照:" << hist_ago << "\n";
+        oss << "当前+历史:" << mem_total << "\n";
+        oss << "本小时(历史+当前-快照):" << hour_steps << "\n";
+
+        oss << hour_records.str();
+        oss << "\n";
+        return oss.str();
+#else
+        return "未编译 SUPPORT_CYWEE_SERVICE";
+#endif
+    }
+
     bool DebugOutputViewEventListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
     {
         std::string viewId = view.GetViewId();
@@ -1216,6 +1349,10 @@ namespace OHOS
         case 22:
             str = get_hp4570_register_dump();
             break;
+        case 23:
+            refreshbutton_is_visible = 1;
+            str = get_step_debug_info();
+            break;
         default:
             str = "--";
             break;
index 19e299e..ded9363 100644 (file)
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <time.h>
 #include "cywee_dml.h"
 #include "custom_utils.h"
 #include <sys/stat.h>
 #include "cywee_data.h"
 #include "debug_print.h"
 
+#if STEP_DEBUG
+#define STEP_DBG_PRINT(fmt, ...) PRINT("[步数调试] " fmt, ##__VA_ARGS__)
+#else
+#define STEP_DBG_PRINT(fmt, ...) ((void)0)
+#endif
+
+/* 读取今日 hourly 步数文件,累加各小时增量(与活动页 file_sum 一致) */
+static uint32_t cywee_parse_today_step_file_sum(void)
+{
+    char path_[64] = {0};
+    char timeday[12] = {0};
+    uapi_get_YYMMDD_Time_extend(timeday, '_', 0);
+    sprintf_s(path_, 64, "%s%s.bin", CWM_HISTORY_DATA_PATH, timeday);
+
+    FILE *fp = fopen(path_, "rb");
+    if (fp == NULL) {
+        return 0;
+    }
+    fseek(fp, 0, SEEK_END);
+    long size = ftell(fp);
+    fseek(fp, 0, SEEK_SET);
+    if (size <= 0 || (size % 8) != 0) {
+        fclose(fp);
+        return 0;
+    }
+
+    time_t currtime = time(NULL);
+    struct tm *timeinfo_now = localtime(&currtime);
+    if (timeinfo_now == NULL) {
+        fclose(fp);
+        return 0;
+    }
+    uint8_t mon_now = (uint8_t)timeinfo_now->tm_mon;
+    uint8_t day_now = (uint8_t)timeinfo_now->tm_mday;
+
+    uint32_t file_sum = 0;
+    for (long i = 0; i < size / 8; i++) {
+        int32_t timetemp = 0;
+        int32_t sing_date = 0;
+        if (fread(&timetemp, sizeof(timetemp), 1, fp) != 1 ||
+            fread(&sing_date, sizeof(sing_date), 1, fp) != 1) {
+            break;
+        }
+        time_t ts = (time_t)timetemp;
+        struct tm *timeinfo = localtime(&ts);
+        if (timeinfo == NULL ||
+            timeinfo->tm_mday != day_now || timeinfo->tm_mon != mon_now) {
+            continue;
+        }
+        if (sing_date > 0) {
+            file_sum += (uint32_t)sing_date;
+        }
+    }
+    fclose(fp);
+    return file_sum;
+}
+
 static void CWM_OS_restoreData(int* data, uint32_t len,uint8_t type)
 {
     FILE * fp=NULL;
@@ -263,6 +321,11 @@ void cywee_save_data(void)
     uapi_dev_runtime_set_goal_storage_status(1);
 
     int step = cywee_get_sport_data(STEP) + cywee_get_sport_data(HISTORY_STEP);
+    STEP_DBG_PRINT("保存步数 写入文件合计=%d(当前+历史) 当前步数=%u 历史步数=%u 一小时前=%u\r\n",
+        step, cywee_get_sport_data(STEP), cywee_get_sport_data(HISTORY_STEP),
+        cywee_get_sport_data(HISTORY_STEP_));
+    STEP_DBG_PRINT("保存步数文件 当日合计=%s 一小时前快照=%s\r\n",
+        CWM_STEP_DATA_PATH, CWM_STEP_DATA_PATH_AGO);
     int calories = cywee_get_sport_data(NEW_CALORIES) + cywee_get_sport_data(HISTORY_CALORIES);
     float distances = cywee_get_sport_data_float(NEW_DISTANCE) + cywee_get_sport_data_float(HISTORY_DISTANCE);
     int fat = cywee_get_sport_data(NEW_FAT) + cywee_get_sport_data(HISTORY_FAT);
@@ -395,6 +458,8 @@ static void CWM_OS_saveData_history(void* new_data,void *history ,uint32_t len,u
         break;
     case STEP:
        sprintf_s(path_,64,"%s%s.bin",CWM_HISTORY_DATA_PATH,timeday);
+        STEP_DBG_PRINT("整点历史步数 日期后缀=%s 文件=%s 整点unix时间=%u 本小时增量=%d\r\n",
+            timeday, path_, time, *(int*)new_data - *(int*)history);
 
         i_data= *(int*)new_data-*(int*)history;//新数据减去旧数据,作为这一个小时的值
         // {   
@@ -752,12 +817,27 @@ int32_t cywee_get_now_hour_data(void*value, uint8_t type)
     switch (type)
     {
         case STEP:
-            
-            data = cywee_get_sport_data(HISTORY_STEP) + cywee_get_sport_data(STEP) - cywee_get_sport_data(HISTORY_STEP_);
-            if(data<0)
-            return 0;
-            memcpy_s(value,4,&data,4);
+        {
+            int total = (int)cywee_get_sport_data(HISTORY_STEP) + (int)cywee_get_sport_data(STEP);
+            int snap = (int)cywee_get_sport_data(HISTORY_STEP_);
+
+            if (snap <= 0 || snap > total) {
+                int file_sum = (int)cywee_parse_today_step_file_sum();
+                data = total - file_sum;
+                STEP_DBG_PRINT("本小时(快照异常) 合计=%d 快照=%d 文件合计=%d 本小时=%d\r\n",
+                    total, snap, file_sum, data);
+            } else {
+                data = total - snap;
+                STEP_DBG_PRINT("当前小时 历史步数=%u 当前步数=%u 一小时前=%u 本小时步数=%d\r\n",
+                    cywee_get_sport_data(HISTORY_STEP), cywee_get_sport_data(STEP),
+                    cywee_get_sport_data(HISTORY_STEP_), data);
+            }
+            if (data < 0) {
+                data = 0;
+            }
+            memcpy_s(value, 4, &data, 4);
             return 1;
+        }
 
         case CALORIES:
             data = cywee_get_sport_data(HISTORY_CALORIES) + cywee_get_sport_data(NEW_CALORIES);
index 7eeb555..c99475f 100644 (file)
 
 #include "custom_save_runtime.h"
 #include "cywee_data.h"
+#include "debug_print.h"
+
+#if STEP_DEBUG
+#define STEP_DBG_PRINT(fmt, ...) PRINT("[步数调试] " fmt, ##__VA_ARGS__)
+#else
+#define STEP_DBG_PRINT(fmt, ...) ((void)0)
+#endif
 #include "cywee_dml.h"
 #include "cwm_lib_dml.h"
 #ifdef SUPPORT_GNSS
@@ -1315,6 +1322,8 @@ void cywee_inactivity_mode_change(int mode)
 
 void cywee_activity_mode_change(int mode)
 {
+    STEP_DBG_PRINT("切换活动模式->%d 前: 当前步数=%d 历史步数=%d 一小时前快照=%d\r\n",
+        mode, stepcount, history_step, history_step_);
     if(mode != SPORT_ACTIVE_MODE_NORMAL)
     {
         memset(&c_act_result_t,0,sizeof(act_result_t));
@@ -1390,6 +1399,8 @@ void cywee_activity_mode_change(int mode)
         default:
             break;
     }
+    STEP_DBG_PRINT("切换活动模式->%d 后: 当前步数=%d 历史步数=%d 一小时前快照=%d\r\n",
+        mode, stepcount, history_step, history_step_);
 }
 
 // 缓存最近一次的有效GSensor数据(最多9组,适配1~9组取数)
index 0c19369..c2ecf81 100644 (file)
@@ -19,6 +19,11 @@ extern "C" {
 #include "stdint.h"
 #include "errcode.h"
 
+/* 步数异常调试(日志搜「步数调试」):定位结束后翻倍等问题,验证完成后改为 0 */
+#ifndef STEP_DEBUG
+#define STEP_DEBUG 1
+#endif
+
 #define SLEEP_TIME_PATH "/user/userdata/sleep/%s/sleep_time.bin"
 #define SLEEP_STATUS_PATH "/user/userdata/sleep/%s/sleep_status.bin"
 #define NAP_PATH "/user/userdata/sleep/%s/nap.bin"