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_horizontal.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, "AlgoHorizontal" };
25 } // namespace
26
Init(Type type)27 bool AlgoHorizontal::Init(Type type)
28 {
29 CALL_DEBUG_ENTER;
30 algoCallback_ = std::bind(&AlgoHorizontal::StartAlgorithm, this, std::placeholders::_1, std::placeholders::_2);
31 CHKPF(algoCallback_);
32 SENSOR_DATA_CB.SubscribeSensorEvent(type, algoCallback_);
33 return true;
34 }
35
StartAlgorithm(int32_t sensorTypeId,AccelData * sensorData)36 bool AlgoHorizontal::StartAlgorithm(int32_t sensorTypeId, AccelData* sensorData)
37 {
38 CALL_DEBUG_ENTER;
39 if (!SetData(sensorTypeId, sensorData)) {
40 FI_HILOGE("Failed to get data");
41 return false;
42 }
43 ExecuteOperation();
44 return true;
45 }
46
ExecuteOperation()47 void AlgoHorizontal::ExecuteOperation()
48 {
49 CALL_DEBUG_ENTER;
50 algoPara_.pitch = -atan2(algoPara_.y, algoPara_.z) * (ANGLE_180_DEGREE / PI);
51 algoPara_.roll = atan2(algoPara_.x, algoPara_.z) * (ANGLE_180_DEGREE / PI);
52 FI_HILOGD("pitch:%{public}f, roll:%{public}f", algoPara_.pitch, algoPara_.roll);
53
54 if ((((abs(algoPara_.pitch) > ANGLE_HOR_LOW_THRHD) && (abs(algoPara_.pitch) < ANGLE_HOR_UP_THRHD)) &&
55 ((abs(algoPara_.roll) > ANGLE_HOR_LOW_THRHD) && (abs(algoPara_.roll) < ANGLE_HOR_UP_THRHD))) ||
56 (((abs(algoPara_.pitch) > 0) && (abs(algoPara_.pitch) < ANGLE_HOR_FLIPPED_THRHD)) &&
57 ((abs(algoPara_.roll) > 0) && (abs(algoPara_.roll) < ANGLE_VER_FLIPPED_THRHD)))) {
58 if (state_ == HORIZONTAL) {
59 return;
60 }
61 counter_--;
62 if (counter_ == 0) {
63 counter_ = COUNTER_THRESHOLD;
64 UpdateStateAndReport(VALUE_ENTER, HORIZONTAL, TYPE_HORIZONTAL_POSITION);
65 }
66 } else {
67 counter_ = COUNTER_THRESHOLD;
68 if (state_ == NON_HORIZONTAL) {
69 return;
70 }
71 UpdateStateAndReport(VALUE_EXIT, NON_HORIZONTAL, TYPE_HORIZONTAL_POSITION);
72 }
73 }
74 } // namespace DeviceStatus
75 } // namespace Msdp
76 } // namespace OHOS
77