• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 namespace OHOS {
19 namespace Msdp {
Init(DevicestatusDataUtils::DevicestatusType type)20 bool AlgoAbsoluteStill::Init(DevicestatusDataUtils::DevicestatusType type)
21 {
22     DEV_HILOGD(SERVICE, "Enter");
23     algoCallback_ = std::bind(&AlgoAbsoluteStill::StartAlgorithm, this, std::placeholders::_1, std::placeholders::_2);
24     if (algoCallback_ == nullptr) {
25         DEV_HILOGE(SERVICE, "algoCallback is nullptr");
26         return false;
27     }
28     SensorDataCallback::GetInstance().SubscribeSensorEvent(type, algoCallback_);
29     return true;
30 }
31 
StartAlgorithm(int32_t sensorTypeId,AccelData * sensorData)32 bool AlgoAbsoluteStill::StartAlgorithm(int32_t sensorTypeId, AccelData* sensorData)
33 {
34     DEV_HILOGD(SERVICE, "Enter");
35     if (!GetData(sensorTypeId, sensorData)) {
36         DEV_HILOGE(SERVICE, "Failed to get data");
37         return false;
38     }
39     ExecuteOperation();
40     return true;
41 }
42 
ExecuteOperation()43 void AlgoAbsoluteStill::ExecuteOperation()
44 {
45     DEV_HILOGD(SERVICE, "Enter");
46     algoPara_.resultantAcc =
47         sqrt((algoPara_.x * algoPara_.x) + (algoPara_.y * algoPara_.y) + (algoPara_.z * algoPara_.z));
48     DEV_HILOGD(SERVICE, "resultantAcc:%{public}f", algoPara_.resultantAcc);
49     if ((algoPara_.resultantAcc > RESULTANT_ACC_LOW_THRHD) && (algoPara_.resultantAcc < RESULTANT_ACC_UP_THRHD)) {
50         if (state_ == STILL) {
51             return;
52         }
53         counter_--;
54         if (counter_ == 0) {
55             counter_ = COUNTER_THRESHOLD;
56             UpdateStateAndReport(DevicestatusDataUtils::VALUE_ENTER, STILL, DevicestatusDataUtils::TYPE_STILL);
57         }
58     } else {
59         counter_ = COUNTER_THRESHOLD;
60         if (state_ == NON_STILL) {
61             return;
62         }
63         UpdateStateAndReport(DevicestatusDataUtils::VALUE_EXIT, NON_STILL, DevicestatusDataUtils::TYPE_STILL);
64     }
65 }
66 } // namespace Msdp
67 } // namespace OHOS
68