mcu_hi3321_watch/tjd/ui/app/spo2/TjdUiAppBloodOxygenPresenter.cpp
2025-05-26 20:15:20 +08:00

256 lines
7.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description:
*
* Author: wuchangxin
*
* Create: 2024-6-23
*--------------------------------------------------------------------------*/
#include "TjdUiAppBloodOxygenPresenter.h"
#include "TjdUiAppBloodOxygenView.h"
#include "TjdUiAppMainView.h"
#include "TjdUiRegisterManager.h"
#include "TjdUiScreenManage.h"
#include "hal_tick.h"
#include "los_typedef.h"
#include "motor.h"
#include "power_display_service.h"
#include "rtc_api.h"
#include "service_hrsensor.h"
#include "sql_fit.h"
#include "sql_setting.h"
#include "sys_config.h"
#include "ui_screennotify.h"
#include <stdio.h>
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#endif
#define SPO2_SCREEN_AUTO_OFF_TIME (1000 * 60 * 60)
#define SPO2_SWIPEVIEW_ID "swipeContainer_"
namespace TJD {
TJD_REGIST_NATIVE_MENU(TJD_APP_VIEW_SPO2, TjdUiAppSpO2View, TjdUiAppSpO2Presenter, IMG_MENU_LIST_MENU_SPO2,
STR_ID_01);
static TjdUiAppSpO2Presenter *g_pv_AppSpO2Presenter = nullptr;
TjdUiAppSpO2Presenter::TjdUiAppSpO2Presenter() { g_pv_AppSpO2Presenter = this; }
TjdUiAppSpO2Presenter::~TjdUiAppSpO2Presenter()
{
if (timer_ != nullptr) {
delete timer_;
timer_ = nullptr;
}
g_pv_AppSpO2Presenter = nullptr;
}
TjdUiAppSpO2Presenter *TjdUiAppSpO2Presenter::GetInstance(void) { return g_pv_AppSpO2Presenter; }
void TjdUiAppSpO2Presenter::Notify()
{
if (timerFlag) {
SpO2Process();
}
}
void TjdUiAppSpO2Presenter::OnStart()
{
static_print_info("TjdUiAppSpO2Presenter::OnStart...");
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(this, KeyModelType::APP_KEY_TYPE);
TimeTaskStart();
SetPeriod(1000);
}
void TjdUiAppSpO2Presenter::OnPause() { Presenter::OnPause(); }
void TjdUiAppSpO2Presenter::OnResume() { Presenter::OnResume(); }
void TjdUiAppSpO2Presenter::OnStop()
{
static_print_info("TjdUiAppSpO2Presenter::OnStop...");
TjdUiCommonOnKeyListener::GetInstance()->ClearOnKeyActListener(KeyModelType::APP_KEY_TYPE);
TimeTaskStop();
}
bool TjdUiAppSpO2Presenter::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
printf("TjdUiAppSpO2Presenter::OnClick animator view [view.GetViewId() = %s] [%s]]\n", view.GetViewId(),
TjdUiAppSpO2View::GetInstance()->GetAnimatorView()->GetViewId());
if (view.GetViewId() == std::string(TjdUiAppSpO2View::GetInstance()->GetAnimatorView()->GetViewId())) {
TimeTaskStart();
return true;
}
return false;
}
void TjdUiAppSpO2Presenter::OnSwipe(OHOS::UISwipeView &view)
{
static_print_info("swipeView page:%d", view.GetCurrentPage());
}
bool TjdUiAppSpO2Presenter::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
{
if (!TjdUiCommonOnKeyListener::CheckIsExitEvent(event)) {
return true;
}
ExitSpO2View();
return false;
}
void TjdUiAppSpO2Presenter::TimeTaskStart(void)
{
if (timerFlag != true) {
SpO2ControlHandler(SpO2Action::Start, nullptr);
TjdUiScreenManage::SetScreenKeepOnTimeout(SPO2_SCREEN_AUTO_OFF_TIME);
timerFlag = true;
exitTimerCount_ = 0;
refreshStatus = true;
OHOS::UIImageAnimatorView *animator = TjdUiAppSpO2View::GetInstance()->GetAnimatorView();
if (animator != nullptr) {
if (animator->GetState() == 0)
animator->Resume();
}
if (TjdUiAppSpO2View::GetInstance()->flickerTimer_) {
TjdUiAppSpO2View::GetInstance()->flickerTimer_->Start();
}
}
}
uint8_t TjdUiAppSpO2Presenter::SpO2ControlHandler(SpO2Action action, void *param)
{
uint8_t ret = 0;
service_hrs_api *hrsensorInfo = tjd_service_hrs_get_ops();
hr_api_info_t hr_api_info = {HR_SERVICE_UI_BO, 0xffffffff};
switch (action) {
case SpO2Action::Start:
needsUpdate_ = true;
hrsensorInfo->open(hr_api_info);
break;
case SpO2Action::Stop:
needsUpdate_ = false;
hrsensorInfo->close();
break;
case SpO2Action::GetStatus:
ret = SpO2GetWearStatus();
break;
default:
break;
}
return ret;
}
uint8_t TjdUiAppSpO2Presenter::SpO2GetWearStatus(void)
{
service_hrs_api *hrsensorInfo = tjd_service_hrs_get_ops();
uint8_t wearStatus = hrsensorInfo->hrs_wear_status();
sys_ui_log_i("wearStatus is %s", wearStatus ? (wearStatus == 1 ? "weared" : "init") : "no wear");
if (wearStatus == 0) {
if (refreshStatus && refreshCount++ > 3) {
refreshStatus = false;
refreshCount = 0;
TimeTaskStop();
TjdUiAppSpO2View::GetInstance()->ChangedView(SPO2UNWEAR_VIEW, SPO2SWIPE_VIEW);
}
return 0;
}
refreshCount = 0; // 若已配带,刷新计数
if (wearStatus == 1) {
return 1;
}
return 2;
}
void TjdUiAppSpO2Presenter::TimeTaskStop(void)
{
if (timerFlag != false) {
timerFlag = false;
refreshCount = 0;
exitTimerCount_ = 0;
TjdUiAppSpO2View::spo2WaitTime_ = 0;
TjdUiScreenManage::ResetScreenKeepOnTimeout();
SpO2ControlHandler(SpO2Action::Stop, nullptr);
OHOS::UIImageAnimatorView *animator = TjdUiAppSpO2View::GetInstance()->GetAnimatorView();
if (animator != nullptr) {
animator->Stop();
}
if (TjdUiAppSpO2View::GetInstance()->flickerTimer_) {
TjdUiAppSpO2View::GetInstance()->flickerTimer_->Stop();
}
TjdUiAppSpO2View::GetInstance()->InitRodViewInfo();
TjdUiAppSpO2View::GetInstance()->UpdateRodViewGroup();
}
}
void TjdUiAppSpO2Presenter::SpO2Process(void)
{
spo2CurValue = tjd_service_hrsensor_get_cur_spo2value();
// uint8_t *tempArray = nullptr;
// sql_fit_get_spo2_daydata(&tempArray);
uint32_t index = GetCurTimeIndex();
// uint8_t spo2Value = tempArray[index];
SpO2ControlHandler(SpO2Action::GetStatus, nullptr);
printf("TjdUiAppSpO2Presenter::SpO2Process spo2CurValue:%d, index:%d\n", spo2CurValue, index);
if (needsUpdate_ && TjdUiAppSpO2View::spo2WaitTime_ >= 10)//5s内先不更新数据
TjdUiAppSpO2View::GetInstance()->UpdateSpO2ViewData(index, spo2CurValue);
if (spo2CurValue && exitTimerCount_++ > 5) {
TimeTaskStop();
motor_param_t motorParam = {.cycle1_on = 250, .cycle1_off = 750, .cycle1_cnt = 1, .duty = 100};
tjd_driver_motor_user_start(&motorParam);
}
}
uint32_t TjdUiAppSpO2Presenter::GetCurTimeIndex(void)
{
struct rtc_time local_time;
struct rtc_class_ops *handel = tjd_driver_rtc_get_ops();
handel->get_rtc_time(&local_time);
uint32_t totalMinutes = local_time.tm_hour * 60 + local_time.tm_min;
// 计算索引值每5分钟一个索引
uint32_t index = totalMinutes / 60;
return index;
}
void TjdUiAppSpO2Presenter::SpO2PointArrayInit(void)
{
uint8_t *storeArray;
static OHOS::Point array[24];
int32_t num = sql_fit_get_spo2_daydata(&storeArray);
for (int i = 0; i < 24; ++i) {
array[i].x = i;
array[i].y = storeArray[i] * 39 / 100;
}
SpO2PointArray = array;
}
OHOS::Point *TjdUiAppSpO2Presenter::GetSpO2PointArray(void) { return SpO2PointArray; }
void TjdUiAppSpO2Presenter::ExitSpO2View(void) { OHOS::NativeAbility::GetInstance().ChangePreSlice(); }
} // namespace TJD