coverity issues fix
[platform/core/system/sensord.git] / src / fusion-sensor / pedometer / zero_crossing_step_detection.cpp
1 /*
2  *  Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 #include "zero_crossing_step_detection.h"
18
19 static bool detect_zero_crossing(double last_accel, double new_accel, bool up)
20 {
21         if (up)
22                 return (last_accel < 0 && new_accel >= 0);
23
24         return (last_accel > 0 && new_accel <= 0);
25 }
26
27 zero_crossing_step_detection::zero_crossing_step_detection(bool up)
28 : m_up(up)
29 , m_last_acceleration(0)
30 , m_last_timestamp(UNKNOWN_TIMESTAMP)
31 , m_last_zero_crossing_time(UNKNOWN_TIMESTAMP)
32 , m_time_sum(0)
33 {
34 }
35
36 zero_crossing_step_detection::~zero_crossing_step_detection()
37 {
38 }
39
40 bool zero_crossing_step_detection::detect_step(timestamp_t timestamp, double acceleration)
41 {
42         bool step_detected = false;
43
44         if (m_last_timestamp != UNKNOWN_TIMESTAMP) {
45                 // zero crossing detected
46                 if (detect_zero_crossing(m_last_acceleration, acceleration, m_up)) {
47                         m_time_sum += timestamp - m_last_timestamp;
48                         m_last_timestamp = timestamp;
49                         m_last_zero_crossing_time = timestamp;
50                         step_detected = true;
51                 }
52         } else {
53                 m_last_timestamp = timestamp;
54         }
55         m_last_acceleration = acceleration;
56         return step_detected;
57 }
58
59 void zero_crossing_step_detection::clr_time_sum() {
60         m_time_sum = 0.0;
61 }
62
63 double zero_crossing_step_detection::get_time_sum() {
64         return m_time_sum / 1E9;
65 }
66
67 void zero_crossing_step_detection::reset(void)
68 {
69         m_last_acceleration = 0;
70         m_time_sum = 0;
71         m_last_timestamp = UNKNOWN_TIMESTAMP;
72         m_last_zero_crossing_time = UNKNOWN_TIMESTAMP;
73 }