sensord: enable samsung pedometer sensor for fused location
[platform/core/system/sensord.git] / src / sensor / pedometer / pedometer.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 "pedometer.h"
18 #include "step_event.h"
19
20 #include <math.h>
21
22 pedometer::pedometer()
23 : m_step_detection()
24 , m_total_length(0)
25 , m_step_count(0)
26 , m_pedometer_filter()
27 , m_some_speed(false)
28 {
29 }
30
31 pedometer::~pedometer()
32 {
33 }
34
35 void pedometer::set_savitzky_filter(bool enable)
36 {
37         m_step_detection.set_use_savitzky(enable);
38 }
39
40 void pedometer::reset(void)
41 {
42         m_total_length = 0;
43         m_step_count = 0;
44         m_step_detection.reset();
45         m_pedometer_filter.reset();
46         m_some_speed = false;
47 }
48
49 bool pedometer::get_pedometer(timestamp_t timestamp, double acc, pedometer_info *info)
50 {
51         bool result = false;
52         step_event event;
53
54         if (m_step_detection.get_step(timestamp, acc, &event)) {
55                 if (event.m_timestamp != UNKNOWN_TIMESTAMP) {
56                         m_step_count++;
57                         m_total_length += event.m_step_length;
58                         m_pedometer_filter.get_step(timestamp, event.m_step_length);
59                         double speed = m_pedometer_filter.get_speed(timestamp);
60                         info->timestamp = timestamp;
61                         info->is_step_detected = true;
62                         info->step_count = m_step_count;
63                         info->step_length = event.m_step_length;
64                         info->total_step_length = m_total_length;
65                         info->step_speed = speed;
66                         result = true;
67                         m_some_speed = speed != 0;
68                 }
69         }
70         if (m_some_speed) {
71                 double speed = m_pedometer_filter.get_speed(timestamp);
72                 if (speed == 0) {
73                         m_some_speed = false;
74                         info->timestamp = timestamp;
75                         info->is_step_detected = true;
76                         info->step_count = m_step_count;
77                         info->step_length = 0;
78                         info->total_step_length = m_total_length;
79                         info->step_speed = 0;
80                         result = true;
81                 }
82         }
83         return result;
84 }