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