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 <gravity_sensor.h>
32 #include <sensor_plugin_loader.h>
34 #define INITIAL_VALUE -1
35 #define INITIAL_TIME 0
36 #define GRAVITY 9.80665
38 #define SENSOR_NAME "GRAVITY_SENSOR"
40 gravity_sensor::gravity_sensor()
41 : m_orientation_sensor(NULL)
45 , m_timestamp(INITIAL_TIME)
47 m_name = string(SENSOR_NAME);
49 register_supported_event(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
52 gravity_sensor::~gravity_sensor()
54 INFO("gravity_sensor is destroyed!");
57 bool gravity_sensor::init()
59 m_orientation_sensor = sensor_plugin_loader::get_instance().get_sensor(ORIENTATION_SENSOR);
61 if (!m_orientation_sensor) {
62 ERR("Failed to load orientation sensor: 0x%x", m_orientation_sensor);
66 INFO("%s is created!", sensor_base::get_name());
70 sensor_type_t gravity_sensor::get_type(void)
72 return GRAVITY_SENSOR;
75 bool gravity_sensor::on_start(void)
79 m_orientation_sensor->add_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
80 m_orientation_sensor->start();
86 bool gravity_sensor::on_stop(void)
90 m_orientation_sensor->delete_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
91 m_orientation_sensor->stop();
97 bool gravity_sensor::add_interval(int client_id, unsigned int interval)
100 m_orientation_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)
108 m_orientation_sensor->delete_interval(client_id , true);
110 return sensor_base::delete_interval(client_id, true);
113 void gravity_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
115 sensor_event_t gravity_event;
116 vector<sensor_event_t> orientation_event;
117 ((virtual_sensor *)m_orientation_sensor)->synthesize(event, orientation_event);
119 if (!orientation_event.empty()) {
122 m_timestamp = orientation_event[0].data.timestamp;
123 gravity_event.data.values[0] = GRAVITY * sin(orientation_event[0].data.values[1]);
124 gravity_event.data.values[1] = GRAVITY * sin(orientation_event[0].data.values[0]);
125 gravity_event.data.values[2] = GRAVITY * cos(orientation_event[0].data.values[1] *
126 orientation_event[0].data.values[0]);
127 gravity_event.data.values_num = 3;
128 gravity_event.data.timestamp = m_timestamp;
129 gravity_event.data.data_accuracy = SENSOR_ACCURACY_GOOD;
130 gravity_event.data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
132 outs.push_back(gravity_event);
137 int gravity_sensor::get_sensor_data(const unsigned int event_type, sensor_data_t &data)
139 sensor_data_t orientation_data;
141 if (event_type != GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME)
144 m_orientation_sensor->get_sensor_data(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, orientation_data);
146 data.data_accuracy = SENSOR_ACCURACY_GOOD;
147 data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
148 data.timestamp = orientation_data.timestamp;
149 data.values[0] = GRAVITY * sin(orientation_data.values[1]);
150 data.values[1] = GRAVITY * sin(orientation_data.values[0]);
151 data.values[2] = GRAVITY * cos(orientation_data.values[1] * orientation_data.values[0]);
157 bool gravity_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
159 properties.sensor_unit_idx = SENSOR_UNIT_DEGREE;
160 properties.sensor_min_range = -GRAVITY;
161 properties.sensor_max_range = GRAVITY;
162 properties.sensor_resolution = 1;
163 strncpy(properties.sensor_vendor, "Samsung", MAX_KEY_LENGTH);
164 strncpy(properties.sensor_name, SENSOR_NAME, MAX_KEY_LENGTH);
169 extern "C" void *create(void)
171 gravity_sensor *inst;
174 inst = new gravity_sensor();
176 ERR("Failed to create gravity_sensor class, errno : %d, errstr : %s", err, strerror(err));
183 extern "C" void destroy(void *inst)
185 delete (gravity_sensor *)inst;