1f857971dSopenharmony_ci/* 2f857971dSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f857971dSopenharmony_ci * you may not use this file except in compliance with the License. 5f857971dSopenharmony_ci * You may obtain a copy of the License at 6f857971dSopenharmony_ci * 7f857971dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f857971dSopenharmony_ci * 9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f857971dSopenharmony_ci * See the License for the specific language governing permissions and 13f857971dSopenharmony_ci * limitations under the License. 14f857971dSopenharmony_ci */ 15f857971dSopenharmony_ci 16f857971dSopenharmony_ci#ifdef DEVICE_STATUS_SENSOR_ENABLE 17f857971dSopenharmony_ci#include "algo_vertical.h" 18f857971dSopenharmony_ci 19f857971dSopenharmony_ci#include "devicestatus_define.h" 20f857971dSopenharmony_ci 21f857971dSopenharmony_ci#undef LOG_TAG 22f857971dSopenharmony_ci#define LOG_TAG "AlgoVertical" 23f857971dSopenharmony_ci 24f857971dSopenharmony_cinamespace OHOS { 25f857971dSopenharmony_cinamespace Msdp { 26f857971dSopenharmony_cinamespace DeviceStatus { 27f857971dSopenharmony_cinamespace { 28f857971dSopenharmony_ciconstexpr float JUDGE_FLOAT { 1e-6 }; 29f857971dSopenharmony_ci} // namespace 30f857971dSopenharmony_ci 31f857971dSopenharmony_cibool AlgoVertical::Init(Type type) 32f857971dSopenharmony_ci{ 33f857971dSopenharmony_ci CALL_DEBUG_ENTER; 34f857971dSopenharmony_ci algoCallback_ = [this](int32_t sensorTypeId, AccelData* sensorData) { 35f857971dSopenharmony_ci return this->StartAlgorithm(sensorTypeId, sensorData); 36f857971dSopenharmony_ci }; 37f857971dSopenharmony_ci CHKPF(algoCallback_); 38f857971dSopenharmony_ci SENSOR_DATA_CB.SubscribeSensorEvent(type, algoCallback_); 39f857971dSopenharmony_ci return true; 40f857971dSopenharmony_ci} 41f857971dSopenharmony_ci 42f857971dSopenharmony_cibool AlgoVertical::StartAlgorithm(int32_t sensorTypeId, AccelData* sensorData) 43f857971dSopenharmony_ci{ 44f857971dSopenharmony_ci CALL_DEBUG_ENTER; 45f857971dSopenharmony_ci if (!SetData(sensorTypeId, sensorData)) { 46f857971dSopenharmony_ci FI_HILOGE("Failed to get data"); 47f857971dSopenharmony_ci return false; 48f857971dSopenharmony_ci } 49f857971dSopenharmony_ci ExecuteOperation(); 50f857971dSopenharmony_ci return true; 51f857971dSopenharmony_ci} 52f857971dSopenharmony_ci 53f857971dSopenharmony_civoid AlgoVertical::ExecuteOperation() 54f857971dSopenharmony_ci{ 55f857971dSopenharmony_ci CALL_DEBUG_ENTER; 56f857971dSopenharmony_ci if ((abs(algoPara_.y) <= JUDGE_FLOAT) && (abs(algoPara_.z) <= JUDGE_FLOAT)) { 57f857971dSopenharmony_ci return; 58f857971dSopenharmony_ci } 59f857971dSopenharmony_ci algoPara_.pitch = -atan2(algoPara_.y, algoPara_.z) * (ANGLE_180_DEGREE / PI); 60f857971dSopenharmony_ci algoPara_.roll = atan2(algoPara_.x, algoPara_.z) * (ANGLE_180_DEGREE / PI); 61f857971dSopenharmony_ci FI_HILOGD("pitch:%{public}f, roll:%{public}f", algoPara_.pitch, algoPara_.roll); 62f857971dSopenharmony_ci 63f857971dSopenharmony_ci if (((abs(algoPara_.pitch) > ANGLE_VER_LOW_THRHD) && (abs(algoPara_.pitch) < ANGLE_VER_UP_THRHD)) || 64f857971dSopenharmony_ci ((abs(algoPara_.roll) > ANGLE_VER_LOW_THRHD) && (abs(algoPara_.roll) < ANGLE_VER_UP_THRHD))) { 65f857971dSopenharmony_ci if (state_ == VERTICAL) { 66f857971dSopenharmony_ci return; 67f857971dSopenharmony_ci } 68f857971dSopenharmony_ci counter_--; 69f857971dSopenharmony_ci if (counter_ == 0) { 70f857971dSopenharmony_ci counter_ = COUNTER_THRESHOLD; 71f857971dSopenharmony_ci UpdateStateAndReport(VALUE_ENTER, VERTICAL, TYPE_VERTICAL_POSITION); 72f857971dSopenharmony_ci } 73f857971dSopenharmony_ci } else { 74f857971dSopenharmony_ci counter_ = COUNTER_THRESHOLD; 75f857971dSopenharmony_ci if (state_ == NON_VERTICAL) { 76f857971dSopenharmony_ci return; 77f857971dSopenharmony_ci } 78f857971dSopenharmony_ci UpdateStateAndReport(VALUE_EXIT, NON_VERTICAL, TYPE_VERTICAL_POSITION); 79f857971dSopenharmony_ci } 80f857971dSopenharmony_ci} 81f857971dSopenharmony_ci} // namespace DeviceStatus 82f857971dSopenharmony_ci} // namespace Msdp 83f857971dSopenharmony_ci} // namespace OHOS 84f857971dSopenharmony_ci#endif // DEVICE_STATUS_SENSOR_ENABLE