From fe083a266b7f801dec1988c431bc069a3cb6932d Mon Sep 17 00:00:00 2001 From: Caisking Date: Tue, 24 Mar 2026 14:35:43 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E5=BF=83=E7=8E=87=E8=A1=80?= =?utf8?q?=E6=B0=A7=E7=9A=84=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../bloodoxygen/include/BloodOxygenModel.h | 2 + .../src/BloodDetectionPresenter.cpp | 4 +- .../bloodoxygen/src/BloodOxygenModel.cpp | 56 ++++++++++++++++--- .../nativeui/heartrate/include/HeartModel.h | 2 + .../nativeui/heartrate/src/HeartModel.cpp | 51 ++++++++++++++--- .../nativeui/traing/src/TraingModel.cpp | 15 ++--- 6 files changed, 104 insertions(+), 26 deletions(-) diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/include/BloodOxygenModel.h b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/include/BloodOxygenModel.h index 2558f89a2..6eebd5cdb 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/include/BloodOxygenModel.h +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/include/BloodOxygenModel.h @@ -58,6 +58,8 @@ namespace OHOS void Blood_Result(void); void Blood_Control(BloodState state); + void Blood_Switch(bool enable); + void SwitchPpgSpo2Sensor(bool enable); void ClearDisplayData(); void CleanAllData(); diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodDetectionPresenter.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodDetectionPresenter.cpp index ee0910adb..0853c8b51 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodDetectionPresenter.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodDetectionPresenter.cpp @@ -36,7 +36,7 @@ namespace OHOS if (!BloodCheckState) { - BloodOxygenModel::GetInstance()->Blood_Start(); + BloodOxygenModel::GetInstance()->Blood_Switch(true); BloodCheckState = true; } } @@ -49,7 +49,7 @@ namespace OHOS if (BloodCheckState) { - BloodOxygenModel::GetInstance()->Blood_Stop(); + BloodOxygenModel::GetInstance()->Blood_Switch(false); BloodCheckState = false; } } diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodOxygenModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodOxygenModel.cpp index cb15e40f9..eab51e09d 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodOxygenModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bloodoxygen/src/BloodOxygenModel.cpp @@ -30,6 +30,17 @@ #define BLOOD_TIMER_DATA 1 namespace OHOS { +static osTimerId_t g_delayed_blood_stop_timer = nullptr; +static uint32_t g_blood_stop_request_token = 0; +static uint32_t g_blood_stop_execute_token = 0; + +static void blood_delayed_stop_timer_callback(void *arg) { + (void)arg; + if (g_blood_stop_execute_token != g_blood_stop_request_token) { + return; + } + ppg_spo2_stop(); +} /** * @brief Constructor for the BloodOxygenModel class. */ @@ -64,10 +75,8 @@ void BloodOxygenModel::ClearDisplayData() { void BloodOxygenModel::Blood_Control(BloodState state) { if (state == BloodState::End && startspo2 == true) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "startspo2 End"); -#ifdef SUPPORT_PPG_SERVICE - ppg_spo2_stop(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg_spo2_stop"); -#endif + SwitchPpgSpo2Sensor(false); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg_spo2_stop(delayed)"); startspo2 = false; #ifdef SYSTEM_LOG_SERVICE uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE_END); @@ -83,10 +92,8 @@ void BloodOxygenModel::Blood_Control(BloodState state) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "充电不能检测"); return; } -#ifdef SUPPORT_PPG_SERVICE - ppg_spo2_start(); + SwitchPpgSpo2Sensor(true); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ppg_spo2_start"); -#endif startspo2 = true; #ifdef SYSTEM_LOG_SERVICE uapi_save_runtime_log(RUNTIME_LOG_OXYGEN_MEASURE); @@ -95,6 +102,41 @@ void BloodOxygenModel::Blood_Control(BloodState state) { } } +void BloodOxygenModel::SwitchPpgSpo2Sensor(bool enable) { +#ifdef SUPPORT_PPG_SERVICE + if (enable) { + g_blood_stop_request_token++; + if (g_delayed_blood_stop_timer != nullptr) { + osTimerStop(g_delayed_blood_stop_timer); + } + ppg_spo2_start(); + return; + } + g_blood_stop_request_token++; + g_blood_stop_execute_token = g_blood_stop_request_token; + if (g_delayed_blood_stop_timer == nullptr) { + g_delayed_blood_stop_timer = + osTimerNew((osTimerFunc_t)blood_delayed_stop_timer_callback, osTimerOnce, nullptr, nullptr); + } + if (g_delayed_blood_stop_timer != nullptr) { + osTimerStop(g_delayed_blood_stop_timer); + osTimerStart(g_delayed_blood_stop_timer, osMs2Tick(3 * 1000)); + } else { + ppg_spo2_stop(); + } +#else + (void)enable; +#endif +} + +void BloodOxygenModel::Blood_Switch(bool enable) { + if (enable) { + Blood_Start(); + } else { + Blood_Stop(); + } +} + uint16_t BloodOxygenModel::GetSPO2data() { return currentspo2; } uint16_t BloodOxygenModel::GetShowSPO2data() { return displayspo2; } diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/include/HeartModel.h b/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/include/HeartModel.h index e5393ee87..bdd0d5cc0 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/include/HeartModel.h +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/include/HeartModel.h @@ -143,6 +143,7 @@ public: void Heart_Result(void); void Heart_Control(HeartState state, HeartCheck check); + void Heart_Switch(bool enable, HeartCheck check, uint32_t stopDelayMs = 0); void ClearDisplayData(); void Heart_Update_View(void); void CleanAllData(void); @@ -163,6 +164,7 @@ public: void heart_data_timer(uint32_t interval = 1000); void heart_stop_data_timer(); + void SwitchPpgHeartSensor(bool enable); private: HealthAllDatas healthDatas; diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/src/HeartModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/src/HeartModel.cpp index 3e7f0081a..5b7926c52 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/src/HeartModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/heartrate/src/HeartModel.cpp @@ -184,6 +184,47 @@ using namespace OHOS; // #endif namespace OHOS { +#ifdef SUPPORT_PPG_SERVICE +static osTimerId_t g_delayed_ppg_stop_timer = nullptr; +static uint32_t g_ppg_stop_request_token = 0; +static uint32_t g_ppg_stop_execute_token = 0; + +static void ppg_delayed_stop_timer_callback(void *arg) { + (void)arg; + if (g_ppg_stop_execute_token != g_ppg_stop_request_token) { + return; + } + ppg_hr_stop(); +} +#endif + +void HeartModel::SwitchPpgHeartSensor(bool enable) { +#ifdef SUPPORT_PPG_SERVICE + if (enable) { + g_ppg_stop_request_token++; + if (g_delayed_ppg_stop_timer != nullptr) { + osTimerStop(g_delayed_ppg_stop_timer); + } + ppg_hr_start(); + } else { + g_ppg_stop_request_token++; + g_ppg_stop_execute_token = g_ppg_stop_request_token; + if (g_delayed_ppg_stop_timer == nullptr) { + g_delayed_ppg_stop_timer = + osTimerNew((osTimerFunc_t)ppg_delayed_stop_timer_callback, osTimerOnce, nullptr, nullptr); + } + if (g_delayed_ppg_stop_timer != nullptr) { + osTimerStop(g_delayed_ppg_stop_timer); + osTimerStart(g_delayed_ppg_stop_timer, osMs2Tick(3 * 1000)); + } else { + ppg_hr_stop(); + } + } +#else + (void)enable; +#endif +} + void HeartModel::ClearHeartData(void) { pretime = 0; entryTimes = 0; @@ -433,9 +474,7 @@ void HeartModel::Heart_Control(HeartState state, HeartCheck check) { unwear_counter_ = 0; display_counter_ = 0; displayheart = 0; -#ifdef SUPPORT_PPG_SERVICE - ppg_hr_stop(); -#endif + SwitchPpgHeartSensor(false); heartstate = false; if (checkstate == HeartCheck::CARDCHECK) { //重置传感器状态 @@ -465,14 +504,12 @@ void HeartModel::Heart_Control(HeartState state, HeartCheck check) { return; } // 如果处于训练模式,提前返回 - if (uapi_dev_runtime_get_train_status() == 1){ + if (uapi_dev_runtime_get_train_status() == 1 && check != HeartCheck::EXERCISE){ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "处于训练模式,不进行心率检测"); return; } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "HeartState Start"); -#ifdef SUPPORT_PPG_SERVICE - ppg_hr_start(); -#endif + SwitchPpgHeartSensor(true); heartstate = true; checkstate = check; #ifdef SYSTEM_LOG_SERVICE diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp index 5061bc18e..21e15a60d 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp @@ -80,6 +80,7 @@ #include "NativeAbility.h" #include "AppViewIDs.h" #include "sport_countdown/CountdownPresenter.h" +#include "heartrate/include/HeartModel.h" #if defined(SUPPORT_GNSS) #include "NMEA0183.h" @@ -3225,11 +3226,8 @@ static void gnss_delayed_stop_timer_callback(void *arg) { //运动心率开启准备 void TraingModel::start_training_heart() { - //开启心率检测 - #ifdef SUPPORT_PPG_SERVICE - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动心率检测服务"); - ppg_hr_start(); - #endif + // 心率统一通过 HeartModel 接口控制 + HeartModel::GetInstance()->SwitchPpgHeartSensor(true); //提前设置训练状态为1 uapi_dev_runtime_set_train_status(1); } @@ -3266,11 +3264,8 @@ static void gnss_delayed_stop_timer_callback(void *arg) { "GPS延迟关闭已安排,等待10s确认后再真正关闭"); #endif } - //关闭心率检测 - #ifdef SUPPORT_PPG_SERVICE - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭心率检测服务"); - ppg_hr_stop(); - #endif + // 心率统一通过 HeartModel 接口控制,延迟 3 秒关闭防抖 + HeartModel::GetInstance()->SwitchPpgHeartSensor(false); //设置训练状态为0 uapi_dev_runtime_set_train_status(0); -- 2.17.1