UpdateTime();
}
- /* 更新时间 */
- 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;
- }
-
- uint8_t hour = local_time.tm_hour % HOURS_IN_DAY;
- uint8_t minute = local_time.tm_min % MINUTES_IN_HOUR;
- char timebuf[64] = {0};
- //时间显示样式
- #if TIMEFORMAT
- if (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) {
- if (hour > HOUR_FORMAT_12) {
- hour -= HOUR_FORMAT_12;
- }
- }
- #endif
- //是否显示秒
- #if SECONDTEST
- uint8_t second = local_time.tm_sec;
- sprintf(timebuf, "%02d:%02d:%02d", hour, minute, second);
- #else
- sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute);
- #endif
-
- 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) + "号 " + weekStr;
-
- if (datelabel != nullptr)
- datelabel->SetText(dateStr.c_str());
-
-
- //电量显示配置
- if (batterySetting == BATTERY_DISPLAY_NONE)
- return;
-
-
- UpdateBatteryColor();
-
- float voltage = 0.0F;
- int power = 0;
- #ifdef SUPPORT_BATTERY_SERVICE
- power = battery_get_voltage(&voltage);
- #endif
- 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)
- {
- bool isCharging = false;
- #ifdef SUPPORT_BATTERY_SERVICE
- isCharging = get_charge_stat(); // 获取充电状态
+ /* 更新时间 */
+ 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;
+ }
+
+ uint8_t hour = local_time.tm_hour % HOURS_IN_DAY;
+ uint8_t minute = local_time.tm_min % MINUTES_IN_HOUR;
+ char timebuf[64] = {0};
+ //时间显示样式
+ #if TIMEFORMAT
+ if (uapi_dev_runtime_get_time_hour_show_model() == HOUR_FORMAT_12) {
+ if (hour > HOUR_FORMAT_12) {
+ hour -= HOUR_FORMAT_12;
+ }
+ }
#endif
+ //是否显示秒
+ #if SECONDTEST
+ uint8_t second = local_time.tm_sec;
+ sprintf(timebuf, "%02d:%02d:%02d", hour, minute, second);
+ #else
+ sprintf_s(timebuf, sizeof(timebuf), "%02d:%02d", hour, minute);
+ #endif
+
+ if (timerlabel != nullptr)
+ timerlabel->SetText(timebuf);
+
+
+ // 防止数组越界:确保 tm_wday 在有效范围内 (0-6)
+ int wday = local_time.tm_wday;
+ if (wday < 0 || wday > 6) {
+ wday = 0; // 默认使用星期天
+ }
+ std::string weekStr = Cnweekdays[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());
+
+ //电量显示配置
+ if (batterySetting == BATTERY_DISPLAY_NONE)
+ return;
+
+ UpdateBatteryColor();
+
+ float voltage = 0.0F;
+ int power = 0;
+ #ifdef SUPPORT_BATTERY_SERVICE
+ power = battery_get_voltage(&voltage);
+ #endif
+ 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)
+ {
+ bool isCharging = false;
+ #ifdef SUPPORT_BATTERY_SERVICE
+ isCharging = get_charge_stat(); // 获取充电状态
+ #endif
+
+ if (isCharging)
+ {
+ if (!isBatteryIconVisible)
+ {
+ if (batteryicon != nullptr) {
+ batteryicon->SetVisible(true);
+ isBatteryIconVisible = true;
+ }
+ }
+ SetViewColor(Color::Green().full); // 充电状态为绿色
+ return;
+ }
+ else
+ {
+ if (isBatteryIconVisible)
+ {
+ if (batteryicon != nullptr) {
+ batteryicon->SetVisible(false);
+ isBatteryIconVisible = false;
+ }
+ }
+
+ if (power_ <= BATTERY_LOW_THRESHOLD)
+ {
+ SetViewColor(Color::Red().full);
+ }
+ else if (power_ <= BATTERY_MEDIUM_THRESHOLD)
+ {
+ SetViewColor(Color::Yellow().full);
+ }
+ else
+ {
+ if (batterySetting == BATTERY_DISPLAY_BLACK)
+ {
+ SetViewColor(Color::Black().full);
+ }
+ else
+ {
+ SetViewColor(Color::White().full);
+ }
+ }
+ }
+ }
+
+ void MainClockView::SetViewColor(int64_t color)
+ {
+ if (color == color_)
+ {
+ return;
+ }
+ color_ = color;
+ if (batteryprogress != nullptr) {
+ batteryprogress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_);
+ }
+ }
+
+ /*刷新步数*/
+ void MainClockView::RefreshStep(int step)
+ {
+ if (stepLabel == nullptr)
+ return;
+
+ char stepChar[STEP_CHAR_NUM] = {0};
+ (void)sprintf_s(stepChar, STEP_CHAR_NUM, "%d", step);
+ stepLabel->SetText(stepChar);
+ }
+
+ /*刷新心率*/
+ void MainClockView::RefreshHeartRate(int value)
+ {
+ if (heartlabel == nullptr)
+ return;
+
+ if (value == 0)
+ {
+ heartlabel->SetText("--");
+ return;
+ }
+ char heartRateChar[STEP_CHAR_NUM] = {0};
+ (void)sprintf_s(heartRateChar, STEP_CHAR_NUM, "%d", value);
+ heartlabel->SetText(heartRateChar);
+ }
+
+ /*初始化步数*/
+ void MainClockView::InitStepValue()
+ {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue");
+ uint32_t stepValue;
+ int weekToday = ActivityModel::GetInstance()->GetWeekToday();
+ ActivityModel::GetInstance()->GetStepData((StepDataDistribute)weekToday, &stepValue);
+ char stepChar[STEP_CHAR_NUM] = {0};
+ if (sprintf_s(stepChar, STEP_CHAR_NUM, "%d", stepValue) == -1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue error");
+ return;
+ }
+ if (stepLabel == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue nullptr");
+ return;
+ }
+ stepLabel->SetText(stepChar);
+ }
- if (isCharging)
- {
- if (!isBatteryIconVisible)
- {
- batteryicon->SetVisible(true);
- isBatteryIconVisible = true;
- }
- SetViewColor(Color::Green().full); // 充电状态为绿色
- return;
- }
- else
- {
- if (isBatteryIconVisible)
- {
- batteryicon->SetVisible(false);
- isBatteryIconVisible = false;
- }
-
- if (power_ <= BATTERY_LOW_THRESHOLD)
- {
- SetViewColor(Color::Red().full);
- }
- else if (power_ <= BATTERY_MEDIUM_THRESHOLD)
- {
- SetViewColor(Color::Yellow().full);
- }
- else
- {
- if (batterySetting == BATTERY_DISPLAY_BLACK)
- {
- SetViewColor(Color::Black().full);
- }
- else
- {
- SetViewColor(Color::White().full);
- }
- }
- }
- }
-
- void MainClockView::SetViewColor(int64_t color)
- {
- if (color == color_)
- {
- return;
- }
- color_ = color;
- batteryprogress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_);
- }
-
- /*刷新步数*/
- void MainClockView::RefreshStep(int step)
- {
- if (stepLabel == nullptr)
- return;
-
- char stepChar[STEP_CHAR_NUM] = {0};
- (void)sprintf_s(stepChar, STEP_CHAR_NUM, "%d", step);
- stepLabel->SetText(stepChar);
- }
-
- /*刷新心率*/
- void MainClockView::RefreshHeartRate(int value)
- {
- if (heartlabel == nullptr)
- return;
-
- if (value == 0)
- {
- heartlabel->SetText("--");
- return;
- }
- char heartRateChar[STEP_CHAR_NUM] = {0};
- (void)sprintf_s(heartRateChar, STEP_CHAR_NUM, "%d", value);
- heartlabel->SetText(heartRateChar);
- }
-
- /*初始化步数*/
- void MainClockView::InitStepValue()
- {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue");
- uint32_t stepValue;
- int weekToday = ActivityModel::GetInstance()->GetWeekToday();
- ActivityModel::GetInstance()->GetStepData((StepDataDistribute)weekToday, &stepValue);
- char stepChar[STEP_CHAR_NUM] = {0};
- if (sprintf_s(stepChar, STEP_CHAR_NUM, "%d", stepValue) == -1)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue error");
- return;
- }
- if (stepLabel == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainClockView::InitStepValue nullptr");
- return;
- }
- stepLabel->SetText(stepChar);
- }
/*初始化指针图片*/
bool MainClockView::InitCenterCircleView(void)
{