4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <sys/types.h>
29 #include <sf_common.h>
30 #include <sensor_base.h>
31 #include <virtual_sensor.h>
32 #include <gravity_sensor.h>
33 #include <sensor_plugin_loader.h>
35 #define INITIAL_VALUE -1
36 #define INITIAL_TIME 0
37 #define TIME_CONSTANT 0.18
38 #define GRAVITY 9.80665
40 #define SENSOR_NAME "GRAVITY_SENSOR"
42 gravity_sensor::gravity_sensor()
43 : m_accel_sensor(NULL)
47 , m_time(INITIAL_TIME)
49 m_name = string(SENSOR_NAME);
51 register_supported_event(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
54 gravity_sensor::~gravity_sensor()
56 INFO("gravity_sensor is destroyed!");
59 bool gravity_sensor::init()
61 m_accel_sensor = sensor_plugin_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
63 if (!m_accel_sensor) {
64 ERR("cannot load accel sensor_hal[%s]", sensor_base::get_name());
68 INFO("%s is created!", sensor_base::get_name());
72 sensor_type_t gravity_sensor::get_type(void)
74 return GRAVITY_SENSOR;
77 bool gravity_sensor::on_start(void)
81 m_accel_sensor->add_client(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME);
82 m_accel_sensor->start();
88 bool gravity_sensor::on_stop(void)
92 m_accel_sensor->delete_client(ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME);
93 m_accel_sensor->stop();
99 bool gravity_sensor::add_interval(int client_id, unsigned int interval, bool is_processor)
101 m_accel_sensor->add_interval(client_id, interval, true);
102 return sensor_base::add_interval(client_id, interval, true);
105 bool gravity_sensor::delete_interval(int client_id, bool is_processor)
107 m_accel_sensor->delete_interval(client_id, true);
108 return sensor_base::delete_interval(client_id, true);
111 void gravity_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
113 if (event.event_type == ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME) {
115 calibrate_gravity(event, x, y, z);
117 sensor_event_t event;
118 event.event_type = GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
119 event.data.data_accuracy = SENSOR_ACCURACY_GOOD;
120 event.data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
121 event.data.timestamp = m_time;
122 event.data.values_num = 3;
123 event.data.values[0] = x;
124 event.data.values[1] = y;
125 event.data.values[2] = z;
126 outs.push_back(event);
128 AUTOLOCK(m_value_mutex);
137 int gravity_sensor::get_sensor_data(const unsigned int data_id, sensor_data_t &data)
139 if (data_id != GRAVITY_BASE_DATA_SET)
142 AUTOLOCK(m_value_mutex);
144 data.data_accuracy = SENSOR_ACCURACY_GOOD;
145 data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
146 data.time_stamp = m_time;
148 data.values[0] = m_x;
149 data.values[1] = m_y;
150 data.values[2] = m_z;
155 bool gravity_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
157 m_accel_sensor->get_properties(type, properties);
159 if (type != GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME)
162 properties.sensor_min_range = properties.sensor_min_range / GRAVITY;
163 properties.sensor_max_range = properties.sensor_max_range / GRAVITY;
164 properties.sensor_resolution = properties.sensor_resolution / GRAVITY;
165 strncpy(properties.sensor_name, "Gravity Sensor", MAX_KEY_LENGTH);
170 void gravity_sensor::calibrate_gravity(const sensor_event_t &raw, float &x, float &y, float &z)
172 unsigned long long timestamp;
175 float last_x = 0, last_y = 0, last_z = 0;
178 AUTOLOCK(m_value_mutex);
184 timestamp = get_timestamp();
185 dt = (timestamp - m_time) / US_TO_SEC;
187 alpha = TIME_CONSTANT / (TIME_CONSTANT + dt);
192 x = (alpha * last_x) + ((1 - alpha) * raw.data.values[0] / GRAVITY);
193 y = (alpha * last_y) + ((1 - alpha) * raw.data.values[1] / GRAVITY);
194 z = (alpha * last_z) + ((1 - alpha) * raw.data.values[2] / GRAVITY);
197 extern "C" void *create(void)
199 gravity_sensor *inst;
202 inst = new gravity_sensor();
203 } catch (int ErrNo) {
204 ERR("Failed to create gravity_sensor class, errno : %d, errstr : %s", err, strerror(err));
211 extern "C" void destroy(void *inst)
213 delete (gravity_sensor *)inst;