#880,在二维码扫码页面添加 电池百分比显示 1070/1
authorluozheng <1732036298@qq.com>
Mon, 30 Sep 2024 08:07:52 +0000 (16:07 +0800)
committerluozheng <1732036298@qq.com>
Mon, 30 Sep 2024 08:07:52 +0000 (16:07 +0800)
code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp
code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h
code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodePresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/watchQRcode/src/WatchQRCodeView.cpp

index 166bf83..d6dcb5c 100644 (file)
@@ -660,15 +660,12 @@ namespace OHOS
         }
         else //亮屏状态下每2s刷新一次下拉数据
         {
-            MainPresenterSample::g_PullUpDownTimes = UPDATETIME;
             if (PullUpDownView::GetInstance() != nullptr)
             {
                 PullUpDownView::GetInstance()->UpdateSignalStrengthStatusAndBatteryPosition();
             }
-            else
-            {
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PullUpDownView::GetInstance fail!!");
-            }
+            if (MainPresenterSample::g_PullUpDownTimes != UPDATETIME) // 如果是亮屏后息屏再亮屏(此时息屏时间没超过 UPDATETIME)则恢复
+                    MainPresenterSample::g_PullUpDownTimes = UPDATETIME;
         }
         return;
     }
index 44843ea..5d5b47d 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-06-12 11:44:04
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-07-17 16:21:55
+ * @LastEditTime: 2024-09-30 15:48:53
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/watchQRcode/include/WatchQRCodeView.h
  */
 #ifndef WATCH_QR_CODE_VIEW_H
@@ -14,6 +14,7 @@
 #include "components/ui_label_ext.h"
 namespace OHOS
 {
+    static constexpr const char *WATCHQRCODEVIEWID = "WatchQRCodeView";
     class WatchQRCodePresenter;
     class WatchQRCodeView : public View<WatchQRCodePresenter>
     {
@@ -28,6 +29,14 @@ namespace OHOS
         virtual void OnTearDownView() override;
         virtual void OnStop() override;
 
+        /* -------------------------------------------------------------------------- */
+        static uint8_t refresh_time;
+        std::string get_voltage_and_version_string(void);
+        void update_label(void);
+        void start_refresh_timer(uint32_t interval = 2000);
+        void stop_refresh_timer(void);
+        /* -------------------------------------------------------------------------- */
+
     private:
         void WatchQRCodeViewInit();
         bool WatchQRCodeInitViewGroup(void);
index 35feb19..f732f98 100644 (file)
@@ -51,8 +51,13 @@ namespace OHOS
     bool WatchQRCodePresenter::OnClick(UIView &view, const ClickEvent &event)
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WatchQRCodePresenter::OnDrag");
-        UNUSED(view);
         UNUSED(event);
+        std::string viewId = view.GetViewId();
+        if (viewId == WATCHQRCODEVIEWID)
+        {
+            if (view_)
+                view_->start_refresh_timer();
+        }
 
         return true;
     }
index 9f1d7ee..f97b783 100644 (file)
@@ -5,6 +5,9 @@
 #include "ui_resource_string.h"
 #include "iostream"
 #include "string"
+#include "graphic_service.h"
+#include <cmsis_os2.h>
+#include <cmsis_os.h>
 namespace OHOS
 {
 
@@ -51,18 +54,16 @@ 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);
     }
 
@@ -76,16 +77,12 @@ namespace OHOS
             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;
     }
 
@@ -137,10 +134,8 @@ namespace OHOS
             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);
@@ -174,6 +169,7 @@ namespace OHOS
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WatchQRCodeView::OnStart");
         View<WatchQRCodePresenter>::OnStart();
+        start_refresh_timer();
         WatchQRCodeViewInit();
         /* --------------------------------- 添加按键监听 --------------------------------- */
         RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::WatchQRCodePresenter::GetInstance();
@@ -206,10 +202,142 @@ namespace OHOS
     {
         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;
+        }
+    }
 }