#include "ui_resource_string.h"
#include "iostream"
#include "string"
+#include "graphic_service.h"
+#include <cmsis_os2.h>
+#include <cmsis_os.h>
namespace OHOS
{
DeviceNameLabel = nullptr;
}
- if (WatchQRCodeGroupView != nullptr)
- {
- delete WatchQRCodeGroupView;
- WatchQRCodeGroupView = nullptr;
- }
+ delete WatchQRCodeGroupView;
+ WatchQRCodeGroupView = nullptr;
}
}
extern "C"
{
#include "build_version.h"
-
+#include "battery_hal.h"
+ extern uint8_t battery_get_voltage(float *voltage);
const char *get_version_string(void);
}
return false;
}
WatchQRCodeGroupView->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
+ WatchQRCodeGroupView->SetViewId(WATCHQRCODEVIEWID);
WatchQRCodeGroupView->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
WatchQRCodeGroupView->SetTouchable(true);
WatchQRCodeGroupView->SetDraggable(true);
- UIView::OnDragListener *dragListener = static_cast<UIView::OnDragListener *>(WatchQRCodePresenter::GetInstance());
- if (dragListener == nullptr)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::dragListener new fail!");
- return false;
- }
- WatchQRCodeGroupView->SetOnDragListener(dragListener);
+ WatchQRCodeGroupView->SetOnDragListener(presenter_);
+ WatchQRCodeGroupView->SetOnClickListener(presenter_);
return true;
}
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::DeviceNameLabel new label fail");
return false;
}
- std::string temp = get_version_string();
- std::string version_string = "V" + temp;
- DeviceNameLabel->SetText(version_string.c_str());
- DeviceNameLabel->SetPosition(83, 402, 300, 35);
+ DeviceNameLabel->SetText(get_voltage_and_version_string().c_str());
+ DeviceNameLabel->SetPosition(83, 390, 300, 35);
DeviceNameLabel->SetStyle(STYLE_TEXT_FONT, 30);
DeviceNameLabel->SetStyle(STYLE_LINE_HEIGHT, 35);
DeviceNameLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::OnStart");
View<WatchQRCodePresenter>::OnStart();
+ start_refresh_timer();
WatchQRCodeViewInit();
/* --------------------------------- 添加按键监听 --------------------------------- */
RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::WatchQRCodePresenter::GetInstance();
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::OnStop");
View<WatchQRCodePresenter>::OnStop();
+ stop_refresh_timer();
RootView::GetInstance()->RemoveAll();
RootView::GetInstance()->ClearOnKeyActListener();
ClearAll();
return;
}
+ std::string WatchQRCodeView::get_voltage_and_version_string(void)
+ {
+ // 电量:
+ float voltage = 0.0F;
+ uint8_t value = 0;
+ value = battery_get_voltage(&voltage);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "battery_get_voltage = %d", value);
+
+ // 版本号:
+ std::string temp = get_version_string();
+ std::string version_string = "V" + temp;
+ std::string str = "bat:" + std::to_string(value) + "%" + " " + version_string;
+ return str;
+ }
+
+ void WatchQRCodeView::update_label(void)
+ {
+ if (DeviceNameLabel)
+ {
+ std::string str = get_voltage_and_version_string();
+ DeviceNameLabel->SetText(str.c_str());
+ DeviceNameLabel->Invalidate();
+ }
+ }
+
+ // 声明定时器变量
+ static osTimerId_t g_timer = nullptr;
+ static uint8_t UPDATETIME_OUT = 30; // 30秒
+ uint8_t WatchQRCodeView::refresh_time = UPDATETIME_OUT;
+ /*定时器到期时的处理*/
+ static void QRCodeView_TimerCallbackProc(int data)
+ {
+ if (uapi_get_screen_state() == 0) // 如果在亮屏状态
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state() == 0");
+ if (WatchQRCodeView::GetInstance())
+ {
+ WatchQRCodeView::GetInstance()->update_label();
+ }
+ if (WatchQRCodeView::refresh_time != UPDATETIME_OUT) // 如果是亮屏后息屏再亮屏(此时息屏时间没超过 UPDATETIME_OUT)则恢复
+ {
+ WatchQRCodeView::refresh_time = UPDATETIME_OUT;
+ }
+ }
+ else
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state() == 1");
+ WatchQRCodeView::refresh_time -= 2;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d", WatchQRCodeView::refresh_time);
+ if (WatchQRCodeView::refresh_time <= 0) // 息屏状态下超时删除定时器
+ {
+ if (WatchQRCodeView::GetInstance())
+ {
+ GraphicService::GetInstance()->PostGraphicEvent(std::bind(&WatchQRCodeView::stop_refresh_timer, WatchQRCodeView::GetInstance()));
+ }
+ }
+ else
+ {
+ if (WatchQRCodeView::GetInstance())
+ {
+ WatchQRCodeView::GetInstance()->update_label();
+ }
+ }
+ }
+ return;
+ }
+
+ /*
+ * @brief 回调函数
+ */
+ static void QRCodeView_TimerCallback(int data)
+ {
+ GraphicService::GetInstance()->PostGraphicEvent(std::bind(QRCodeView_TimerCallbackProc, data));
+ }
+
+ /*
+ * @brief 开启定时器函数
+ * @param interval 时间间隔,单位ms
+ */
+ void WatchQRCodeView::start_refresh_timer(uint32_t interval)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "start_refresh_timer");
+ /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
+ if (g_timer != nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " Timer already exists!!");
+ return;
+ }
+ /* --------------------------------- 2.创建定时器 -------------------------------- */
+ g_timer = osTimerNew((osTimerFunc_t)QRCodeView_TimerCallback, osTimerPeriodic, nullptr, nullptr);
+ // 检查定时器创建是否成功
+ if (g_timer == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
+ return;
+ }
+ /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */
+ int tick = osMs2Tick(interval);
+ /* --------------------------------- 4.启动定时器 -------------------------------- */
+ osStatus_t ret = osTimerStart(g_timer, tick);
+ /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 nullptr -------------------- */
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ if (g_timer != nullptr)
+ {
+ ret = osTimerDelete(g_timer);
+ }
+ g_timer = nullptr;
+ return;
+ }
+ return;
+ }
+
+ void WatchQRCodeView::stop_refresh_timer(void)
+ {
+ // 检查定时器是否存在
+ if (g_timer != nullptr)
+ {
+ osTimerStop(g_timer);
+ // 删除定时器并置为 nullptr
+ osStatus_t ret = osTimerDelete(g_timer);
+ if (ret != osOK)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!", ret);
+ }
+ g_timer = nullptr;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_timer stop success");
+ refresh_time = UPDATETIME_OUT;
+ }
+ }
}