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