• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_absolute_still.h"
17 
18 #include "fi_log.h"
19 
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 namespace {
24 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "AlgoAbsoluteStill" };
25 } // namespace
26 
Init(Type type)27 bool AlgoAbsoluteStill::Init(Type type)
28 {
29     CALL_DEBUG_ENTER;
30     algoCallback_ = std::bind(&AlgoAbsoluteStill::StartAlgorithm, this, std::placeholders::_1, std::placeholders::_2);
31     if (algoCallback_ == nullptr) {
32         FI_HILOGE("algoCallback is nullptr");
33         return false;
34     }
35     SENSOR_DATA_CB.SubscribeSensorEvent(type, algoCallback_);
36     return true;
37 }
38 
StartAlgorithm(int32_t sensorTypeId,AccelData * sensorData)39 bool AlgoAbsoluteStill::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 AlgoAbsoluteStill::ExecuteOperation()
51 {
52     CALL_DEBUG_ENTER;
53     algoPara_.resultantAcc =
54         sqrt((algoPara_.x * algoPara_.x) + (algoPara_.y * algoPara_.y) + (algoPara_.z * algoPara_.z));
55     FI_HILOGD("resultantAcc:%{public}f", algoPara_.resultantAcc);
56     if ((algoPara_.resultantAcc > RESULTANT_ACC_LOW_THRHD) && (algoPara_.resultantAcc < RESULTANT_ACC_UP_THRHD)) {
57         if (state_ == STILL) {
58             return;
59         }
60         counter_--;
61         if (counter_ == 0) {
62             counter_ = COUNTER_THRESHOLD;
63             UpdateStateAndReport(VALUE_ENTER, STILL, TYPE_ABSOLUTE_STILL);
64         }
65     } else {
66         counter_ = COUNTER_THRESHOLD;
67         if (state_ == UNSTILL) {
68             return;
69         }
70         UpdateStateAndReport(VALUE_EXIT, UNSTILL, TYPE_ABSOLUTE_STILL);
71     }
72 }
73 } // namespace DeviceStatus
74 } // namespace Msdp
75 } // namespace OHOS
76