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.
21 #include <sf_common.h>
23 #include <accel_sensor.h>
24 #include <sensor_plugin_loader.h>
30 #define GRAVITY 9.80665
33 #define RAW_DATA_TO_G_UNIT(X) (((float)(X))/((float)G_TO_MG))
34 #define RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(X) (GRAVITY * (RAW_DATA_TO_G_UNIT(X)))
36 #define SENSOR_NAME "ACCELEROMETER_SENSOR"
38 accel_sensor::accel_sensor()
40 , m_interval(POLL_1HZ_MS)
42 m_name = string(SENSOR_NAME);
44 vector<unsigned int> supported_events = {
45 ACCELEROMETER_RAW_DATA_EVENT,
46 ACCELEROMETER_UNPROCESSED_DATA_EVENT,
49 for_each(supported_events.begin(), supported_events.end(),
50 bind1st(mem_fun(&sensor_base::register_supported_event), this));
52 physical_sensor::set_poller(accel_sensor::working, this);
55 accel_sensor::~accel_sensor()
57 INFO("accel_sensor is destroyed!\n");
60 bool accel_sensor::init()
62 m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(ACCELEROMETER_SENSOR);
65 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
69 sensor_properties_s properties;
71 if (m_sensor_hal->get_properties(properties) == false) {
72 ERR("sensor->get_properties() is failed!\n");
76 m_raw_data_unit = properties.resolution / GRAVITY * G_TO_MG;
78 INFO("m_raw_data_unit accel : [%f]\n", m_raw_data_unit);
80 INFO("%s is created!\n", sensor_base::get_name());
84 sensor_type_t accel_sensor::get_type(void)
86 return ACCELEROMETER_SENSOR;
89 bool accel_sensor::working(void *inst)
91 accel_sensor *sensor = (accel_sensor*)inst;
92 return sensor->process_event();
95 bool accel_sensor::process_event(void)
97 sensor_event_t base_event;
99 if (!m_sensor_hal->is_data_ready(true))
102 m_sensor_hal->get_sensor_data(base_event.data);
105 AUTOLOCK(m_client_info_mutex);
107 if (get_client_cnt(ACCELEROMETER_UNPROCESSED_DATA_EVENT)) {
108 base_event.sensor_id = get_id();
109 base_event.event_type = ACCELEROMETER_UNPROCESSED_DATA_EVENT;
113 if (get_client_cnt(ACCELEROMETER_RAW_DATA_EVENT)) {
114 base_event.sensor_id = get_id();
115 base_event.event_type = ACCELEROMETER_RAW_DATA_EVENT;
116 raw_to_base(base_event.data);
123 bool accel_sensor::on_start(void)
125 if (!m_sensor_hal->enable()) {
126 ERR("m_sensor_hal start fail\n");
133 bool accel_sensor::on_stop(void)
135 if (!m_sensor_hal->disable()) {
136 ERR("m_sensor_hal stop fail\n");
143 bool accel_sensor::get_properties(sensor_properties_s &properties)
145 return m_sensor_hal->get_properties(properties);
148 int accel_sensor::get_sensor_data(unsigned int type, sensor_data_t &data)
150 if (m_sensor_hal->get_sensor_data(data) < 0) {
151 ERR("Failed to get sensor data");
155 if (type == ACCELEROMETER_RAW_DATA_EVENT) {
158 ERR("Does not support type: 0x%x", type);
165 bool accel_sensor::set_interval(unsigned long interval)
169 m_interval = interval;
171 INFO("Polling interval is set to %dms", interval);
173 return m_sensor_hal->set_interval(interval);
176 void accel_sensor::raw_to_base(sensor_data_t &data)
178 data.value_count = 3;
179 data.values[0] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[0] * m_raw_data_unit);
180 data.values[1] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[1] * m_raw_data_unit);
181 data.values[2] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data.values[2] * m_raw_data_unit);
184 extern "C" sensor_module* create(void)
186 accel_sensor *sensor;
189 sensor = new(std::nothrow) accel_sensor;
191 ERR("Failed to create module, err: %d, cause: %s", err, strerror(err));
195 sensor_module *module = new(std::nothrow) sensor_module;
196 retvm_if(!module || !sensor, NULL, "Failed to allocate memory");
198 module->sensors.push_back(sensor);