增加秒钟测试自定义表盘 1331/1 1331/head 1333/target/1
authorCaisking <3449750381@qq.com>
Mon, 4 Nov 2024 05:10:51 +0000 (13:10 +0800)
committerCaisking <3449750381@qq.com>
Mon, 4 Nov 2024 05:10:51 +0000 (13:10 +0800)
code/sdk/application/wearable/fd230app/nativeui/clock/src/MainClockView.cpp

index 39c387a..72edf73 100644 (file)
@@ -34,6 +34,8 @@ extern uint8_t battery_get_voltage(float *voltage);
 
 #define TIMEVIEW_UP 0
 #define TIMEVIEW_DOWN 1
+#define SECONDTEST 0
+#define TIMEFORMAT 0
 
 std::string Enweekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
 std::string Cnweekdays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
@@ -472,72 +474,75 @@ namespace OHOS
     }
 
     /* 更新时间 */
-    void MainClockView::UpdateTime(void)
-    {
-        struct tm local_time = {0};
-        time_t seconds = time(nullptr);
+    void MainClockView::UpdateTime(void) {  
+        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;
-        }
+        // Get local time  
+        if (localtime_r(&seconds, &local_time) == nullptr) {  
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get localtime error!");  
+            return;  
+        }  
 
-        // 处理小时和分钟
+        // Extract hour and minute  
         uint8_t hour = local_time.tm_hour % HOURS_IN_DAY;
         uint8_t minute = local_time.tm_min % MINUTES_IN_HOUR;
-
-        // 处理12小时制显示
-        char timebuf[64] = {0};
-        const char *phour = (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) ? (hour < HOUR_FORMAT_12 ? "AM" : "PM") : nullptr;
-
-        if (phour)
-        {
-            if (hour > HOUR_FORMAT_12)
-                hour -= HOUR_FORMAT_12;
-            sprintf_s(timebuf, sizeof(timebuf), "%s %02d:%02d", phour, hour, minute);
-        }
-        else
-        {
-            sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute);
-        }
-
-        // 更新界面上的文本
-        if (timerlabel != nullptr)
-            timerlabel->SetText(timebuf);
-
-        // 更新星期和日期
-        std::string weekStr = Cnweekdays[local_time.tm_wday];
-        std::string dateStr = std::to_string(local_time.tm_mon + 1) + "月" +
-                              std::to_string(local_time.tm_mday) + "号";
-
-        dateStr = dateStr + " " + weekStr;
-
-        // if (weekLabel != nullptr)
-        //     weekLabel->SetText(weekStr.c_str());
-        if (datelabel != nullptr)
-            datelabel->SetText(dateStr.c_str());
-
-        // 更新电量
-        if (batterySetting == BATTERY_DISPLAY_NONE)
-            return;
-
-        UpdateBatteryColor();
-
-        float voltage = 0.0F;
-        int power = battery_get_voltage(&voltage);
-        if (power == power_)
-            return;
-
-        power_ = power;
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress power_:%d",
-                      power_);
-        if (batteryprogress != nullptr && batterylabel != nullptr)
-        {
-            batteryprogress->SetValue(power_);
-            std::string batteryStr = std::to_string(power_) + "%";
-            batterylabel->SetText(batteryStr.c_str());
-        }
+        char timebuf[64] = {0};  
+
+        // Format time string based on defined macros  
+        #if SECONDTEST  
+            uint8_t second = local_time.tm_sec;  
+            // uint16_t millisecond = local_time.tm_sec % 1000;  
+            sprintf(timebuf, "%02d:%02d:%02d", hour, minute, second);  
+        #else  
+            sprintf(timebuf, "%02d:%02d", hour, minute);  
+        #endif  
+
+        #if TIMEFORMAT  
+            // Handle 12-hour format display  
+            const char *phour = (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) ?   
+                                (hour < HOUR_FORMAT_12 ? "AM" : "PM") : nullptr;  
+
+            if (phour) {  
+                if (hour > HOUR_FORMAT_12)  
+                    hour -= HOUR_FORMAT_12;  
+                sprintf_s(timebuf, sizeof(timebuf), "%s %02d:%02d", phour, hour, minute);  
+            } else {  
+                sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute);  
+            }  
+        #endif  
+
+        // Update display labels  
+        if (timerlabel != nullptr)  
+            timerlabel->SetText(timebuf);  
+
+        // Update date and week day  
+        std::string weekStr = Cnweekdays[local_time.tm_wday];  
+        std::string dateStr = std::to_string(local_time.tm_mon + 1) + "月" +  
+                            std::to_string(local_time.tm_mday) + "号 " + weekStr;  
+
+        if (datelabel != nullptr)  
+            datelabel->SetText(dateStr.c_str());  
+
+        // Update battery status  
+        if (batterySetting == BATTERY_DISPLAY_NONE)  
+            return;  
+
+        UpdateBatteryColor();  
+
+        float voltage = 0.0F;  
+        int power = battery_get_voltage(&voltage);  
+        if (power == power_)  
+            return;  
+
+        power_ = power;  
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress power_:%d", power_);  
+
+        if (batteryprogress != nullptr && batterylabel != nullptr) {  
+            batteryprogress->SetValue(power_);  
+            std::string batteryStr = std::to_string(power_) + "%";  
+            batterylabel->SetText(batteryStr.c_str());  
+        }  
     }
 
     void MainClockView::UpdateBatteryColor(void)