From 8635521eb9ef8ecd27112d4c029ee88e7078d710 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Wed, 24 Jul 2024 19:01:15 +0800 Subject: [PATCH] =?utf8?q?#68=20=E4=BF=AE=E5=A4=8D=E7=94=B5=E9=87=8F?= =?utf8?q?=E8=BF=9B=E5=BA=A6=E6=9D=A1=E5=BC=82=E5=B8=B8=E6=98=BE=E7=A4=BA?= =?utf8?q?=E7=BA=A2=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../main/include/dial/DialBoxProgressView.h | 2 +- .../main/include/dial/DialModelPowerStandby.h | 7 ++ .../main/src/dial/DialBoxProgressView.cpp | 83 ++++++++----------- .../main/src/dial/DialModelPowerStandby.cpp | 43 +++++----- 4 files changed, 67 insertions(+), 68 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h index 068a9cd5c..d286f4d9e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h @@ -28,7 +28,7 @@ namespace OHOS } void HandleFloatData(float data) override; void UpdataChargColor(); - void UpdateBatteryColor(); + void UpdateBatteryColor(int value); void SetViewColor(int64_t color); private: diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelPowerStandby.h b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelPowerStandby.h index d0c5156df..334bb2664 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelPowerStandby.h +++ b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelPowerStandby.h @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-04-27 11:55:14 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-07-24 18:34:02 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelPowerStandby.h + */ /*** * @Date: 2024-04-27 11:55:14 * @LastEditors: Caisking 3449750381@qq.com @@ -37,6 +43,7 @@ namespace OHOS bool GetDialFloatData(DialDataType &type, float &out) override; void UpdatePowerStanby(); void NotifyPowerStanbyUpdate(); + float SmoothPowerUpdate(float newPower); private: uint8_t PowerStanby_ = 0; diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp index da4911a2c..13986340c 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp @@ -1,16 +1,7 @@ -/*** - * @Date: 2024-04-15 10:55:58 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-17 14:54:10 - * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp - */ -/* - * Copyright (c) @CompanyNameMagicTag. 2023. All rights reserved. - */ - #include "DialBoxProgressView.h" #include "wearable_log.h" #include "battery_hal.h" + extern uint8_t battery_get_voltage(float *voltage); // 定义电量宏 @@ -19,9 +10,8 @@ extern uint8_t battery_get_voltage(float *voltage); namespace OHOS { - DialBoxProgressView::DialBoxProgressView(void) + DialBoxProgressView::DialBoxProgressView(void) : value_(0), color_(Color::White().full) // 初始化 color_ { - value_ = 0; } DialBoxProgressView::~DialBoxProgressView(void) @@ -30,31 +20,26 @@ namespace OHOS void DialBoxProgressView::HandleFloatData(float data) { - float ratio = data; - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DialBoxProgressView:%d", data); - if (ratio > 1.0f) - { - ratio = 1.0f; - } - else if (ratio < 0.0f) - { - ratio = 0.0f; - } - int32_t value = ratio * (GetRangeMax() - GetRangeMin()); + // 进行平滑处理 + static float lastData = 0.0f; + float smoothedData = (lastData + data) / 2; // 简单取平均 + lastData = smoothedData; + + // 将平滑后的值限制在 0 到 1 之间 + float ratio = smoothedData < 0.0f ? 0.0f : (smoothedData > 1.0f ? 1.0f : smoothedData); + int32_t value = static_cast(ratio * (GetRangeMax() - GetRangeMin())); - // 更新电池颜色 - UpdateBatteryColor(); + UpdateBatteryColor(value); // 在值变化或充电状态变化时更新颜色 - if (value_ == value) + // 第一次初始化或值变化时更新 + if (value_ != value) { - return; + value_ = value; + SetValue(value_); } - - value_ = value; - SetValue(value_); } - void DialBoxProgressView::UpdateBatteryColor() + void DialBoxProgressView::UpdateBatteryColor(int value) { bool isCharging = get_charge_stat(); // 获取充电状态 @@ -63,29 +48,33 @@ namespace OHOS SetViewColor(Color::Green().full); // 充电状态为绿色 return; } + + // 电量未发生变化且未充电 + if (value == value_) + return; + + // 根据值设置电池颜色 + if (value_ > BATTERY_MEDIUM_THRESHOLD) + { + SetViewColor(Color::White().full); + } + else if (value_ > BATTERY_LOW_THRESHOLD) + { + SetViewColor(Color::Yellow().full); + } else { - if (value_ <= BATTERY_LOW_THRESHOLD) - { - SetViewColor(Color::Red().full); - } - else if (BATTERY_LOW_THRESHOLD < value_ && value_ <= BATTERY_MEDIUM_THRESHOLD) - { - SetViewColor(Color::Yellow().full); - } - else - { - SetViewColor(Color::White().full); - } + SetViewColor(Color::Red().full); } } + void DialBoxProgressView::SetViewColor(int64_t color) { - if (color == color_) + // 仅在颜色变化时设置 + if (color != color_) { - return; + color_ = color; + SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_); } - color_ = color; - SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_); } } \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp index a06e20346..557c69ce8 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp @@ -1,21 +1,9 @@ -/*** +/*** * @Date: 2024-07-06 16:27:31 * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-19 11:47:44 + * @LastEditTime: 2024-07-24 18:31:39 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp */ -/*** - * @Date: 2024-04-27 11:56:33 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-04-27 11:58:02 - * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp - */ -/* - * Copyright (c) CompanyNameMagicTag 2023. All rights reserved. - * Description: DialModelPowerStanby - * Author: - * Create: 2023-12 - */ #include "RegisterDialModel.h" #include "DialModelPowerStandby.h" @@ -29,6 +17,12 @@ REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_POWER_STANDBY, OHOS::DialModelPowerSt namespace OHOS { + float DialModelPowerStanby::SmoothPowerUpdate(float newPower) + { + const float alpha = 0.1; // 平滑因子,总值范围是0-1 + PowerStanby_ = alpha * newPower + (1 - alpha) * PowerStanby_; + return PowerStanby_; + } bool DialModelPowerStanby::GetDialFloatData(DialDataType &type, float &out) { (void)type; @@ -40,14 +34,23 @@ namespace OHOS } float voltage = 0.0F; - PowerStanby_ = battery_get_voltage(&voltage); + PowerStanby_ = SmoothPowerUpdate(battery_get_voltage(&voltage)); WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Battery voltage obtained: %.2f", voltage); // 计算映射值 - float minPower = 0; // 最小电量映射值 - float maxPower = 100; // 最大电量映射值 - float mappedValue = static_cast(PowerStanby_ - minPower) / (maxPower - minPower); // 进行映射计算 + float minPower = 0; + float maxPower = 100; + + float mappedValue = 0.0F; + if (maxPower - minPower != 0) + { + mappedValue = (PowerStanby_ - minPower) / (maxPower - minPower); + } + else + { + mappedValue = (PowerStanby_ == maxPower) ? 1.0f : 0.0f; // 边界处理 + } WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Raw power value: %d, Mapped value: %.2f", PowerStanby_, mappedValue); @@ -62,7 +65,7 @@ namespace OHOS void DialModelPowerStanby::UpdatePowerStanby() { float voltage = 0.0F; - PowerStanby_ = battery_get_voltage(&voltage); + PowerStanby_ = SmoothPowerUpdate(battery_get_voltage(&voltage)); if (PowerStanby_ == 0) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get battery voltage"); @@ -78,7 +81,7 @@ namespace OHOS if (onChangeListener_ != nullptr) { onChangeListener_->OnDialFloatDataUpdate(DialDataType::DATA_TYPE_POWER_STANDBY, PowerStanby_); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"[UpdatePowerStanby!]\r\n"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[UpdatePowerStanby!]\r\n"); } } } -- 2.17.1