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