表盘:修改条形进度条解析,增加对应接口
authorCaisking <3449750381@qq.com>
Fri, 25 Oct 2024 13:44:27 +0000 (21:44 +0800)
committeraliyun:aliyun0384013184_1JeGs <3449750381@qq.com>
Sat, 26 Oct 2024 08:12:52 +0000 (08:12 +0000)
code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h
code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelDateWeekStr.cpp [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelPowerStandby.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp

index 7d6be25..b1d2c22 100644 (file)
@@ -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
 #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
index e1d0a56..f69ca2f 100644 (file)
@@ -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, // 数据类型的最大值
 };
index 3c497cd..9c06fca 100644 (file)
@@ -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<int32_t>(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<uint32_t>(type) << TYPE_SHIFT;
+}
+
+uint32_t DialBoxProgressView::ConvertToRequiredFormat(DialBoxDataType type,
+                                                      float data) {
+
+  uint32_t typeBitPattern = DialBoxDataTypeToBitPattern(type);
+  uint32_t dataAsInt = static_cast<uint32_t>(data);
+
+  return (typeBitPattern | dataAsInt);
+}
+
+DialBoxDataType DialBoxProgressView::ExtractDataType(uint32_t rawData) {
+
+  uint32_t typeValue = (rawData & TYPE_MASK) >> TYPE_SHIFT;
+  return static_cast<DialBoxDataType>(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 (file)
index 0000000..d111d67
--- /dev/null
@@ -0,0 +1,59 @@
+#include "DialModelDateWeekStr.h"
+#include "RegisterDialModel.h"
+#include "sys/time.h"
+#include "wearable_log.h"
+#include <chrono>
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <time.h>
+
+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
index 5e86d71..2624173 100644 (file)
@@ -1,78 +1,74 @@
 
 
-#include "RegisterDialModel.h"
 #include "DialModelPowerStandby.h"
 #include "DialBoxProgressView.h"
-#include <string>
+#include "RegisterDialModel.h"
 #include "battery_hal.h"
 #include "wearable_log.h"
+#include <string>
+
 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
index 9958976..af4d019 100644 (file)
@@ -323,7 +323,7 @@ bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) {
           // 结束按键\r
           {BUTTONEND,\r
            [&]() {\r
-             ControlSensor(ExerciseState::Pause);\r
+            //  ControlSensor(ExerciseState::Pause);\r
              TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, true);\r
              TraingCardView::GetInstance()->RefreshEndView();\r
            }},\r
@@ -332,7 +332,7 @@ bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) {
           // 返回\r
           {BUTTONBACK,\r
            [&]() {\r
-             ControlSensor(ExerciseState::Continue);\r
+            //  ControlSensor(ExerciseState::Continue);\r
              TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, false);\r
            }},\r
           // 确定达到目标\r