From d03543a73b47ec006eeff3f0e5cf042e295fcd16 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 25 Oct 2024 21:44:27 +0800 Subject: [PATCH] =?utf8?q?=E8=A1=A8=E7=9B=98:=E4=BF=AE=E6=94=B9=E6=9D=A1?= =?utf8?q?=E5=BD=A2=E8=BF=9B=E5=BA=A6=E6=9D=A1=E8=A7=A3=E6=9E=90=EF=BC=8C?= =?utf8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E5=BA=94=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../main/include/dial/DialBoxProgressView.h | 66 ++++--- .../nativeui/main/include/dial/DialDataBase.h | 2 + .../main/src/dial/DialBoxProgressView.cpp | 161 ++++++++++-------- .../main/src/dial/DialModelDateWeekStr.cpp | 59 +++++++ .../main/src/dial/DialModelPowerStandby.cpp | 134 +++++++-------- .../training_card/src/TraingCardPresenter.cpp | 4 +- 6 files changed, 252 insertions(+), 174 deletions(-) create mode 100644 code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelDateWeekStr.cpp 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 7d6be25ea..b1d2c22c9 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 @@ -1,18 +1,3 @@ -/*** - * @Date: 2024-05-30 15:59:37 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-24 20:40:41 - * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h - */ -/*** - * @Date: 2024-05-30 15:59:37 - * @LastEditors: Caisking 3449750381@qq.com - * @LastEditTime: 2024-07-17 15:01:17 - * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h - */ -/* - * Copyright (c) @CompanyNameMagicTag. 2023. All rights reserved. - */ #ifndef DIAL_BOX_PROGRESS_VIEW_H #define DIAL_BOX_PROGRESS_VIEW_H @@ -20,26 +5,35 @@ #include "DialView.h" #include "components/ui_box_progress.h" -namespace OHOS -{ - class DialBoxProgressView : public UIBoxProgress, public DialView - { - public: - DialBoxProgressView(); - ~DialBoxProgressView() override; - static DialBoxProgressView *GetInstance() - { - static DialBoxProgressView instance; - return &instance; - } - void HandleFloatData(float data) override; - void UpdataChargColor(); - void UpdateBatteryColor(); - void SetViewColor(int64_t color); +enum DialBoxDataType { + // 剩余电量百分比 + BATTERY_LEVEL = 1, + // 步数目标 + STEP_TARGET = 2, +}; +namespace OHOS { +class DialBoxProgressView : public UIBoxProgress, public DialView { +public: + DialBoxProgressView(); + ~DialBoxProgressView() override; + static DialBoxProgressView *GetInstance() { + static DialBoxProgressView instance; + return &instance; + } + void HandleFloatData(float data) override; + void UpdataChargColor(); + void UpdateBatteryColor(); + void SetViewColor(int64_t color); - private: - int32_t value_ = -1; - int64_t color_ = Color::White().full; - }; -} + uint32_t DialBoxDataTypeToBitPattern(DialBoxDataType type); + uint32_t ConvertToRequiredFormat(DialBoxDataType type, float data); + DialBoxDataType ExtractDataType(uint32_t rawData); + uint32_t ExtractValue(uint32_t rawData); + +private: + int32_t value_ = -1; + int64_t color_ = Color::White().full; + DialBoxDataType datatype_ = BATTERY_LEVEL; +}; +} // namespace OHOS #endif \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h index e1d0a5621..f69ca2fc0 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h +++ b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h @@ -37,6 +37,7 @@ enum class DialDataType { DATA_TYPE_MINUTE_TENS, // 分钟(十位) DATA_TYPE_MINUTE_UNITS, // 分钟(单位) DATA_TYPE_WEEK_NUM, // 星期(数字) + DATA_TYPE_STEPS_GOAL, // 运动步数目标 DATA_TYPE_MULTI_FLOAT_BASE = 0x2fff, // 多个浮点数据类型基值 DATA_TYPE_HOUR_MIN, // 时分 @@ -46,6 +47,7 @@ enum class DialDataType { DATA_TYPE_DATE_STR, // 日期(string) DATA_TYPE_WEATHER_STR, // 天气(string) DATA_TYPE_TIMESTR_STR, // 时间(string) + DATA_TYPE_DATE_WEEK_STR, // 日期星期(string) DATA_TYPE_MAX = 0x7fff, // 数据类型的最大值 }; 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 3c497cd44..9c06fca88 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,6 +1,6 @@ #include "DialBoxProgressView.h" -#include "wearable_log.h" #include "battery_hal.h" +#include "wearable_log.h" extern uint8_t battery_get_voltage(float *voltage); @@ -8,69 +8,96 @@ extern uint8_t battery_get_voltage(float *voltage); #define BATTERY_LOW_THRESHOLD 10 #define BATTERY_MEDIUM_THRESHOLD 20 -namespace OHOS -{ - DialBoxProgressView::DialBoxProgressView(void) : value_(0), color_(Color::White().full) // 初始化 color_ - { - } - - DialBoxProgressView::~DialBoxProgressView(void) - { - } - - void DialBoxProgressView::HandleFloatData(float data) - { - // 值限制在 0 到 1 之间 - float ratio = data < 0.0f ? 0.0f : (data > 1.0f ? 1.0f : data); - int32_t value = static_cast(ratio * (GetRangeMax() - GetRangeMin())); - if (value != value_) - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor value:%d", value); - - UpdateBatteryColor(); // 在值变化或充电状态变化时更新颜色 - - // 第一次初始化或值变化时更新 - if (value != value_) - { - value_ = value; - SetValue(value_); - } - } - - void DialBoxProgressView::UpdateBatteryColor() - { - bool isCharging = get_charge_stat(); // 获取充电状态 - - if (isCharging) - { - SetViewColor(Color::Green().full); // 充电状态为绿色 - return; - } - - // 根据值设置电池颜色 - if (value_ > BATTERY_MEDIUM_THRESHOLD) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor White: %d", value_); - SetViewColor(Color::White().full); - } - else if (value_ <= BATTERY_MEDIUM_THRESHOLD && value_ > BATTERY_LOW_THRESHOLD) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor Yellow: %d", value_); - SetViewColor(Color::Yellow().full); - } - else if (value_ <= BATTERY_LOW_THRESHOLD && value_ > 0) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor Red: %d", value_); - SetViewColor(Color::Red().full); - } - } - - void DialBoxProgressView::SetViewColor(int64_t color) - { - // 仅在颜色变化时设置 - if (color != color_) - { - color_ = color; - SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_); - } - } -} \ No newline at end of file +// 从转换后的uint32_t数据中提取数据类型 +namespace OHOS { +DialBoxProgressView::DialBoxProgressView(void) + : value_(0), color_(Color::White().full) // 初始化 color_ +{} + +DialBoxProgressView::~DialBoxProgressView(void) {} + +/* --------------------------------- 编码解码函数 + * --------------------------------- */ + +// 定义用于位操作的掩码和位移值,可根据实际需求调整 +constexpr uint32_t TYPE_MASK = 0xFF000000; +constexpr uint32_t TYPE_SHIFT = 24; +constexpr uint32_t VALUE_MASK = 0x00FFFFFF; + +uint32_t +DialBoxProgressView::DialBoxDataTypeToBitPattern(DialBoxDataType type) { + return static_cast(type) << TYPE_SHIFT; +} + +uint32_t DialBoxProgressView::ConvertToRequiredFormat(DialBoxDataType type, + float data) { + + uint32_t typeBitPattern = DialBoxDataTypeToBitPattern(type); + uint32_t dataAsInt = static_cast(data); + + return (typeBitPattern | dataAsInt); +} + +DialBoxDataType DialBoxProgressView::ExtractDataType(uint32_t rawData) { + + uint32_t typeValue = (rawData & TYPE_MASK) >> TYPE_SHIFT; + return static_cast(typeValue); +} + +uint32_t DialBoxProgressView::ExtractValue(uint32_t rawData) { + + return rawData & VALUE_MASK; +} + +/* -------------------------------------------------------------------------- */ +void DialBoxProgressView::HandleFloatData(float data) { + // 获取数据类型 + datatype_ = ExtractDataType(data); //类型为前4位 + float value = ExtractValue(data); //数值为后28位 + WEARABLE_LOGE("DialBoxProgressView", "HandleFloatData: %d %f", datatype_, + value); + + if (datatype_ == BATTERY_LEVEL) { + UpdateBatteryColor(); // 在值变化或充电状态变化时更新颜色 + } else if (datatype_ == STEP_TARGET) { + SetViewColor(0xFFf0c440); + } + if (value != value_) { + value_ = value; + SetValue(value_); + } +} + +void DialBoxProgressView::UpdateBatteryColor() { + bool isCharging = get_charge_stat(); // 获取充电状态 + + if (isCharging) { + SetViewColor(Color::Green().full); // 充电状态为绿色 + return; + } + + // 根据值设置电池颜色 + if (value_ > BATTERY_MEDIUM_THRESHOLD) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor White: %d", + value_); + SetViewColor(Color::White().full); + } else if (value_ <= BATTERY_MEDIUM_THRESHOLD && + value_ > BATTERY_LOW_THRESHOLD) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor Yellow: %d", + value_); + SetViewColor(Color::Yellow().full); + } else if (value_ <= BATTERY_LOW_THRESHOLD && value_ > 0) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateBatteryColor Red: %d", + value_); + SetViewColor(Color::Red().full); + } +} + +void DialBoxProgressView::SetViewColor(int64_t color) { + // 仅在颜色变化时设置 + if (color != color_) { + color_ = color; + SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_); + } +} +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelDateWeekStr.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelDateWeekStr.cpp new file mode 100644 index 000000000..d111d67d3 --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelDateWeekStr.cpp @@ -0,0 +1,59 @@ +#include "DialModelDateWeekStr.h" +#include "RegisterDialModel.h" +#include "sys/time.h" +#include "wearable_log.h" +#include +#include +#include +#include +#include +#include + +REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_DATE_WEEK_STR, + OHOS::DialModelDateWeekStr::GetInstance()); + +namespace OHOS { +// Cnweekdays 是一个包含周字符串的数组 +constexpr const char *Cnweekdays[] = {"周天", "周一", "周二", "周三", + "周四", "周五", "周六"}; + +/*获取当前日期和星期信息 */ + +// 获取表盘文本数据(日期和星期信息) +bool DialModelDateWeekStr::GetDialTextData(DialDataType &type, + std::string *&out, int16_t &strNum) { + (void)type; + struct tm local_time = {0}; + time_t seconds = time(nullptr); + + // 获取本地时间 + if (localtime_r(&seconds, &local_time) == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get localtime error!"); + return false; + } + + // 使用ostringstream构建日期字符串 + std::ostringstream oss; + oss << (local_time.tm_year + 1900) << "年" << std::setw(2) + << std::setfill('0') << (local_time.tm_mon + 1) << "月" << std::setw(2) + << std::setfill('0') << local_time.tm_mday << "日"; + + // 获取星期 + dateweekstr = oss.str() + " " + Cnweekdays[local_time.tm_wday]; + + out = &dateweekstr; + strNum = 1; + + return true; +} + +void DialModelDateWeekStr::UpdateDateWeekStr(const std::string *&dialWeekStr) { + dateweekstr.clear(); + dateweekstr = *dialWeekStr; // 更新字符串 + if (onChangeListener_ != nullptr) { + onChangeListener_->OnDialTextDataUpdate( + DialDataType::DATA_TYPE_DATE_WEEK_STR, &dateweekstr, + dateweekstr.size()); + } +} +} // namespace OHOS \ 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 5e86d710f..2624173f2 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,78 +1,74 @@ -#include "RegisterDialModel.h" #include "DialModelPowerStandby.h" #include "DialBoxProgressView.h" -#include +#include "RegisterDialModel.h" #include "battery_hal.h" #include "wearable_log.h" +#include + extern uint8_t battery_get_voltage(float *voltage); -REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_POWER_STANDBY, OHOS::DialModelPowerStanby::GetInstance()); - -namespace OHOS -{ - - bool DialModelPowerStanby::GetDialFloatData(DialDataType &type, float &out) - { - (void)type; - // if (!PowerStanbyUpdated_) - // { - // UpdatePowerStanby(); - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdatePower %d", PowerStanby_); - // return true; - // } - - float voltage = 0.0F; - PowerStanby_ = battery_get_voltage(&voltage); - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Battery voltage obtained: %.2f", voltage); - - // 计算映射值 - 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); - - // 如果需要在 0 到 1 范围映射 - out = mappedValue; - - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Mapped value sent to caller: %.2f", out); - - return true; - } - - void DialModelPowerStanby::UpdatePowerStanby() - { - float voltage = 0.0F; - PowerStanby_ = battery_get_voltage(&voltage); - if (PowerStanby_ == 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get battery voltage"); - return; - } - PowerStanbyUpdated_ = true; - NotifyPowerStanbyUpdate(); - } - - void DialModelPowerStanby::NotifyPowerStanbyUpdate() { - if (onChangeListener_ == nullptr) { - onChangeListener_ = new OnDialDataUpdateListener(); - } - if (onChangeListener_ != nullptr) - { - onChangeListener_->OnDialFloatDataUpdate(DialDataType::DATA_TYPE_POWER_STANDBY, PowerStanby_); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[UpdatePowerStanby!]\r\n"); - } - } +REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_POWER_STANDBY, + OHOS::DialModelPowerStanby::GetInstance()); + +namespace OHOS { + +bool DialModelPowerStanby::GetDialFloatData(DialDataType &type, float &out) { + (void)type; + + float voltage = 0.0F; + PowerStanby_ = battery_get_voltage(&voltage); + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Battery voltage obtained: %.2f", + voltage); + + // 计算映射值 + // 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_LOGE(WEARABLE_LOG_MODULE_APP, + // "Raw power value: %d, Mapped value: %.2f", PowerStanby_, + // mappedValue); + // 如果需要在 0 到 1 范围映射 + + // 需要前4个为类型 + // 后28位为数据 + + out = DialBoxProgressView::GetInstance()->ConvertToRequiredFormat( + DialBoxDataType::BATTERY_LEVEL, PowerStanby_); + + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Mapped value sent to caller: %.2f", + out); + + return true; +} + +void DialModelPowerStanby::UpdatePowerStanby() { + float voltage = 0.0F; + PowerStanby_ = battery_get_voltage(&voltage); + if (PowerStanby_ == 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get battery voltage"); + return; + } + PowerStanbyUpdated_ = true; + NotifyPowerStanbyUpdate(); +} + +void DialModelPowerStanby::NotifyPowerStanbyUpdate() { + if (onChangeListener_ == nullptr) { + onChangeListener_ = new OnDialDataUpdateListener(); + } + if (onChangeListener_ != nullptr) { + onChangeListener_->OnDialFloatDataUpdate( + DialDataType::DATA_TYPE_POWER_STANDBY, PowerStanby_); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[UpdatePowerStanby!]\r\n"); + } } +} // namespace OHOS diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp index 99589761b..af4d019d0 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp @@ -323,7 +323,7 @@ bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) { // 结束按键 {BUTTONEND, [&]() { - ControlSensor(ExerciseState::Pause); + // ControlSensor(ExerciseState::Pause); TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, true); TraingCardView::GetInstance()->RefreshEndView(); }}, @@ -332,7 +332,7 @@ bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) { // 返回 {BUTTONBACK, [&]() { - ControlSensor(ExerciseState::Continue); + // ControlSensor(ExerciseState::Continue); TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, false); }}, // 确定达到目标 -- 2.17.1